]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/internal/libm.h
WASI libc prototype implementation.
[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
74#ifdef __wasilibc_unmodified_upstream
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
88/*
89 * WebAssembly doesn't have floating-point status flags, so there's no reason
90 * to force evaluations.
91 * */
92#define FORCE_EVAL(x) ((void)(x))
93#endif
94
95/* Get two 32 bit ints from a double. */
96#define EXTRACT_WORDS(hi,lo,d) \
97do { \
98 union {double f; uint64_t i;} __u; \
99 __u.f = (d); \
100 (hi) = __u.i >> 32; \
101 (lo) = (uint32_t)__u.i; \
102} while (0)
103
104/* Get the more significant 32 bit int from a double. */
105#define GET_HIGH_WORD(hi,d) \
106do { \
107 union {double f; uint64_t i;} __u; \
108 __u.f = (d); \
109 (hi) = __u.i >> 32; \
110} while (0)
111
112/* Get the less significant 32 bit int from a double. */
113#define GET_LOW_WORD(lo,d) \
114do { \
115 union {double f; uint64_t i;} __u; \
116 __u.f = (d); \
117 (lo) = (uint32_t)__u.i; \
118} while (0)
119
120/* Set a double from two 32 bit ints. */
121#define INSERT_WORDS(d,hi,lo) \
122do { \
123 union {double f; uint64_t i;} __u; \
124 __u.i = ((uint64_t)(hi)<<32) | (uint32_t)(lo); \
125 (d) = __u.f; \
126} while (0)
127
128/* Set the more significant 32 bits of a double from an int. */
129#define SET_HIGH_WORD(d,hi) \
130do { \
131 union {double f; uint64_t i;} __u; \
132 __u.f = (d); \
133 __u.i &= 0xffffffff; \
134 __u.i |= (uint64_t)(hi) << 32; \
135 (d) = __u.f; \
136} while (0)
137
138/* Set the less significant 32 bits of a double from an int. */
139#define SET_LOW_WORD(d,lo) \
140do { \
141 union {double f; uint64_t i;} __u; \
142 __u.f = (d); \
143 __u.i &= 0xffffffff00000000ull; \
144 __u.i |= (uint32_t)(lo); \
145 (d) = __u.f; \
146} while (0)
147
148/* Get a 32 bit int from a float. */
149#define GET_FLOAT_WORD(w,d) \
150do { \
151 union {float f; uint32_t i;} __u; \
152 __u.f = (d); \
153 (w) = __u.i; \
154} while (0)
155
156/* Set a float from a 32 bit int. */
157#define SET_FLOAT_WORD(d,w) \
158do { \
159 union {float f; uint32_t i;} __u; \
160 __u.i = (w); \
161 (d) = __u.f; \
162} while (0)
163
164#undef __CMPLX
165#undef CMPLX
166#undef CMPLXF
167#undef CMPLXL
168
169#define __CMPLX(x, y, t) \
170 ((union { _Complex t __z; t __xy[2]; }){.__xy = {(x),(y)}}.__z)
171
172#define CMPLX(x, y) __CMPLX(x, y, double)
173#define CMPLXF(x, y) __CMPLX(x, y, float)
174#define CMPLXL(x, y) __CMPLX(x, y, long double)
175
176/* fdlibm kernel functions */
177
178hidden int __rem_pio2_large(double*,double*,int,int,int);
179
180hidden int __rem_pio2(double,double*);
181hidden double __sin(double,double,int);
182hidden double __cos(double,double);
183hidden double __tan(double,double,int);
184hidden double __expo2(double);
185hidden double complex __ldexp_cexp(double complex,int);
186
187hidden int __rem_pio2f(float,double*);
188hidden float __sindf(double);
189hidden float __cosdf(double);
190hidden float __tandf(double,int);
191hidden float __expo2f(float);
192hidden float complex __ldexp_cexpf(float complex,int);
193
194hidden int __rem_pio2l(long double, long double *);
195hidden long double __sinl(long double, long double, int);
196hidden long double __cosl(long double, long double);
197hidden long double __tanl(long double, long double, int);
198
199/* polynomial evaluation */
200hidden long double __polevll(long double, const long double *, int);
201hidden long double __p1evll(long double, const long double *, int);
202
203extern int __signgam;
204hidden double __lgamma_r(double, int *);
205hidden float __lgammaf_r(float, int *);
206
207#endif