Skip to content

fixed point library

CppPhil edited this page Jan 8, 2017 · 3 revisions

fixed point library

The fixed point library supplied can be useful on systems that have limited support for floating point numbers, such as the samr21-xpro board used throughout the RIOTNerf project.

features

  • Unsigned 32bit fixed point numbers

  • Signed 32bit fixed point numbers

  • Unsigned 64bit fixed point numbers

  • Signed 64bit fixed point numbers

  • Conversions from and to float

  • Conversions from and to double

  • Conversions from and to integer types

  • Extraction of the fractional part of a given fixed point number

  • Extraction of the whole part of a given fixed point number

  • Addition of fixed point numbers

  • Subtraction of fixed point numbers

  • Multiplication of fixed point numbers

  • Division of fixed point numbers

API

Documentation

For the best documentation viewing experience it is recommended to generate the Doxygen documentation. See the wiki entry for the Doxygen documentation to figure out how. You may also want to view the source code directly.

types

  • fp32 : 32bit signed fixed point number
  • fp64 : 64bit signed fixed point number
  • fpu32 : 32bit unsigned fixed point number
  • fpu64 : 64bit unsigned fixed point number

conversions

/*! converts a double to an fp32

  • Note: the whole and fractional parts may not be too large to fit into a 32 bit integer. **/

fp32 double_to_fp32(double x);

/*! converts a double to an fp64

  • Note: the whole and fractional parts may not be too large to fit into a 64 bit integer. **/

fp64 double_to_fp64(double x);

/*! converts a double to an fpu32

  • Note: the whole and fractional parts may not be too large to fit into a 32 bit integer.
  • Note: do not use negative numbers. **/

fpu32 double_to_fpu32(double x);

/*! converts a double to an fpu64

  • Note: the whole and fractional parts may not be too large to fit into a 64 bit integer.
  • Note: do not use negative numbers. **/

fpu64 double_to_fpu64(double x);

/*! converts a float to an fp32

  • Note: the whole and fractional parts may not be too large to fit into a 32 bit integer. **/

fp32 float_to_fp32(float x);

/*! converts a float to an fp64

  • Note: the whole and fractional parts may not be too large to fit into a 64 bit integer. **/

fp64 float_to_fp64(float x);

/*! converts a float to an fpu32

  • Note: the whole and fractional parts may not be too large to fit into a 32 bit integer.
  • Note: do not use negative numbers. **/

fpu32 float_to_fpu32(float x);

/*! converts a float to an fpu64

  • Note: the whole and fractional parts may not be too large to fit into a 64 bit integer.
  • Note: do not use negative numbers. **/

fpu64 float_to_fpu64(float x);

/*! converts an fp32 to a double */

double fp32_to_double(fp32 x);

/*! converts an fp64 to a double */

double fp64_to_double(fp64 x);

/*! converts an fpu32 to a double */

double fpu32_to_double(fpu32 x);

/*! converts an fpu64 to a double */

double fpu64_to_double(fpu64 x);

/*! converts an fp32 to a float */

float fp32_to_float(fp32 x);

/*! converts an fp64 to a float */

float fp64_to_float(fp64 x);

/*! converts an fpu32 to a float */

float fpu32_to_float(fpu32 x);

/*! converts an fpu64 to a float */

float fpu64_to_float(fpu64 x);

/*! converts a 16 bit signed integer to an fp32 */

fp32 int_to_fp32(int16_t x);

/*! converts a 32 bit signed integer to an fp64 */

fp64 int_to_fp64(int32_t x);

/*! converts a 16 bit unsigned integer to an fpu32 */

fpu32 int_to_fpu32(uint16_t x);

/*! converts a 32 bit unsigned integer to an fpu64 */

fpu64 int_to_fpu64(uint32_t x);

/*! converts an fp32 to an integer;

  • will occasionally return incorrect results for negative numbers or something. **/

int32_t fp32_to_int(fp32 x);

/*! converts an fp64 to an integer;

  • will occasionally return incorrect results for negative numbers or something. **/

int64_t fp64_to_int(fp64 x);

/*! converts an fpu32 to an integer;

  • will occasionally return incorrect results for negative numbers or something. **/

uint32_t fpu32_to_int(fpu32 x);

/*! converts an fpu64 to an integer;

  • will occasionally return incorrect results for negative numbers or something. **/

uint64_t fpu64_to_int(fpu64 x);

extracting the fractional part

/*! extracts the fractional part of an fp32. */

fp32 fp32_fraction(fp32 x);

/*! extracts the fractional part of an fp64. */

fp64 fp64_fraction(fp64 x);

/*! extracts the fractional part of an fpu32. */

fpu32 fpu32_fraction(fpu32 x);

/*! extracts the fractional part of an fpu64. */

fpu64 fpu64_fraction(fpu64 x);

extracting the whole part

/*! extracts the whole part of an fp32.

  • Note: do not call this function with negative numbers. **/

fp32 fp32_whole(fp32 x);

/*! extracts the whole part of an fp64.

  • Note: do not call this function with negative numbers. **/

fp64 fp64_whole(fp64 x);

/*! extracts the whole part of an fpu32.

  • Note: do not call this function with negative numbers. **/

fpu32 fpu32_whole(fpu32 x);

/*! extracts the whole part of an fpu64.

  • Note: do not call this function with negative numbers. **/

fpu64 fpu64_whole(fpu64 x);

Addition

/*! adds two fp32s.

  • Note: be careful not to cause an overflow. **/

fp32 fp32_add(fp32 a, fp32 b);

/*! adds two fp64s.

  • Note: be careful not to cause an overflow. **/

fp64 fp64_add(fp64 a, fp64 b);

/*! adds two fpu32s.

  • Note: be careful not to cause an overflow. **/

fpu32 fpu32_add(fpu32 a, fpu32 b);

/*! adds two fpu64s.

  • Note: be careful not to cause an overflow. **/

fpu64 fpu64_add(fpu64 a, fpu64 b);

Subtraction

/*! subtracts two fp32s.

  • Note: be careful not to cause an underflow. **/

fp32 fp32_sub(fp32 a, fp32 b);

/*! subtracts two fp64s.

  • Note: be careful not to cause an underflow. **/

fp64 fp64_sub(fp64 a, fp64 b);

/*! subtracts two fpu32s.

  • Note: be careful not to cause an underflow. **/

fpu32 fpu32_sub(fpu32 a, fpu32 b);

/*! subtracts two fpu64s.

  • Note: be careful not to cause an underflow. **/

fpu64 fpu64_sub(fpu64 a, fpu64 b);

Multiplication

/*! multiplies two fp32s.

  • Note: be careful not to cause an overflow. **/

fp32 fp32_mul(fp32 a, fp32 b);

/*! multiplies two fp64s.

  • Note: be careful not to cause an overflow. **/

fp64 fp64_mul(fp64 a, fp64 b);

/*! multiplies two fpu32s.

  • Note: be careful not to cause an overflow. **/

fpu32 fpu32_mul(fpu32 a, fpu32 b);

/*! multiplies two fpu64s.

  • Note: be careful not to cause an overflow. **/

fpu64 fpu64_mul(fpu64 a, fpu64 b);

Division

/*! divides two fp32s.

  • Note: a may not be larger than 127.
  • Note: this operation may result in the loss of precision. **/

fp32 fp32_div(fp32 a, fp32 b);

/*! divides two fp64s.

  • Note: a may not be larger than 32767.
  • Note: this operation may result in the loss of precision. **/

fp64 fp64_div(fp64 a, fp64 b);

/*! divides two fpu32s.

  • Note: a may not be larger than 255.
  • Note: this operation may result in the loss of precision. **/

fpu32 fpu32_div(fpu32 a, fpu32 b);

/*! divides two fpu64s.

  • Note: a may not be larger than 65535.
  • Note: this operation may result in the loss of precision. **/

fpu64 fpu64_div(fpu64 a, fpu64 b);

Unit testing framework

features

You can define test cases using the TEST_CASE and the TEST_CASE_END macros.

You can do assertions using the TEST_ASSERT macro.

In the test_functions array you can control which test cases are to be run and in which order they shall be run.

The unit testing framework keeps track of the amount of test cases run and will print how many tests were run after an execution of the tests.

The unit testing framework can determine whether the test run failed or succeeded and will print the appropriate information to the console.

If a test case failed it will print the test case that failed and on which assertion it had failed.

API

TEST_CASE(identifier) : defines a test case called identifier must be ended with TEST_CASE_END

TEST_ASSERT(test) : boolean tests an the expression test if the assertion fails as test did not evaluate to true the test runner will abort the test run and print the failing test case as well as the expression of the assert that failed.

Function bool run_all_tests(void); This function can be run from anywhere and will run all the tests registered in the test_functions array. Returns true if all the test cases ran successfully; returns false otherwise.

building the fixed_point test application

To build the fixed_point test application use

make all term