1 | /* |
2 | Copyright (c) 2012, Broadcom Europe Ltd |
3 | All rights reserved. |
4 | |
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the following conditions are met: |
7 | * Redistributions of source code must retain the above copyright |
8 | notice, this list of conditions and the following disclaimer. |
9 | * Redistributions in binary form must reproduce the above copyright |
10 | notice, this list of conditions and the following disclaimer in the |
11 | documentation and/or other materials provided with the distribution. |
12 | * Neither the name of the copyright holder nor the |
13 | names of its contributors may be used to endorse or promote products |
14 | derived from this software without specific prior written permission. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #ifndef KHRN_INT_MATH_H |
29 | #define KHRN_INT_MATH_H |
30 | |
31 | #include "interface/khronos/common/khrn_int_util.h" |
32 | #include <math.h> |
33 | #ifndef __VIDEOCORE__ // threadsx/nucleus define LONG which clashses |
34 | #include "interface/vcos/vcos.h" |
35 | #endif |
36 | |
37 | #define PI 3.1415926535897932384626433832795f |
38 | #define SQRT_2 1.4142135623730950488016887242097f |
39 | #define EPS 1.0e-10f |
40 | |
41 | static INLINE float floor_(float x) |
42 | { |
43 | return floorf(x); |
44 | } |
45 | |
46 | static INLINE float ceil_(float x) |
47 | { |
48 | return ceilf(x); |
49 | } |
50 | |
51 | /* |
52 | Preconditions: |
53 | |
54 | - |
55 | |
56 | Postconditions: |
57 | |
58 | returns the magnitude of x. returns +infinity if x is infinite. returns a nan |
59 | if x is nan. |
60 | */ |
61 | |
62 | static INLINE float absf_(float x) |
63 | { |
64 | return (x < 0.0f) ? -x : x; |
65 | } |
66 | |
67 | static INLINE float modf_(float x, float y) |
68 | { |
69 | return fmodf(x, y); |
70 | } |
71 | |
72 | static INLINE void sin_cos_(float *s, float *c, float angle) |
73 | { |
74 | *s = sinf(angle); |
75 | *c = cosf(angle); |
76 | } |
77 | |
78 | static INLINE float sin_(float angle) |
79 | { |
80 | return sinf(angle); |
81 | } |
82 | |
83 | static INLINE float cos_(float angle) |
84 | { |
85 | return cosf(angle); |
86 | } |
87 | |
88 | static INLINE float atan2_(float y, float x) |
89 | { |
90 | return atan2f(y, x); |
91 | } |
92 | |
93 | extern float acos_(float x); |
94 | |
95 | static INLINE float exp_(float x) |
96 | { |
97 | return expf(x); |
98 | } |
99 | |
100 | extern float mod_one_(float x); |
101 | |
102 | #ifdef _VIDEOCORE |
103 | #include <vc/intrinsics.h> |
104 | |
105 | static INLINE float nan_recip_(float x) |
106 | { |
107 | float est = _frecip(x); /* initial estimate, accurate to ~12 bits */ |
108 | return est * (2.0f - (x * est)); /* refine with newton-raphson to get accuracy to ~24 bits */ |
109 | } |
110 | |
111 | static INLINE float rsqrt_(float x) |
112 | { |
113 | #ifndef NDEBUG |
114 | vcos_verify(x > 0.0f); |
115 | #endif |
116 | float est = _frsqrt(x); /* initial estimate, accurate to ~12 bits */ |
117 | return est * (1.5f - ((x * 0.5f) * est * est)); /* refine with newton-raphson to get accuracy to ~24 bits */ |
118 | } |
119 | #else |
120 | static INLINE float nan_recip_(float x) |
121 | { |
122 | return 1.0f / x; |
123 | } |
124 | |
125 | static INLINE float rsqrt_(float x) |
126 | { |
127 | #ifndef NDEBUG |
128 | vcos_verify(x > 0.0f); |
129 | #endif |
130 | return 1.0f / sqrtf(x); |
131 | } |
132 | #endif |
133 | |
134 | static INLINE float recip_(float x) |
135 | { |
136 | #ifndef NDEBUG |
137 | vcos_verify(x != 0.0f); |
138 | #endif |
139 | return nan_recip_(x); |
140 | } |
141 | |
142 | static INLINE bool is_nan_(float x) |
143 | { |
144 | uint32_t bits = float_to_bits(x); |
145 | return ((bits & 0x7f800000) == 0x7f800000) && /* max exponent */ |
146 | (bits << 9); /* non-zero mantissa */ |
147 | } |
148 | |
149 | static INLINE bool nan_lt_(float x, float y) |
150 | { |
151 | return |
152 | #ifndef KHRN_NAN_COMPARISONS_CORRECT |
153 | !is_nan_(x) && !is_nan_(y) && |
154 | #endif |
155 | (x < y); |
156 | } |
157 | |
158 | static INLINE bool nan_gt_(float x, float y) |
159 | { |
160 | return |
161 | #ifndef KHRN_NAN_COMPARISONS_CORRECT |
162 | !is_nan_(x) && !is_nan_(y) && |
163 | #endif |
164 | (x > y); |
165 | } |
166 | |
167 | static INLINE bool nan_ne_(float x, float y) |
168 | { |
169 | return |
170 | #ifndef KHRN_NAN_COMPARISONS_CORRECT |
171 | !is_nan_(x) && !is_nan_(y) && |
172 | #endif |
173 | (x != y); |
174 | } |
175 | |
176 | static INLINE float sqrt_(float x) |
177 | { |
178 | vcos_assert(!nan_lt_(x, 0.0f)); |
179 | return sqrtf(x); |
180 | } |
181 | |
182 | #endif |
183 | |