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@871 | 31 | /* Conversion of trailing data to a double-precision floating point number. */ |
paul@850 | 32 | |
paul@850 | 33 | static double __TOFLOAT(__attr attr) |
paul@850 | 34 | { |
paul@850 | 35 | return __get_trailing_data(attr, __builtins___float_float); |
paul@850 | 36 | } |
paul@850 | 37 | |
paul@850 | 38 | /* Numeric formatting using snprintf. |
paul@850 | 39 | NOTE: This might be moved elsewhere and used by other types. */ |
paul@850 | 40 | |
paul@850 | 41 | static __attr format_number(double n, int size) |
paul@850 | 42 | { |
paul@850 | 43 | char *s = (char *) __ALLOCATE(size, sizeof(char)); |
paul@850 | 44 | int digits; |
paul@850 | 45 | |
paul@850 | 46 | /* Allocation should raise a memory error if it fails, so this loop should |
paul@850 | 47 | terminate via the return statement or an allocation failure. */ |
paul@850 | 48 | |
paul@850 | 49 | while (1) |
paul@850 | 50 | { |
paul@850 | 51 | digits = snprintf(s, size, "%f", n); |
paul@850 | 52 | |
paul@850 | 53 | if (digits < size) |
paul@850 | 54 | { |
paul@850 | 55 | s = (char *) __REALLOCATE(s, (digits + 1) * sizeof(char)); |
paul@850 | 56 | return __new_str(s, digits); |
paul@850 | 57 | } |
paul@850 | 58 | |
paul@850 | 59 | size = digits + 1; |
paul@850 | 60 | s = (char *) __REALLOCATE(s, size * sizeof(char)); |
paul@850 | 61 | } |
paul@850 | 62 | |
paul@850 | 63 | return __NULL; |
paul@850 | 64 | } |
paul@850 | 65 | |
paul@900 | 66 | static __attr make_result(__attr self, __attr other, double value) |
paul@900 | 67 | { |
paul@900 | 68 | __attr result; |
paul@900 | 69 | |
paul@900 | 70 | if (self.value->temporary) |
paul@900 | 71 | { |
paul@900 | 72 | __set_trailing_data(self, __builtins___float_float, value); |
paul@900 | 73 | return self; |
paul@900 | 74 | } |
paul@900 | 75 | else if (other.value->temporary) |
paul@900 | 76 | { |
paul@900 | 77 | __set_trailing_data(other, __builtins___float_float, value); |
paul@900 | 78 | return other; |
paul@900 | 79 | } |
paul@900 | 80 | else |
paul@900 | 81 | { |
paul@900 | 82 | result = __new_float(value); |
paul@900 | 83 | result.value->temporary = 1; |
paul@900 | 84 | return result; |
paul@900 | 85 | } |
paul@900 | 86 | } |
paul@900 | 87 | |
paul@879 | 88 | /* Floating point operations. Exceptions are raised in the signal handler. */ |
paul@850 | 89 | |
paul@850 | 90 | __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other) |
paul@850 | 91 | { |
paul@850 | 92 | /* self and other interpreted as float */ |
paul@850 | 93 | double i = __TOFLOAT(self); |
paul@850 | 94 | double j = __TOFLOAT(other); |
paul@900 | 95 | return make_result(self, other, i + j); |
paul@850 | 96 | } |
paul@850 | 97 | |
paul@850 | 98 | __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) |
paul@850 | 99 | { |
paul@850 | 100 | /* self and other interpreted as float */ |
paul@850 | 101 | double i = __TOFLOAT(self); |
paul@850 | 102 | double j = __TOFLOAT(other); |
paul@900 | 103 | return make_result(self, other, i - j); |
paul@850 | 104 | } |
paul@850 | 105 | |
paul@850 | 106 | __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) |
paul@850 | 107 | { |
paul@850 | 108 | /* self and other interpreted as float */ |
paul@850 | 109 | double i = __TOFLOAT(self); |
paul@850 | 110 | double j = __TOFLOAT(other); |
paul@900 | 111 | return make_result(self, other, i * j); |
paul@850 | 112 | } |
paul@850 | 113 | |
paul@850 | 114 | __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) |
paul@850 | 115 | { |
paul@850 | 116 | /* self and other interpreted as float */ |
paul@850 | 117 | double i = __TOFLOAT(self); |
paul@850 | 118 | double j = __TOFLOAT(other); |
paul@900 | 119 | return make_result(self, other, i / j); |
paul@850 | 120 | } |
paul@850 | 121 | |
paul@850 | 122 | __attr __fn_native_float_float_mod(__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@900 | 127 | return make_result(self, other, fmod(i, j)); |
paul@850 | 128 | } |
paul@850 | 129 | |
paul@850 | 130 | __attr __fn_native_float_float_neg(__attr __self, __attr self) |
paul@850 | 131 | { |
paul@850 | 132 | /* self interpreted as float */ |
paul@850 | 133 | double i = __TOFLOAT(self); |
paul@879 | 134 | return __new_float(-i); |
paul@850 | 135 | } |
paul@850 | 136 | |
paul@850 | 137 | __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) |
paul@850 | 138 | { |
paul@850 | 139 | /* self and other interpreted as float */ |
paul@850 | 140 | double i = __TOFLOAT(self); |
paul@850 | 141 | double j = __TOFLOAT(other); |
paul@850 | 142 | double result; |
paul@850 | 143 | |
paul@850 | 144 | errno = 0; |
paul@850 | 145 | result = pow(i, j); |
paul@850 | 146 | |
paul@850 | 147 | /* Test for overflow. */ |
paul@850 | 148 | |
paul@850 | 149 | if (errno == ERANGE) |
paul@850 | 150 | __raise_overflow_error(); |
paul@850 | 151 | |
paul@850 | 152 | /* Return the result. */ |
paul@900 | 153 | return make_result(self, other, result); |
paul@850 | 154 | } |
paul@850 | 155 | |
paul@850 | 156 | __attr __fn_native_float_float_le(__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@850 | 161 | |
paul@850 | 162 | /* Return a boolean result. */ |
paul@850 | 163 | return i <= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 164 | } |
paul@850 | 165 | |
paul@850 | 166 | __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other) |
paul@850 | 167 | { |
paul@850 | 168 | /* self and other interpreted as float */ |
paul@850 | 169 | double i = __TOFLOAT(self); |
paul@850 | 170 | double j = __TOFLOAT(other); |
paul@850 | 171 | |
paul@850 | 172 | /* Return a boolean result. */ |
paul@850 | 173 | return i < j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 174 | } |
paul@850 | 175 | |
paul@850 | 176 | __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other) |
paul@850 | 177 | { |
paul@850 | 178 | /* self and other interpreted as float */ |
paul@850 | 179 | double i = __TOFLOAT(self); |
paul@850 | 180 | double j = __TOFLOAT(other); |
paul@850 | 181 | |
paul@850 | 182 | /* Return a boolean result. */ |
paul@850 | 183 | return i >= j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 184 | } |
paul@850 | 185 | |
paul@850 | 186 | __attr __fn_native_float_float_gt(__attr __self, __attr self, __attr other) |
paul@850 | 187 | { |
paul@850 | 188 | /* self and other interpreted as float */ |
paul@850 | 189 | double i = __TOFLOAT(self); |
paul@850 | 190 | double j = __TOFLOAT(other); |
paul@850 | 191 | |
paul@850 | 192 | /* Return a boolean result. */ |
paul@850 | 193 | return i > j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 194 | } |
paul@850 | 195 | |
paul@850 | 196 | __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other) |
paul@850 | 197 | { |
paul@850 | 198 | /* self and other interpreted as float */ |
paul@850 | 199 | double i = __TOFLOAT(self); |
paul@850 | 200 | double j = __TOFLOAT(other); |
paul@850 | 201 | |
paul@850 | 202 | /* Return a boolean result. */ |
paul@850 | 203 | return i == j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 204 | } |
paul@850 | 205 | |
paul@850 | 206 | __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other) |
paul@850 | 207 | { |
paul@850 | 208 | /* self and other interpreted as float */ |
paul@850 | 209 | double i = __TOFLOAT(self); |
paul@850 | 210 | double j = __TOFLOAT(other); |
paul@850 | 211 | |
paul@850 | 212 | /* Return a boolean result. */ |
paul@850 | 213 | return i != j ? __builtins___boolean_True : __builtins___boolean_False; |
paul@850 | 214 | } |
paul@850 | 215 | |
paul@850 | 216 | __attr __fn_native_float_float_str(__attr __self, __attr self) |
paul@850 | 217 | { |
paul@850 | 218 | /* self interpreted as float */ |
paul@850 | 219 | double i = __TOFLOAT(self); |
paul@850 | 220 | |
paul@850 | 221 | /* Return a new string. */ |
paul@850 | 222 | return format_number(i, 64); |
paul@850 | 223 | } |
paul@850 | 224 | |
paul@850 | 225 | __attr __fn_native_float_float_int(__attr __self, __attr self) |
paul@850 | 226 | { |
paul@850 | 227 | /* self interpreted as float */ |
paul@850 | 228 | double i = __TOFLOAT(self); |
paul@850 | 229 | |
paul@850 | 230 | /* NOTE: Test for conversion failure. */ |
paul@850 | 231 | return __new_int((int) i); |
paul@850 | 232 | } |
paul@850 | 233 | |
paul@850 | 234 | /* Module initialisation. */ |
paul@850 | 235 | |
paul@850 | 236 | void __main_native_float() |
paul@850 | 237 | { |
paul@850 | 238 | } |