Convert from ISO-8859-1 to UTF-8. See aa812e8.
[tinycc.git] / win32 / include / tcc / tcc_libm.h
blob0a62e6fb7064096ef886fe8e604766d9bd8d5e3f
1 #ifndef _TCC_LIBM_H_
2 #define _TCC_LIBM_H_
4 #include "../math.h"
6 /* TCC uses 8 bytes for double and long double, so effectively the l variants
7 * are never used. For now, they just run the normal (double) variant.
8 */
11 * most of the code in this file is taken from MUSL rs-1.0 (MIT license)
12 * - musl-libc: http://git.musl-libc.org/cgit/musl/tree/src/math?h=rs-1.0
13 * - License: http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT?h=rs-1.0
16 /*******************************************************************************
17 Start of code based on MUSL
18 *******************************************************************************/
20 musl as a whole is licensed under the following standard MIT license:
22 ----------------------------------------------------------------------
23 Copyright © 2005-2014 Rich Felker, et al.
25 Permission is hereby granted, free of charge, to any person obtaining
26 a copy of this software and associated documentation files (the
27 "Software"), to deal in the Software without restriction, including
28 without limitation the rights to use, copy, modify, merge, publish,
29 distribute, sublicense, and/or sell copies of the Software, and to
30 permit persons to whom the Software is furnished to do so, subject to
31 the following conditions:
33 The above copyright notice and this permission notice shall be
34 included in all copies or substantial portions of the Software.
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
39 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
40 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
41 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 ----------------------------------------------------------------------
46 /* fpclassify */
48 __CRT_INLINE int __cdecl __fpclassify (double x) {
49 union {double f; uint64_t i;} u = {x};
50 int e = u.i>>52 & 0x7ff;
51 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
52 if (e==0x7ff) return u.i<<12 ? FP_NAN : FP_INFINITE;
53 return FP_NORMAL;
56 __CRT_INLINE int __cdecl __fpclassifyf (float x) {
57 union {float f; uint32_t i;} u = {x};
58 int e = u.i>>23 & 0xff;
59 if (!e) return u.i<<1 ? FP_SUBNORMAL : FP_ZERO;
60 if (e==0xff) return u.i<<9 ? FP_NAN : FP_INFINITE;
61 return FP_NORMAL;
64 __CRT_INLINE int __cdecl __fpclassifyl (long double x) {
65 return __fpclassify(x);
69 /* signbit */
71 __CRT_INLINE int __cdecl __signbit (double x) {
72 union {double d; uint64_t i;} y = { x };
73 return y.i>>63;
76 __CRT_INLINE int __cdecl __signbitf (float x) {
77 union {float f; uint32_t i; } y = { x };
78 return y.i>>31;
81 __CRT_INLINE int __cdecl __signbitl (long double x) {
82 return __signbit(x);
86 /* fmin*, fmax* */
88 #define TCCFP_FMIN_EVAL (isnan(x) ? y : \
89 isnan(y) ? x : \
90 (signbit(x) != signbit(y)) ? (signbit(x) ? x : y) : \
91 x < y ? x : y)
93 __CRT_INLINE double __cdecl fmin (double x, double y) {
94 return TCCFP_FMIN_EVAL;
97 __CRT_INLINE float __cdecl fminf (float x, float y) {
98 return TCCFP_FMIN_EVAL;
101 __CRT_INLINE long double __cdecl fminl (long double x, long double y) {
102 return TCCFP_FMIN_EVAL;
105 #define TCCFP_FMAX_EVAL (isnan(x) ? y : \
106 isnan(y) ? x : \
107 (signbit(x) != signbit(y)) ? (signbit(x) ? y : x) : \
108 x < y ? y : x)
110 __CRT_INLINE double __cdecl fmax (double x, double y) {
111 return TCCFP_FMAX_EVAL;
114 __CRT_INLINE float __cdecl fmaxf (float x, float y) {
115 return TCCFP_FMAX_EVAL;
118 __CRT_INLINE long double __cdecl fmaxl (long double x, long double y) {
119 return TCCFP_FMAX_EVAL;
123 /* *round* */
125 #define TCCFP_FORCE_EVAL(x) do { \
126 if (sizeof(x) == sizeof(float)) { \
127 volatile float __x; \
128 __x = (x); \
129 } else if (sizeof(x) == sizeof(double)) { \
130 volatile double __x; \
131 __x = (x); \
132 } else { \
133 volatile long double __x; \
134 __x = (x); \
136 } while(0)
138 __CRT_INLINE double __cdecl round (double x) {
139 union {double f; uint64_t i;} u = {x};
140 int e = u.i >> 52 & 0x7ff;
141 double y;
143 if (e >= 0x3ff+52)
144 return x;
145 if (u.i >> 63)
146 x = -x;
147 if (e < 0x3ff-1) {
148 /* raise inexact if x!=0 */
149 TCCFP_FORCE_EVAL(x + 0x1p52);
150 return 0*u.f;
152 y = (double)(x + 0x1p52) - 0x1p52 - x;
153 if (y > 0.5)
154 y = y + x - 1;
155 else if (y <= -0.5)
156 y = y + x + 1;
157 else
158 y = y + x;
159 if (u.i >> 63)
160 y = -y;
161 return y;
164 __CRT_INLINE long __cdecl lround (double x) {
165 return round(x);
168 __CRT_INLINE long long __cdecl llround (double x) {
169 return round(x);
172 __CRT_INLINE float __cdecl roundf (float x) {
173 return round(x);
176 __CRT_INLINE long __cdecl lroundf (float x) {
177 return round(x);
180 __CRT_INLINE long long __cdecl llroundf (float x) {
181 return round(x);
184 __CRT_INLINE long double __cdecl roundl (long double x) {
185 return round(x);
188 __CRT_INLINE long __cdecl lroundl (long double x) {
189 return round(x);
192 __CRT_INLINE long long __cdecl llroundl (long double x) {
193 return round(x);
197 /*******************************************************************************
198 End of code based on MUSL
199 *******************************************************************************/
201 #endif /* _TCC_LIBM_H_ */