Skip to content

Commit 0656856

Browse files
author
Geoffrey Hunter
committed
Merge branch 'develop'
2 parents 240b857 + eaa7cd2 commit 0656856

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2382
-2023
lines changed

.vscode/c_cpp_properties.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"/usr/include",
7+
"/usr/local/include",
8+
"${workspaceRoot}"
9+
],
10+
"defines": [],
11+
"intelliSenseMode": "clang-x64",
12+
"browse": {
13+
"path": [
14+
"/usr/include",
15+
"/usr/local/include",
16+
"${workspaceRoot}",
17+
"${workspaceRoot}/build/external/include"
18+
],
19+
"limitSymbolsToIncludedHeaders": true,
20+
"databaseFilename": ""
21+
},
22+
"macFrameworkPath": [
23+
"/System/Library/Frameworks",
24+
"/Library/Frameworks"
25+
]
26+
},
27+
{
28+
"name": "Linux",
29+
"includePath": [
30+
"/usr/include/c++/6",
31+
"/usr/include/x86_64-linux-gnu/c++/6",
32+
"/usr/include/c++/6/backward",
33+
"/usr/lib/gcc/x86_64-linux-gnu/6/include",
34+
"/usr/local/include",
35+
"/usr/lib/gcc/x86_64-linux-gnu/6/include-fixed",
36+
"/usr/include/x86_64-linux-gnu",
37+
"/usr/include",
38+
"${workspaceRoot}",
39+
"${workspaceRoot}/include"
40+
],
41+
"defines": [],
42+
"intelliSenseMode": "clang-x64",
43+
"browse": {
44+
"path": [
45+
"/usr/include/c++/6",
46+
"/usr/include/x86_64-linux-gnu/c++/6",
47+
"/usr/include/c++/6/backward",
48+
"/usr/lib/gcc/x86_64-linux-gnu/6/include",
49+
"/usr/local/include",
50+
"/usr/lib/gcc/x86_64-linux-gnu/6/include-fixed",
51+
"/usr/include/x86_64-linux-gnu",
52+
"/usr/include",
53+
"${workspaceRoot}",
54+
"${workspaceRoot}/include"
55+
],
56+
"limitSymbolsToIncludedHeaders": true,
57+
"databaseFilename": ""
58+
}
59+
},
60+
{
61+
"name": "Win32",
62+
"includePath": [
63+
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
64+
"${workspaceRoot}"
65+
],
66+
"defines": [
67+
"_DEBUG",
68+
"UNICODE"
69+
],
70+
"intelliSenseMode": "msvc-x64",
71+
"browse": {
72+
"path": [
73+
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
74+
"${workspaceRoot}"
75+
],
76+
"limitSymbolsToIncludedHeaders": true,
77+
"databaseFilename": ""
78+
}
79+
}
80+
],
81+
"version": 3
82+
}

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [v8.0.0-beta.1] - 2018-01-09
10+
11+
### Added
12+
- Added Visual Studio Code project files, closes #89.
13+
- Added generic fixed-point classes (both slow and fast) with storage type for bits (aka base type) and overflow type templated, closes #90.
14+
15+
### Changed
16+
- Improved the comments and code formatting of Fp32f.hpp.
17+
18+
### Removed
19+
- All existing fixed-point classes! This is in favour of the generic fixed-point classes mentioned in the 'Added' section.
20+
921
## [v7.0.1] - 2018-01-04
1022

1123
### Added

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
99

1010
include(CodeCoverage)
1111

12+
set(HEADER_ONLY true)
13+
1214
#=================================================================================================#
1315
#========================================= INPUT ARGUMENTS =======================================#
1416
#=================================================================================================#
@@ -53,7 +55,10 @@ endif ()
5355
#include_directories(../)
5456
include_directories(include)
5557

56-
add_subdirectory(src)
58+
if(!HEADER_ONLY)
59+
add_subdirectory(src)
60+
endif()
61+
5762
if(BUILD_TESTS)
5863
add_subdirectory(test)
5964
endif()

README.rst

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,114 @@ A microcontroller-friendly fixed-point library specifically designed for embedde
1717
Description
1818
===========
1919

20-
32-bit and 64-bit fixed-point libraries for fast arithmetic operations. Suitable for performing computationally intensive operations
21-
on a computing platform that does not have a floating-point unit (like most smaller embedded systems, such as Cortex-M3, CortexM0,
22-
ATmega, PSoC 5, PSoC 5 LP, PSoC 4, Arduino platforms e.t.c). Common applications include BLDC motor control and image processing.
23-
Best performance on a 32-bit or higher architecture (although 8-bit architectures should still be fine).
20+
MFixedPoint is a header-only fixed-point C++ library suitable for fast arithmetic operations on systems which don't have a FPU (e.g. embedded systems). Suitable for performing computationally intensive operations on a computing platform that does not have a floating-point unit (like most smaller embedded systems, such as Cortex-M3, CortexM0, ATmega, PSoC 5, PSoC 5 LP, PSoC 4, Arduino platforms e.t.c). Common applications include BLDC motor control and image processing. Best performance on a 32-bit or higher architecture (although 8-bit architectures should still be fine).
2421

25-
The libraries are designed to be a fully-functional data types within their limits (e.g. supports operator overloads and implicit/explicit casting). Can be used with
26-
most libraries that use data type templates.
22+
The libraries are designed to be a fully-functional data types within their limits (e.g. supports operator overloads and implicit/explicit casting). Can be used with most libraries that use data type templates.
2723

28-
Fixed-point numbers are signed. Q is the number of bits used for the decimal part of the number (the rest are used for the integer part). Q can vary from 0 up to the bit-width of the fixed-point number.
24+
Fixed-point numbers are signed.
2925

30-
The 32-bit Libraries (Fp32f, Fp32s)
31-
-----------------------------------
26+
NOTE: This fixed point library will usually be slower when running of a CPU which has associated floating point unit (FPU), e.g. when running on your desktop/laptop. The benchmark performance tests (found in :code:`benchmark/`) suggest simple fixed-point operations such as addition/subtraction/multiplication/division are about 10x slower than their float/double counterparts when there is a FPU. However, this library is designed to be used on CPU's where there is no FPU present. This library comes in useful when there is no FPU present, which is the case for lower-end microcontrollers such as ARM Cortex M0/M3, Atmel ATMEGA, TI MSP430's e.t.c.
3227

33-
Intermediary overflows are protected with int64_t casting, end-result overflows will wrap like usual.
28+
The "Slow" Fixed-Point Library (FpS)
29+
------------------------------------
3430

35-
The 64-bit Libraries (Fp64f, Fp64s)
36-
-----------------------------------
31+
The "slow" fixed-point class is called :code:`FpS` (note that this class is not that slow, and is the recommend fixed-point class for almost all use cases). It allows for airthemtic between two fixed-point numbers that have different numbers of fractional bits. The underlying storage type of the fixed-point number and the overflow type are provided as the template parameters.
3732

38-
Intermediary overflows are **NOT** protected from overflowing, due to the inability of intermediate casting to :code:`int128_t` on most embedded platforms.
33+
It is recommended that you use one of the predefined :code:`FpSxx` aliases (available with :code:`#include <MFixedPoint/FpS.h>`), which include:
3934

40-
On any 32-bit or lower architecture, 64-bit numbers will be slower than 32-bit numbers. Use only if 32-bit numbers don't offer
41-
the range/precision required.
35+
.. code:: cpp
4236
43-
The Fast Libraries (Fp32f, Fp64f)
44-
---------------------------------
37+
FpS8
38+
FpS16
39+
FpS32
40+
FpS64 // Note: FpS64 is not protected from intermediatary overflows!
4541
46-
The number of bits used for the decimal part of the number (Q) is given as a template parameter (e.g. :code:`Fp32f<12>(3.4)` will create the number 3.4 with 12 bits of decimal precision). It is not stored in the fixed-point object. This gives the fastest possible arithmetic speeds, at the expense of loosing some functionality and a tad more code space.
42+
The number of fractional bits is stored in the fixed-point object (it is a template parameter in the fast library). This gives slightly slower arithmetic speed than the fast library, but allows for more functionality and should use less code space..
4743

48-
You have to be aware that when adding numbers with different Q, you have to perform the bit-shifting yourself. Also, if you want to convert a fast fixed-point number to a double, you cannot use a cast (e.g. :code:`(double)myFp32fNum` won't work, you have to use provided functions (e.g. :code:`Fix32ToDouble(myFp32fNum);`).
44+
The extra functionality includes the ability to add two numbers with a different number of fractional bits transparently, and to ability to cast the fixed-point number into different types (e.g. :code:`(double)myFpSNum` will convert the number to a double).
45+
46+
When adding two fixed-point numbers which have a different number of fractional bits, the result's number of fractional bits is always that of lowest of the two operands. For example :code:`FpS32(3.4, 10) + FpS32(1.2, 14)` will result in same object being created as would the code :code:`FpS32(4.6, 10)`.
47+
48+
Casting to an :code:`int` rounds to negative infinity; e.g. 5.67 becomes 5, and -12.2 becomes -13.
49+
50+
Create a fixed point number:
51+
52+
.. code:: cpp
53+
54+
#include "MFixedPoint/FpS.hpp"
55+
56+
// Create a 32-bit fixed-point number.
57+
// Assign a value of 12.34
58+
// Use 8 bits for the fractional part, leaving 24 for the integer part.
59+
FpS32 fp1(12.34, 8);
60+
61+
62+
Addition/Subtraction/Multiplication/Division:
63+
64+
.. code:: cpp
65+
66+
FpS32 fp1(5.0, 8);
67+
FpS32 fp2(1.5, 8);
68+
69+
printf("add = %.2f\n", (fp1 + fp2).ToDouble()); // Prints "add = 6.50"
70+
printf("sub = %.2f\n", (fp1 - fp2).ToDouble()); // Prints "sub = 3.50"
71+
printf("mult = %.2f\n", (fp1 * fp2).ToDouble()); // Prints "mult = 7.50"
72+
printf("div = %.2f\n", (fp1 / fp2).ToDouble()); // Prints "div = 3.33"
73+
74+
Modulus:
75+
76+
.. code:: cpp
4977
50-
The Slow Libraries (Fp32s, Fp64s)
51-
---------------------------------
78+
FpS32 fp1(5.1, 10);
79+
FpS32 fp2(1.5, 8);
5280
53-
The number of bits used for the decimal part of the number (Q) is given as a function argument (e.g. :code:`Fp32s(3.4, 12)` will create the number 3.4 with 12 bits of decimal precision). The Q is stored in the fixed-point object (it is a template parameter in the fast libraries). This gives slightly slower arithmetic speed than the fast libraries, but allows for more functionality and should use less code space..
81+
printf("mod = %.2f\n", (fp1 % fp2).ToDouble()); // Prints "mod = 0.60"
5482
55-
The extra functionality includes the ability to add two numbers with a different Q transparently, and to ability to cast the fixed-point number into different types (e.g. :code:`(double)myFp32sNum` will convert the number to a double).
83+
Conversion/Casting:
5684

57-
When adding two fixed-point numbers which have a different Q, the result's Q is always that of lowest Q of the two operands. For example :code:`Fp32s(3.4, 10) + Fp32s(1.2, 14)` will result in same object being created as would the code :code:`Fp32s(4.6, 10)`.
85+
.. code:: cpp
5886
59-
Casting to an :code:`int` rounds down to the nearest integer; e.g. 5.67 becomes 5, and -12.2 becomes -13.
87+
FpS32 fp1(2.22, 8);
88+
89+
// Using the ToXXX() functions...
90+
printf("ToInt<int32_t>() = %i\n", fp1.ToInt<int32_t>()); // Prints "ToInt<int32_t>() = 2"
91+
printf("ToDouble() = %.2f\n", fp1.ToDouble()); // Prints "ToDouble() = 2.22"
92+
93+
// Direct casting is also supported
94+
printf("(int32_t)fp1 = %i\n", (int32_t)fp1); // Prints "(int32_t)fp1 = 2"
95+
printf("(double)fp1 = %.2f\n", (double)fp1); // Prints "(double)fp1 = 2.22"
96+
97+
98+
Overflows
99+
---------
100+
101+
:code:`FpS8, FpS16, FpS32` are protected from intermediary overflows. :code:`FpS64` is not, due to the lack of a :code:`int128_t` type on most embeded platforms.
102+
103+
On any 32-bit architecture, :code:`FpS64` numbers will be slower than :code:`FpS64` numbers. Use only if 32-bit numbers don't offer the range/precision required.
104+
105+
The "Fast" Fixed-Point Library (FpF)
106+
------------------------------------
107+
108+
The number of fractional bits is given as a template parameter (e.g. :code:`FpF<int32_t, 12>(3.4)` will create the number 3.4 with 12 bits of decimal precision). It is not stored in the fixed-point object. This gives the fastest possible arithmetic speeds, at the expense of loosing some functionality and a tad more code space.
109+
110+
You have to be aware that when adding numbers with different Q, you have to perform the bit-shifting yourself. Also, if you want to convert a fast fixed-point number to a double, you cannot use a cast (e.g. :code:`(double)myFp32fNum` won't work, you have to use provided functions (e.g. :code:`Fix32ToDouble(myFp32fNum);`).
60111

61112
Benchmarking
62113
============
63114

64115
This library contains a benchmarking program in :code:`benchmark/` which runs operations on the fixed-point libraries and reports back on their performance. It is run automatically as part of :code:`make all`.
65116

66-
Do not pay much attention to the benchmarking results when run on a pre-emptive OS such as Linux.
117+
The benchmarking is compared to software-based float arithmetic (using the custom header SoftFloat.hpp), since most benchmarking will be run on a development computer which has an FPU which will be used if float + float was written in code. If benchmarking on a device which does not have an FPU, you should compare the fixed-point operations against the native software float arithmetic implementation instead. Software-based 32-bit float addition and multiplication are performed and compared with the equivalent fixed-point operations.
67118

68119
Platform Independent
69120
====================
70121

71122
The library is designed to be platform independent. Port-specific functions are declared in separate files, Port.cpp and Port.hpp. These files include functions for printing debug information. Fill in the functions as desired.
72123

73124
This library has been tested on:
74-
- An ARM Cortex-M3 microcontroller
75-
- Linux
125+
126+
- ARM Cortex-M3 microcontrollers
127+
- Linux (Ubuntu)
76128
- A CodeAnywhere "DevBox"
77129

78130
Configuration
@@ -88,7 +140,7 @@ Either use cmake with the provided :code:`CMakeLists.txt` in the root directory,
88140
The cmake method will build the fixed point library and automatically runs all unit tests and the benchmark program.
89141

90142

91-
::
143+
.. code:: bash
92144
93145
~$ git clone https://github.com/mbedded-ninja/MFixedPoint.git
94146
~$ cd MFixedPoint
@@ -99,7 +151,7 @@ The cmake method will build the fixed point library and automatically runs all u
99151
100152
You can then the tests by calling:
101153

102-
::
154+
.. code:: bash
103155
104156
~/MFixedPoint/build$ ./test/MFixedPointTests
105157
@@ -108,7 +160,7 @@ Usage
108160

109161
See the unit tests in :code:`test/` for more usage examples!
110162

111-
::
163+
.. code:: cpp
112164
113165
// Include the API header which provides access to all of the fixed-point
114166
// data types
@@ -150,6 +202,11 @@ See the unit tests in :code:`test/` for more usage examples!
150202
return 0;
151203
}
152204
205+
Visual Studio Code
206+
==================
207+
208+
Project files for Visual Studio Code are included in this repository. Include paths have been added to :code:`c_cpp_properties.json` to improve auto-complete. This includes the directory :code:`${workspaceRoot}/build/external/include` (which contains the 3rd party libraries MFixedPoint depends on that are automatically downloaded by CMake) but is only valid once CMake has been run at least once from with a build directory called :code:`build`.
209+
153210
Code Dependencies
154211
=================
155212

benchmark/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ file(GLOB_RECURSE MFixedPoint_Benchmark_SRC
33
"*.hpp")
44

55
add_executable (MFixedPoint_Benchmark ${MFixedPoint_Benchmark_SRC})
6-
6+
target_compile_options(MFixedPoint_Benchmark PUBLIC -Wall)
77

88
target_link_libraries(MFixedPoint_Benchmark LINK_PUBLIC MFixedPoint)
99

0 commit comments

Comments
 (0)