Debian Codegraph @

mingw-w64-crt/math/arm-common/log2.c

raw
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6
7 #include <math.h>
8 #include <stdint.h>
9
10 typedef union ieee754_double_ {
11 struct __attribute__((__packed__)) {
12 uint64_t f52 : 52;
13 uint64_t exp : 11;
14 uint64_t sgn : 1;
15 };
16 double f;
17 } ieee754_double;
18
19 typedef union ieee754_float_ {
20 struct __attribute__((__packed__)) {
21 uint32_t f23 : 23;
22 uint32_t exp : 8;
23 uint32_t sgn : 1;
24 };
25 float f;
26 } ieee754_float;
27
28 double log2(double x)
29 {
30 ieee754_double u = { .f = x };
31 if (u.sgn == 0 && u.f52 == 0 && u.exp > 0 && u.exp < 0x7ff) {
32 // Handle exact powers of two exactly
33 return (int)u.exp - 1023;
34 }
35 return log(x) / 0.69314718246459960938;
36 }
37
38 float log2f(float x)
39 {
40 ieee754_float u = { .f = x };
41 if (u.sgn == 0 && u.f23 == 0 && u.exp > 0 && u.exp < 0xff) {
42 // Handle exact powers of two exactly
43 return (int)u.exp - 127;
44 }
45 return logf(x) / 0.69314718246459960938f;
46 }
47
48 long double log2l(long double x)
49 {
50 return log2(x);
51 }