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 <limits.h> /* INT_MAX, INT_MIN */ |
paul@354 | 20 | #include <math.h> /* ceil, log10, pow */ |
paul@583 | 21 | #include <stdio.h> /* fdopen, snprintf */ |
paul@354 | 22 | #include <errno.h> /* errno */ |
paul@583 | 23 | #include <string.h> /* strlen */ |
paul@354 | 24 | #include "native/common.h" |
paul@354 | 25 | #include "types.h" |
paul@354 | 26 | #include "exceptions.h" |
paul@354 | 27 | #include "ops.h" |
paul@354 | 28 | #include "progconsts.h" |
paul@354 | 29 | #include "progops.h" |
paul@354 | 30 | #include "progtypes.h" |
paul@354 | 31 | #include "main.h" |
paul@354 | 32 | |
paul@354 | 33 | /* Integer operations. */ |
paul@354 | 34 | |
paul@583 | 35 | __attr __fn_native_int_int_new(__attr __args[]) |
paul@583 | 36 | { |
paul@583 | 37 | __attr * const _data = &__args[1]; |
paul@583 | 38 | |
paul@583 | 39 | return __new_int(_data->intvalue); |
paul@583 | 40 | } |
paul@583 | 41 | |
paul@354 | 42 | __attr __fn_native_int_int_add(__attr __args[]) |
paul@354 | 43 | { |
paul@354 | 44 | __attr * const _data = &__args[1]; |
paul@354 | 45 | __attr * const other = &__args[2]; |
paul@354 | 46 | /* _data and other interpreted as int */ |
paul@354 | 47 | int i = _data->intvalue; |
paul@354 | 48 | int j = other->intvalue; |
paul@354 | 49 | |
paul@354 | 50 | /* Test for overflow. */ |
paul@354 | 51 | if (((i > 0) && (j > 0) && (i > INT_MAX - j)) || |
paul@354 | 52 | ((i < 0) && (j < 0) && (i < INT_MIN - j))) |
paul@354 | 53 | |
paul@354 | 54 | __raise_overflow_error(); |
paul@354 | 55 | |
paul@354 | 56 | /* Return the new integer. */ |
paul@354 | 57 | return __new_int(i + j); |
paul@354 | 58 | } |
paul@354 | 59 | |
paul@354 | 60 | __attr __fn_native_int_int_sub(__attr __args[]) |
paul@354 | 61 | { |
paul@354 | 62 | __attr * const _data = &__args[1]; |
paul@354 | 63 | __attr * const other = &__args[2]; |
paul@354 | 64 | /* _data and other interpreted as int */ |
paul@354 | 65 | int i = _data->intvalue; |
paul@354 | 66 | int j = other->intvalue; |
paul@354 | 67 | |
paul@354 | 68 | /* Test for overflow. */ |
paul@354 | 69 | if (((i < 0) && (j > 0) && (i < INT_MIN + j)) || |
paul@354 | 70 | ((i > 0) && (j < 0) && (i > INT_MAX + j))) |
paul@354 | 71 | |
paul@354 | 72 | __raise_overflow_error(); |
paul@354 | 73 | |
paul@354 | 74 | /* Return the new integer. */ |
paul@354 | 75 | return __new_int(i - j); |
paul@354 | 76 | } |
paul@354 | 77 | |
paul@354 | 78 | __attr __fn_native_int_int_mul(__attr __args[]) |
paul@354 | 79 | { |
paul@354 | 80 | __attr * const _data = &__args[1]; |
paul@354 | 81 | __attr * const other = &__args[2]; |
paul@354 | 82 | /* _data and other interpreted as int */ |
paul@354 | 83 | int i = _data->intvalue; |
paul@354 | 84 | int j = other->intvalue; |
paul@354 | 85 | |
paul@354 | 86 | /* Test for overflow. */ |
paul@354 | 87 | if (((i > 0) && (j > 0) && (i > INT_MAX / j)) || |
paul@354 | 88 | ((i < 0) && (j < 0) && (i > INT_MAX / j)) || |
paul@354 | 89 | ((i < 0) && (j > 0) && (i < INT_MIN / j)) || |
paul@354 | 90 | ((i > 0) && (j < 0) && (j < INT_MIN / i))) |
paul@354 | 91 | |
paul@354 | 92 | __raise_overflow_error(); |
paul@354 | 93 | |
paul@354 | 94 | /* Return the new integer. */ |
paul@354 | 95 | return __new_int(i * j); |
paul@354 | 96 | } |
paul@354 | 97 | |
paul@354 | 98 | __attr __fn_native_int_int_div(__attr __args[]) |
paul@354 | 99 | { |
paul@354 | 100 | __attr * const _data = &__args[1]; |
paul@354 | 101 | __attr * const other = &__args[2]; |
paul@354 | 102 | /* _data and other interpreted as int */ |
paul@354 | 103 | int i = _data->intvalue; |
paul@354 | 104 | int j = other->intvalue; |
paul@354 | 105 | |
paul@354 | 106 | /* Test for division by zero or overflow. */ |
paul@354 | 107 | if (j == 0) |
paul@354 | 108 | __raise_zero_division_error(); |
paul@354 | 109 | else if ((j == -1) && (i == INT_MIN)) |
paul@354 | 110 | __raise_overflow_error(); |
paul@354 | 111 | |
paul@354 | 112 | /* Return the new integer. */ |
paul@354 | 113 | return __new_int(i / j); |
paul@354 | 114 | } |
paul@354 | 115 | |
paul@354 | 116 | __attr __fn_native_int_int_mod(__attr __args[]) |
paul@354 | 117 | { |
paul@354 | 118 | __attr * const _data = &__args[1]; |
paul@354 | 119 | __attr * const other = &__args[2]; |
paul@354 | 120 | /* _data and other interpreted as int */ |
paul@354 | 121 | int i = _data->intvalue; |
paul@354 | 122 | int j = other->intvalue; |
paul@354 | 123 | |
paul@354 | 124 | /* Test for division by zero or overflow. */ |
paul@354 | 125 | if (j == 0) |
paul@354 | 126 | __raise_zero_division_error(); |
paul@354 | 127 | else if ((j == -1) && (i == INT_MIN)) |
paul@354 | 128 | __raise_overflow_error(); |
paul@354 | 129 | |
paul@354 | 130 | /* Return the new integer. */ |
paul@354 | 131 | return __new_int(i % j); |
paul@354 | 132 | } |
paul@354 | 133 | |
paul@354 | 134 | __attr __fn_native_int_int_neg(__attr __args[]) |
paul@354 | 135 | { |
paul@354 | 136 | __attr * const _data = &__args[1]; |
paul@354 | 137 | /* _data interpreted as int */ |
paul@354 | 138 | int i = _data->intvalue; |
paul@354 | 139 | |
paul@354 | 140 | /* Test for overflow. */ |
paul@354 | 141 | if (i == INT_MIN) |
paul@354 | 142 | __raise_overflow_error(); |
paul@354 | 143 | |
paul@354 | 144 | /* Return the new integer. */ |
paul@354 | 145 | return __new_int(-i); |
paul@354 | 146 | } |
paul@354 | 147 | |
paul@354 | 148 | __attr __fn_native_int_int_pow(__attr __args[]) |
paul@354 | 149 | { |
paul@354 | 150 | __attr * const _data = &__args[1]; |
paul@354 | 151 | __attr * const other = &__args[2]; |
paul@354 | 152 | /* _data and other interpreted as int */ |
paul@354 | 153 | int i = _data->intvalue; |
paul@354 | 154 | int j = other->intvalue; |
paul@354 | 155 | int k; |
paul@354 | 156 | |
paul@354 | 157 | errno = 0; |
paul@354 | 158 | k = (int) pow(i, j); |
paul@354 | 159 | |
paul@354 | 160 | /* Test for overflow. */ |
paul@354 | 161 | |
paul@354 | 162 | if (errno == ERANGE) |
paul@354 | 163 | __raise_overflow_error(); |
paul@354 | 164 | |
paul@354 | 165 | /* Return the new integer. */ |
paul@354 | 166 | return __new_int(k); |
paul@354 | 167 | } |
paul@354 | 168 | |
paul@354 | 169 | __attr __fn_native_int_int_and(__attr __args[]) |
paul@354 | 170 | { |
paul@354 | 171 | __attr * const _data = &__args[1]; |
paul@354 | 172 | __attr * const other = &__args[2]; |
paul@354 | 173 | /* _data and other interpreted as int */ |
paul@354 | 174 | int i = _data->intvalue; |
paul@354 | 175 | int j = other->intvalue; |
paul@354 | 176 | |
paul@354 | 177 | /* Return the new integer. */ |
paul@354 | 178 | /* NOTE: No overflow test applied. */ |
paul@354 | 179 | return __new_int(i & j); |
paul@354 | 180 | } |
paul@354 | 181 | |
paul@354 | 182 | __attr __fn_native_int_int_not(__attr __args[]) |
paul@354 | 183 | { |
paul@354 | 184 | __attr * const _data = &__args[1]; |
paul@354 | 185 | /* _data interpreted as int */ |
paul@354 | 186 | int i = _data->intvalue; |
paul@354 | 187 | |
paul@354 | 188 | /* Return the new integer. */ |
paul@354 | 189 | return __new_int(~i); |
paul@354 | 190 | } |
paul@354 | 191 | |
paul@354 | 192 | __attr __fn_native_int_int_or(__attr __args[]) |
paul@354 | 193 | { |
paul@354 | 194 | __attr * const _data = &__args[1]; |
paul@354 | 195 | __attr * const other = &__args[2]; |
paul@354 | 196 | /* _data and other interpreted as int */ |
paul@354 | 197 | int i = _data->intvalue; |
paul@354 | 198 | int j = other->intvalue; |
paul@354 | 199 | |
paul@354 | 200 | /* Return the new integer. */ |
paul@354 | 201 | /* NOTE: No overflow test applied. */ |
paul@354 | 202 | return __new_int(i | j); |
paul@354 | 203 | } |
paul@354 | 204 | |
paul@354 | 205 | __attr __fn_native_int_int_xor(__attr __args[]) |
paul@354 | 206 | { |
paul@354 | 207 | __attr * const _data = &__args[1]; |
paul@354 | 208 | __attr * const other = &__args[2]; |
paul@354 | 209 | /* _data and other interpreted as int */ |
paul@354 | 210 | int i = _data->intvalue; |
paul@354 | 211 | int j = other->intvalue; |
paul@354 | 212 | |
paul@354 | 213 | /* Return the new integer. */ |
paul@354 | 214 | /* NOTE: No overflow test applied. */ |
paul@354 | 215 | return __new_int(i ^ j); |
paul@354 | 216 | } |
paul@354 | 217 | |
paul@354 | 218 | __attr __fn_native_int_int_lt(__attr __args[]) |
paul@354 | 219 | { |
paul@354 | 220 | __attr * const _data = &__args[1]; |
paul@354 | 221 | __attr * const other = &__args[2]; |
paul@354 | 222 | /* _data and other interpreted as int */ |
paul@354 | 223 | int i = _data->intvalue; |
paul@354 | 224 | int j = other->intvalue; |
paul@354 | 225 | |
paul@354 | 226 | /* Return a boolean result. */ |
paul@354 | 227 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 228 | } |
paul@354 | 229 | |
paul@354 | 230 | __attr __fn_native_int_int_gt(__attr __args[]) |
paul@354 | 231 | { |
paul@354 | 232 | __attr * const _data = &__args[1]; |
paul@354 | 233 | __attr * const other = &__args[2]; |
paul@354 | 234 | /* _data and other interpreted as int */ |
paul@354 | 235 | int i = _data->intvalue; |
paul@354 | 236 | int j = other->intvalue; |
paul@354 | 237 | |
paul@354 | 238 | /* Return a boolean result. */ |
paul@354 | 239 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 240 | } |
paul@354 | 241 | |
paul@354 | 242 | __attr __fn_native_int_int_eq(__attr __args[]) |
paul@354 | 243 | { |
paul@354 | 244 | __attr * const _data = &__args[1]; |
paul@354 | 245 | __attr * const other = &__args[2]; |
paul@354 | 246 | /* _data and other interpreted as int */ |
paul@354 | 247 | int i = _data->intvalue; |
paul@354 | 248 | int j = other->intvalue; |
paul@354 | 249 | |
paul@354 | 250 | /* Return a boolean result. */ |
paul@354 | 251 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 252 | } |
paul@354 | 253 | |
paul@354 | 254 | __attr __fn_native_int_int_ne(__attr __args[]) |
paul@354 | 255 | { |
paul@354 | 256 | __attr * const _data = &__args[1]; |
paul@354 | 257 | __attr * const other = &__args[2]; |
paul@354 | 258 | /* _data and other interpreted as int */ |
paul@354 | 259 | int i = _data->intvalue; |
paul@354 | 260 | int j = other->intvalue; |
paul@354 | 261 | |
paul@354 | 262 | /* Return a boolean result. */ |
paul@354 | 263 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@354 | 264 | } |
paul@354 | 265 | |
paul@354 | 266 | __attr __fn_native_int_int_str(__attr __args[]) |
paul@354 | 267 | { |
paul@354 | 268 | __attr * const _data = &__args[1]; |
paul@354 | 269 | /* _data interpreted as int */ |
paul@354 | 270 | int i = _data->intvalue; |
paul@354 | 271 | /* Employ a buffer big enough to fit the largest integer plus an extra |
paul@354 | 272 | character, a minus sign, and the null terminator. */ |
paul@354 | 273 | unsigned int n = (int) log10(INT_MAX) + 3; |
paul@354 | 274 | char *s = (char *) __ALLOCATE(n, sizeof(char)); |
paul@354 | 275 | |
paul@354 | 276 | snprintf(s, n, "%d", i); |
paul@354 | 277 | |
paul@354 | 278 | /* Return a new string. */ |
paul@583 | 279 | return __new_str(s, strlen(s)); |
paul@354 | 280 | } |
paul@354 | 281 | |
paul@354 | 282 | /* Module initialisation. */ |
paul@354 | 283 | |
paul@354 | 284 | void __main_native_int() |
paul@354 | 285 | { |
paul@354 | 286 | } |