Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / device / include / stdckdint.h
blob3494e26268e439c36cfbe884fcc0fe3907e3da95
1 /*-------------------------------------------------------------------------
2 stdckdint.h: Checked integer arithmetic
4 Copyright (C) 2021, Philipp Klaus Krause, pkk@spth.de
5 Copyright (C) 2023, Philipp Klaus Krause, philipp@colecovision.eu
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA.
22 As a special exception, if you link this library with other files,
23 some of which are compiled with SDCC, to produce an executable,
24 this library does not by itself cause the resulting executable to
25 be covered by the GNU General Public License. This exception does
26 not however invalidate any other reasons why the executable file
27 might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------*/
30 #ifndef __SDCC_STDCKDINT_H
31 #define __SDCC_STDCKDINT_H 1
33 _Static_assert (sizeof(long long) >= 2 * sizeof(long));
35 // Default implementation: Suitable for additive operators for everything smaller than long long, and for multiplication for everything smaller than long long except for unsigned long times unsigned long. Not very efficient. Todo: Replace by more efficient implementation using _BitInt once all ports support _BitInt.
36 #define __CKD_DEFAULT_IMPL(T,O) \
37 (T *r, signed long long a, signed long long b) \
38 { \
39 signed long long result = a O b; \
40 *r = result; \
41 return (*r != result); \
44 #define __CKD_ULL_IMPL(T,O) \
45 (T *r, unsigned long long a, unsigned long long b) \
46 { \
47 unsigned long long result = a O b; \
48 *r = result; \
49 return (*r != result); \
52 inline _Bool __ckd_add_schar __CKD_DEFAULT_IMPL(signed char, +)
53 inline _Bool __ckd_add_uchar __CKD_DEFAULT_IMPL(unsigned char, +)
54 inline _Bool __ckd_add_short __CKD_DEFAULT_IMPL(short, +)
55 inline _Bool __ckd_add_ushort __CKD_DEFAULT_IMPL(unsigned short, +)
56 inline _Bool __ckd_add_int __CKD_DEFAULT_IMPL(int, +)
57 inline _Bool __ckd_add_uint __CKD_DEFAULT_IMPL(unsigned int, +)
58 inline _Bool __ckd_add_long __CKD_DEFAULT_IMPL(long, +)
59 inline _Bool __ckd_add_ulong __CKD_DEFAULT_IMPL(unsigned long, +)
61 inline _Bool __ckd_sub_schar __CKD_DEFAULT_IMPL(signed char, -)
62 inline _Bool __ckd_sub_uchar __CKD_DEFAULT_IMPL(unsigned char, -)
63 inline _Bool __ckd_sub_short __CKD_DEFAULT_IMPL(short, -)
64 inline _Bool __ckd_sub_ushort __CKD_DEFAULT_IMPL(unsigned short, -)
65 inline _Bool __ckd_sub_int __CKD_DEFAULT_IMPL(int, -)
66 inline _Bool __ckd_sub_uint __CKD_DEFAULT_IMPL(unsigned int, -)
67 inline _Bool __ckd_sub_long __CKD_DEFAULT_IMPL(long, -)
68 inline _Bool __ckd_sub_ulong __CKD_DEFAULT_IMPL(unsigned long, -)
70 inline _Bool __ckd_mul_schar __CKD_DEFAULT_IMPL(signed char, *)
71 inline _Bool __ckd_mul_uchar __CKD_DEFAULT_IMPL(unsigned char, *)
72 inline _Bool __ckd_mul_short __CKD_DEFAULT_IMPL(short, *)
73 inline _Bool __ckd_mul_ushort __CKD_DEFAULT_IMPL(unsigned short, *)
74 inline _Bool __ckd_mul_int __CKD_DEFAULT_IMPL(int, *)
75 inline _Bool __ckd_mul_uint __CKD_DEFAULT_IMPL(unsigned int, *)
76 inline _Bool __ckd_mul_long __CKD_DEFAULT_IMPL(long, *)
77 inline _Bool __ckd_mul_ulong __CKD_DEFAULT_IMPL(unsigned long, *)
79 inline _Bool __ckd_mul_ulongull __CKD_ULL_IMPL(unsigned long, *)
81 #if 0 // Elegant C, but inefficient asm - SDCC can't inline the calls!
82 #define __ckd_add_default(r, a, b) \
83 _Generic ((r), \
84 signed char * : __ckd_add_schar, \
85 unsigned char * : __ckd_add_uchar, \
86 short * : __ckd_add_short, \
87 unsigned short * : __ckd_add_ushort, \
88 int * : __ckd_add_int, \
89 unsigned int * : __ckd_add_uint, \
90 long * : __ckd_add_long, \
91 unsigned long * : __ckd_add_ulong) \
92 ((r), (a), (b))
93 #else
94 #define __ckd_add_default(r, a, b) \
95 _Generic ((r), \
96 signed char * : __ckd_add_schar((r), (a), (b)), \
97 unsigned char * : __ckd_add_uchar((r), (a), (b)), \
98 short * : __ckd_add_short((r), (a), (b)), \
99 unsigned short * : __ckd_add_ushort((r), (a), (b)), \
100 int * : __ckd_add_int((r), (a), (b)), \
101 unsigned int * : __ckd_add_uint((r), (a), (b)), \
102 long * : __ckd_add_long((r), (a), (b)), \
103 unsigned long * : __ckd_add_ulong((r), (a), (b)))
104 #endif
106 // Elegant C, but inefficient asm - SDCC can't inline the calls! - but the alternative fails stm8 regression tests?
107 #define __ckd_sub_default(r, a, b) \
108 _Generic ((r), \
109 signed char * : __ckd_sub_schar, \
110 unsigned char * : __ckd_sub_uchar, \
111 short * : __ckd_sub_short, \
112 unsigned short * : __ckd_sub_ushort, \
113 int * : __ckd_sub_int, \
114 unsigned int * : __ckd_sub_uint, \
115 long * : __ckd_sub_long, \
116 unsigned long * : __ckd_sub_ulong) \
117 ((r), (a), (b))
119 #if 0 // Elegant C, but inefficient asm - SDCC can't inline the calls!
120 #define __ckd_mul_default(r, a, b) \
121 _Generic ((r), \
122 signed char * : __ckd_mul_schar, \
123 unsigned char * : __ckd_mul_uchar, \
124 short * : __ckd_mul_short, \
125 unsigned short * : __ckd_mul_ushort, \
126 int * : __ckd_mul_int, \
127 unsigned int * : __ckd_mul_uint, \
128 long * : __ckd_mul_long, \
129 unsigned long * : __ckd_mul_ulong \
130 ((r), (a), (b)))
131 #else
132 #define __ckd_mul_default(r, a, b) \
133 _Generic ((r), \
134 signed char * : __ckd_mul_schar((r), (a), (b)), \
135 unsigned char * : __ckd_mul_uchar((r), (a), (b)), \
136 short * : __ckd_mul_short((r), (a), (b)), \
137 unsigned short * : __ckd_mul_ushort((r), (a), (b)), \
138 int * : __ckd_mul_int((r), (a), (b)), \
139 unsigned int * : __ckd_mul_uint((r), (a), (b)), \
140 long * : __ckd_mul_long((r), (a), (b)), \
141 unsigned long * : __ckd_mul_ulong((r), (a), (b)))
142 #endif
145 extern _Bool __ckd_add_unimplemented (void *, unsigned long long, unsigned long long);
147 #define ckd_add(r, a, b) \
148 _Generic ((a), \
149 signed long long: __ckd_add_unimplemented(r, a, b), \
150 unsigned long long: __ckd_add_unimplemented(r, a, b), \
151 default: \
152 _Generic ((b), \
153 signed long long: __ckd_add_unimplemented(r, a, b), \
154 unsigned long long: __ckd_add_unimplemented(r, a, b), \
155 default: __ckd_add_default(r, a, b))) \
157 extern _Bool __ckd_sub_unimplemented (void *, unsigned long long, unsigned long long);
159 #define ckd_sub(r, a, b) \
160 _Generic ((a), \
161 signed long long: __ckd_sub_unimplemented(r, a, b), \
162 unsigned long long: __ckd_sub_unimplemented(r, a, b), \
163 default: \
164 _Generic ((b), \
165 signed long long: __ckd_sub_unimplemented(r, a, b), \
166 unsigned long long: __ckd_sub_unimplemented(r, a, b), \
167 default: __ckd_sub_default(r, a, b))) \
169 extern _Bool __ckd_mul_unimplemented (void *, unsigned long long, unsigned long long);
171 #define ckd_mul(r, a, b) \
172 _Generic ((a), \
173 signed long long: __ckd_mul_unimplemented(r, a, b), \
174 unsigned long long: __ckd_mul_unimplemented(r, a, b), \
175 unsigned long: \
176 _Generic ((b), \
177 signed long long: __ckd_mul_unimplemented(r, a, b), \
178 unsigned long long: __ckd_mul_unimplemented(r, a, b), \
179 unsigned long: __ckd_mul_ulongull(r, a, b), \
180 default: __ckd_mul_default(r, a, b)), \
181 default: \
182 _Generic ((b), \
183 signed long long: __ckd_mul_unimplemented(r, a, b), \
184 unsigned long long: __ckd_mul_unimplemented(r, a, b), \
185 default: __ckd_mul_default(r, a, b))) \
187 #endif