From feb58a200db9bcac1837905052da0a4abe6b213c Mon Sep 17 00:00:00 2001 From: Pharap Date: Sun, 12 Nov 2017 11:57:28 +0000 Subject: [PATCH] Add FIXED_POINTS_NO_RANDOM (#8) * Add FIXED_POINTS_NO_RANDOM * Document FIXED_POINTS_NO_RANDOM --- README.md | 1 + src/FixedPoints/Details.h | 2 ++ src/FixedPoints/Utils.h | 12 ++++++++++++ 3 files changed, 15 insertions(+) diff --git a/README.md b/README.md index fbecdbf..c49edda 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ This means: These are symbols you can define prior to library inclusion to alter the behaviour of the library. - `FIXED_POINTS_USE_NAMESPACE`: Define this to wrap all classes and functions in the namespace `FixedPoints`. Useful for preventing naming conflicts. +- `FIXED_POINTS_NO_RANDOM`: Define this to disable the random utility functions. Useful for systems that don't have access to `long random(void)` from avr-libc. ## Contents This library supplies two core types and sixteen type aliases. diff --git a/src/FixedPoints/Details.h b/src/FixedPoints/Details.h index 3818853..5c183b4 100644 --- a/src/FixedPoints/Details.h +++ b/src/FixedPoints/Details.h @@ -167,6 +167,7 @@ namespace FIXED_POINTS_DETAILS using DecimalLiteralF = decltype(0.0F); using DecimalLiteralL = decltype(0.0L); +#if !defined(FIXED_POINTS_NO_RANDOM) template< typename T > struct RandomHelper; @@ -217,6 +218,7 @@ namespace FIXED_POINTS_DETAILS { static inline int64_t Random() { return (static_cast(random()) << 32) | static_cast(random()); } }; +#endif /////////////////////// // Here be dragons!! // diff --git a/src/FixedPoints/Utils.h b/src/FixedPoints/Utils.h index cde6210..4619a3c 100644 --- a/src/FixedPoints/Utils.h +++ b/src/FixedPoints/Utils.h @@ -23,6 +23,7 @@ // FIXED_POINTS_BEGIN_NAMESPACE + template< unsigned Integer, unsigned Fraction > constexpr UFixed floorFixed(const UFixed & value); @@ -60,6 +61,7 @@ constexpr UFixed nextafterFixed(const UFixed UFixed randomUFixed(void); @@ -68,11 +70,13 @@ UFixed randomUFixed(const UFixed & exclusi template< unsigned Integer, unsigned Fraction > UFixed randomUFixed(const UFixed & inclusiveLowerBound, const UFixed & exclusiveUpperBound); +#endif // // Signed Random // +#if !defined(FIXED_POINTS_NO_RANDOM) template< unsigned Integer, unsigned Fraction > SFixed randomSFixed(void); @@ -81,6 +85,8 @@ SFixed randomSFixed(const SFixed & exclusi template< unsigned Integer, unsigned Fraction > SFixed randomSFixed(const SFixed & inclusiveLowerBound, const SFixed & exclusiveUpperBound); +#endif + FIXED_POINTS_END_NAMESPACE // @@ -88,6 +94,7 @@ FIXED_POINTS_END_NAMESPACE // FIXED_POINTS_BEGIN_NAMESPACE + template< unsigned Integer, unsigned Fraction > constexpr UFixed floorFixed(const UFixed & value) { @@ -199,6 +206,7 @@ constexpr UFixed nextafterFixed(const UFixed UFixed randomUFixed(void) { @@ -219,11 +227,13 @@ UFixed randomUFixed(const UFixed & inclusi using InternalType = typename UFixed::InternalType; return UFixed::fromInternal(inclusiveLowerBound.getInternal() + (FIXED_POINTS_DETAILS::RandomHelper::Random() % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); } +#endif // // Signed Random // +#if !defined(FIXED_POINTS_NO_RANDOM) template< unsigned Integer, unsigned Fraction > SFixed randomSFixed(void) { @@ -245,4 +255,6 @@ SFixed randomSFixed(const SFixed & inclusi auto value = FIXED_POINTS_DETAILS::RandomHelper::Random(); return SFixed::fromInternal(inclusiveLowerBound.getInternal() + (abs(value) % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); } +#endif + FIXED_POINTS_END_NAMESPACE