Merge pull request #110 from tesselode/fixes
[wdl/wdl-ol.git] / WDL / cmath / custom_math.h
bloba624b3a2440fb8f2482dc8dd4156ab0f3470e7ba
1 /*
2 custom_math.h
3 Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 portable definitions for ansi c and cross compiler compatibility.
24 contains: numeric constants, custom math functions, macros & other.
27 #ifndef _CUSTOM_MATH_H_
28 #define _CUSTOM_MATH_H_
30 #include "math.h"
32 /* check for "c89" mode */
33 #if (defined _MSC_VER && defined __STDC__) || \
34 (defined __GNUC__ && defined __STRICT_ANSI__)
35 #define _CMATH_ANSI
36 #endif
38 /* enable inline */
39 #if defined __cplusplus || (!defined _CMATH_ANSI && defined _CMATH_USE_INLINE)
40 #ifdef _MSC_VER
41 #define _CMATH_INLINE __inline
42 #else
43 #define _CMATH_INLINE inline
44 #endif
45 #else
46 #define _CMATH_INLINE
47 #endif
49 /* align type to size of type */
50 #if defined __GNUC__ || defined __TINYC__
51 #define _CMATH_ALIGN(x) __attribute__ ((aligned(x)))
52 #define _CMATH_ALIGN_T(x) __attribute__ ((aligned(sizeof(x))))
53 #else
54 #define _CMATH_ALIGN(x)
55 #define _CMATH_ALIGN_T(x)
56 #endif
58 /* printf max integer */
59 #ifndef _CMATH_ANSI
60 #ifdef _WIN32
61 #define _CMATH_PR_STD_UINT "I64u"
62 #define _CMATH_PR_STD_INT "I64i"
63 #define _CMATH_PR_STD_HEX "I64x"
64 #else
65 #define _CMATH_PR_STD_UINT "llu"
66 #define _CMATH_PR_STD_INT "lli"
67 #define _CMATH_PR_STD_HEX "llx"
68 #endif
69 #else
70 #define _CMATH_PR_STD_UINT "u"
71 #define _CMATH_PR_STD_INT "d"
72 #define _CMATH_PR_STD_HEX "x"
73 #endif
75 /* msvc specifics */
76 #ifdef _MSC_VER
77 #pragma warning(disable : 4514)
79 #define MK_L(x) (x)
80 #define MK_UL(x) (x)
81 #define MK_LL(x) (x)
82 #define MK_ULL(x) (x)
83 #else
84 #define MK_L(x) (x##L)
85 #define MK_UL(x) (x##UL)
86 #ifdef _CMATH_ANSI
87 #define MK_LL(x) (x##L)
88 #define MK_ULL(x) (x##UL)
89 #else
90 #define MK_LL(x) (x##LL)
91 #define MK_ULL(x) (x##ULL)
92 #endif
93 #endif
95 /* definitions depending on c standard */
96 #ifdef _CMATH_ANSI
97 #define cmath_std_signbit MK_UL(0x7fffffff)
98 #define cmath_std_float_t float
99 #define cmath_std_int_t int
100 #else
101 #define cmath_std_signbit MK_ULL(0x7fffffffffffffff)
102 #define cmath_std_float_t double
103 #ifdef _MSC_VER
104 #define cmath_std_int_t __int64
105 #else
106 #define cmath_std_int_t long long
107 #endif
108 #endif
110 /* types and constants */
111 #ifndef cmath_t
112 #define cmath_t double
113 #endif
115 #define cmath_std_uint_t unsigned cmath_std_int_t
117 #define cmath_pi 3.1415926535897932384626433832795
118 #define cmath_pi2 6.2831853071795864769252867665590
119 #define cmath_pi_2 1.5707963267948966192313216916398
120 #define cmath_e 2.7182818284590452353602874713526
121 #define cmath_sqrt2 1.4142135623730950488016887242097
122 #define cmath_pi_180 0.0174532925199432957692369076848
123 #define cmath_180_pi 57.295779513082320876798154814105
125 #define cmath_int8_t char
126 #define cmath_uint8_t unsigned char
127 #define cmath_int16_t short
128 #define cmath_uint16_t unsigned short
129 #define cmath_int32_t int
130 #define cmath_uint32_t unsigned int
132 /* aliased types */
133 #ifdef __GNUC__
134 #define _CMATH_MAY_ALIAS __attribute__((__may_alias__))
135 #else
136 #define _CMATH_MAY_ALIAS
137 #endif
139 typedef cmath_t _CMATH_MAY_ALIAS cmath_t_a;
141 /* possible approximations */
142 #define cmath_sin sin
143 #define cmath_cos cos
144 #define cmath_tan tan
145 #define cmath_asin asin
146 #define cmath_acos acos
147 #define cmath_atan atan
148 #define cmath_atan2 atan2
149 #define cmath_sinh sinh
150 #define cmath_cosh cosh
151 #define cmath_tanh tanh
152 #define cmath_exp exp
153 #define cmath_pow pow
154 #define cmath_sqrt sqrt
155 #define cmath_log log
156 #define cmath_log2 log2
157 #define cmath_log10 log10
159 /* methods */
160 #define cmath_array_size(x) \
161 (sizeof(x) / sizeof(*(x)))
163 #define poly_order(x) \
164 (sizeof(x) / sizeof(*(x)) - 1)
166 #define cmath_cabs(a, b) \
167 cmath_sqrt((a)*(a) + (b)*(b))
169 #define cmath_carg(a, b) \
170 cmath_atan2((b), (a))
172 #define cmath_radians(x) \
173 ((x)*cmath_pi_180)
175 #define cmath_degrees(x) \
176 ((x)*cmath_180_pi)
178 _CMATH_INLINE
179 cmath_t cmath_powi(const cmath_t x, register cmath_uint16_t n)
181 register cmath_t result = 1;
182 while (n--)
183 result *= x;
184 return result;
187 _CMATH_INLINE
188 cmath_t cmath_abs(const cmath_t x)
190 register union
192 cmath_std_int_t i;
193 cmath_std_float_t j;
194 } u;
195 u.j = (cmath_std_float_t)x;
196 u.i &= cmath_std_signbit;
197 return u.j;
200 _CMATH_INLINE
201 cmath_t cmath_round(const cmath_t x)
203 if (x < 0.0)
204 return (cmath_t)(cmath_std_int_t)(x - 0.5);
205 else
206 return (cmath_t)(cmath_std_int_t)(x + 0.5);
209 #endif /* _CUSTOM_MATH_H_ */