]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/internal/libm.h
Format changes to musl code to fit musl's style.
[wasi-libc.git] / libc-top-half / musl / src / internal / libm.h
CommitLineData
320054e8
DG
1/* origin: FreeBSD /usr/src/lib/msun/src/math_private.h */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
13#ifndef _LIBM_H
14#define _LIBM_H
15
16#include <stdint.h>
17#include <float.h>
18#include <math.h>
19#include <complex.h>
20#include <endian.h>
21
22#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
23#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
24union ldshape {
25 long double f;
26 struct {
27 uint64_t m;
28 uint16_t se;
29 } i;
30};
31#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
32/* This is the m68k variant of 80-bit long double, and this definition only works
33 * on archs where the alignment requirement of uint64_t is <= 4. */
34union ldshape {
35 long double f;
36 struct {
37 uint16_t se;
38 uint16_t pad;
39 uint64_t m;
40 } i;
41};
42#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN
43union ldshape {
44 long double f;
45 struct {
46 uint64_t lo;
47 uint32_t mid;
48 uint16_t top;
49 uint16_t se;
50 } i;
51 struct {
52 uint64_t lo;
53 uint64_t hi;
54 } i2;
55};
56#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN
57union ldshape {
58 long double f;
59 struct {
60 uint16_t se;
61 uint16_t top;
62 uint32_t mid;
63 uint64_t lo;
64 } i;
65 struct {
66 uint64_t hi;
67 uint64_t lo;
68 } i2;
69};
70#else
71#error Unsupported long double representation
72#endif
73
e5f14be3 74#ifdef __wasilibc_unmodified_upstream // WASI has no floating-point status flags
320054e8
DG
75#define FORCE_EVAL(x) do { \
76 if (sizeof(x) == sizeof(float)) { \
77 volatile float __x; \
78 __x = (x); \
79 } else if (sizeof(x) == sizeof(double)) { \
80 volatile double __x; \
81 __x = (x); \
82 } else { \
83 volatile long double __x; \
84 __x = (x); \
85 } \
86} while(0)
87#else
ec9f1c39
DG
88/* WebAssembly doesn't have floating-point status flags, so there's no reason
89 * to force evaluations. */
320054e8
DG
90#define FORCE_EVAL(x) ((void)(x))
91#endif
92
93/* Get two 32 bit ints from a double. */
94#define EXTRACT_WORDS(hi,lo,d) \
95do { \
96 union {double f; uint64_t i;} __u; \
97 __u.f = (d); \
98 (hi) = __u.i >> 32; \
99 (lo) = (uint32_t)__u.i; \
100} while (0)
101
102/* Get the more significant 32 bit int from a double. */
103#define GET_HIGH_WORD(hi,d) \
104do { \
105 union {double f; uint64_t i;} __u; \
106 __u.f = (d); \
107 (hi) = __u.i >> 32; \
108} while (0)
109
110/* Get the less significant 32 bit int from a double. */
111#define GET_LOW_WORD(lo,d) \
112do { \
113 union {double f; uint64_t i;} __u; \
114 __u.f = (d); \
115 (lo) = (uint32_t)__u.i; \
116} while (0)
117
118/* Set a double from two 32 bit ints. */
119#define INSERT_WORDS(d,hi,lo) \
120do { \
121 union {double f; uint64_t i;} __u; \
122 __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo); \
123 (d) = __u.f; \
124} while (0)
125
126/* Set the more significant 32 bits of a double from an int. */
127#define SET_HIGH_WORD(d,hi) \
128do { \
129 union {double f; uint64_t i;} __u; \
130 __u.f = (d); \
131 __u.i &= 0xffffffff; \
132 __u.i |= (uint64_t)(hi) << 32; \
133 (d) = __u.f; \
134} while (0)
135
136/* Set the less significant 32 bits of a double from an int. */
137#define SET_LOW_WORD(d,lo) \
138do { \
139 union {double f; uint64_t i;} __u; \
140 __u.f = (d); \
141 __u.i &= 0xffffffff00000000ull; \
142 __u.i |= (uint32_t)(lo); \
143 (d) = __u.f; \
144} while (0)
145
146/* Get a 32 bit int from a float. */
147#define GET_FLOAT_WORD(w,d) \
148do { \
149 union {float f; uint32_t i;} __u; \
150 __u.f = (d); \
151 (w) = __u.i; \
152} while (0)
153
154/* Set a float from a 32 bit int. */
155#define SET_FLOAT_WORD(d,w) \
156do { \
157 union {float f; uint32_t i;} __u; \
158 __u.i = (w); \
159 (d) = __u.f; \
160} while (0)
161
162#undef __CMPLX
163#undef CMPLX
164#undef CMPLXF
165#undef CMPLXL
166
167#define __CMPLX(x, y, t) \
168 ((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
169
170#define CMPLX(x, y) __CMPLX(x, y, double)
171#define CMPLXF(x, y) __CMPLX(x, y, float)
172#define CMPLXL(x, y) __CMPLX(x, y, long double)
173
174/* fdlibm kernel functions */
175
176hidden int __rem_pio2_large(double*,double*,int,int,int);
177
178hidden int __rem_pio2(double,double*);
179hidden double __sin(double,double,int);
180hidden double __cos(double,double);
181hidden double __tan(double,double,int);
182hidden double __expo2(double);
183hidden double complex __ldexp_cexp(double complex,int);
184
185hidden int __rem_pio2f(float,double*);
186hidden float __sindf(double);
187hidden float __cosdf(double);
188hidden float __tandf(double,int);
189hidden float __expo2f(float);
190hidden float complex __ldexp_cexpf(float complex,int);
191
192hidden int __rem_pio2l(long double, long double *);
193hidden long double __sinl(long double, long double, int);
194hidden long double __cosl(long double, long double);
195hidden long double __tanl(long double, long double, int);
196
197/* polynomial evaluation */
198hidden long double __polevll(long double, const long double *, int);
199hidden long double __p1evll(long double, const long double *, int);
200
201extern int __signgam;
202hidden double __lgamma_r(double, int *);
203hidden float __lgammaf_r(float, int *);
204
205#endif