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@850 | 19 | #include <math.h> /* pow */ |
paul@850 | 20 | #include <stdio.h> /* snprintf */ |
paul@850 | 21 | #include <errno.h> /* errno */ |
paul@850 | 22 | #include "native/common.h" |
paul@850 | 23 | #include "types.h" |
paul@850 | 24 | #include "exceptions.h" |
paul@850 | 25 | #include "ops.h" |
paul@850 | 26 | #include "progconsts.h" |
paul@850 | 27 | #include "progops.h" |
paul@850 | 28 | #include "progtypes.h" |
paul@850 | 29 | #include "main.h" |
paul@850 | 30 | |
paul@880 | 31 | /* A table of preallocated float instances. */ |
paul@880 | 32 | |
paul@880 | 33 | struct float_table |
paul@880 | 34 | { |
paul@880 | 35 | struct float_table *next; |
paul@880 | 36 | char data[]; |
paul@880 | 37 | }; |
paul@880 | 38 | |
paul@880 | 39 | static struct float_table *next_float = 0; |
paul@880 | 40 | |
paul@880 | 41 | /* Preallocate some float instances. */ |
paul@880 | 42 | |
paul@880 | 43 | static void preallocate_floats(int num) |
paul@880 | 44 | { |
paul@880 | 45 | struct float_table *latest; |
paul@880 | 46 | int i; |
paul@880 | 47 | |
paul@880 | 48 | for (i = 0; i < num; i++) |
paul@880 | 49 | { |
paul@880 | 50 | /* Allocate a table entry. */ |
paul@880 | 51 | |
paul@880 | 52 | latest = (struct float_table *) |
paul@880 | 53 | __ALLOCATE(1, sizeof(struct float_table *) + |
paul@880 | 54 | __INSTANCESIZE(__builtins___float_float)); |
paul@880 | 55 | |
paul@880 | 56 | /* Reference the last entry from the new entry. */ |
paul@880 | 57 | |
paul@880 | 58 | latest->next = next_float; |
paul@880 | 59 | next_float = latest; |
paul@880 | 60 | } |
paul@880 | 61 | } |
paul@880 | 62 | |
paul@880 | 63 | static __attr new_float(double n) |
paul@880 | 64 | { |
paul@880 | 65 | struct float_table *this_float; |
paul@880 | 66 | __attr attr; |
paul@880 | 67 | |
paul@880 | 68 | if (!next_float) |
paul@880 | 69 | preallocate_floats(1000); |
paul@880 | 70 | |
paul@880 | 71 | /* Reference the next preallocated entry. */ |
paul@880 | 72 | |
paul@880 | 73 | this_float = next_float; |
paul@880 | 74 | |
paul@880 | 75 | /* Initialise the embedded instance. */ |
paul@880 | 76 | |
paul@880 | 77 | __init((__ref) &this_float->data, |
paul@880 | 78 | &__INSTANCETABLE(__builtins___float_float), |
paul@880 | 79 | &__builtins___float_float); |
paul@880 | 80 | |
paul@880 | 81 | /* Populate the float with the value. */ |
paul@880 | 82 | |
paul@880 | 83 | attr = __ATTRVALUE(&this_float->data); |
paul@880 | 84 | __set_trailing_data(attr, __builtins___float_float, n); |
paul@880 | 85 | |
paul@880 | 86 | /* Make the next entry available and detach it from this one. */ |
paul@880 | 87 | |
paul@880 | 88 | next_float = this_float->next; |
paul@880 | 89 | this_float->next = 0; |
paul@880 | 90 | |
paul@880 | 91 | return attr; |
paul@880 | 92 | } |
paul@880 | 93 | |
paul@871 | 94 | /* Conversion of trailing data to a double-precision floating point number. */ |
paul@850 | 95 | |
paul@850 | 96 | static double __TOFLOAT(__attr attr) |
paul@850 | 97 | { |
paul@850 | 98 | return __get_trailing_data(attr, __builtins___float_float); |
paul@850 | 99 | } |
paul@850 | 100 | |
paul@850 | 101 | /* Numeric formatting using snprintf. |
paul@850 | 102 | NOTE: This might be moved elsewhere and used by other types. */ |
paul@850 | 103 | |
paul@850 | 104 | static __attr format_number(double n, int size) |
paul@850 | 105 | { |
paul@850 | 106 | char *s = (char *) __ALLOCATE(size, sizeof(char)); |
paul@850 | 107 | int digits; |
paul@850 | 108 | |
paul@850 | 109 | /* Allocation should raise a memory error if it fails, so this loop should |
paul@850 | 110 | terminate via the return statement or an allocation failure. */ |
paul@850 | 111 | |
paul@850 | 112 | while (1) |
paul@850 | 113 | { |
paul@850 | 114 | digits = snprintf(s, size, "%f", n); |
paul@850 | 115 | |
paul@850 | 116 | if (digits < size) |
paul@850 | 117 | { |
paul@850 | 118 | s = (char *) __REALLOCATE(s, (digits + 1) * sizeof(char)); |
paul@850 | 119 | return __new_str(s, digits); |
paul@850 | 120 | } |
paul@850 | 121 | |
paul@850 | 122 | size = digits + 1; |
paul@850 | 123 | s = (char *) __REALLOCATE(s, size * sizeof(char)); |
paul@850 | 124 | } |
paul@850 | 125 | |
paul@850 | 126 | return __NULL; |
paul@850 | 127 | } |
paul@850 | 128 | |
paul@879 | 129 | /* Floating point operations. Exceptions are raised in the signal handler. */ |
paul@850 | 130 | |
paul@850 | 131 | __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other) |
paul@850 | 132 | { |
paul@850 | 133 | /* self and other interpreted as float */ |
paul@850 | 134 | double i = __TOFLOAT(self); |
paul@850 | 135 | double j = __TOFLOAT(other); |
paul@880 | 136 | return new_float(i + j); |
paul@850 | 137 | } |
paul@850 | 138 | |
paul@850 | 139 | __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) |
paul@850 | 140 | { |
paul@850 | 141 | /* self and other interpreted as float */ |
paul@850 | 142 | double i = __TOFLOAT(self); |
paul@850 | 143 | double j = __TOFLOAT(other); |
paul@880 | 144 | return new_float(i - j); |
paul@850 | 145 | } |
paul@850 | 146 | |
paul@850 | 147 | __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) |
paul@850 | 148 | { |
paul@850 | 149 | /* self and other interpreted as float */ |
paul@850 | 150 | double i = __TOFLOAT(self); |
paul@850 | 151 | double j = __TOFLOAT(other); |
paul@880 | 152 | return new_float(i * j); |
paul@850 | 153 | } |
paul@850 | 154 | |
paul@850 | 155 | __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) |
paul@850 | 156 | { |
paul@850 | 157 | /* self and other interpreted as float */ |
paul@850 | 158 | double i = __TOFLOAT(self); |
paul@850 | 159 | double j = __TOFLOAT(other); |
paul@880 | 160 | return new_float(i / j); |
paul@850 | 161 | } |
paul@850 | 162 | |
paul@850 | 163 | __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other) |
paul@850 | 164 | { |
paul@850 | 165 | /* self and other interpreted as float */ |
paul@850 | 166 | double i = __TOFLOAT(self); |
paul@850 | 167 | double j = __TOFLOAT(other); |
paul@880 | 168 | return new_float(fmod(i, j)); |
paul@850 | 169 | } |
paul@850 | 170 | |
paul@850 | 171 | __attr __fn_native_float_float_neg(__attr __self, __attr self) |
paul@850 | 172 | { |
paul@850 | 173 | /* self interpreted as float */ |
paul@850 | 174 | double i = __TOFLOAT(self); |
paul@880 | 175 | return new_float(-i); |
paul@850 | 176 | } |
paul@850 | 177 | |
paul@850 | 178 | __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) |
paul@850 | 179 | { |
paul@850 | 180 | /* self and other interpreted as float */ |
paul@850 | 181 | double i = __TOFLOAT(self); |
paul@850 | 182 | double j = __TOFLOAT(other); |
paul@850 | 183 | double result; |
paul@850 | 184 | |
paul@850 | 185 | errno = 0; |
paul@850 | 186 | result = pow(i, j); |
paul@850 | 187 | |
paul@850 | 188 | /* Test for overflow. */ |
paul@850 | 189 | |
paul@850 | 190 | if (errno == ERANGE) |
paul@850 | 191 | __raise_overflow_error(); |
paul@850 | 192 | |
paul@850 | 193 | /* Return the result. */ |
paul@880 | 194 | return new_float(result); |
paul@850 | 195 | } |
paul@850 | 196 | |
paul@850 | 197 | __attr __fn_native_float_float_le(__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 | |
paul@850 | 203 | /* Return a boolean result. */ |
paul@850 | 204 | return i <= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 205 | } |
paul@850 | 206 | |
paul@850 | 207 | __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other) |
paul@850 | 208 | { |
paul@850 | 209 | /* self and other interpreted as float */ |
paul@850 | 210 | double i = __TOFLOAT(self); |
paul@850 | 211 | double j = __TOFLOAT(other); |
paul@850 | 212 | |
paul@850 | 213 | /* Return a boolean result. */ |
paul@850 | 214 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 215 | } |
paul@850 | 216 | |
paul@850 | 217 | __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other) |
paul@850 | 218 | { |
paul@850 | 219 | /* self and other interpreted as float */ |
paul@850 | 220 | double i = __TOFLOAT(self); |
paul@850 | 221 | double j = __TOFLOAT(other); |
paul@850 | 222 | |
paul@850 | 223 | /* Return a boolean result. */ |
paul@850 | 224 | return i >= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 225 | } |
paul@850 | 226 | |
paul@850 | 227 | __attr __fn_native_float_float_gt(__attr __self, __attr self, __attr other) |
paul@850 | 228 | { |
paul@850 | 229 | /* self and other interpreted as float */ |
paul@850 | 230 | double i = __TOFLOAT(self); |
paul@850 | 231 | double j = __TOFLOAT(other); |
paul@850 | 232 | |
paul@850 | 233 | /* Return a boolean result. */ |
paul@850 | 234 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 235 | } |
paul@850 | 236 | |
paul@850 | 237 | __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other) |
paul@850 | 238 | { |
paul@850 | 239 | /* self and other interpreted as float */ |
paul@850 | 240 | double i = __TOFLOAT(self); |
paul@850 | 241 | double j = __TOFLOAT(other); |
paul@850 | 242 | |
paul@850 | 243 | /* Return a boolean result. */ |
paul@850 | 244 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 245 | } |
paul@850 | 246 | |
paul@850 | 247 | __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other) |
paul@850 | 248 | { |
paul@850 | 249 | /* self and other interpreted as float */ |
paul@850 | 250 | double i = __TOFLOAT(self); |
paul@850 | 251 | double j = __TOFLOAT(other); |
paul@850 | 252 | |
paul@850 | 253 | /* Return a boolean result. */ |
paul@850 | 254 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 255 | } |
paul@850 | 256 | |
paul@850 | 257 | __attr __fn_native_float_float_str(__attr __self, __attr self) |
paul@850 | 258 | { |
paul@850 | 259 | /* self interpreted as float */ |
paul@850 | 260 | double i = __TOFLOAT(self); |
paul@850 | 261 | |
paul@850 | 262 | /* Return a new string. */ |
paul@850 | 263 | return format_number(i, 64); |
paul@850 | 264 | } |
paul@850 | 265 | |
paul@850 | 266 | __attr __fn_native_float_float_int(__attr __self, __attr self) |
paul@850 | 267 | { |
paul@850 | 268 | /* self interpreted as float */ |
paul@850 | 269 | double i = __TOFLOAT(self); |
paul@850 | 270 | |
paul@850 | 271 | /* NOTE: Test for conversion failure. */ |
paul@850 | 272 | return __new_int((int) i); |
paul@850 | 273 | } |
paul@850 | 274 | |
paul@850 | 275 | /* Module initialisation. */ |
paul@850 | 276 | |
paul@850 | 277 | void __main_native_float() |
paul@850 | 278 | { |
paul@850 | 279 | } |