📄 math.txt
字号:
Sign Res=arg1 op arg2 Program Approx. CYCLES
A:math32.h:
B:math24.h:
C:math16.h: Code min aver max
.B. S 24 = 16 * 16 31 47 47 49
A.. S 32 = 16 * 16 41 55 56 61
A.. - 32 = 16 * 16 27 49 49 49
.B. - 24 = 24 * 24 33 54 54 54
A.. S 32 = 32 * 16 49 70 70 73
A.. - 32 = 32 * 16 43 67 67 67
A.. S/- 32 = 32 * 32 57 87 87 87
ABC - 16 = 16 / 8 19 232 232 232
AB. - 24 = 24 / 8 20 365 365 365
A.. - 32 = 32 / 8 21 514 514 514
ABC - 16 = 16 / 16 22 235 239 283
.B. - 24 = 24 / 16 25 392 407 488
A.. - 32 = 32 / 16 26 549 575 677
.B. - 24 = 24 / 24 29 443 452 563
A.. - 32 = 32 / 32 36 715 729 939
ABC S 16 = 16 / 8 33 193 197 206
AB. S 24 = 24 / 8 35 302 306 317
A.. S 32 = 32 / 8 37 427 432 444
ABC S 16 = 16 / 16 43 244 252 305
.B. S 24 = 24 / 16 45 377 398 464
A.. S 32 = 32 / 16 47 526 557 639
.B. S 24 = 24 / 24 52 452 470 587
A.. S 32 = 32 / 32 61 724 750 965
ABC - 8 = 16 % 8 18 222 222 222
.B. - 8 = 24 % 8 19 352 352 352
A.. - 8 = 32 % 8 20 498 498 498
ABC - 16 = 16 % 16 20 228 231 260
.B. - 16 = 24 % 16 23 374 393 454
A.. - 16 = 32 % 16 24 520 554 632
.B. - 24 = 24 % 24 37 435 442 531
A.. - 32 = 32 % 32 34 706 718 898
ABC S 8 = 16 % 8 26 184 185 187
.B. S 8 = 24 % 8 28 291 291 295
A.. S 8 = 32 % 8 30 413 413 418
ABC S 16 = 16 % 16 40 239 245 278
.B. S 16 = 24 % 16 42 369 381 424
A.. S 16 = 32 % 16 44 515 534 587
.B. S 24 = 24 % 24 50 446 458 550
A.. S 32 = 32 % 32 60 717 736 919
FIXED POINT LIBRARIES
---------------------
math16x.h : 16 bit fixed point, 8_8, signed and unsigned
math24x.h : 24 bit fixed point 8_16, 16_8, signed and unsigned
math32x.h : 32 bit fixed point 8_24, 16_16, 24_8, signed and unsigned
The libraries can be used separately or combined.
The timing stated is measured in instruction cycles (4*clock) and
includes parameter transfer, call, return and assignment of the
return value. The timing values are found by executing a large number
of iterations using selected argument values.
Sign: -: unsigned, S: signed
Sign Res=arg1 op arg2 Program Approx. CYCLES
math16x.h: Code min aver max
S 8_8 = 8_8 * 8_8 44 50 52 56
- 8_8 = 8_8 * 8_8 23 38 38 38
S 8_8 = 8_8 / 8_8 46 422 440 507
- 8_8 = 8_8 / 8_8 29 437 453 533
math24x.h: Code min aver max
S 16_8 = 16_8 * 16_8 74 83 87 91
- 16_8 = 16_8 * 16_8 50 71 71 71
S 16_8 = 16_8 / 16_8 55 686 718 861
- 16_8 = 16_8 / 16_8 36 709 734 901
S 8_16 = 8_16 * 8_16 89 98 102 106
- 8_16 = 8_16 * 8_16 65 86 86 86
S 8_16 = 8_16 / 8_16 55 846 895 1061
- 8_16 = 8_16 / 8_16 36 877 918 1117
math32x.h: Code min aver max
S 24_8 = 24_8 * 24_8 150 124 130 134
- 24_8 = 24_8 * 24_8 123 112 112 112
S 24_8 = 24_8 / 24_8 64 1014 1060 1311
- 24_8 = 24_8 / 24_8 43 1045 1081 1365
S 16_16= 16_16*16_16 135 147 153 157
- 16_16= 16_16*16_16 108 135 135 135
S 16_16= 16_16/16_16 64 1206 1275 1559
- 16_16= 16_16/16_16 43 1245 1303 1629
S 8_24 = 8_24 * 8_24 150 162 168 172
- 8_24 = 8_24 * 8_24 123 150 150 150
S 8_24 = 8_24 / 8_24 64 1398 1491 1807
- 8_24 = 8_24 / 8_24 43 1445 1524 1893
FLOATING POINT LIBRARIES
------------------------
math16f.h : 16 bit floating point basic math
math24f.h : 24 bit floating point basic math
math24lb.h : 24 bit floating point library
math32f.h : 32 bit floating point basic math
math32lb.h : 32 bit floating point library
NOTE: The timing values includes parameter transfer, call and
return and also assignment of the return value.
Basic 32 bit math: Approx. CYCLES
Size min aver max
a * b: multiplication 131 128 132 139
a / b: division 104 441 495 580
a + b: addition 154 38 123 193
a - b: subtraction add+5 45 130 200
int32 -> float32 67 41 66 110
float32 -> int32 80 36 74 137
Basic 24 bit math: Approx. CYCLES
Size min aver max
a * b: multiplication 82 78 81 93
a / b: division 87 270 297 345
a + b: addition 132 32 104 161
a - b: subtraction add+5 39 111 168
int24 -> float24 52 33 60 95
float24 -> int24 70 31 68 112
Basic 16 bit math: Approx. CYCLES
Size min aver max
a * b: multiplication 51 46 49 57
a / b: division 73 121 136 151
a + b: addition 104 26 80 123
a - b: subtraction add+5 33 87 130
int16 -> float16 58 37 65 98
float16 -> int16 53 26 57 96
The following operations are handled by inline code:
assignment, comparing with constants, multiplication and division
by a multiple of 2 (i.e. a*0.5, b * 1024.0, c/4.0)
Floating point library functions:
float24 sqrt(float24); // square root
Input range: positive number including zero
Accuracy: ME:1-2, relative error: < 3*10**-5 (*)
Timing: min aver max 637 706 782 (**)
Size: 153 words + basic 24 bit math library
Minimum complete program example: 382 words
float32 sqrt(float32); // square root
Input range: positive number including zero
Accuracy: ME:1-2, relative error: 1.2*10**-7 (*)
Timing: min aver max 1699 1893 2200 (**)
Size: 168 words + basic 32 bit math library
Minimum complete program example: 580 words
float24 log(float24); // natural log function
Input range: positive number above zero
Accuracy: ME:1, relative error: < 1.5*10**-5 (*)
Timing: min aver max 1210 1714 1985 (**)
Size: 210 words + basic 24 bit math library
Minimum complete program example: 584 words
float32 log(float32); // natural log function
Input range: positive number above zero
Accuracy: ME:1, relative error: < 6*10**-8 (*)
Timing: min aver max 1713 2377 2699 (**)
Size: 264 words + basic 32 bit math library
Minimum complete program example: 743 words
float24 log10(float24); // log10 function
Input range: positive number above zero
Accuracy: ME:1-2, relative error: < 3*10**-5 (*)
Timing: min aver max 1293 1794 2067 (**)
Size: 15 words + size of log()
Minimum complete program example: 599 words
float32 log10(float32); // log10 function
Input range: positive number above zero
Accuracy: ME:1-2, relative error: < 1.2*10**-7 (*)
Timing: min aver max 1840 2502 2793 (**)
Size: 17 words + size of log()
Minimum complete program example: 760 words
float24 exp(float24); // exponential (e**x) function
Input range: -87.3365447506, +88.7228391117
Accuracy: ME:1, relative error: < 1.5*10**-5 (*)
Timing: min aver max 903 1539 1725 (**)
Size: 247 words + 95(floor24) + basic 24 bit math
Minimum complete program example: 641 words
float32 exp(float32); // exponential (e**x) function
Input range: -87.3365447506, +88.7228391117
Accuracy: ME:1, relative error: < 6*10**-8 (*)
Timing: min aver max 1920 2073 2301 (**)
Size: 317 words + 134(floor32) + basic 32 bit math
Minimum complete program example: 834 words
float24 exp10(float24); // 10**x function
Input range: -37.9297794537, +38.531839445
Accuracy: ME:1, relative error: < 1.5*10**-5 (*)
Timing: min aver max 917 1610 1739 (**)
Size: 254 words + 95(floor24) + basic 24 bit math
Minimum complete program example: 648 words
float32 exp10(float32); // 10**x function
Input range: -37.9297794537, +38.531839445
Accuracy: ME:1, relative error: < 6*10**-8 (*)
Timing: min aver max 1565 2086 2335 (**)
Size: 323 words + 134(floor32) + basic 32 bit math
Minimum complete program example: 840 words
float24 sin(float24); // sine function, input in radians
float24 cos(float24); // cosine function, input in radians
Input range: -512.0, +512.0
Accuracy: error: < 3*10**-5 (*)
The relative error can be larger when the output is near
0 (for example near sin(2*PI)), but the absolute error is
lower than the stated value.
Timing: min aver max 374 1195 1396 (**)
Size: 211 words + basic 24 bit math library
Minimum complete program example: 563 words
float32 sin(float32); // sine function, input in radians
float32 cos(float32); // cosine function, input in radians
Input range: -512.0, +512.0
* can be used over a much wider range if lower accuracy
is accepted (degrades gradually to 1 significant
decimal digit at input value 10**6)
Accuracy: error: < 1.2*10**-7 (*)
The relative error can be larger when the output is near
0 (for example near sin(2*PI)), but the absolute error is
lower than the stated value.
Timing: min aver max 1789 2193 2529 (**)
Size: 352 words + basic 32 bit math library
Minimum complete program example: 807 words
(*) The accuracy of the math functions have been checked using
many thousands of calculations. ME=1 means that the mantissa
value can be wrong by +/- 1 (i.e. 1 bit). The relative error
is then 1.5*10**-5 for 24 bit floating point, and 6*10**-8
for 32 bit floating point. Only a small fraction of the
calculations may have the stated error.
(**) The min and max timing stated have been found by simulating
many thousands calculations. However, the min and max
limits are not quaranteed to be correct. When using a 4 MHz
oscillator, one instruction cycle is 1 microsecond.
FAST AND COMPACT INLINE OPERATIONS
----------------------------------
The compiler will use inline code for efficiency at some
important operations:
Integer:
- converting to left and right shifts: a*8, a/2
- selecting high/low bytes/words: a/256, a%256, b%0x10000
- replacing remainder by AND operation: a%64, a%0x80
Fixed Point:
- converting to left and right shifts: a*8, a/2
- all operations except multiplication and division are
implemented inline
Floating point:
- add/sub (incr/decr) of exponent: a*128.0, a/2
- operations == and != : a==b, a!=0.0
- comparing with constants: a>0, a<=10.0
- inverting the sign bit: a=-a, b=-a
FIXED POINT EXAMPLE
-------------------
#pragma chip PIC18C242
#include "math24x.h"
uns16 data;
fixed16_8 tx, av, mg, a, vx, prev, kp;
void main(void)
{
vx = 3.127;
tx += data; // automatic type cast
data = kp; // assign integer part
if (tx < 0)
tx = -tx; // make positive
av = tx/20.0;
mg = av * 1.25;
a = mg * 0.98; // 0.980469: error on constant: 0.000478
prev = vx;
vx = a/5.0 + prev;
kp = vx * 0.036; // 0.03515626: error on constant: 0.024
kp = vx / (1.0/0.036); // 27.7773437 error on constant: 0.0000156
}
// CODE: 266 code words including library (129)
FLOATING POINT EXAMPLE
----------------------
// CODE: 596 code words including library (424)
// The statements are identical to the above fixed point example
// to enable code size comparison
#pragma chip PIC18C242
#include "math24f.h"
uns16 data;
float tx, av, mg, a, vx, prev, kp;
void main(void)
{
InitFpFlags(); // enable rounding as default
vx = 3.127;
tx += data; // automatic type cast
data = kp; // assign integer part
if (tx < 0)
tx = -tx; // make positive
av = tx/20.0;
mg = av * 1.25;
a = mg * 0.98;
prev = vx;
vx = a/5.0 + prev;
kp = vx * 0.036;
kp = vx / (1.0/0.036);
}
HOW TO SAVE CODE
----------------
Choices that influence code size:
1. What libraries to include (24/32 bit float or fixed point)
2. Rounding can be disabled permanently.
#define DISABLE_ROUNDING
#include "math32f.h"
3. Optimization, currently available on division only.
Note that speed is default. Also note that the saving
is only 5 - 7 instructions. Timing difference is up
to 15-20 percent.
#define FP_OPTIM_SIZE // optimize for SIZE
#define FP_OPTIM_SPEED // optimize for SPEED: default
The recommended strategy is to select a main library for the
demanding math operations. Different floating and fixed point
operations should only be mixed if there is a good reason for it.
Mixing different data types is possible to save code and RAM
space. For example using a small type in an array and a larger
type for the math operations.
So, first decide what math library to include. For floating point
the main decision is between the 24 bit or the 32 bit library.
If you use 32 bit operations, this can be combined with 24 (and 16)
bit floating point types to save RAM.
Automatic type conversion:
integer <-> float/double
integer <-> fixed point
float <-> double
fixed point <-> float/double : requires additional functions
In general, using the smallest possible data type will save code
and RAM space. This must be balanced against the extra work to
analyze the program to prevent overflow and too large accumulated
errors. If there is plenty of code space in the controller, and
timing is no problem, then large types can be used. Otherwise
analysis is required to get optimal selections.
It is recommended to keep the number of called library functions
as low as possible. Although function selection is done
automatically by the compiler, it is possible to use type casts or
even make a custom library by copying the required functions from
existing libraries. All libraries are written in C. CC8E can print
a warning for each operator function that is called (option -wO).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -