paul@850 | 1 | /* Native functions for floating point operations. |
paul@850 | 2 | |
paul@871 | 3 | Copyright (C) 2016, 2017, 2018, 2019 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@872 | 19 | #include <setjmp.h> |
paul@872 | 20 | #include <signal.h> |
paul@850 | 21 | #include <math.h> /* pow */ |
paul@850 | 22 | #include <stdio.h> /* snprintf */ |
paul@850 | 23 | #include <errno.h> /* errno */ |
paul@850 | 24 | #include "native/common.h" |
paul@850 | 25 | #include "types.h" |
paul@850 | 26 | #include "exceptions.h" |
paul@850 | 27 | #include "ops.h" |
paul@850 | 28 | #include "progconsts.h" |
paul@850 | 29 | #include "progops.h" |
paul@850 | 30 | #include "progtypes.h" |
paul@850 | 31 | #include "main.h" |
paul@850 | 32 | |
paul@871 | 33 | /* Conversion of trailing data to a double-precision 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@872 | 70 | extern jmp_buf __fpe_env; |
paul@850 | 71 | |
paul@850 | 72 | /* Floating point operations. */ |
paul@850 | 73 | |
paul@850 | 74 | __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other) |
paul@850 | 75 | { |
paul@850 | 76 | /* self and other interpreted as float */ |
paul@850 | 77 | double i = __TOFLOAT(self); |
paul@850 | 78 | double j = __TOFLOAT(other); |
paul@872 | 79 | int signum; |
paul@850 | 80 | |
paul@872 | 81 | /* Perform the operation while handling exceptions. */ |
paul@872 | 82 | signum = setjmp(__fpe_env); |
paul@872 | 83 | if (!signum) |
paul@872 | 84 | return __new_float(i + j); |
paul@850 | 85 | |
paul@872 | 86 | /* Exception occurred. */ |
paul@872 | 87 | if (signum == FPE_FLTOVF) |
paul@850 | 88 | __raise_overflow_error(); |
paul@872 | 89 | |
paul@850 | 90 | return __NULL; |
paul@850 | 91 | } |
paul@850 | 92 | |
paul@850 | 93 | __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) |
paul@850 | 94 | { |
paul@850 | 95 | /* self and other interpreted as float */ |
paul@850 | 96 | double i = __TOFLOAT(self); |
paul@850 | 97 | double j = __TOFLOAT(other); |
paul@872 | 98 | int signum; |
paul@850 | 99 | |
paul@872 | 100 | /* Perform the operation while handling exceptions. */ |
paul@872 | 101 | signum = setjmp(__fpe_env); |
paul@872 | 102 | if (!signum) |
paul@872 | 103 | return __new_float(i - j); |
paul@850 | 104 | |
paul@872 | 105 | /* Exception occurred. */ |
paul@872 | 106 | if (signum == FPE_FLTOVF) |
paul@850 | 107 | __raise_overflow_error(); |
paul@872 | 108 | |
paul@850 | 109 | return __NULL; |
paul@850 | 110 | } |
paul@850 | 111 | |
paul@850 | 112 | __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) |
paul@850 | 113 | { |
paul@850 | 114 | /* self and other interpreted as float */ |
paul@850 | 115 | double i = __TOFLOAT(self); |
paul@850 | 116 | double j = __TOFLOAT(other); |
paul@872 | 117 | int signum; |
paul@850 | 118 | |
paul@872 | 119 | /* Perform the operation while handling exceptions. */ |
paul@872 | 120 | signum = setjmp(__fpe_env); |
paul@872 | 121 | if (!signum) |
paul@872 | 122 | return __new_float(i * j); |
paul@850 | 123 | |
paul@872 | 124 | /* Exception occurred. */ |
paul@872 | 125 | if (signum == FPE_FLTOVF) |
paul@872 | 126 | __raise_overflow_error(); |
paul@872 | 127 | else if (signum == FPE_FLTUND) |
paul@872 | 128 | __raise_underflow_error(); |
paul@850 | 129 | |
paul@850 | 130 | return __NULL; |
paul@850 | 131 | } |
paul@850 | 132 | |
paul@850 | 133 | __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) |
paul@850 | 134 | { |
paul@850 | 135 | /* self and other interpreted as float */ |
paul@850 | 136 | double i = __TOFLOAT(self); |
paul@850 | 137 | double j = __TOFLOAT(other); |
paul@872 | 138 | int signum; |
paul@850 | 139 | |
paul@872 | 140 | /* Perform the operation while handling exceptions. */ |
paul@872 | 141 | signum = setjmp(__fpe_env); |
paul@872 | 142 | if (!signum) |
paul@872 | 143 | return __new_float(i / j); |
paul@850 | 144 | |
paul@872 | 145 | /* Exception occurred. */ |
paul@872 | 146 | if (signum == FPE_FLTOVF) |
paul@872 | 147 | __raise_overflow_error(); |
paul@872 | 148 | else if (signum == FPE_FLTUND) |
paul@872 | 149 | __raise_underflow_error(); |
paul@872 | 150 | else if (signum == FPE_FLTDIV) |
paul@872 | 151 | __raise_zero_division_error(); |
paul@850 | 152 | |
paul@850 | 153 | return __NULL; |
paul@850 | 154 | } |
paul@850 | 155 | |
paul@850 | 156 | __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other) |
paul@850 | 157 | { |
paul@850 | 158 | /* self and other interpreted as float */ |
paul@850 | 159 | double i = __TOFLOAT(self); |
paul@850 | 160 | double j = __TOFLOAT(other); |
paul@872 | 161 | int signum; |
paul@850 | 162 | |
paul@872 | 163 | /* Perform the operation while handling exceptions. */ |
paul@872 | 164 | signum = setjmp(__fpe_env); |
paul@872 | 165 | if (!signum) |
paul@872 | 166 | return __new_float(fmod(i, j)); |
paul@850 | 167 | |
paul@872 | 168 | /* Exception occurred. */ |
paul@872 | 169 | if (signum == FPE_FLTOVF) |
paul@872 | 170 | __raise_overflow_error(); |
paul@872 | 171 | else if (signum == FPE_FLTDIV) |
paul@872 | 172 | __raise_zero_division_error(); |
paul@850 | 173 | |
paul@850 | 174 | return __NULL; |
paul@850 | 175 | } |
paul@850 | 176 | |
paul@850 | 177 | __attr __fn_native_float_float_neg(__attr __self, __attr self) |
paul@850 | 178 | { |
paul@850 | 179 | /* self interpreted as float */ |
paul@850 | 180 | double i = __TOFLOAT(self); |
paul@872 | 181 | int signum; |
paul@850 | 182 | |
paul@872 | 183 | /* Perform the operation while handling exceptions. */ |
paul@872 | 184 | signum = setjmp(__fpe_env); |
paul@872 | 185 | if (!signum) |
paul@872 | 186 | return __new_float(-i); |
paul@850 | 187 | |
paul@872 | 188 | /* Exception occurred. */ |
paul@872 | 189 | if (signum == FPE_FLTOVF) |
paul@850 | 190 | __raise_overflow_error(); |
paul@872 | 191 | |
paul@850 | 192 | return __NULL; |
paul@850 | 193 | } |
paul@850 | 194 | |
paul@850 | 195 | __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) |
paul@850 | 196 | { |
paul@850 | 197 | /* self and other interpreted as float */ |
paul@850 | 198 | double i = __TOFLOAT(self); |
paul@850 | 199 | double j = __TOFLOAT(other); |
paul@850 | 200 | double result; |
paul@850 | 201 | |
paul@850 | 202 | errno = 0; |
paul@850 | 203 | result = pow(i, j); |
paul@850 | 204 | |
paul@850 | 205 | /* Test for overflow. */ |
paul@850 | 206 | |
paul@850 | 207 | if (errno == ERANGE) |
paul@850 | 208 | __raise_overflow_error(); |
paul@850 | 209 | |
paul@850 | 210 | /* Return the result. */ |
paul@850 | 211 | return __new_float(result); |
paul@850 | 212 | } |
paul@850 | 213 | |
paul@850 | 214 | __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other) |
paul@850 | 215 | { |
paul@850 | 216 | /* self and other interpreted as float */ |
paul@850 | 217 | double i = __TOFLOAT(self); |
paul@850 | 218 | double j = __TOFLOAT(other); |
paul@850 | 219 | |
paul@850 | 220 | /* Return a boolean result. */ |
paul@850 | 221 | return i <= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 222 | } |
paul@850 | 223 | |
paul@850 | 224 | __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other) |
paul@850 | 225 | { |
paul@850 | 226 | /* self and other interpreted as float */ |
paul@850 | 227 | double i = __TOFLOAT(self); |
paul@850 | 228 | double j = __TOFLOAT(other); |
paul@850 | 229 | |
paul@850 | 230 | /* Return a boolean result. */ |
paul@850 | 231 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 232 | } |
paul@850 | 233 | |
paul@850 | 234 | __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other) |
paul@850 | 235 | { |
paul@850 | 236 | /* self and other interpreted as float */ |
paul@850 | 237 | double i = __TOFLOAT(self); |
paul@850 | 238 | double j = __TOFLOAT(other); |
paul@850 | 239 | |
paul@850 | 240 | /* Return a boolean result. */ |
paul@850 | 241 | return i >= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 242 | } |
paul@850 | 243 | |
paul@850 | 244 | __attr __fn_native_float_float_gt(__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 | |
paul@850 | 250 | /* Return a boolean result. */ |
paul@850 | 251 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 252 | } |
paul@850 | 253 | |
paul@850 | 254 | __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other) |
paul@850 | 255 | { |
paul@850 | 256 | /* self and other interpreted as float */ |
paul@850 | 257 | double i = __TOFLOAT(self); |
paul@850 | 258 | double j = __TOFLOAT(other); |
paul@850 | 259 | |
paul@850 | 260 | /* Return a boolean result. */ |
paul@850 | 261 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 262 | } |
paul@850 | 263 | |
paul@850 | 264 | __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other) |
paul@850 | 265 | { |
paul@850 | 266 | /* self and other interpreted as float */ |
paul@850 | 267 | double i = __TOFLOAT(self); |
paul@850 | 268 | double j = __TOFLOAT(other); |
paul@850 | 269 | |
paul@850 | 270 | /* Return a boolean result. */ |
paul@850 | 271 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 272 | } |
paul@850 | 273 | |
paul@850 | 274 | __attr __fn_native_float_float_str(__attr __self, __attr self) |
paul@850 | 275 | { |
paul@850 | 276 | /* self interpreted as float */ |
paul@850 | 277 | double i = __TOFLOAT(self); |
paul@850 | 278 | |
paul@850 | 279 | /* Return a new string. */ |
paul@850 | 280 | return format_number(i, 64); |
paul@850 | 281 | } |
paul@850 | 282 | |
paul@850 | 283 | __attr __fn_native_float_float_int(__attr __self, __attr self) |
paul@850 | 284 | { |
paul@850 | 285 | /* self interpreted as float */ |
paul@850 | 286 | double i = __TOFLOAT(self); |
paul@850 | 287 | |
paul@850 | 288 | /* NOTE: Test for conversion failure. */ |
paul@850 | 289 | return __new_int((int) i); |
paul@850 | 290 | } |
paul@850 | 291 | |
paul@850 | 292 | /* Module initialisation. */ |
paul@850 | 293 | |
paul@850 | 294 | void __main_native_float() |
paul@850 | 295 | { |
paul@850 | 296 | } |