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