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_
32 /* check for "c89" mode */
33 #if (defined _MSC_VER && defined __STDC__) || \
34 (defined __GNUC__ && defined __STRICT_ANSI__)
39 #if defined __cplusplus || (!defined _CMATH_ANSI && defined _CMATH_USE_INLINE)
41 #define _CMATH_INLINE __inline
43 #define _CMATH_INLINE inline
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))))
54 #define _CMATH_ALIGN(x)
55 #define _CMATH_ALIGN_T(x)
58 /* printf max integer */
61 #define _CMATH_PR_STD_UINT "I64u"
62 #define _CMATH_PR_STD_INT "I64i"
63 #define _CMATH_PR_STD_HEX "I64x"
65 #define _CMATH_PR_STD_UINT "llu"
66 #define _CMATH_PR_STD_INT "lli"
67 #define _CMATH_PR_STD_HEX "llx"
70 #define _CMATH_PR_STD_UINT "u"
71 #define _CMATH_PR_STD_INT "d"
72 #define _CMATH_PR_STD_HEX "x"
77 #pragma warning(disable : 4514)
84 #define MK_L(x) (x##L)
85 #define MK_UL(x) (x##UL)
87 #define MK_LL(x) (x##L)
88 #define MK_ULL(x) (x##UL)
90 #define MK_LL(x) (x##LL)
91 #define MK_ULL(x) (x##ULL)
95 /* definitions depending on c standard */
97 #define cmath_std_signbit MK_UL(0x7fffffff)
98 #define cmath_std_float_t float
99 #define cmath_std_int_t int
101 #define cmath_std_signbit MK_ULL(0x7fffffffffffffff)
102 #define cmath_std_float_t double
104 #define cmath_std_int_t __int64
106 #define cmath_std_int_t long long
110 /* types and constants */
112 #define cmath_t double
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
134 #define _CMATH_MAY_ALIAS __attribute__((__may_alias__))
136 #define _CMATH_MAY_ALIAS
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
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) \
175 #define cmath_degrees(x) \
179 cmath_t
cmath_powi(const cmath_t x
, register cmath_uint16_t n
)
181 register cmath_t result
= 1;
188 cmath_t
cmath_abs(const cmath_t x
)
195 u
.j
= (cmath_std_float_t
)x
;
196 u
.i
&= cmath_std_signbit
;
201 cmath_t
cmath_round(const cmath_t x
)
204 return (cmath_t
)(cmath_std_int_t
)(x
- 0.5);
206 return (cmath_t
)(cmath_std_int_t
)(x
+ 0.5);
209 #endif /* _CUSTOM_MATH_H_ */