summaryrefslogtreecommitdiff
path: root/src/internal/libm.h
blob: 2c2191f6343be73a185e90d8d220f6dec3e51bc6 (plain) (blame)
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
/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 */

#ifndef _LIBM_H
#define _LIBM_H

#include <stdint.h>
#include <float.h>
#include <math.h>
#include <complex.h>

#include "longdbl.h"

#include "libc.h"

union fshape {
	float value;
	uint32_t bits;
};

union dshape {
	double value;
	uint64_t bits;
};

/* Get two 32 bit ints from a double.  */
#define EXTRACT_WORDS(hi,lo,d)                                  \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  (hi) = __u.bits >> 32;                                        \
  (lo) = (uint32_t)__u.bits;                                    \
} while (0)

/* Get a 64 bit int from a double.  */
#define EXTRACT_WORD64(i,d)                                     \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  (i) = __u.bits;                                               \
} while (0)

/* Get the more significant 32 bit int from a double.  */
#define GET_HIGH_WORD(i,d)                                      \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  (i) = __u.bits >> 32;                                         \
} while (0)

/* Get the less significant 32 bit int from a double.  */
#define GET_LOW_WORD(i,d)                                       \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  (i) = (uint32_t)__u.bits;                                     \
} while (0)

/* Set a double from two 32 bit ints.  */
#define INSERT_WORDS(d,hi,lo)                                   \
do {                                                            \
  union dshape __u;                                             \
  __u.bits = ((uint64_t)(hi) << 32) | (uint32_t)(lo);           \
  (d) = __u.value;                                              \
} while (0)

/* Set a double from a 64 bit int.  */
#define INSERT_WORD64(d,i)                                      \
do {                                                            \
  union dshape __u;                                             \
  __u.bits = (i);                                               \
  (d) = __u.value;                                              \
} while (0)

/* Set the more significant 32 bits of a double from an int.  */
#define SET_HIGH_WORD(d,hi)                                     \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  __u.bits &= 0xffffffff;                                       \
  __u.bits |= (uint64_t)(hi) << 32;                             \
  (d) = __u.value;                                              \
} while (0)

/* Set the less significant 32 bits of a double from an int.  */
#define SET_LOW_WORD(d,lo)                                      \
do {                                                            \
  union dshape __u;                                             \
  __u.value = (d);                                              \
  __u.bits &= 0xffffffff00000000ull;                            \
  __u.bits |= (uint32_t)(lo);                                   \
  (d) = __u.value;                                              \
} while (0)

/* Get a 32 bit int from a float.  */
#define GET_FLOAT_WORD(i,d)                                     \
do {                                                            \
  union fshape __u;                                             \
  __u.value = (d);                                              \
  (i) = __u.bits;                                               \
} while (0)

/* Set a float from a 32 bit int.  */
#define SET_FLOAT_WORD(d,i)                                     \
do {                                                            \
  union fshape __u;                                             \
  __u.bits = (i);                                               \
  (d) = __u.value;                                              \
} while (0)

/* fdlibm kernel functions */

int    __rem_pio2_large(double*,double*,int,int,int);

int    __rem_pio2(double,double*);
double __sin(double,double,int);
double __cos(double,double);
double __tan(double,double,int);
double __expo2(double);
double complex __ldexp_cexp(double complex,int);

int    __rem_pio2f(float,double*);
float  __sindf(double);
float  __cosdf(double);
float  __tandf(double,int);
float  __expo2f(float);
float complex __ldexp_cexpf(float complex,int);

long double __sinl(long double, long double, int);
long double __cosl(long double, long double);
long double __tanl(long double, long double, int);

/* polynomial evaluation */
long double __polevll(long double, long double *, int);
long double __p1evll(long double, long double *, int);

// FIXME: not needed when -fexcess-precision=standard is supported (>=gcc4.5)
/*
 * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
 */
#if 1
#define STRICT_ASSIGN(type, lval, rval) do {    \
        volatile type __v = (rval);             \
        (lval) = __v;                           \
} while (0)
#else
#define STRICT_ASSIGN(type, lval, rval) ((lval) = (type)(rval))
#endif


/* complex */

union dcomplex {
	double complex z;
	double a[2];
};
union fcomplex {
	float complex z;
	float a[2];
};
union lcomplex {
	long double complex z;
	long double a[2];
};

// FIXME: move to complex.h ?
#define creal(z) ((double)(z))
#define crealf(z) ((float)(z))
#define creall(z) ((long double)(z))
#define cimag(z) ((union dcomplex){(z)}.a[1])
#define cimagf(z) ((union fcomplex){(z)}.a[1])
#define cimagl(z) ((union lcomplex){(z)}.a[1])

/* x + y*I is not supported properly by gcc */
#define cpack(x,y) ((union dcomplex){.a={(x),(y)}}.z)
#define cpackf(x,y) ((union fcomplex){.a={(x),(y)}}.z)
#define cpackl(x,y) ((union lcomplex){.a={(x),(y)}}.z)

#endif