1.1 --- a/templates/native/int.c Sun Mar 19 00:56:43 2017 +0100
1.2 +++ b/templates/native/int.c Fri Mar 24 22:39:37 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,20 +31,15 @@
1.12
1.13 /* Integer operations. */
1.14
1.15 -__attr __fn_native_int_int_new(__attr __self, __attr _data)
1.16 +__attr __fn_native_int_int_add(__attr __self, __attr self, __attr other)
1.17 {
1.18 - return __new_int(_data.intvalue);
1.19 -}
1.20 -
1.21 -__attr __fn_native_int_int_add(__attr __self, __attr _data, __attr other)
1.22 -{
1.23 - /* _data and other interpreted as int.__data__ */
1.24 - int i = _data.intvalue;
1.25 - int j = other.intvalue;
1.26 + /* self and other interpreted as int */
1.27 + int i = __TOINT(self);
1.28 + int j = __TOINT(other);
1.29
1.30 /* Test for overflow. */
1.31 - if (((i > 0) && (j > 0) && (i > INT_MAX - j)) ||
1.32 - ((i < 0) && (j < 0) && (i < INT_MIN - j)))
1.33 + if (((i > 0) && (j > 0) && (i > __MAXINT - j)) ||
1.34 + ((i < 0) && (j < 0) && (i < __MININT - j)))
1.35
1.36 __raise_overflow_error();
1.37
1.38 @@ -53,15 +47,15 @@
1.39 return __new_int(i + j);
1.40 }
1.41
1.42 -__attr __fn_native_int_int_sub(__attr __self, __attr _data, __attr other)
1.43 +__attr __fn_native_int_int_sub(__attr __self, __attr self, __attr other)
1.44 {
1.45 - /* _data and other interpreted as int.__data__ */
1.46 - int i = _data.intvalue;
1.47 - int j = other.intvalue;
1.48 + /* self and other interpreted as int */
1.49 + int i = __TOINT(self);
1.50 + int j = __TOINT(other);
1.51
1.52 /* Test for overflow. */
1.53 - if (((i < 0) && (j > 0) && (i < INT_MIN + j)) ||
1.54 - ((i > 0) && (j < 0) && (i > INT_MAX + j)))
1.55 + if (((i < 0) && (j > 0) && (i < __MININT + j)) ||
1.56 + ((i > 0) && (j < 0) && (i > __MAXINT + j)))
1.57
1.58 __raise_overflow_error();
1.59
1.60 @@ -69,17 +63,17 @@
1.61 return __new_int(i - j);
1.62 }
1.63
1.64 -__attr __fn_native_int_int_mul(__attr __self, __attr _data, __attr other)
1.65 +__attr __fn_native_int_int_mul(__attr __self, __attr self, __attr other)
1.66 {
1.67 - /* _data and other interpreted as int.__data__ */
1.68 - int i = _data.intvalue;
1.69 - int j = other.intvalue;
1.70 + /* self and other interpreted as int */
1.71 + int i = __TOINT(self);
1.72 + int j = __TOINT(other);
1.73
1.74 /* Test for overflow. */
1.75 - if (((i > 0) && (j > 0) && (i > INT_MAX / j)) ||
1.76 - ((i < 0) && (j < 0) && (i > INT_MAX / j)) ||
1.77 - ((i < 0) && (j > 0) && (i < INT_MIN / j)) ||
1.78 - ((i > 0) && (j < 0) && (j < INT_MIN / i)))
1.79 + if (((i > 0) && (j > 0) && (i > __MAXINT / j)) ||
1.80 + ((i < 0) && (j < 0) && (i > __MAXINT / j)) ||
1.81 + ((i < 0) && (j > 0) && (i < __MININT / j)) ||
1.82 + ((i > 0) && (j < 0) && (j < __MININT / i)))
1.83
1.84 __raise_overflow_error();
1.85
1.86 @@ -87,56 +81,56 @@
1.87 return __new_int(i * j);
1.88 }
1.89
1.90 -__attr __fn_native_int_int_div(__attr __self, __attr _data, __attr other)
1.91 +__attr __fn_native_int_int_div(__attr __self, __attr self, __attr other)
1.92 {
1.93 - /* _data and other interpreted as int.__data__ */
1.94 - int i = _data.intvalue;
1.95 - int j = other.intvalue;
1.96 + /* self and other interpreted as int */
1.97 + int i = __TOINT(self);
1.98 + int j = __TOINT(other);
1.99
1.100 /* Test for division by zero or overflow. */
1.101 if (j == 0)
1.102 __raise_zero_division_error();
1.103 - else if ((j == -1) && (i == INT_MIN))
1.104 + else if ((j == -1) && (i == __MININT))
1.105 __raise_overflow_error();
1.106
1.107 /* Return the new integer. */
1.108 return __new_int(i / j);
1.109 }
1.110
1.111 -__attr __fn_native_int_int_mod(__attr __self, __attr _data, __attr other)
1.112 +__attr __fn_native_int_int_mod(__attr __self, __attr self, __attr other)
1.113 {
1.114 - /* _data and other interpreted as int.__data_ */
1.115 - int i = _data.intvalue;
1.116 - int j = other.intvalue;
1.117 + /* self and other interpreted as int */
1.118 + int i = __TOINT(self);
1.119 + int j = __TOINT(other);
1.120
1.121 /* Test for division by zero or overflow. */
1.122 if (j == 0)
1.123 __raise_zero_division_error();
1.124 - else if ((j == -1) && (i == INT_MIN))
1.125 + else if ((j == -1) && (i == __MININT))
1.126 __raise_overflow_error();
1.127
1.128 /* Return the new integer. */
1.129 return __new_int(i % j);
1.130 }
1.131
1.132 -__attr __fn_native_int_int_neg(__attr __self, __attr _data)
1.133 +__attr __fn_native_int_int_neg(__attr __self, __attr self)
1.134 {
1.135 - /* _data interpreted as int.__data_ */
1.136 - int i = _data.intvalue;
1.137 + /* self interpreted as int */
1.138 + int i = __TOINT(self);
1.139
1.140 /* Test for overflow. */
1.141 - if (i == INT_MIN)
1.142 + if (i == __MININT)
1.143 __raise_overflow_error();
1.144
1.145 /* Return the new integer. */
1.146 return __new_int(-i);
1.147 }
1.148
1.149 -__attr __fn_native_int_int_pow(__attr __self, __attr _data, __attr other)
1.150 +__attr __fn_native_int_int_pow(__attr __self, __attr self, __attr other)
1.151 {
1.152 - /* _data and other interpreted as int.__data_ */
1.153 - int i = _data.intvalue;
1.154 - int j = other.intvalue;
1.155 + /* self and other interpreted as int */
1.156 + int i = __TOINT(self);
1.157 + int j = __TOINT(other);
1.158 int k;
1.159
1.160 errno = 0;
1.161 @@ -151,95 +145,96 @@
1.162 return __new_int(k);
1.163 }
1.164
1.165 -__attr __fn_native_int_int_and(__attr __self, __attr _data, __attr other)
1.166 +__attr __fn_native_int_int_and(__attr __self, __attr self, __attr other)
1.167 {
1.168 - /* _data and other interpreted as int.__data_ */
1.169 - int i = _data.intvalue;
1.170 - int j = other.intvalue;
1.171 + /* self and other interpreted as int */
1.172 + int i = __TOINT(self);
1.173 + int j = __TOINT(other);
1.174
1.175 /* Return the new integer. */
1.176 /* NOTE: No overflow test applied. */
1.177 return __new_int(i & j);
1.178 }
1.179
1.180 -__attr __fn_native_int_int_not(__attr __self, __attr _data)
1.181 +__attr __fn_native_int_int_not(__attr __self, __attr self)
1.182 {
1.183 - /* _data interpreted as int.__data_ */
1.184 - int i = _data.intvalue;
1.185 + /* self interpreted as int */
1.186 + int i = __TOINT(self);
1.187
1.188 /* Return the new integer. */
1.189 return __new_int(~i);
1.190 }
1.191
1.192 -__attr __fn_native_int_int_or(__attr __self, __attr _data, __attr other)
1.193 +__attr __fn_native_int_int_or(__attr __self, __attr self, __attr other)
1.194 {
1.195 - /* _data and other interpreted as int.__data_ */
1.196 - int i = _data.intvalue;
1.197 - int j = other.intvalue;
1.198 + /* self and other interpreted as int */
1.199 + int i = __TOINT(self);
1.200 + int j = __TOINT(other);
1.201
1.202 /* Return the new integer. */
1.203 /* NOTE: No overflow test applied. */
1.204 return __new_int(i | j);
1.205 }
1.206
1.207 -__attr __fn_native_int_int_xor(__attr __self, __attr _data, __attr other)
1.208 +__attr __fn_native_int_int_xor(__attr __self, __attr self, __attr other)
1.209 {
1.210 - /* _data and other interpreted as int.__data_ */
1.211 - int i = _data.intvalue;
1.212 - int j = other.intvalue;
1.213 + /* self and other interpreted as int */
1.214 + int i = __TOINT(self);
1.215 + int j = __TOINT(other);
1.216
1.217 /* Return the new integer. */
1.218 /* NOTE: No overflow test applied. */
1.219 return __new_int(i ^ j);
1.220 }
1.221
1.222 -__attr __fn_native_int_int_lt(__attr __self, __attr _data, __attr other)
1.223 +__attr __fn_native_int_int_lt(__attr __self, __attr self, __attr other)
1.224 {
1.225 - /* _data and other interpreted as int.__data_ */
1.226 - int i = _data.intvalue;
1.227 - int j = other.intvalue;
1.228 + /* self and other interpreted as int */
1.229 + int i = __TOINT(self);
1.230 + int j = __TOINT(other);
1.231
1.232 /* Return a boolean result. */
1.233 return i < j ? __builtins___boolean_True : __builtins___boolean_False;
1.234 }
1.235
1.236 -__attr __fn_native_int_int_gt(__attr __self, __attr _data, __attr other)
1.237 +__attr __fn_native_int_int_gt(__attr __self, __attr self, __attr other)
1.238 {
1.239 - /* _data and other interpreted as int.__data_ */
1.240 - int i = _data.intvalue;
1.241 - int j = other.intvalue;
1.242 + /* self and other interpreted as int */
1.243 + int i = __TOINT(self);
1.244 + int j = __TOINT(other);
1.245
1.246 /* Return a boolean result. */
1.247 return i > j ? __builtins___boolean_True : __builtins___boolean_False;
1.248 }
1.249
1.250 -__attr __fn_native_int_int_eq(__attr __self, __attr _data, __attr other)
1.251 +__attr __fn_native_int_int_eq(__attr __self, __attr self, __attr other)
1.252 {
1.253 - /* _data and other interpreted as int.__data_ */
1.254 - int i = _data.intvalue;
1.255 - int j = other.intvalue;
1.256 + /* self and other interpreted as int */
1.257 + int i = __TOINT(self);
1.258 + int j = __TOINT(other);
1.259
1.260 /* Return a boolean result. */
1.261 return i == j ? __builtins___boolean_True : __builtins___boolean_False;
1.262 }
1.263
1.264 -__attr __fn_native_int_int_ne(__attr __self, __attr _data, __attr other)
1.265 +__attr __fn_native_int_int_ne(__attr __self, __attr self, __attr other)
1.266 {
1.267 - /* _data and other interpreted as int.__data_ */
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 }
1.277
1.278 -__attr __fn_native_int_int_str(__attr __self, __attr _data)
1.279 +__attr __fn_native_int_int_str(__attr __self, __attr self)
1.280 {
1.281 - /* _data interpreted as int.__data_ */
1.282 - int i = _data.intvalue;
1.283 + /* self interpreted as int */
1.284 + int i = __TOINT(self);
1.285 +
1.286 /* Employ a buffer big enough to fit the largest integer plus an extra
1.287 character, a minus sign, and the null terminator. */
1.288 - unsigned int n = (int) log10(INT_MAX) + 3;
1.289 + unsigned int n = (int) log10(__MAXINT) + 3;
1.290 char *s = (char *) __ALLOCATE(n, sizeof(char));
1.291
1.292 snprintf(s, n, "%d", i);