1
1
#include <stdio.h>
2
2
#include <stdint.h>
3
-
3
+ /* Backup for targets that don't support i128 - TODO: replace this with software emulation!*/
4
+ #if !defined(__SIZEOF_INT128__ ) || defined(__LCC__ )
5
+ #define __int128_t long long
6
+ #define __int128 __int128_t
7
+ #define __uint128_t unsigned long long
8
+ #define uint128_t unsigned long long
9
+ #endif
4
10
#if !defined(__TINYC__ ) && !defined(__LCC__ )
5
11
#include <stdbool.h>
6
12
#elif defined(__LCC__ )
12
18
#define false 0
13
19
#define true 1
14
20
#endif
15
-
21
+ #ifdef __LCC__
22
+ #define __func__ "unknown"
23
+ #endif
16
24
#include <stdlib.h>
17
25
#include <string.h>
18
26
#include <math.h>
@@ -39,18 +47,20 @@ inline void* _mm_malloc(size_t size, size_t align){
39
47
inline void _mm_free (void * p ){
40
48
free (p );
41
49
}
50
+ #endif
51
+ #if (defined(__TINYC__ ) || defined(__SDCC ) || defined(__LCC__ ))
42
52
inline uint16_t __builtin_bswap16 (uint16_t val ){
43
53
uint16_t res ;
44
- uint8_t * ptr = & val ;
45
- uint8_t * rptr = & res ;
54
+ uint8_t * ptr = ( uint8_t * ) & val ;
55
+ uint8_t * rptr = ( uint8_t * ) & res ;
46
56
rptr [0 ] = ptr [1 ];
47
57
rptr [1 ] = ptr [0 ];
48
58
return res ;
49
59
}
50
60
inline uint32_t __builtin_bswap32 (uint32_t val ){
51
61
uint32_t res ;
52
- uint8_t * ptr = & val ;
53
- uint8_t * rptr = & res ;
62
+ uint8_t * ptr = ( uint8_t * ) & val ;
63
+ uint8_t * rptr = ( uint8_t * ) & res ;
54
64
rptr [0 ] = ptr [3 ];
55
65
rptr [1 ] = ptr [2 ];
56
66
rptr [2 ] = ptr [1 ];
@@ -59,8 +69,8 @@ inline uint32_t __builtin_bswap32(uint32_t val){
59
69
}
60
70
inline uint64_t __builtin_bswap64 (uint64_t val ){
61
71
uint64_t res ;
62
- uint8_t * ptr = & val ;
63
- uint8_t * rptr = & res ;
72
+ uint8_t * ptr = ( uint8_t * ) & val ;
73
+ uint8_t * rptr = ( uint8_t * ) & res ;
64
74
rptr [0 ] = ptr [7 ];
65
75
rptr [1 ] = ptr [6 ];
66
76
rptr [2 ] = ptr [5 ];
@@ -71,6 +81,28 @@ inline uint64_t __builtin_bswap64(uint64_t val){
71
81
rptr [7 ] = ptr [0 ];
72
82
return res ;
73
83
}
84
+ inline uint128_t __builtin_bswap128 (uint128_t val ){
85
+ uint128_t res ;
86
+ uint8_t * ptr = (uint8_t * )& val ;
87
+ uint8_t * rptr = (uint8_t * )& res ;
88
+ rptr [0 ] = ptr [15 ];
89
+ rptr [1 ] = ptr [14 ];
90
+ rptr [2 ] = ptr [13 ];
91
+ rptr [3 ] = ptr [12 ];
92
+ rptr [4 ] = ptr [11 ];
93
+ rptr [5 ] = ptr [10 ];
94
+ rptr [6 ] = ptr [9 ];
95
+ rptr [7 ] = ptr [8 ];
96
+ rptr [8 ] = ptr [7 ];
97
+ rptr [9 ] = ptr [6 ];
98
+ rptr [10 ] = ptr [5 ];
99
+ rptr [11 ] = ptr [4 ];
100
+ rptr [12 ] = ptr [3 ];
101
+ rptr [13 ] = ptr [2 ];
102
+ rptr [14 ] = ptr [1 ];
103
+ rptr [16 ] = ptr [0 ];
104
+ return res ;
105
+ }
74
106
#endif
75
107
#ifdef __clang__
76
108
static inline __uint128_t __builtin_bswap128 (__uint128_t val ){
@@ -115,41 +147,41 @@ __uint128_t __builtin_bswap128(__uint128_t val);
115
147
#define FORCE_NOT_ZST char force_not_zst;
116
148
#endif
117
149
118
- #if !(defined(__SDCC ) || defined(_MSC_VER ))
150
+ #if !(defined(__SDCC ) || defined(_MSC_VER ) || defined( __LCC__ ) )
119
151
#include <alloca.h>
152
+ #define register_alloca_aligned (type , align , hash )
153
+ #define loc_alloc_aligned (name , type , align , hash ) name = (void*)((((size_t)(alloca(sizeof(type) + align) + align - 1)) / align)*align);
120
154
#elif defined(_MSC_VER )
121
155
#define alloca _alloca
156
+ #define register_alloca_aligned (type , align , hash )
157
+ #define loc_alloc_aligned (name , type , align , hash ) name = (void*)((((size_t)(alloca(sizeof(type) + align) + align - 1)) / align)*align);
122
158
#else
123
- #define alloca (arg ) ((void*)0)
124
- #endif
125
- /* Backup for targets that don't support i128 - TODO: replace this with software emulation!*/
126
- #if !defined(__SIZEOF_INT128__ ) || defined(__LCC__ )
127
- #define __int128_t long long
128
- #define __int128 __int128_t
129
- #define __uint128_t unsigned long long
159
+ #define register_alloca_aligned (type , align , hash ) char hash[sizeof(type) + align];
160
+ #define loc_alloc_aligned (name , type , align , hash ) name = (void*)((((size_t)(&hash + align - 1)) / align)*align);
130
161
#endif
162
+
131
163
#ifdef __clang__
132
164
#define __atomic_compare_exchange_4 __atomic_compare_exchange_n
133
165
#define __atomic_compare_exchange_8 __atomic_compare_exchange_n
134
166
#define _Float128 long double
135
- #elif defined(__SDCC )
167
+ #elif ( defined(__SDCC ) || defined( __LCC__ ) )
136
168
// WARNING! Assumes a single-threaded, no-interrupt eviroment!
137
169
bool __atomic_compare_exchange_4 (uint32_t * ptr , uint32_t * expected , uint32_t desired , bool weak , int success_memorder , int failure_memorder ){
138
- if (* ptr == expected ){
170
+ if (* ptr == * expected ){
139
171
* ptr = desired ;
140
172
return true;
141
173
}
142
174
return false;
143
175
}
144
176
bool __atomic_compare_exchange_8 (uint64_t * ptr , uint64_t * expected , uint64_t desired , bool weak , int success_memorder , int failure_memorder ){
145
- if (* ptr == expected ){
177
+ if (* ptr == * expected ){
146
178
* ptr = desired ;
147
179
return true;
148
180
}
149
181
return false;
150
182
}
151
183
bool __atomic_compare_exchange_n (uintptr_t * ptr , uintptr_t * expected , uintptr_t desired , bool weak , int success_memorder , int failure_memorder ){
152
- if (* ptr == expected ){
184
+ if (* ptr == * expected ){
153
185
* ptr = desired ;
154
186
return true;
155
187
}
@@ -195,7 +227,7 @@ static inline void *System_Runtime_InteropServices_NativeMemory_AlignedReallocpv
195
227
#define eprintf (...) printf(__VA_ARGS__)
196
228
#define BUILTIN_UNSUPORTED (NAME ,OUTPUT , ARGLIST ) static inline OUTPUT NAME ARGLIST { eprintf("Function " #NAME "is not yet supported!"); abort();}
197
229
#endif
198
- #ifdef __SDCC
230
+ #if defined( __SDCC ) || defined( __LCC__ )
199
231
#define NAN (0.0 / 0.0)
200
232
#endif
201
233
#ifdef __SDCC
@@ -326,7 +358,7 @@ static inline __uint128_t System_UInt128_op_Explicitf64u128(double val) {
326
358
/*Assumes a 64 bit OS.*/
327
359
#define System_Buffers_Binary_BinaryPrimitives_ReverseEndiannessisizeisize (val ) (intptr_t) __builtin_bswap64((uint64_t)val)
328
360
#define System_Buffers_Binary_BinaryPrimitives_ReverseEndiannessusizeusize __builtin_bswap64
329
- #ifndef __SDCC
361
+ #if !(defined( __SDCC ) || defined( __LCC__ ))
330
362
static inline int32_t System_Numerics_BitOperations_TrailingZeroCountusizei32 (uintptr_t val ) {if (val == 0 ) return sizeof (uintptr_t ) * 8 ; return (int32_t ) __builtin_ctzl ((uint64_t )val );}
331
363
static inline int32_t System_Numerics_BitOperations_TrailingZeroCountu32i32 (uint32_t val ) {if (val == 0 ) return sizeof (uint32_t ) * 8 ; return (int32_t ) __builtin_ctzl ((uint32_t )val );}
332
364
static inline int32_t System_Numerics_BitOperations_TrailingZeroCounti32i32 (int32_t val ) {if (val == 0 ) return sizeof (int32_t ) * 8 ; return (int32_t ) __builtin_ctzl ((uint32_t )val );}
@@ -341,6 +373,7 @@ static inline int32_t System_Numerics_BitOperations_LeadingZeroCountusizei32(uin
341
373
#define System_Numerics_BitOperations_PopCountu64i32 (val ) __builtin_popcountl((uint64_t)val)
342
374
#define System_UInt128_PopCountu128u128 (val ) __builtin_popcountl((uint64_t)val)
343
375
376
+ #define System_Console_WriteLinev () printf("\n")
344
377
#define System_Console_WriteLinestv (msg ) printf("%s\n", msg)
345
378
#define System_Console_WriteLinef64v (val ) printf("%f\n", val)
346
379
@@ -484,8 +517,12 @@ double fabsf64(double val);
484
517
#define System_Double_Sinf64f64 sin
485
518
#define System_Double_Absf64f64 fabsf64
486
519
#define System_Single_Absf32f32 fabsf32
520
+ #define System_Single_Sqrtf32f32 (x ) (float)sqrt((double)x)
487
521
#define System_MathF_Sqrtf32f32 (x ) (float)sqrt((double)x)
522
+ #define System_Double_Sqrtf64f64 sqrt
488
523
#define System_MathF_Sqrtf64f64 sqrt
524
+ #define System_Single_Floorf32f32 (x ) (float)floor((double)x)
525
+ #define System_Single_Ceilingf32f32 (x ) (float)ceil((double)x)
489
526
#define System_MathF_Roundf32f32 (x ) (float)round((double)x)
490
527
#define System_Single_Powf32f32f32 (a , b ) (float)pow(a, b)
491
528
#define System_Single_Powf64f64f64 pow
@@ -555,11 +592,14 @@ BUILTIN_UNSUPORTED(System_Double_Log2f64f64,double,(double input))
555
592
#define System_Math_Floorf64f64 (input ) floor(input)
556
593
#define System_Math_Sqrtf64f64 (input ) sqrt(input)
557
594
#define System_Math_Ceilingf64f64 (input ) ceil(input)
595
+ #define System_Double_Ceilingf64f64 (input ) ceil(input)
558
596
#define System_MathF_Ceilingf32f32 (input ) (float)ceil((double)input)
559
597
#define System_Math_Floorf64f64 (input ) floor(input)
598
+ #define System_Double_Floorf64f64 (input ) floor(input)
560
599
#define System_MathF_Floorf32f32 (input ) (float)floor((double)input)
561
600
#define System_Math_Truncatef64f64 (input ) trunc(input)
562
-
601
+ #define System_Double_Truncatef64f64 (input ) trunc(input)
602
+ #define System_Single_Truncatef32f32 (input ) (float)trunc((double)input)
563
603
static inline uint32_t System_Threading_Interlocked_CompareExchangeru32u32u32u32 (uint32_t * addr , uint32_t value , uint32_t comparand )
564
604
{
565
605
uint32_t res = 0 ;
@@ -612,7 +652,7 @@ static inline intptr_t System_Threading_Interlocked_CompareExchangerisizeisizeis
612
652
return comparand ;
613
653
}
614
654
}
615
- #ifndef __SDCC
655
+ #if !(defined( __SDCC ) || defined( __LCC__ ))
616
656
static inline uint32_t System_Threading_Interlocked_Exchangeru32u32u32 (uint32_t * addr , uint32_t val )
617
657
{
618
658
uint32_t ret ;
@@ -704,7 +744,7 @@ static inline uint8_t System_Byte_RotateLeftu8i32u8(uint8_t val, int32_t amount)
704
744
if (amount == 0 ) return val ;
705
745
return ((val << amount ) | (val >> ( (sizeof (uint8_t )* 8 ) - amount )));
706
746
}
707
- #ifndef __SDCC
747
+ #if !(defined( __SDCC ) || defined( __LCC__ ))
708
748
static inline unsigned __int128 System_UInt128_LeadingZeroCountu128u128 (unsigned __int128 val ){ if (val == 0 ) return 128 ; return __builtin_clzl (val ); }
709
749
#endif
710
750
static inline uint32_t System_Math_Minu32u32u32 (uint32_t lhs , uint32_t rhs )
0 commit comments