-
Notifications
You must be signed in to change notification settings - Fork 17
/
math_log10f.c
135 lines (109 loc) · 3.67 KB
/
math_log10f.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
The MIT License (MIT)
Copyright (c) 2015 Lachlan Tychsen-Smith ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Based on:
log10(x) = log10((1+m) * (2^n))
log(x) = n * log10(2) + log10(1 + m)
log(1+m) = Poly(1+m)
where Poly(x) is the Minimax approximation of log10(x) over the
range [1, 2]
Test func : log10f(x)
Test Range: 1 < x < 10000
Peak Error: ~0.000040%
RMS Error: ~0.000008%
*/
#include "math.h"
#include "math_neon.h"
const float __log10f_rng = 0.3010299957f;
const float __log10f_lut[8] = {
-0.99697286229624, //p0
-1.07301643912502, //p4
-2.46980061535534, //p2
-0.07176870463131, //p6
2.247870219989470, //p1
0.366547581117400, //p5
1.991005185100089, //p3
0.006135635201050, //p7
};
float log10f_c(float x)
{
float a, b, c, d, xx;
int m;
union {
float f;
int i;
} r;
//extract exponent
r.f = x;
m = (r.i >> 23);
m = m - 127;
r.i = r.i - (m << 23);
//Taylor Polynomial (Estrins)
xx = r.f * r.f;
a = (__log10f_lut[4] * r.f) + (__log10f_lut[0]);
b = (__log10f_lut[6] * r.f) + (__log10f_lut[2]);
c = (__log10f_lut[5] * r.f) + (__log10f_lut[1]);
d = (__log10f_lut[7] * r.f) + (__log10f_lut[3]);
a = a + b * xx;
c = c + d * xx;
xx = xx * xx;
r.f = a + c * xx;
//add exponent
r.f = r.f + ((float) m) * __log10f_rng;
return r.f;
}
float log10f_neon_hfp(float x)
{
#ifdef __MATH_NEON
asm volatile (
"vdup.f32 d0, d0[0] \n\t" //d0 = {x,x};
//extract exponent
"vmov.i32 d2, #127 \n\t" //d2 = 127;
"vshr.u32 d6, d0, #23 \n\t" //d6 = d0 >> 23;
"vsub.i32 d6, d6, d2 \n\t" //d6 = d6 - d2;
"vshl.u32 d1, d6, #23 \n\t" //d1 = d6 << 23;
"vsub.i32 d0, d0, d1 \n\t" //d0 = d0 + d1;
//polynomial:
"vmul.f32 d1, d0, d0 \n\t" //d1 = d0*d0 = {x^2, x^2}
"vld1.32 {d2, d3, d4, d5}, [%1] \n\t" //q1 = {p0, p4, p2, p6}, q2 = {p1, p5, p3, p7} ;
"vmla.f32 q1, q2, d0[0] \n\t" //q1 = q1 + q2 * d0[0]
"vmla.f32 d2, d3, d1[0] \n\t" //d2 = d2 + d3 * d1[0]
"vmul.f32 d1, d1, d1 \n\t" //d1 = d1 * d1 = {x^4, x^4}
"vmla.f32 d2, d1, d2[1] \n\t" //d2 = d2 + d1 * d2[1]
//add exponent
"vdup.32 d7, %0 \n\t" //d7 = {rng, rng}
"vcvt.f32.s32 d6, d6 \n\t" //d6 = (float) d6
"vmla.f32 d2, d6, d7 \n\t" //d2 = d2 + d6 * d7
"vmov.f32 s0, s4 \n\t" //s0 = s4
:: "r"(__log10f_rng), "r"(__log10f_lut)
: "d0", "d1", "q1", "q2", "d6", "d7"
);
#endif
}
float log10f_neon_sfp(float x)
{
#ifdef __MATH_NEON
asm volatile ("vmov.f32 s0, r0 \n\t");
log10f_neon_hfp(x);
asm volatile ("vmov.f32 r0, s0 \n\t");
#else
return log10f_c(x);
#endif
};