๐Ÿ“ฆ orlp / commonc

๐Ÿ“„ math.c ยท 379 lines
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379/*

    Copyright 2012 Orson Peters. All rights reserved.

    Redistribution of this work, with or without modification, is permitted if
    Orson Peters is attributed as the original author or licensor of
    this work, but not in any way that suggests that Orson Peters endorses
    you or your use of the work.

    This work is provided by Orson Peters "as is" and any express or implied
    warranties are disclaimed. Orson Peters is not liable for any damage
    arising in any way out of the use of this work.

*/

#include "commonc/config.h"
#include "commonc/math.h"
#include "commonc/endian.h"
#include "commonc/integer.h"

/* Returns the larger of a and b. */

int cc_max(int a, int b) {
    if (a > b) return a;
    return b;
}


long cc_maxl(long a, long b) {
    if (a > b) return a;
    return b;
}


long long cc_maxll(long long a, long long b) {
    if (a > b) return a;
    return b;
}


unsigned int cc_maxu(unsigned int a, unsigned int b) {
    if (a > b) return a;
    return b;
}


unsigned long cc_maxul(unsigned long a, unsigned long b) {
    if (a > b) return a;
    return b;
}


unsigned long long cc_maxull(unsigned long long a, unsigned long long b) {
    if (a > b) return a;
    return b;
}


double cc_fmax(double a, double b) {
    if (a > b) return a;
    return b;
}


float cc_fmaxf(float a, float b) {
    if (a > b) return a;
    return b;
}

/*

    int cc_copysign(int a, int b);
    long cc_copysignl(long a, long b);
    long long cc_copysignll(long long a, long long b);
    double cc_fcopysign(double a, double b);
    float cc_fcopysignf(float a, float b);

    Returns a with the sign of b.

*/


inline int cc_copysign(int a, int b) {
    #ifdef CC_NO_BRANCH
    return ((b > 0) - (b < 0)) * a;
    #else
    if (!b) return 0;
    if ((a ^ b) < 0) a = -a;

    return a;
    #endif
}


inline long cc_copysignl(long a, long b) {
    #ifdef CC_NO_BRANCH
    return ((b > 0) - (b < 0)) * a;
    #else
    if (!b) return 0;
    if ((a ^ b) < 0) a = -a;

    return a;
    #endif
}

inline long long cc_copysignll(long long a, long long b) {
    #ifdef CC_NO_BRANCH
    return ((b > 0) - (b < 0)) * a;
    #else
    if (!b) return 0;
    if ((a ^ b) < 0) a = -a;

    return a;
    #endif
}


inline double cc_fcopysign(double a, double b) {
    /*#ifdef CC_IEEE754
    union {
        double d;
        uint64_t l;

        #ifdef CC_LITTLE_ENDIAN
        struct {
            uint32_t lw;
            uint32_t hw;
        };
        #else
        struct {
            uint32_t hw;
            uint32_t lw;
        };
        #endif
    } al, bl;

    al.d = a;
    bl.d = b;

    al.hw &= 0x7FFFFFFF;
    al.hw |= bl.hw & 0x80000000;

    return al.d;
    #else*/

    return ((b > 0) - (b < 0)) * a;

    /*#endif*/
}


inline float cc_fcopysignf(float a, float b) {
    /*#ifdef CC_IEEE754
    union {
        float f;
        uint32_t i;
    } ai, bi;

    ai.f = a;
    bi.f = b;

    ai.i &= 0x7FFFFFFF;
    ai.i |= bi.i & 0x80000000;

    return ai.f;
    #else*/

    return ((b > 0) - (b < 0)) * a;

    /*#endif*/
}


int cc_sign(int x) {
    return (x > 0) - (x < 0);
}


int cc_signl(long x) {
    return (x > 0) - (x < 0);
}


int cc_signll(long long x) {
    return (x > 0) - (x < 0);
}


int cc_fsign(double x) {
    return (x > 0.0) - (x < 0.0);
}


int cc_fsignf(float x) {
    return (x > 0.0f) - (x < 0.0f);
}




/*
    Returns the absolute value of x (always positive)
*/




int cc_abs(int x) {
    if (x < 0) x = -x;

    return x;
}


long cc_absl(long x) {
    if (x < 0) x = -x;

    return x;
}


long long cc_absll(long long x) {
    if (x < 0) x = -x;

    return x;
}


float cc_fabsf(float x) {
    if (x < 0.0f) x = -x;

    return x;
}


double cc_fabs(double x) {
    if (x < 0.0) x = -x;

    return x;
}


/*

    long cc_round(double x);
    long cc_roundf(float x);

    Rounds x to the nearest integer (both negative and positive). Warning, this _may_ use "banker" rounding, that means 0.5 doesn't always get rounded up, instead if gets rounded to the nearest even integer. Note the _may_, if either CC_IEEE754 or CC_DOUBLE_PREC is not defined than it uses regular up-rounding. This version can be many times faster that simply casting to long.

*/


long cc_round(double x) {
    #if defined(CC_IEEE754) && defined(CC_DOUBLE_PREC)
    const double magic_round = 6755399441055744.0; /* http://stereopsis.com/sree/fpu2006.html */

    union {
        double d;

        struct {
            #ifdef CC_LITTLE_ENDIAN
            uint32_t lw;
            uint32_t hw;
            #else
            uint32_t hw;
            uint32_t lw;
            #endif
        };
    } bit_hack;

    bit_hack.d = x;
    bit_hack.d += magic_round;

    return bit_hack.lw;

    #else

    return (long) (x + cc_fcopysign(0.5, x));

    #endif
}

/*

    double cc_sin(double x);
    double cc_sin_limrange(double x);
    float cc_sinf(float x);
    float cc_sinf_limrange(float x);

    Approximation of sin(x) by the formula f(x) = ax^5 + bx^3 + cx

    Constants chosen such that:

        f(pi/2) = 1
        f'(pi/2) = 0
        f(pi/6) = 1/2

    Resulting in:

        a =   9 / (4  * pi^5)
        b = -41 / (8  * pi^3)
        c = 201 / (64 * pi)

    Worst error: 0.000296
    Average error: 0.000124
    Average relative error: 0.02%

    The limrange version has valid range [-pi/2, pi/2], the others have full range.

*/


inline float cc_sinf_limrange(float x) {
    const float a =  0.00735246819687011731341356165096815f;
    const float b = -0.16528911397014738207016302002888890f;
    const float c =  0.99969198629596757779830113868360584f;

    float x2 = x * x;

    return x*(c + x2*(b + a*x2));
}


inline double cc_sin_limrange(double x) {
    const double a =  0.00735246819687011731341356165096815;
    const double b = -0.16528911397014738207016302002888890;
    const double c =  0.99969198629596757779830113868360584;

    double x2 = x * x;

    return x*(c + x2*(b + a*x2));
}


inline float cc_sinf(float x) {
    long k;

    /* find offset of x from the range -pi to pi */
    k = cc_round(CC_1_PI_F * x);

    /* bring x into range */
    x -= k * CC_PI_F;

    /* calculate sine */
    x = cc_sinf_limrange(x);

    /* if x is in an odd pi count we must flip */
    #ifdef CC_NO_BRANCH
    x *= (1 - 2 * (k & 1));
    #else
    if (k & 1) x = -x;
    #endif

    return x;
}


inline double cc_sin(double x) {
    long k;

    /* find offset of x from the range -pi to pi */
    k = cc_round(CC_1_PI * x);

    /* bring x into range */
    x -= k * CC_PI;

    /* calculate sine */
    x = cc_sin_limrange(x);

    /* if x is in an odd pi count we must flip */
    #ifdef CC_NO_BRANCH
    x *= (1 - 2 * (k & 1));
    #else
    if (k & 1) x = -x;
    #endif

    return x;
}