1.1 --- a/templates/native/int.c Thu Mar 23 16:40:57 2017 +0100
1.2 +++ b/templates/native/int.c Thu Mar 23 23:36:32 2017 +0100
1.3 @@ -16,7 +16,6 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 */
1.6
1.7 -#include <limits.h> /* INT_MAX, INT_MIN */
1.8 #include <math.h> /* ceil, log10, pow */
1.9 #include <stdio.h> /* fdopen, snprintf */
1.10 #include <errno.h> /* errno */
1.11 @@ -32,24 +31,17 @@
1.12
1.13 /* Integer operations. */
1.14
1.15 -__attr __fn_native_int_int_new(__attr __args[])
1.16 -{
1.17 - __attr * const _data = &__args[1];
1.18 -
1.19 - return __new_int(_data->intvalue);
1.20 -}
1.21 -
1.22 __attr __fn_native_int_int_add(__attr __args[])
1.23 {
1.24 - __attr * const _data = &__args[1];
1.25 + __attr * const self = &__args[1];
1.26 __attr * const other = &__args[2];
1.27 - /* _data and other interpreted as int */
1.28 - int i = _data->intvalue;
1.29 - int j = other->intvalue;
1.30 + /* self and other interpreted as int */
1.31 + int i = __TOINT(*self);
1.32 + int j = __TOINT(*other);
1.33
1.34 /* Test for overflow. */
1.35 - if (((i > 0) && (j > 0) && (i > INT_MAX - j)) ||
1.36 - ((i < 0) && (j < 0) && (i < INT_MIN - j)))
1.37 + if (((i > 0) && (j > 0) && (i > __MAXINT - j)) ||
1.38 + ((i < 0) && (j < 0) && (i < __MININT - j)))
1.39
1.40 __raise_overflow_error();
1.41
1.42 @@ -59,15 +51,15 @@
1.43
1.44 __attr __fn_native_int_int_sub(__attr __args[])
1.45 {
1.46 - __attr * const _data = &__args[1];
1.47 + __attr * const self = &__args[1];
1.48 __attr * const other = &__args[2];
1.49 - /* _data and other interpreted as int */
1.50 - int i = _data->intvalue;
1.51 - int j = other->intvalue;
1.52 + /* self and other interpreted as int */
1.53 + int i = __TOINT(*self);
1.54 + int j = __TOINT(*other);
1.55
1.56 /* Test for overflow. */
1.57 - if (((i < 0) && (j > 0) && (i < INT_MIN + j)) ||
1.58 - ((i > 0) && (j < 0) && (i > INT_MAX + j)))
1.59 + if (((i < 0) && (j > 0) && (i < __MININT + j)) ||
1.60 + ((i > 0) && (j < 0) && (i > __MAXINT + j)))
1.61
1.62 __raise_overflow_error();
1.63
1.64 @@ -77,17 +69,17 @@
1.65
1.66 __attr __fn_native_int_int_mul(__attr __args[])
1.67 {
1.68 - __attr * const _data = &__args[1];
1.69 + __attr * const self = &__args[1];
1.70 __attr * const other = &__args[2];
1.71 - /* _data and other interpreted as int */
1.72 - int i = _data->intvalue;
1.73 - int j = other->intvalue;
1.74 + /* self and other interpreted as int */
1.75 + int i = __TOINT(*self);
1.76 + int j = __TOINT(*other);
1.77
1.78 /* Test for overflow. */
1.79 - if (((i > 0) && (j > 0) && (i > INT_MAX / j)) ||
1.80 - ((i < 0) && (j < 0) && (i > INT_MAX / j)) ||
1.81 - ((i < 0) && (j > 0) && (i < INT_MIN / j)) ||
1.82 - ((i > 0) && (j < 0) && (j < INT_MIN / i)))
1.83 + if (((i > 0) && (j > 0) && (i > __MAXINT / j)) ||
1.84 + ((i < 0) && (j < 0) && (i > __MAXINT / j)) ||
1.85 + ((i < 0) && (j > 0) && (i < __MININT / j)) ||
1.86 + ((i > 0) && (j < 0) && (j < __MININT / i)))
1.87
1.88 __raise_overflow_error();
1.89
1.90 @@ -97,16 +89,16 @@
1.91
1.92 __attr __fn_native_int_int_div(__attr __args[])
1.93 {
1.94 - __attr * const _data = &__args[1];
1.95 + __attr * const self = &__args[1];
1.96 __attr * const other = &__args[2];
1.97 - /* _data and other interpreted as int */
1.98 - int i = _data->intvalue;
1.99 - int j = other->intvalue;
1.100 + /* self and other interpreted as int */
1.101 + int i = __TOINT(*self);
1.102 + int j = __TOINT(*other);
1.103
1.104 /* Test for division by zero or overflow. */
1.105 if (j == 0)
1.106 __raise_zero_division_error();
1.107 - else if ((j == -1) && (i == INT_MIN))
1.108 + else if ((j == -1) && (i == __MININT))
1.109 __raise_overflow_error();
1.110
1.111 /* Return the new integer. */
1.112 @@ -115,16 +107,16 @@
1.113
1.114 __attr __fn_native_int_int_mod(__attr __args[])
1.115 {
1.116 - __attr * const _data = &__args[1];
1.117 + __attr * const self = &__args[1];
1.118 __attr * const other = &__args[2];
1.119 - /* _data and other interpreted as int */
1.120 - int i = _data->intvalue;
1.121 - int j = other->intvalue;
1.122 + /* self and other interpreted as int */
1.123 + int i = __TOINT(*self);
1.124 + int j = __TOINT(*other);
1.125
1.126 /* Test for division by zero or overflow. */
1.127 if (j == 0)
1.128 __raise_zero_division_error();
1.129 - else if ((j == -1) && (i == INT_MIN))
1.130 + else if ((j == -1) && (i == __MININT))
1.131 __raise_overflow_error();
1.132
1.133 /* Return the new integer. */
1.134 @@ -133,12 +125,12 @@
1.135
1.136 __attr __fn_native_int_int_neg(__attr __args[])
1.137 {
1.138 - __attr * const _data = &__args[1];
1.139 - /* _data interpreted as int */
1.140 - int i = _data->intvalue;
1.141 + __attr * const self = &__args[1];
1.142 + /* self interpreted as int */
1.143 + int i = __TOINT(*self);
1.144
1.145 /* Test for overflow. */
1.146 - if (i == INT_MIN)
1.147 + if (i == __MININT)
1.148 __raise_overflow_error();
1.149
1.150 /* Return the new integer. */
1.151 @@ -147,11 +139,11 @@
1.152
1.153 __attr __fn_native_int_int_pow(__attr __args[])
1.154 {
1.155 - __attr * const _data = &__args[1];
1.156 + __attr * const self = &__args[1];
1.157 __attr * const other = &__args[2];
1.158 - /* _data and other interpreted as int */
1.159 - int i = _data->intvalue;
1.160 - int j = other->intvalue;
1.161 + /* self and other interpreted as int */
1.162 + int i = __TOINT(*self);
1.163 + int j = __TOINT(*other);
1.164 int k;
1.165
1.166 errno = 0;
1.167 @@ -168,11 +160,11 @@
1.168
1.169 __attr __fn_native_int_int_and(__attr __args[])
1.170 {
1.171 - __attr * const _data = &__args[1];
1.172 + __attr * const self = &__args[1];
1.173 __attr * const other = &__args[2];
1.174 - /* _data and other interpreted as int */
1.175 - int i = _data->intvalue;
1.176 - int j = other->intvalue;
1.177 + /* self and other interpreted as int */
1.178 + int i = __TOINT(*self);
1.179 + int j = __TOINT(*other);
1.180
1.181 /* Return the new integer. */
1.182 /* NOTE: No overflow test applied. */
1.183 @@ -181,9 +173,9 @@
1.184
1.185 __attr __fn_native_int_int_not(__attr __args[])
1.186 {
1.187 - __attr * const _data = &__args[1];
1.188 - /* _data interpreted as int */
1.189 - int i = _data->intvalue;
1.190 + __attr * const self = &__args[1];
1.191 + /* self interpreted as int */
1.192 + int i = __TOINT(*self);
1.193
1.194 /* Return the new integer. */
1.195 return __new_int(~i);
1.196 @@ -191,11 +183,11 @@
1.197
1.198 __attr __fn_native_int_int_or(__attr __args[])
1.199 {
1.200 - __attr * const _data = &__args[1];
1.201 + __attr * const self = &__args[1];
1.202 __attr * const other = &__args[2];
1.203 - /* _data and other interpreted as int */
1.204 - int i = _data->intvalue;
1.205 - int j = other->intvalue;
1.206 + /* self and other interpreted as int */
1.207 + int i = __TOINT(*self);
1.208 + int j = __TOINT(*other);
1.209
1.210 /* Return the new integer. */
1.211 /* NOTE: No overflow test applied. */
1.212 @@ -204,11 +196,11 @@
1.213
1.214 __attr __fn_native_int_int_xor(__attr __args[])
1.215 {
1.216 - __attr * const _data = &__args[1];
1.217 + __attr * const self = &__args[1];
1.218 __attr * const other = &__args[2];
1.219 - /* _data and other interpreted as int */
1.220 - int i = _data->intvalue;
1.221 - int j = other->intvalue;
1.222 + /* self and other interpreted as int */
1.223 + int i = __TOINT(*self);
1.224 + int j = __TOINT(*other);
1.225
1.226 /* Return the new integer. */
1.227 /* NOTE: No overflow test applied. */
1.228 @@ -217,11 +209,11 @@
1.229
1.230 __attr __fn_native_int_int_lt(__attr __args[])
1.231 {
1.232 - __attr * const _data = &__args[1];
1.233 + __attr * const self = &__args[1];
1.234 __attr * const other = &__args[2];
1.235 - /* _data and other interpreted as int */
1.236 - int i = _data->intvalue;
1.237 - int j = other->intvalue;
1.238 + /* self and other interpreted as int */
1.239 + int i = __TOINT(*self);
1.240 + int j = __TOINT(*other);
1.241
1.242 /* Return a boolean result. */
1.243 return i < j ? __builtins___boolean_True : __builtins___boolean_False;
1.244 @@ -229,11 +221,11 @@
1.245
1.246 __attr __fn_native_int_int_gt(__attr __args[])
1.247 {
1.248 - __attr * const _data = &__args[1];
1.249 + __attr * const self = &__args[1];
1.250 __attr * const other = &__args[2];
1.251 - /* _data and other interpreted as int */
1.252 - int i = _data->intvalue;
1.253 - int j = other->intvalue;
1.254 + /* self and other interpreted as int */
1.255 + int i = __TOINT(*self);
1.256 + int j = __TOINT(*other);
1.257
1.258 /* Return a boolean result. */
1.259 return i > j ? __builtins___boolean_True : __builtins___boolean_False;
1.260 @@ -241,11 +233,11 @@
1.261
1.262 __attr __fn_native_int_int_eq(__attr __args[])
1.263 {
1.264 - __attr * const _data = &__args[1];
1.265 + __attr * const self = &__args[1];
1.266 __attr * const other = &__args[2];
1.267 - /* _data and other interpreted as int */
1.268 - int i = _data->intvalue;
1.269 - int j = other->intvalue;
1.270 + /* self and other interpreted as int */
1.271 + int i = __TOINT(*self);
1.272 + int j = __TOINT(*other);
1.273
1.274 /* Return a boolean result. */
1.275 return i == j ? __builtins___boolean_True : __builtins___boolean_False;
1.276 @@ -253,11 +245,11 @@
1.277
1.278 __attr __fn_native_int_int_ne(__attr __args[])
1.279 {
1.280 - __attr * const _data = &__args[1];
1.281 + __attr * const self = &__args[1];
1.282 __attr * const other = &__args[2];
1.283 - /* _data and other interpreted as int */
1.284 - int i = _data->intvalue;
1.285 - int j = other->intvalue;
1.286 + /* self and other interpreted as int */
1.287 + int i = __TOINT(*self);
1.288 + int j = __TOINT(*other);
1.289
1.290 /* Return a boolean result. */
1.291 return i != j ? __builtins___boolean_True : __builtins___boolean_False;
1.292 @@ -265,12 +257,12 @@
1.293
1.294 __attr __fn_native_int_int_str(__attr __args[])
1.295 {
1.296 - __attr * const _data = &__args[1];
1.297 - /* _data interpreted as int */
1.298 - int i = _data->intvalue;
1.299 + __attr * const self = &__args[1];
1.300 + /* self interpreted as int */
1.301 + int i = __TOINT(*self);
1.302 /* Employ a buffer big enough to fit the largest integer plus an extra
1.303 character, a minus sign, and the null terminator. */
1.304 - unsigned int n = (int) log10(INT_MAX) + 3;
1.305 + unsigned int n = (int) log10(__MAXINT) + 3;
1.306 char *s = (char *) __ALLOCATE(n, sizeof(char));
1.307
1.308 snprintf(s, n, "%d", i);