codegen: add a 'size' argument to ALU_WRITES_FLAGS
[ajla.git] / fn_impl.c
blob5dd6df67ff2fb996b44129488c2b709e4ff670ec
1 /*
2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version.
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
19 #include "ajla.h"
21 #include "str.h"
23 #ifndef HAVE_GETENV
24 char *getenv(const char attr_unused *name)
26 return NULL;
28 #endif
30 #ifndef HAVE_MEMPCPY
31 void *mempcpy(void *dst, const void *src, size_t length)
33 (void)memcpy(dst, src, length);
34 return (char *)dst + length;
36 #endif
38 #ifndef HAVE_STRERROR
39 char *strerror(int errnum)
41 static char buffer[6 + 1 + (sizeof(int) * 5 / 2 + 1) + 1] = "error ";
42 char *b = buffer + 6;
43 str_add_signed(&b, NULL, (intbig_t)errnum, 10);
44 return buffer;
46 #endif
48 #ifndef HAVE_STRNLEN
49 size_t strnlen(const char *s, size_t maxlen)
51 size_t l;
52 for (l = 0; l < maxlen; l++)
53 if (unlikely(!s[l]))
54 break;
55 return l;
57 #endif
59 #if !defined(HAVE_CBRT)
60 double cbrt(double x)
62 if (unlikely(x <= 0)) {
63 if (x == 0)
64 return x;
65 else
66 return -pow(-x, 1./3.);
68 return pow(x, 1./3.);
70 #endif
72 #if !defined(HAVE_CBRTF)
73 float cbrtf(float x)
75 if (unlikely(x <= 0)) {
76 if (x == 0)
77 return x;
78 else
79 return -powf(-x, 1./3.);
81 return powf(x, 1./3.);
83 #endif
85 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_CBRTL)
86 float cbrtl(float x)
88 if (unlikely(x <= 0)) {
89 if (x == 0)
90 return x;
91 else
92 return -powl(-x, 1./3.);
94 return powl(x, 1.L/3.L);
96 #endif
98 #if !defined(HAVE_ASINH)
99 double asinh(double x)
101 return log(x + sqrt(x * x + 1));
103 #endif
105 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_ASINHL)
106 long double asinhl(long double x)
108 return logl(x + sqrtl(x * x + 1));
110 #endif
112 #if !defined(HAVE_ACOSH)
113 double acosh(double x)
115 return log(x + sqrt(x * x - 1));
117 #endif
119 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_ACOSHL)
120 long double acoshl(long double x)
122 return logl(x + sqrtl(x * x - 1));
124 #endif
126 #if !defined(HAVE_ATANH)
127 double atanh(double x)
129 return 0.5 * log((1 + x) / (1 - x));
131 #endif
133 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_ATANHL)
134 long double atanhl(long double x)
136 return 0.5 * logl((1 + x) / (1 - x));
138 #endif
140 #if !defined(HAVE_EXP2)
141 double exp2(double x)
143 return pow(2, x);
145 #endif
147 #if !defined(HAVE_EXP2F)
148 float exp2f(float x)
150 return powf(2, x);
152 #endif
154 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_EXP2L)
155 long double exp2l(long double x)
157 return powl(2, x);
159 #endif
161 #if defined(HAVE___FLOAT128) && defined(HAVE_LIBQUADMATH) && defined(HAVE_QUADMATH_H) && !defined(HAVE_EXP2Q)
162 __float128 exp2q(__float128 x)
164 return powq(2, x);
166 #endif
168 #if !defined(HAVE_EXP10)
169 double exp10(double x)
171 return pow(10, x);
173 #endif
175 #if !defined(HAVE_EXP10F)
176 float exp10f(float x)
178 return powf(10, x);
180 #endif
182 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_EXP10L)
183 long double exp10l(long double x)
185 return powl(10, x);
187 #endif
189 #if defined(HAVE___FLOAT128) && defined(HAVE_LIBQUADMATH) && defined(HAVE_QUADMATH_H) && !defined(HAVE_EXP10Q)
190 __float128 exp10q(__float128 x)
192 return powq(10, x);
194 #endif
196 #if !defined(HAVE_LOG2)
197 double log2(double x)
199 return log(x) / log(2);
201 #endif
203 #if !defined(HAVE_LOG2F)
204 float log2f(float x)
206 return logf(x) / logf(2);
208 #endif
210 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_LOG2L)
211 long double log2l(long double x)
213 return logl(x) / logl(2);
215 #endif
217 #if !defined(HAVE_TRUNC)
218 double trunc(double x)
220 if (x >= 0)
221 return floor(x);
222 else
223 return ceil(x);
225 #endif
227 #if !defined(HAVE_TRUNCF)
228 float truncf(float x)
230 if (x >= 0)
231 return floorf(x);
232 else
233 return ceilf(x);
235 #endif
237 #if defined(HAVE_LONG_DOUBLE) && !defined(HAVE_TRUNCL)
238 long double truncl(long double x)
240 if (x >= 0)
241 return floorl(x);
242 else
243 return ceill(x);
245 #endif
247 #if !defined(HAVE_RINT)
248 double rint(double x)
250 if (x >= 0)
251 return floor(x + 0.5);
252 else
253 return ceil(x - 0.5);
255 #endif
257 #if !defined(HAVE_RINTF)
258 float rintf(float x)
260 return rint(x);
262 #endif
264 #if !defined(HAVE_MODFF)
265 float modff(float x, float *iflt)
267 double idbl;
268 double r = modf(x, &idbl);
269 *iflt = idbl;
270 return r;
272 #endif