paul@850 | 1 | /* Native functions for floating point operations. |
paul@850 | 2 | |
paul@850 | 3 | Copyright (C) 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@850 | 4 | |
paul@850 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@850 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@850 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@850 | 8 | version. |
paul@850 | 9 | |
paul@850 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@850 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@850 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@850 | 13 | details. |
paul@850 | 14 | |
paul@850 | 15 | You should have received a copy of the GNU General Public License along with |
paul@850 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@850 | 17 | */ |
paul@850 | 18 | |
paul@850 | 19 | #include <fenv.h> /* feclearexcept, fetestexcept */ |
paul@850 | 20 | #include <math.h> /* pow */ |
paul@850 | 21 | #include <stdio.h> /* snprintf */ |
paul@850 | 22 | #include <errno.h> /* errno */ |
paul@850 | 23 | #include "native/common.h" |
paul@850 | 24 | #include "types.h" |
paul@850 | 25 | #include "exceptions.h" |
paul@850 | 26 | #include "ops.h" |
paul@850 | 27 | #include "progconsts.h" |
paul@850 | 28 | #include "progops.h" |
paul@850 | 29 | #include "progtypes.h" |
paul@850 | 30 | #include "main.h" |
paul@850 | 31 | |
paul@850 | 32 | /* Conversion from a pair of consecutive attributes to a double-precision |
paul@850 | 33 | floating point number. */ |
paul@850 | 34 | |
paul@850 | 35 | static double __TOFLOAT(__attr attr) |
paul@850 | 36 | { |
paul@850 | 37 | return __get_trailing_data(attr, __builtins___float_float); |
paul@850 | 38 | } |
paul@850 | 39 | |
paul@850 | 40 | /* Numeric formatting using snprintf. |
paul@850 | 41 | NOTE: This might be moved elsewhere and used by other types. */ |
paul@850 | 42 | |
paul@850 | 43 | static __attr format_number(double n, int size) |
paul@850 | 44 | { |
paul@850 | 45 | char *s = (char *) __ALLOCATE(size, sizeof(char)); |
paul@850 | 46 | int digits; |
paul@850 | 47 | |
paul@850 | 48 | /* Allocation should raise a memory error if it fails, so this loop should |
paul@850 | 49 | terminate via the return statement or an allocation failure. */ |
paul@850 | 50 | |
paul@850 | 51 | while (1) |
paul@850 | 52 | { |
paul@850 | 53 | digits = snprintf(s, size, "%f", n); |
paul@850 | 54 | |
paul@850 | 55 | if (digits < size) |
paul@850 | 56 | { |
paul@850 | 57 | s = (char *) __REALLOCATE(s, (digits + 1) * sizeof(char)); |
paul@850 | 58 | return __new_str(s, digits); |
paul@850 | 59 | } |
paul@850 | 60 | |
paul@850 | 61 | size = digits + 1; |
paul@850 | 62 | s = (char *) __REALLOCATE(s, size * sizeof(char)); |
paul@850 | 63 | } |
paul@850 | 64 | |
paul@850 | 65 | return __NULL; |
paul@850 | 66 | } |
paul@850 | 67 | |
paul@850 | 68 | /* Floating point exception handling. */ |
paul@850 | 69 | |
paul@850 | 70 | static void init_env(fenv_t *envp, int excepts) |
paul@850 | 71 | { |
paul@850 | 72 | fegetenv(envp); |
paul@850 | 73 | feclearexcept(excepts); |
paul@850 | 74 | } |
paul@850 | 75 | |
paul@850 | 76 | static int test_env(fenv_t *envp, int excepts) |
paul@850 | 77 | { |
paul@850 | 78 | if (fetestexcept(excepts)) |
paul@850 | 79 | { |
paul@850 | 80 | fesetenv(envp); |
paul@850 | 81 | return 1; |
paul@850 | 82 | } |
paul@850 | 83 | return 0; |
paul@850 | 84 | } |
paul@850 | 85 | |
paul@850 | 86 | static int have_result(fenv_t *envp, int excepts) |
paul@850 | 87 | { |
paul@850 | 88 | return !fetestexcept(excepts); |
paul@850 | 89 | } |
paul@850 | 90 | |
paul@850 | 91 | static __attr make_result(fenv_t *envp, double result) |
paul@850 | 92 | { |
paul@850 | 93 | fesetenv(envp); |
paul@850 | 94 | return __new_float(result); |
paul@850 | 95 | } |
paul@850 | 96 | |
paul@850 | 97 | /* Floating point operations. */ |
paul@850 | 98 | |
paul@850 | 99 | __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other) |
paul@850 | 100 | { |
paul@850 | 101 | /* self and other interpreted as float */ |
paul@850 | 102 | double i = __TOFLOAT(self); |
paul@850 | 103 | double j = __TOFLOAT(other); |
paul@850 | 104 | double result; |
paul@850 | 105 | |
paul@850 | 106 | /* Preserve environment, clear exception state. */ |
paul@850 | 107 | fenv_t env; |
paul@850 | 108 | init_env(&env, FE_OVERFLOW); |
paul@850 | 109 | |
paul@850 | 110 | result = i + j; |
paul@850 | 111 | |
paul@850 | 112 | /* Test for result, restore state, return the new float. */ |
paul@850 | 113 | if (have_result(&env, FE_OVERFLOW)) |
paul@850 | 114 | return make_result(&env, result); |
paul@850 | 115 | |
paul@850 | 116 | /* Restore state, raise exception. */ |
paul@850 | 117 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 118 | __raise_overflow_error(); |
paul@850 | 119 | return __NULL; |
paul@850 | 120 | } |
paul@850 | 121 | |
paul@850 | 122 | __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) |
paul@850 | 123 | { |
paul@850 | 124 | /* self and other interpreted as float */ |
paul@850 | 125 | double i = __TOFLOAT(self); |
paul@850 | 126 | double j = __TOFLOAT(other); |
paul@850 | 127 | double result; |
paul@850 | 128 | |
paul@850 | 129 | /* Preserve environment, clear exception state. */ |
paul@850 | 130 | fenv_t env; |
paul@850 | 131 | init_env(&env, FE_OVERFLOW); |
paul@850 | 132 | |
paul@850 | 133 | result = i - j; |
paul@850 | 134 | |
paul@850 | 135 | /* Test for result, restore state, return the new float. */ |
paul@850 | 136 | if (have_result(&env, FE_OVERFLOW)) |
paul@850 | 137 | return make_result(&env, result); |
paul@850 | 138 | |
paul@850 | 139 | /* Restore state, raise exception. */ |
paul@850 | 140 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 141 | __raise_overflow_error(); |
paul@850 | 142 | return __NULL; |
paul@850 | 143 | } |
paul@850 | 144 | |
paul@850 | 145 | __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) |
paul@850 | 146 | { |
paul@850 | 147 | /* self and other interpreted as float */ |
paul@850 | 148 | double i = __TOFLOAT(self); |
paul@850 | 149 | double j = __TOFLOAT(other); |
paul@850 | 150 | double result; |
paul@850 | 151 | |
paul@850 | 152 | /* Preserve environment, clear exception state. */ |
paul@850 | 153 | fenv_t env; |
paul@850 | 154 | init_env(&env, FE_OVERFLOW | FE_UNDERFLOW); |
paul@850 | 155 | |
paul@850 | 156 | result = i * j; |
paul@850 | 157 | |
paul@850 | 158 | /* Test for result, restore state, return the new float. */ |
paul@850 | 159 | if (have_result(&env, FE_OVERFLOW | FE_UNDERFLOW)) |
paul@850 | 160 | return make_result(&env, result); |
paul@850 | 161 | |
paul@850 | 162 | /* Restore state, raise exception. */ |
paul@850 | 163 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 164 | __raise_overflow_error(); |
paul@850 | 165 | if (test_env(&env, FE_UNDERFLOW)) |
paul@850 | 166 | __raise_underflow_error(); |
paul@850 | 167 | return __NULL; |
paul@850 | 168 | } |
paul@850 | 169 | |
paul@850 | 170 | __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) |
paul@850 | 171 | { |
paul@850 | 172 | /* self and other interpreted as float */ |
paul@850 | 173 | double i = __TOFLOAT(self); |
paul@850 | 174 | double j = __TOFLOAT(other); |
paul@850 | 175 | double result; |
paul@850 | 176 | |
paul@850 | 177 | /* Preserve environment, clear exception state. */ |
paul@850 | 178 | fenv_t env; |
paul@850 | 179 | init_env(&env, FE_OVERFLOW | FE_UNDERFLOW | FE_DIVBYZERO); |
paul@850 | 180 | |
paul@850 | 181 | result = i / j; |
paul@850 | 182 | |
paul@850 | 183 | /* Test for result, restore state, return the new float. */ |
paul@850 | 184 | if (have_result(&env, FE_OVERFLOW | FE_UNDERFLOW | FE_DIVBYZERO)) |
paul@850 | 185 | return make_result(&env, result); |
paul@850 | 186 | |
paul@850 | 187 | /* Restore state, raise exception. */ |
paul@850 | 188 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 189 | __raise_overflow_error(); |
paul@850 | 190 | if (test_env(&env, FE_UNDERFLOW)) |
paul@850 | 191 | __raise_underflow_error(); |
paul@850 | 192 | if (test_env(&env, FE_DIVBYZERO)) |
paul@850 | 193 | __raise_zero_division_error(); |
paul@850 | 194 | return __NULL; |
paul@850 | 195 | } |
paul@850 | 196 | |
paul@850 | 197 | __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other) |
paul@850 | 198 | { |
paul@850 | 199 | /* self and other interpreted as float */ |
paul@850 | 200 | double i = __TOFLOAT(self); |
paul@850 | 201 | double j = __TOFLOAT(other); |
paul@850 | 202 | double result; |
paul@850 | 203 | |
paul@850 | 204 | /* Preserve environment, clear exception state. */ |
paul@850 | 205 | fenv_t env; |
paul@850 | 206 | init_env(&env, FE_OVERFLOW | FE_DIVBYZERO); |
paul@850 | 207 | |
paul@850 | 208 | result = fmod(i, j); |
paul@850 | 209 | |
paul@850 | 210 | /* Test for result, restore state, return the new float. */ |
paul@850 | 211 | if (have_result(&env, FE_OVERFLOW | FE_DIVBYZERO)) |
paul@850 | 212 | return make_result(&env, result); |
paul@850 | 213 | |
paul@850 | 214 | /* Restore state, raise exception. */ |
paul@850 | 215 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 216 | __raise_overflow_error(); |
paul@850 | 217 | if (test_env(&env, FE_DIVBYZERO)) |
paul@850 | 218 | __raise_zero_division_error(); |
paul@850 | 219 | return __NULL; |
paul@850 | 220 | } |
paul@850 | 221 | |
paul@850 | 222 | __attr __fn_native_float_float_neg(__attr __self, __attr self) |
paul@850 | 223 | { |
paul@850 | 224 | /* self interpreted as float */ |
paul@850 | 225 | double i = __TOFLOAT(self); |
paul@850 | 226 | double result; |
paul@850 | 227 | |
paul@850 | 228 | /* Preserve environment, clear exception state. */ |
paul@850 | 229 | fenv_t env; |
paul@850 | 230 | init_env(&env, FE_OVERFLOW); |
paul@850 | 231 | |
paul@850 | 232 | result = -i; |
paul@850 | 233 | |
paul@850 | 234 | /* Test for result, restore state, return the new float. */ |
paul@850 | 235 | if (have_result(&env, FE_OVERFLOW)) |
paul@850 | 236 | return make_result(&env, result); |
paul@850 | 237 | |
paul@850 | 238 | /* Restore state, raise exception. */ |
paul@850 | 239 | if (test_env(&env, FE_OVERFLOW)) |
paul@850 | 240 | __raise_overflow_error(); |
paul@850 | 241 | return __NULL; |
paul@850 | 242 | } |
paul@850 | 243 | |
paul@850 | 244 | __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) |
paul@850 | 245 | { |
paul@850 | 246 | /* self and other interpreted as float */ |
paul@850 | 247 | double i = __TOFLOAT(self); |
paul@850 | 248 | double j = __TOFLOAT(other); |
paul@850 | 249 | double result; |
paul@850 | 250 | |
paul@850 | 251 | errno = 0; |
paul@850 | 252 | result = pow(i, j); |
paul@850 | 253 | |
paul@850 | 254 | /* Test for overflow. */ |
paul@850 | 255 | |
paul@850 | 256 | if (errno == ERANGE) |
paul@850 | 257 | __raise_overflow_error(); |
paul@850 | 258 | |
paul@850 | 259 | /* Return the result. */ |
paul@850 | 260 | return __new_float(result); |
paul@850 | 261 | } |
paul@850 | 262 | |
paul@850 | 263 | __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other) |
paul@850 | 264 | { |
paul@850 | 265 | /* self and other interpreted as float */ |
paul@850 | 266 | double i = __TOFLOAT(self); |
paul@850 | 267 | double j = __TOFLOAT(other); |
paul@850 | 268 | |
paul@850 | 269 | /* Return a boolean result. */ |
paul@850 | 270 | return i <= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 271 | } |
paul@850 | 272 | |
paul@850 | 273 | __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other) |
paul@850 | 274 | { |
paul@850 | 275 | /* self and other interpreted as float */ |
paul@850 | 276 | double i = __TOFLOAT(self); |
paul@850 | 277 | double j = __TOFLOAT(other); |
paul@850 | 278 | |
paul@850 | 279 | /* Return a boolean result. */ |
paul@850 | 280 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 281 | } |
paul@850 | 282 | |
paul@850 | 283 | __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other) |
paul@850 | 284 | { |
paul@850 | 285 | /* self and other interpreted as float */ |
paul@850 | 286 | double i = __TOFLOAT(self); |
paul@850 | 287 | double j = __TOFLOAT(other); |
paul@850 | 288 | |
paul@850 | 289 | /* Return a boolean result. */ |
paul@850 | 290 | return i >= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 291 | } |
paul@850 | 292 | |
paul@850 | 293 | __attr __fn_native_float_float_gt(__attr __self, __attr self, __attr other) |
paul@850 | 294 | { |
paul@850 | 295 | /* self and other interpreted as float */ |
paul@850 | 296 | double i = __TOFLOAT(self); |
paul@850 | 297 | double j = __TOFLOAT(other); |
paul@850 | 298 | |
paul@850 | 299 | /* Return a boolean result. */ |
paul@850 | 300 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 301 | } |
paul@850 | 302 | |
paul@850 | 303 | __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other) |
paul@850 | 304 | { |
paul@850 | 305 | /* self and other interpreted as float */ |
paul@850 | 306 | double i = __TOFLOAT(self); |
paul@850 | 307 | double j = __TOFLOAT(other); |
paul@850 | 308 | |
paul@850 | 309 | /* Return a boolean result. */ |
paul@850 | 310 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 311 | } |
paul@850 | 312 | |
paul@850 | 313 | __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other) |
paul@850 | 314 | { |
paul@850 | 315 | /* self and other interpreted as float */ |
paul@850 | 316 | double i = __TOFLOAT(self); |
paul@850 | 317 | double j = __TOFLOAT(other); |
paul@850 | 318 | |
paul@850 | 319 | /* Return a boolean result. */ |
paul@850 | 320 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 321 | } |
paul@850 | 322 | |
paul@850 | 323 | __attr __fn_native_float_float_str(__attr __self, __attr self) |
paul@850 | 324 | { |
paul@850 | 325 | /* self interpreted as float */ |
paul@850 | 326 | double i = __TOFLOAT(self); |
paul@850 | 327 | |
paul@850 | 328 | /* Return a new string. */ |
paul@850 | 329 | return format_number(i, 64); |
paul@850 | 330 | } |
paul@850 | 331 | |
paul@850 | 332 | __attr __fn_native_float_float_int(__attr __self, __attr self) |
paul@850 | 333 | { |
paul@850 | 334 | /* self interpreted as float */ |
paul@850 | 335 | double i = __TOFLOAT(self); |
paul@850 | 336 | |
paul@850 | 337 | /* NOTE: Test for conversion failure. */ |
paul@850 | 338 | return __new_int((int) i); |
paul@850 | 339 | } |
paul@850 | 340 | |
paul@850 | 341 | /* Module initialisation. */ |
paul@850 | 342 | |
paul@850 | 343 | void __main_native_float() |
paul@850 | 344 | { |
paul@850 | 345 | } |