paul@354 | 1 | /* Native functions for integer operations. |
paul@354 | 2 | |
paul@569 | 3 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@354 | 4 | |
paul@354 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@354 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@354 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@354 | 8 | version. |
paul@354 | 9 | |
paul@354 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@354 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@354 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@354 | 13 | details. |
paul@354 | 14 | |
paul@354 | 15 | You should have received a copy of the GNU General Public License along with |
paul@354 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@354 | 17 | */ |
paul@354 | 18 | |
paul@354 | 19 | #include <math.h> /* ceil, log10, pow */ |
paul@583 | 20 | #include <stdio.h> /* fdopen, snprintf */ |
paul@354 | 21 | #include <errno.h> /* errno */ |
paul@583 | 22 | #include <string.h> /* strlen */ |
paul@354 | 23 | #include "native/common.h" |
paul@354 | 24 | #include "types.h" |
paul@354 | 25 | #include "exceptions.h" |
paul@354 | 26 | #include "ops.h" |
paul@354 | 27 | #include "progconsts.h" |
paul@354 | 28 | #include "progops.h" |
paul@354 | 29 | #include "progtypes.h" |
paul@354 | 30 | #include "main.h" |
paul@354 | 31 | |
paul@354 | 32 | /* Integer operations. */ |
paul@354 | 33 | |
paul@762 | 34 | __attr __fn_native_int_is_int(__attr __args[]) |
paul@762 | 35 | { |
paul@762 | 36 | __attr * const obj = &__args[1]; |
paul@762 | 37 | |
paul@762 | 38 | return __INTEGER(*obj) ? __builtins___boolean_True : __builtins___boolean_False; |
paul@762 | 39 | } |
paul@762 | 40 | |
paul@354 | 41 | __attr __fn_native_int_int_add(__attr __args[]) |
paul@354 | 42 | { |
paul@758 | 43 | __attr * const self = &__args[1]; |
paul@354 | 44 | __attr * const other = &__args[2]; |
paul@758 | 45 | /* self and other interpreted as int */ |
paul@758 | 46 | int i = __TOINT(*self); |
paul@758 | 47 | int j = __TOINT(*other); |
paul@354 | 48 | |
paul@354 | 49 | /* Test for overflow. */ |
paul@758 | 50 | if (((i > 0) && (j > 0) && (i > __MAXINT - j)) || |
paul@758 | 51 | ((i < 0) && (j < 0) && (i < __MININT - j))) |
paul@354 | 52 | |
paul@354 | 53 | __raise_overflow_error(); |
paul@354 | 54 | |
paul@354 | 55 | /* Return the new integer. */ |
paul@354 | 56 | return __new_int(i + j); |
paul@354 | 57 | } |
paul@354 | 58 | |
paul@354 | 59 | __attr __fn_native_int_int_sub(__attr __args[]) |
paul@354 | 60 | { |
paul@758 | 61 | __attr * const self = &__args[1]; |
paul@354 | 62 | __attr * const other = &__args[2]; |
paul@758 | 63 | /* self and other interpreted as int */ |
paul@758 | 64 | int i = __TOINT(*self); |
paul@758 | 65 | int j = __TOINT(*other); |
paul@354 | 66 | |
paul@354 | 67 | /* Test for overflow. */ |
paul@758 | 68 | if (((i < 0) && (j > 0) && (i < __MININT + j)) || |
paul@758 | 69 | ((i > 0) && (j < 0) && (i > __MAXINT + j))) |
paul@354 | 70 | |
paul@354 | 71 | __raise_overflow_error(); |
paul@354 | 72 | |
paul@354 | 73 | /* Return the new integer. */ |
paul@354 | 74 | return __new_int(i - j); |
paul@354 | 75 | } |
paul@354 | 76 | |
paul@354 | 77 | __attr __fn_native_int_int_mul(__attr __args[]) |
paul@354 | 78 | { |
paul@758 | 79 | __attr * const self = &__args[1]; |
paul@354 | 80 | __attr * const other = &__args[2]; |
paul@758 | 81 | /* self and other interpreted as int */ |
paul@758 | 82 | int i = __TOINT(*self); |
paul@758 | 83 | int j = __TOINT(*other); |
paul@354 | 84 | |
paul@354 | 85 | /* Test for overflow. */ |
paul@758 | 86 | if (((i > 0) && (j > 0) && (i > __MAXINT / j)) || |
paul@758 | 87 | ((i < 0) && (j < 0) && (i > __MAXINT / j)) || |
paul@758 | 88 | ((i < 0) && (j > 0) && (i < __MININT / j)) || |
paul@758 | 89 | ((i > 0) && (j < 0) && (j < __MININT / i))) |
paul@354 | 90 | |
paul@354 | 91 | __raise_overflow_error(); |
paul@354 | 92 | |
paul@354 | 93 | /* Return the new integer. */ |
paul@354 | 94 | return __new_int(i * j); |
paul@354 | 95 | } |
paul@354 | 96 | |
paul@354 | 97 | __attr __fn_native_int_int_div(__attr __args[]) |
paul@354 | 98 | { |
paul@758 | 99 | __attr * const self = &__args[1]; |
paul@354 | 100 | __attr * const other = &__args[2]; |
paul@758 | 101 | /* self and other interpreted as int */ |
paul@758 | 102 | int i = __TOINT(*self); |
paul@758 | 103 | int j = __TOINT(*other); |
paul@354 | 104 | |
paul@354 | 105 | /* Test for division by zero or overflow. */ |
paul@354 | 106 | if (j == 0) |
paul@354 | 107 | __raise_zero_division_error(); |
paul@758 | 108 | else if ((j == -1) && (i == __MININT)) |
paul@354 | 109 | __raise_overflow_error(); |
paul@354 | 110 | |
paul@354 | 111 | /* Return the new integer. */ |
paul@354 | 112 | return __new_int(i / j); |
paul@354 | 113 | } |
paul@354 | 114 | |
paul@354 | 115 | __attr __fn_native_int_int_mod(__attr __args[]) |
paul@354 | 116 | { |
paul@758 | 117 | __attr * const self = &__args[1]; |
paul@354 | 118 | __attr * const other = &__args[2]; |
paul@758 | 119 | /* self and other interpreted as int */ |
paul@758 | 120 | int i = __TOINT(*self); |
paul@758 | 121 | int j = __TOINT(*other); |
paul@354 | 122 | |
paul@354 | 123 | /* Test for division by zero or overflow. */ |
paul@354 | 124 | if (j == 0) |
paul@354 | 125 | __raise_zero_division_error(); |
paul@758 | 126 | else if ((j == -1) && (i == __MININT)) |
paul@354 | 127 | __raise_overflow_error(); |
paul@354 | 128 | |
paul@354 | 129 | /* Return the new integer. */ |
paul@354 | 130 | return __new_int(i % j); |
paul@354 | 131 | } |
paul@354 | 132 | |
paul@354 | 133 | __attr __fn_native_int_int_neg(__attr __args[]) |
paul@354 | 134 | { |
paul@758 | 135 | __attr * const self = &__args[1]; |
paul@758 | 136 | /* self interpreted as int */ |
paul@758 | 137 | int i = __TOINT(*self); |
paul@354 | 138 | |
paul@354 | 139 | /* Test for overflow. */ |
paul@758 | 140 | if (i == __MININT) |
paul@354 | 141 | __raise_overflow_error(); |
paul@354 | 142 | |
paul@354 | 143 | /* Return the new integer. */ |
paul@354 | 144 | return __new_int(-i); |
paul@354 | 145 | } |
paul@354 | 146 | |
paul@354 | 147 | __attr __fn_native_int_int_pow(__attr __args[]) |
paul@354 | 148 | { |
paul@758 | 149 | __attr * const self = &__args[1]; |
paul@354 | 150 | __attr * const other = &__args[2]; |
paul@758 | 151 | /* self and other interpreted as int */ |
paul@758 | 152 | int i = __TOINT(*self); |
paul@758 | 153 | int j = __TOINT(*other); |
paul@354 | 154 | int k; |
paul@354 | 155 | |
paul@354 | 156 | errno = 0; |
paul@354 | 157 | k = (int) pow(i, j); |
paul@354 | 158 | |
paul@354 | 159 | /* Test for overflow. */ |
paul@354 | 160 | |
paul@354 | 161 | if (errno == ERANGE) |
paul@354 | 162 | __raise_overflow_error(); |
paul@354 | 163 | |
paul@354 | 164 | /* Return the new integer. */ |
paul@354 | 165 | return __new_int(k); |
paul@354 | 166 | } |
paul@354 | 167 | |
paul@354 | 168 | __attr __fn_native_int_int_and(__attr __args[]) |
paul@354 | 169 | { |
paul@758 | 170 | __attr * const self = &__args[1]; |
paul@354 | 171 | __attr * const other = &__args[2]; |
paul@758 | 172 | /* self and other interpreted as int */ |
paul@758 | 173 | int i = __TOINT(*self); |
paul@758 | 174 | int j = __TOINT(*other); |
paul@354 | 175 | |
paul@354 | 176 | /* Return the new integer. */ |
paul@354 | 177 | /* NOTE: No overflow test applied. */ |
paul@354 | 178 | return __new_int(i & j); |
paul@354 | 179 | } |
paul@354 | 180 | |
paul@354 | 181 | __attr __fn_native_int_int_not(__attr __args[]) |
paul@354 | 182 | { |
paul@758 | 183 | __attr * const self = &__args[1]; |
paul@758 | 184 | /* self interpreted as int */ |
paul@758 | 185 | int i = __TOINT(*self); |
paul@354 | 186 | |
paul@354 | 187 | /* Return the new integer. */ |
paul@354 | 188 | return __new_int(~i); |
paul@354 | 189 | } |
paul@354 | 190 | |
paul@354 | 191 | __attr __fn_native_int_int_or(__attr __args[]) |
paul@354 | 192 | { |
paul@758 | 193 | __attr * const self = &__args[1]; |
paul@354 | 194 | __attr * const other = &__args[2]; |
paul@758 | 195 | /* self and other interpreted as int */ |
paul@758 | 196 | int i = __TOINT(*self); |
paul@758 | 197 | int j = __TOINT(*other); |
paul@354 | 198 | |
paul@354 | 199 | /* Return the new integer. */ |
paul@354 | 200 | /* NOTE: No overflow test applied. */ |
paul@354 | 201 | return __new_int(i | j); |
paul@354 | 202 | } |
paul@354 | 203 | |
paul@354 | 204 | __attr __fn_native_int_int_xor(__attr __args[]) |
paul@354 | 205 | { |
paul@758 | 206 | __attr * const self = &__args[1]; |
paul@354 | 207 | __attr * const other = &__args[2]; |
paul@758 | 208 | /* self and other interpreted as int */ |
paul@758 | 209 | int i = __TOINT(*self); |
paul@758 | 210 | int j = __TOINT(*other); |
paul@354 | 211 | |
paul@354 | 212 | /* Return the new integer. */ |
paul@354 | 213 | /* NOTE: No overflow test applied. */ |
paul@354 | 214 | return __new_int(i ^ j); |
paul@354 | 215 | } |
paul@354 | 216 | |
paul@762 | 217 | __attr __fn_native_int_int_le(__attr __args[]) |
paul@762 | 218 | { |
paul@762 | 219 | __attr * const self = &__args[1]; |
paul@762 | 220 | __attr * const other = &__args[2]; |
paul@762 | 221 | /* self and other interpreted as int */ |
paul@762 | 222 | int i = __TOINT(*self); |
paul@762 | 223 | int j = __TOINT(*other); |
paul@762 | 224 | |
paul@762 | 225 | /* Return a boolean result. */ |
paul@762 | 226 | return i <= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@762 | 227 | } |
paul@762 | 228 | |
paul@354 | 229 | __attr __fn_native_int_int_lt(__attr __args[]) |
paul@354 | 230 | { |
paul@758 | 231 | __attr * const self = &__args[1]; |
paul@354 | 232 | __attr * const other = &__args[2]; |
paul@758 | 233 | /* self and other interpreted as int */ |
paul@758 | 234 | int i = __TOINT(*self); |
paul@758 | 235 | int j = __TOINT(*other); |
paul@354 | 236 | |
paul@354 | 237 | /* Return a boolean result. */ |
paul@354 | 238 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 239 | } |
paul@354 | 240 | |
paul@762 | 241 | __attr __fn_native_int_int_ge(__attr __args[]) |
paul@762 | 242 | { |
paul@762 | 243 | __attr * const self = &__args[1]; |
paul@762 | 244 | __attr * const other = &__args[2]; |
paul@762 | 245 | /* self and other interpreted as int */ |
paul@762 | 246 | int i = __TOINT(*self); |
paul@762 | 247 | int j = __TOINT(*other); |
paul@762 | 248 | |
paul@762 | 249 | /* Return a boolean result. */ |
paul@762 | 250 | return i >= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@762 | 251 | } |
paul@762 | 252 | |
paul@354 | 253 | __attr __fn_native_int_int_gt(__attr __args[]) |
paul@354 | 254 | { |
paul@758 | 255 | __attr * const self = &__args[1]; |
paul@354 | 256 | __attr * const other = &__args[2]; |
paul@758 | 257 | /* self and other interpreted as int */ |
paul@758 | 258 | int i = __TOINT(*self); |
paul@758 | 259 | int j = __TOINT(*other); |
paul@354 | 260 | |
paul@354 | 261 | /* Return a boolean result. */ |
paul@354 | 262 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 263 | } |
paul@354 | 264 | |
paul@354 | 265 | __attr __fn_native_int_int_eq(__attr __args[]) |
paul@354 | 266 | { |
paul@758 | 267 | __attr * const self = &__args[1]; |
paul@354 | 268 | __attr * const other = &__args[2]; |
paul@758 | 269 | /* self and other interpreted as int */ |
paul@758 | 270 | int i = __TOINT(*self); |
paul@758 | 271 | int j = __TOINT(*other); |
paul@354 | 272 | |
paul@354 | 273 | /* Return a boolean result. */ |
paul@354 | 274 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 275 | } |
paul@354 | 276 | |
paul@354 | 277 | __attr __fn_native_int_int_ne(__attr __args[]) |
paul@354 | 278 | { |
paul@758 | 279 | __attr * const self = &__args[1]; |
paul@354 | 280 | __attr * const other = &__args[2]; |
paul@758 | 281 | /* self and other interpreted as int */ |
paul@758 | 282 | int i = __TOINT(*self); |
paul@758 | 283 | int j = __TOINT(*other); |
paul@354 | 284 | |
paul@354 | 285 | /* Return a boolean result. */ |
paul@354 | 286 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 287 | } |
paul@354 | 288 | |
paul@354 | 289 | __attr __fn_native_int_int_str(__attr __args[]) |
paul@354 | 290 | { |
paul@758 | 291 | __attr * const self = &__args[1]; |
paul@758 | 292 | /* self interpreted as int */ |
paul@758 | 293 | int i = __TOINT(*self); |
paul@354 | 294 | /* Employ a buffer big enough to fit the largest integer plus an extra |
paul@354 | 295 | character, a minus sign, and the null terminator. */ |
paul@758 | 296 | unsigned int n = (int) log10(__MAXINT) + 3; |
paul@354 | 297 | char *s = (char *) __ALLOCATE(n, sizeof(char)); |
paul@354 | 298 | |
paul@354 | 299 | snprintf(s, n, "%d", i); |
paul@354 | 300 | |
paul@354 | 301 | /* Return a new string. */ |
paul@583 | 302 | return __new_str(s, strlen(s)); |
paul@354 | 303 | } |
paul@354 | 304 | |
paul@354 | 305 | /* Module initialisation. */ |
paul@354 | 306 | |
paul@354 | 307 | void __main_native_int() |
paul@354 | 308 | { |
paul@354 | 309 | } |