1#include <math.h>
2#include <stdint.h>
3
4double frexp(double x, int* e) {
5    union {
6        double d;
7        uint64_t i;
8    } y = {x};
9    int ee = y.i >> 52 & 0x7ff;
10
11    if (!ee) {
12        if (x) {
13            x = frexp(x * 0x1p64, e);
14            *e -= 64;
15        } else
16            *e = 0;
17        return x;
18    } else if (ee == 0x7ff) {
19        return x;
20    }
21
22    *e = ee - 0x3fe;
23    y.i &= 0x800fffffffffffffull;
24    y.i |= 0x3fe0000000000000ull;
25    return y.d;
26}
27