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
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 #define MINIMUM_STACK_SIZE 0x100000
22 #ifndef UNUSUAL_DISABLE_INT128_T
23 #if defined(SIZEOF_UNSIGNED___INT128) && SIZEOF_UNSIGNED___INT128 == 16
24 #define TYPE_FIXED_N 5
25 typedef signed __int128 int128_t
;
26 typedef unsigned __int128 uint128_t
;
31 #define TYPE_FIXED_N 4
35 #define TYPE_INT_N TYPE_FIXED_N
36 #define INT_MASK ((1 << TYPE_INT_N) - 1)
38 #define TYPE_INT_MAX int128_t
40 #define TYPE_INT_MAX int64_t
42 #define TYPE_INT_MAX int32_t
44 #define TYPE_INT_MAX int16_t
46 #define TYPE_INT_MAX int8_t
50 #define ajla_time_t int64_t
51 #define ajla_utime_t uint64_t
54 #if (defined(HAVE_GMP_H) || defined(HAVE_GMP_GMP_H)) && defined(HAVE_LIBGMP)
64 #if !defined(HAVE_FABSF)
67 #if !defined(HAVE_SQRTF)
71 #if !defined(HAVE_SINF)
74 #if !defined(HAVE_COSF)
77 #if !defined(HAVE_TANF)
80 #if !defined(HAVE_ASINF)
83 #if !defined(HAVE_ACOSF)
86 #if !defined(HAVE_ATANF)
90 #if !defined(HAVE_SINHF)
93 #if !defined(HAVE_COSHF)
96 #if !defined(HAVE_TANHF)
99 #if !defined(HAVE_ASINHF)
102 #if !defined(HAVE_ACOSHF)
105 #if !defined(HAVE_ATANHF)
109 #if !defined(HAVE_EXPF)
112 #if !defined(HAVE_LOGF)
115 #if !defined(HAVE_LOG10F)
119 #if !defined(HAVE_CEILF)
122 #if !defined(HAVE_FLOORF)
125 #if !defined(HAVE_FMODF)
128 #if !defined(HAVE_FREXPF)
132 #if !defined(HAVE_ATAN2F)
135 #if !defined(HAVE_POWF)
138 #if !defined(HAVE_LDEXPF)
142 float half_to_float(uint16_t x
);
143 uint16_t float_to_half(float x
);
145 #if FLT_RADIX == 2 && (defined(HAVE___FP16) || defined(HAVE__FLOAT16)) && \
146 !(defined(__ARM_FP) && !defined(__ARM_FP16_FORMAT_IEEE)) && \
147 !(defined(ARCH_ARM32) && CLANG_ATLEAST(5,0,0)) /* clang 5, 6, 7 fail fp16 bist */
148 #define TEST_HALF_FLOAT_CONVERSION
149 #if (defined(__ARM_FP) && __ARM_FP & 2) || defined(__F16C__)
150 #define HAVE_NATIVE_FP16
152 typedef __fp16 real16_t
;
154 typedef _Float16 real16_t
;
156 #define mathfunc_real16_t(fn) cat(fn,f)
157 #define bits_real16_t 11
158 #define isfinite_real16_t(x) isfinite_real32_t(x)
159 #define isnan_real16_t(x) isnan_real32_t(x)
160 #define REAL_MASK_0 (1 << 0)
165 typedef float real32_t
;
166 #define mathfunc_real32_t(fn) cat(fn,f)
167 #define bits_real32_t FLT_MANT_DIG
168 #if defined(HAVE_ISFINITEF)
169 #define isfinite_real32_t(x) isfinitef(x)
171 #if defined(HAVE_ISNANF)
172 #define isnan_real32_t(x) isnanf(x)
174 #define isnan_real32_t(x) isnan(x)
176 #define REAL_MASK_1 (1 << 1)
180 typedef double real64_t
;
181 #define mathfunc_real64_t(fn) fn
182 #define bits_real64_t DBL_MANT_DIG
183 #if defined(HAVE_ISFINITE)
184 #define isfinite_real64_t(x) isfinite(x)
186 #if defined(HAVE_ISNAN) || defined(isnan)
187 #define isnan_real64_t(x) isnan(x)
189 #define REAL_MASK_2 (1 << 2)
192 #if !defined(HAVE_FABSL) || !defined(HAVE_FREXPL) || !defined(HAVE_LDEXPL) || !defined(HAVE_POWL) || !defined(HAVE_SQRTL) || !defined(HAVE_RINTL) || !defined(HAVE_MODFL)
193 #ifdef HAVE_LONG_DOUBLE
194 #undef HAVE_LONG_DOUBLE
198 #if FLT_RADIX == 2 && defined(HAVE_LONG_DOUBLE) && \
199 (defined(LDBL_MIN_EXP) && LDBL_MIN_EXP <= -16381 && defined(LDBL_MAX_EXP) && LDBL_MAX_EXP >= 16384 && defined(LDBL_MANT_DIG) && LDBL_MANT_DIG >= 64) && \
200 !(defined(LDBL_MIN_EXP) && LDBL_MIN_EXP <= -16381 && defined(LDBL_MAX_EXP) && LDBL_MAX_EXP >= 16384 && defined(LDBL_MANT_DIG) && LDBL_MANT_DIG >= 113)
201 typedef long double real80_t
;
202 #define mathfunc_real80_t(fn) cat(fn,l)
203 #define bits_real80_t LDBL_MANT_DIG
204 #if defined(HAVE_ISFINITEL)
205 #define isfinite_real80_t(x) isfinitel(x)
207 #if defined(HAVE_ISNANL) && !defined(__HAIKU__)
208 #define isnan_real80_t(x) isnanl(x)
210 #define REAL_MASK_3 (1 << 3)
213 #if defined(HAVE___FLOAT128) && \
214 defined(FLT128_MIN_EXP) && FLT128_MIN_EXP <= -16381 && defined(FLT128_MAX_EXP) && FLT128_MAX_EXP >= 16384 && defined(FLT128_MANT_DIG) && FLT128_MANT_DIG >= 113 && \
215 defined(HAVE_FABSQ) && defined(HAVE_FREXPQ) && defined(HAVE_LDEXPQ) && defined(HAVE_POWQ) && defined(HAVE_SQRTQ)
216 #define USEABLE___FLOAT128
219 #if FLT_RADIX == 2 && defined(HAVE_LONG_DOUBLE) && \
220 defined(LDBL_MIN_EXP) && LDBL_MIN_EXP <= -16381 && defined(LDBL_MAX_EXP) && LDBL_MAX_EXP >= 16384 && defined(LDBL_MANT_DIG) && LDBL_MANT_DIG >= 113 && \
221 !(!defined(HAVE_ISNANL) && defined(HAVE_ISNANQ) && defined(USEABLE___FLOAT128))
222 typedef long double real128_t
;
223 #define mathfunc_real128_t(fn) cat(fn,l)
224 #define bits_real128_t LDBL_MANT_DIG
225 #if defined(HAVE_ISFINITEL)
226 #define isfinite_real128_t(x) isfinitel(x)
228 #if defined(HAVE_ISNANL)
229 #define isnan_real128_t(x) isnanl(x)
231 #define REAL_MASK_4 (1 << 4)
232 #elif FLT_RADIX == 2 && defined(USEABLE___FLOAT128)
233 #define HAVE_NATIVE_FLOAT128
234 typedef __float128 real128_t
;
235 #define mathfunc_real128_t(fn) cat(fn,q)
236 #define bits_real128_t 113
237 #if defined(HAVE_ISFINITEQ)
238 #define isfinite_real128_t(x) isfiniteq(x)
240 #if defined(HAVE_ISNANQ)
241 #define isnan_real128_t(x) isnanq(x)
243 #define REAL_MASK_4 (1 << 4)
246 #if !defined(REAL_MASK_0) && defined(REAL_MASK_1)
247 #define HALF_FLOAT_CONVERSION
252 #define native_real16_t real32_t
253 static inline native_real16_t
unpack_real16_t(real16_t x
)
255 return half_to_float(x
.val
);
257 static inline real16_t
pack_real16_t(native_real16_t x
)
260 v
.val
= float_to_half(x
);
263 #define mathfunc_real16_t(fn) cat(fn,f)
264 #define bits_real16_t 11
265 #define isfinite_real16_t(x) (((x).val & 0x7fff) < 0x7c00)
266 #define isnan_real16_t(x) (((x).val & 0x7fff) > 0x7c00)
268 #define REAL16_T_IS_UINT16_T 1
269 typedef uint16_t real16_t
;
270 #define native_real16_t real32_t
271 #define unpack_real16_t(x) (half_to_float(x))
272 #define pack_real16_t(x) (float_to_half(x))
273 #define mathfunc_real16_t(fn) cat(fn,f)
274 #define bits_real16_t 11
275 #define isfinite_real16_t(x) (((x) & 0x7fff) < 0x7c00)
276 #define isnan_real16_t(x) (((x) & 0x7fff) > 0x7c00)
278 #define REAL_MASK_0 (1 << 0)
281 #ifndef REAL16_T_IS_UINT16_T
282 #define REAL16_T_IS_UINT16_T 0
285 #ifndef native_real16_t
286 #define native_real16_t real16_t
287 #define unpack_real16_t(x) (x)
288 #define pack_real16_t(x) (x)
290 #ifndef native_real32_t
291 #define native_real32_t real32_t
292 #define unpack_real32_t(x) (x)
293 #define pack_real32_t(x) (x)
295 #ifndef native_real64_t
296 #define native_real64_t real64_t
297 #define unpack_real64_t(x) (x)
298 #define pack_real64_t(x) (x)
300 #ifndef native_real80_t
301 #define native_real80_t real80_t
302 #define unpack_real80_t(x) (x)
303 #define pack_real80_t(x) (x)
305 #ifndef native_real128_t
306 #define native_real128_t real128_t
307 #define unpack_real128_t(x) (x)
308 #define pack_real128_t(x) (x)
311 #define FP_HAVE_INFINITY (HUGE_VAL == HUGE_VAL / 2)
313 #ifndef isfinite_real16_t
314 #define isfinite_real16_t(x) (FP_HAVE_INFINITY && cat(mathfunc_,real16_t)(fabs)(x) < HUGE_VAL)
316 #ifndef isfinite_real32_t
317 #define isfinite_real32_t(x) (FP_HAVE_INFINITY && cat(mathfunc_,real32_t)(fabs)(x) < HUGE_VAL)
319 #ifndef isfinite_real64_t
320 #define isfinite_real64_t(x) (FP_HAVE_INFINITY && cat(mathfunc_,real64_t)(fabs)(x) < HUGE_VAL)
322 #ifndef isfinite_real80_t
323 #define isfinite_real80_t(x) (FP_HAVE_INFINITY && cat(mathfunc_,real80_t)(fabs)(x) < HUGE_VAL)
325 #ifndef isfinite_real128_t
326 #define isfinite_real128_t(x) (FP_HAVE_INFINITY && cat(mathfunc_,real128_t)(fabs)(x) < HUGE_VAL)
329 #if defined(__BORLANDC__) || defined(_MSC_VER)
330 #define isnan_real32_t(x) _isnan(x)
331 #define isnan_real64_t(x) _isnan(x)
332 #define isnan_real80_t(x) _isnanl(x)
335 #ifndef isnan_real16_t
336 #define isnan_real16_t(x) ((x) != (x))
338 #ifndef isnan_real32_t
339 #define isnan_real32_t(x) ((x) != (x))
341 #ifndef isnan_real64_t
342 #define isnan_real64_t(x) ((x) != (x))
344 #ifndef isnan_real80_t
345 #define isnan_real80_t(x) ((x) != (x))
347 #ifndef isnan_real128_t
348 #define isnan_real128_t(x) ((x) != (x))
352 #define REAL_MASK_0 0
355 #define REAL_MASK_1 0
358 #define REAL_MASK_2 0
361 #define REAL_MASK_3 0
364 #define REAL_MASK_4 0
367 #define REAL_MASK (REAL_MASK_0 | REAL_MASK_1 | REAL_MASK_2 | REAL_MASK_3 | REAL_MASK_4)
369 #define TYPE_REAL_N 5
370 #define real_max_t real128_t
371 #elif REAL_MASK & 0x8
372 #define TYPE_REAL_N 4
373 #define real_max_t real80_t
374 #elif REAL_MASK & 0x4
375 #define TYPE_REAL_N 3
376 #define real_max_t real64_t
377 #elif REAL_MASK & 0x2
378 #define TYPE_REAL_N 2
379 #define real_max_t real32_t
380 #elif REAL_MASK & 0x1
381 #define TYPE_REAL_N 1
382 #define real_max_t real16_t
384 #define TYPE_REAL_N 0
387 #define NEED_OP_EMULATION (INT_MASK != 0x1f || REAL_MASK != 0x1f)
389 #if !defined(signbit) || (defined(HAVE___FLOAT128) && !defined(HAVE_SIGNBIT___FLOAT128))
393 int signbit_d(double d
);
394 #define signbit(x) signbit_d((double)(x))
395 #define need_signbit_d
399 /* these macros cause a crash on 64-bit HPUX */
400 #if defined(isunordered) && defined(islessgreater) && defined(isgreater) && defined(isgreaterequal) && defined(isless) && defined(islessequal) && defined(isgreater) && defined(isgreaterequal)
401 #if GNUC_ATLEAST(3,0,0) && defined(__linux__)
402 #define use_is_macros
406 typedef uchar_efficient_t ajla_flat_option_t
;
409 typedef int32_t pcode_t
;
410 typedef uint32_t upcode_t
;
412 #define SIZEOF_IP_T 4
413 typedef uint32_t ip_t
;
414 typedef uint32_t frame_t
;
415 typedef uint32_t stack_size_t
;
417 typedef uint32_t arg_t
;
418 #define NO_FRAME_T ((frame_t)-1)
420 #define frame_t_is_const(f) ((f) != NO_FRAME_T && (f) & sign_bit(frame_t))
421 static inline int32_t frame_t_get_const(frame_t f
)
423 return (int32_t)(f
- 0xc0000000UL
);
425 static inline frame_t
frame_t_from_const(int32_t c
)
427 /* this logic is copied in simplify_instr */
428 /*ajla_assert_lo(c >= -0x40000000L && c < 0x3fffffffL, (file_line, "frame_t_from_const: invalid constant %ld", (long)c));*/
429 return (frame_t
)c
+ 0xc0000000UL
;
432 typedef frame_t ajla_option_t
; /* some code casts ajla_option_t to frame_t */
434 #if (defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP)) || defined(OS_OS2) || defined(OS_WIN32)
438 #if defined(SIZEOF_VOID_P) && SIZEOF_VOID_P >= 8 && !defined(UNUSUAL_NO_POINTER_COMPRESSION) && defined(USE_AMALLOC) && !defined(__hpux) && !defined(__OpenBSD__)
439 #define POINTER_COMPRESSION_POSSIBLE 3
442 #if defined(POINTER_COMPRESSION_POSSIBLE) && defined(HAVE_SYS_MMAN_H) && (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__))
443 #include <sys/mman.h>
445 #undef POINTER_COMPRESSION_POSSIBLE
450 #if defined(C_BIG_ENDIAN) ^ defined(UNUSUAL)
451 #define CODE_ENDIAN 1
453 #define CODE_ENDIAN 0
456 #if !defined(UNUSUAL)
457 #define STACK_INITIAL_SIZE 1024
459 #define STACK_INITIAL_SIZE 1
462 /* valid values: 16 - 128 */
463 #if !defined(UNUSUAL)
464 #define BTREE_MAX_SIZE 16
466 #define BTREE_MAX_SIZE 128
469 #define ARRAY_PREALLOC_SIZE 4
471 #define DEFAULT_TICK_US 10000