Add FIXED_POINTS_NO_RANDOM (#8)

* Add FIXED_POINTS_NO_RANDOM
* Document FIXED_POINTS_NO_RANDOM
pull/9/head
Pharap 7 years ago committed by GitHub
parent 3f68923321
commit feb58a200d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      README.md
  2. 2
      src/FixedPoints/Details.h
  3. 12
      src/FixedPoints/Utils.h

@ -33,6 +33,7 @@ This means:
These are symbols you can define prior to library inclusion to alter the behaviour of the library. 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_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 ## Contents
This library supplies two core types and sixteen type aliases. This library supplies two core types and sixteen type aliases.

@ -167,6 +167,7 @@ namespace FIXED_POINTS_DETAILS
using DecimalLiteralF = decltype(0.0F); using DecimalLiteralF = decltype(0.0F);
using DecimalLiteralL = decltype(0.0L); using DecimalLiteralL = decltype(0.0L);
#if !defined(FIXED_POINTS_NO_RANDOM)
template< typename T > template< typename T >
struct RandomHelper; struct RandomHelper;
@ -217,6 +218,7 @@ namespace FIXED_POINTS_DETAILS
{ {
static inline int64_t Random() { return (static_cast<int64_t>(random()) << 32) | static_cast<int64_t>(random()); } static inline int64_t Random() { return (static_cast<int64_t>(random()) << 32) | static_cast<int64_t>(random()); }
}; };
#endif
/////////////////////// ///////////////////////
// Here be dragons!! // // Here be dragons!! //

@ -23,6 +23,7 @@
// //
FIXED_POINTS_BEGIN_NAMESPACE FIXED_POINTS_BEGIN_NAMESPACE
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction> floorFixed(const UFixed<Integer, Fraction> & value); constexpr UFixed<Integer, Fraction> floorFixed(const UFixed<Integer, Fraction> & value);
@ -60,6 +61,7 @@ constexpr UFixed<Integer, Fraction> nextafterFixed(const UFixed<Integer, Fractio
// Unsigned Random // Unsigned Random
// //
#if !defined(FIXED_POINTS_NO_RANDOM)
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
UFixed<Integer, Fraction> randomUFixed(void); UFixed<Integer, Fraction> randomUFixed(void);
@ -68,11 +70,13 @@ UFixed<Integer, Fraction> randomUFixed(const UFixed<Integer, Fraction> & exclusi
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
UFixed<Integer, Fraction> randomUFixed(const UFixed<Integer, Fraction> & inclusiveLowerBound, const UFixed<Integer, Fraction> & exclusiveUpperBound); UFixed<Integer, Fraction> randomUFixed(const UFixed<Integer, Fraction> & inclusiveLowerBound, const UFixed<Integer, Fraction> & exclusiveUpperBound);
#endif
// //
// Signed Random // Signed Random
// //
#if !defined(FIXED_POINTS_NO_RANDOM)
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
SFixed<Integer, Fraction> randomSFixed(void); SFixed<Integer, Fraction> randomSFixed(void);
@ -81,6 +85,8 @@ SFixed<Integer, Fraction> randomSFixed(const SFixed<Integer, Fraction> & exclusi
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
SFixed<Integer, Fraction> randomSFixed(const SFixed<Integer, Fraction> & inclusiveLowerBound, const SFixed<Integer, Fraction> & exclusiveUpperBound); SFixed<Integer, Fraction> randomSFixed(const SFixed<Integer, Fraction> & inclusiveLowerBound, const SFixed<Integer, Fraction> & exclusiveUpperBound);
#endif
FIXED_POINTS_END_NAMESPACE FIXED_POINTS_END_NAMESPACE
// //
@ -88,6 +94,7 @@ FIXED_POINTS_END_NAMESPACE
// //
FIXED_POINTS_BEGIN_NAMESPACE FIXED_POINTS_BEGIN_NAMESPACE
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
constexpr UFixed<Integer, Fraction> floorFixed(const UFixed<Integer, Fraction> & value) constexpr UFixed<Integer, Fraction> floorFixed(const UFixed<Integer, Fraction> & value)
{ {
@ -199,6 +206,7 @@ constexpr UFixed<Integer, Fraction> nextafterFixed(const UFixed<Integer, Fractio
// Unsigned Random // Unsigned Random
// //
#if !defined(FIXED_POINTS_NO_RANDOM)
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
UFixed<Integer, Fraction> randomUFixed(void) UFixed<Integer, Fraction> randomUFixed(void)
{ {
@ -219,11 +227,13 @@ UFixed<Integer, Fraction> randomUFixed(const UFixed<Integer, Fraction> & inclusi
using InternalType = typename UFixed<Integer, Fraction>::InternalType; using InternalType = typename UFixed<Integer, Fraction>::InternalType;
return UFixed<Integer, Fraction>::fromInternal(inclusiveLowerBound.getInternal() + (FIXED_POINTS_DETAILS::RandomHelper<InternalType>::Random() % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); return UFixed<Integer, Fraction>::fromInternal(inclusiveLowerBound.getInternal() + (FIXED_POINTS_DETAILS::RandomHelper<InternalType>::Random() % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal())));
} }
#endif
// //
// Signed Random // Signed Random
// //
#if !defined(FIXED_POINTS_NO_RANDOM)
template< unsigned Integer, unsigned Fraction > template< unsigned Integer, unsigned Fraction >
SFixed<Integer, Fraction> randomSFixed(void) SFixed<Integer, Fraction> randomSFixed(void)
{ {
@ -245,4 +255,6 @@ SFixed<Integer, Fraction> randomSFixed(const SFixed<Integer, Fraction> & inclusi
auto value = FIXED_POINTS_DETAILS::RandomHelper<InternalType>::Random(); auto value = FIXED_POINTS_DETAILS::RandomHelper<InternalType>::Random();
return SFixed<Integer, Fraction>::fromInternal(inclusiveLowerBound.getInternal() + (abs(value) % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); return SFixed<Integer, Fraction>::fromInternal(inclusiveLowerBound.getInternal() + (abs(value) % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal())));
} }
#endif
FIXED_POINTS_END_NAMESPACE FIXED_POINTS_END_NAMESPACE

Loading…
Cancel
Save