| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* num_co.d - operations on floating-point numbers |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
/* |
| 16 |
IMPLEMENTATION-DEPENDENT |
| 17 |
|
| 18 |
This file contains those functions |
| 19 |
that know the representation of floating-point numbers. |
| 20 |
*/ |
| 21 |
|
| 22 |
#define ECL_INCLUDE_MATH_H |
| 23 |
#include <ecl/ecl.h> |
| 24 |
#include <float.h> |
| 25 |
#include <ecl/internal.h> |
| 26 |
|
| 27 |
/* Coerce X to single-float if one arg, |
| 28 |
otherwise coerce to same float type as second arg */ |
| 29 |
|
| 30 |
@(defun float (x &optional (y OBJNULL)) |
| 31 |
cl_type ty, tx; |
| 32 |
@ |
| 33 |
if (y != OBJNULL) { |
| 34 |
ty = ecl_t_of(y); |
| 35 |
} else { |
| 36 |
ty = t_singlefloat; |
| 37 |
} |
| 38 |
switch (tx = ecl_t_of(x)) { |
| 39 |
case t_singlefloat: |
| 40 |
case t_doublefloat: |
| 41 |
case t_longfloat: |
| 42 |
if (y == OBJNULL || ty == tx) |
| 43 |
break; |
| 44 |
case t_fixnum: |
| 45 |
case t_bignum: |
| 46 |
case t_ratio: |
| 47 |
switch (ty) { |
| 48 |
case t_singlefloat: |
| 49 |
x = ecl_make_single_float(ecl_to_double(x)); break; |
| 50 |
case t_doublefloat: |
| 51 |
x = ecl_make_double_float(ecl_to_double(x)); break; |
| 52 |
case t_longfloat: |
| 53 |
x = ecl_make_long_float(ecl_to_long_double(x)); break; |
| 54 |
default: |
| 55 |
FEwrong_type_nth_arg(@[float],2,y,@[float]); |
| 56 |
} |
| 57 |
break; |
| 58 |
default: |
| 59 |
FEwrong_type_nth_arg(@[float],1,x,@[real]); |
| 60 |
} |
| 61 |
@(return x) |
| 62 |
@) |
| 63 |
|
| 64 |
cl_object |
| 65 |
cl_numerator(cl_object x) |
| 66 |
{ |
| 67 |
switch (ecl_t_of(x)) { |
| 68 |
case t_ratio: |
| 69 |
x = x->ratio.num; |
| 70 |
break; |
| 71 |
case t_fixnum: |
| 72 |
case t_bignum: |
| 73 |
break; |
| 74 |
default: |
| 75 |
FEwrong_type_only_arg(@[numerator],x,@[rational]); |
| 76 |
} |
| 77 |
@(return x) |
| 78 |
} |
| 79 |
|
| 80 |
cl_object |
| 81 |
cl_denominator(cl_object x) |
| 82 |
{ |
| 83 |
switch (ecl_t_of(x)) { |
| 84 |
case t_ratio: |
| 85 |
x = x->ratio.den; |
| 86 |
break; |
| 87 |
case t_fixnum: |
| 88 |
case t_bignum: |
| 89 |
x = ecl_make_fixnum(1); |
| 90 |
break; |
| 91 |
default: |
| 92 |
FEwrong_type_only_arg(@[denominator],x,@[rational]); |
| 93 |
} |
| 94 |
@(return x) |
| 95 |
} |
| 96 |
|
| 97 |
cl_object |
| 98 |
cl_mod(cl_object x, cl_object y) |
| 99 |
{ |
| 100 |
const cl_env_ptr the_env = ecl_process_env(); |
| 101 |
/* INV: #'floor always outputs two values */ |
| 102 |
@floor(2, x, y); |
| 103 |
ecl_return1(the_env, the_env->values[1]); |
| 104 |
} |
| 105 |
|
| 106 |
cl_object |
| 107 |
cl_rem(cl_object x, cl_object y) |
| 108 |
{ |
| 109 |
const cl_env_ptr the_env = ecl_process_env(); |
| 110 |
@truncate(2, x, y); |
| 111 |
ecl_return1(the_env, the_env->values[1]); |
| 112 |
} |
| 113 |
|
| 114 |
cl_object |
| 115 |
cl_decode_float(cl_object x) |
| 116 |
{ |
| 117 |
const cl_env_ptr the_env = ecl_process_env(); |
| 118 |
int e, s = 1; |
| 119 |
cl_object y = ECL_NIL; |
| 120 |
|
| 121 |
switch (ecl_t_of(x)) { |
| 122 |
case t_longfloat: { |
| 123 |
long double d = ecl_long_float(x); |
| 124 |
if (signbit(d)) { |
| 125 |
s = -1; |
| 126 |
d = -d; |
| 127 |
} |
| 128 |
d = frexpl(d, &e); |
| 129 |
x = ecl_make_long_float(d); |
| 130 |
y = ecl_make_long_float(s); |
| 131 |
break; |
| 132 |
} |
| 133 |
case t_doublefloat: { |
| 134 |
double d = ecl_double_float(x); |
| 135 |
if (signbit(d)) { |
| 136 |
s = -1; |
| 137 |
d = -d; |
| 138 |
} |
| 139 |
d = frexp(d, &e); |
| 140 |
x = ecl_make_double_float(d); |
| 141 |
y = ecl_make_double_float(s); |
| 142 |
break; |
| 143 |
} |
| 144 |
case t_singlefloat: { |
| 145 |
float d = ecl_single_float(x); |
| 146 |
if (signbit(d)) { |
| 147 |
s = -1; |
| 148 |
d = -d; |
| 149 |
} |
| 150 |
d = frexpf(d, &e); |
| 151 |
x = ecl_make_single_float(d); |
| 152 |
y = ecl_make_single_float(s); |
| 153 |
break; |
| 154 |
} |
| 155 |
default: |
| 156 |
FEwrong_type_only_arg(@[decode-float],x,@[float]); |
| 157 |
} |
| 158 |
ecl_return3(the_env, x, ecl_make_fixnum(e), y); |
| 159 |
} |
| 160 |
|
| 161 |
cl_object |
| 162 |
cl_scale_float(cl_object x, cl_object y) |
| 163 |
{ |
| 164 |
const cl_env_ptr the_env = ecl_process_env(); |
| 165 |
cl_fixnum k; |
| 166 |
|
| 167 |
if (ECL_FIXNUMP(y)) { |
| 168 |
k = ecl_fixnum(y); |
| 169 |
} else { |
| 170 |
FEwrong_type_nth_arg(@[scale-float],2,y,@[fixnum]); |
| 171 |
} |
| 172 |
switch (ecl_t_of(x)) { |
| 173 |
case t_singlefloat: |
| 174 |
x = ecl_make_single_float(ldexpf(ecl_single_float(x), k)); |
| 175 |
break; |
| 176 |
case t_doublefloat: |
| 177 |
x = ecl_make_double_float(ldexp(ecl_double_float(x), k)); |
| 178 |
break; |
| 179 |
case t_longfloat: |
| 180 |
x = ecl_make_long_float(ldexpl(ecl_long_float(x), k)); |
| 181 |
break; |
| 182 |
default: |
| 183 |
FEwrong_type_nth_arg(@[scale-float],1,x,@[float]); |
| 184 |
} |
| 185 |
ecl_return1(the_env, x); |
| 186 |
} |
| 187 |
|
| 188 |
cl_object |
| 189 |
cl_float_radix(cl_object x) |
| 190 |
{ |
| 191 |
const cl_env_ptr the_env = ecl_process_env(); |
| 192 |
if (ecl_unlikely(cl_floatp(x) != ECL_T)) { |
| 193 |
FEwrong_type_only_arg(@[float-radix],x,@[float]); |
| 194 |
} |
| 195 |
ecl_return1(the_env, ecl_make_fixnum(FLT_RADIX)); |
| 196 |
} |
| 197 |
|
| 198 |
int |
| 199 |
ecl_signbit(cl_object x) |
| 200 |
{ |
| 201 |
switch (ecl_t_of(x)) { |
| 202 |
case t_singlefloat: |
| 203 |
return signbit(ecl_single_float(x)); |
| 204 |
case t_doublefloat: |
| 205 |
return signbit(ecl_double_float(x)); |
| 206 |
case t_longfloat: |
| 207 |
return signbit(ecl_long_float(x)); |
| 208 |
default: |
| 209 |
FEwrong_type_nth_arg(@[float-sign],1,x,@[float]); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
@(defun float_sign (x &optional (y x yp)) |
| 214 |
int negativep; |
| 215 |
@ |
| 216 |
if (!yp) { |
| 217 |
y = cl_float(2, ecl_make_fixnum(1), x); |
| 218 |
} |
| 219 |
negativep = ecl_signbit(x); |
| 220 |
switch (ecl_t_of(y)) { |
| 221 |
case t_singlefloat: { |
| 222 |
float f = ecl_single_float(y); |
| 223 |
if (signbit(f) != negativep) y = ecl_make_single_float(-f); |
| 224 |
break; |
| 225 |
} |
| 226 |
case t_doublefloat: { |
| 227 |
double f = ecl_double_float(y); |
| 228 |
if (signbit(f) != negativep) y = ecl_make_double_float(-f); |
| 229 |
break; |
| 230 |
} |
| 231 |
case t_longfloat: { |
| 232 |
long double f = ecl_long_float(y); |
| 233 |
if (signbit(f) != negativep) y = ecl_make_long_float(-f); |
| 234 |
break; |
| 235 |
} |
| 236 |
default: |
| 237 |
FEwrong_type_nth_arg(@[float-sign],2,y,@[float]); |
| 238 |
} |
| 239 |
@(return y); |
| 240 |
@) |
| 241 |
|
| 242 |
cl_object |
| 243 |
cl_float_digits(cl_object x) |
| 244 |
{ |
| 245 |
const cl_env_ptr the_env = ecl_process_env(); |
| 246 |
switch (ecl_t_of(x)) { |
| 247 |
case t_singlefloat: |
| 248 |
x = ecl_make_fixnum(FLT_MANT_DIG); |
| 249 |
break; |
| 250 |
case t_doublefloat: |
| 251 |
x = ecl_make_fixnum(DBL_MANT_DIG); |
| 252 |
break; |
| 253 |
case t_longfloat: |
| 254 |
x = ecl_make_fixnum(LDBL_MANT_DIG); |
| 255 |
break; |
| 256 |
default: |
| 257 |
FEwrong_type_only_arg(@[float-digits],x,@[float]); |
| 258 |
} |
| 259 |
ecl_return1(the_env, x); |
| 260 |
} |
| 261 |
|
| 262 |
cl_object |
| 263 |
cl_float_precision(cl_object x) |
| 264 |
{ |
| 265 |
const cl_env_ptr the_env = ecl_process_env(); |
| 266 |
int precision; |
| 267 |
switch (ecl_t_of(x)) { |
| 268 |
case t_singlefloat: { |
| 269 |
float f = ecl_single_float(x); |
| 270 |
if (f == 0.0) { |
| 271 |
precision = 0; |
| 272 |
} else { |
| 273 |
int exp; |
| 274 |
frexpf(f, &exp); |
| 275 |
if (exp >= FLT_MIN_EXP) { |
| 276 |
precision = FLT_MANT_DIG; |
| 277 |
} else { |
| 278 |
precision = FLT_MANT_DIG - (FLT_MIN_EXP - exp); |
| 279 |
} |
| 280 |
} |
| 281 |
break; |
| 282 |
} |
| 283 |
case t_doublefloat: { |
| 284 |
double f = ecl_double_float(x); |
| 285 |
if (f == 0.0) { |
| 286 |
precision = 0; |
| 287 |
} else { |
| 288 |
int exp; |
| 289 |
frexp(f, &exp); |
| 290 |
if (exp >= DBL_MIN_EXP) { |
| 291 |
precision = DBL_MANT_DIG; |
| 292 |
} else { |
| 293 |
precision = DBL_MANT_DIG - (DBL_MIN_EXP - exp); |
| 294 |
} |
| 295 |
} |
| 296 |
break; |
| 297 |
} |
| 298 |
case t_longfloat: { |
| 299 |
long double f = ecl_long_float(x); |
| 300 |
if (f == 0.0) { |
| 301 |
precision = 0; |
| 302 |
} else { |
| 303 |
int exp; |
| 304 |
frexpl(f, &exp); |
| 305 |
if (exp >= LDBL_MIN_EXP) { |
| 306 |
precision = LDBL_MANT_DIG; |
| 307 |
} else { |
| 308 |
precision = LDBL_MANT_DIG - (LDBL_MIN_EXP - exp); |
| 309 |
} |
| 310 |
} |
| 311 |
break; |
| 312 |
} |
| 313 |
default: |
| 314 |
FEwrong_type_only_arg(@[float-precision],x,@[float]); |
| 315 |
} |
| 316 |
ecl_return1(the_env, ecl_make_fixnum(precision)); |
| 317 |
} |
| 318 |
|
| 319 |
cl_object |
| 320 |
cl_integer_decode_float(cl_object x) |
| 321 |
{ |
| 322 |
const cl_env_ptr the_env = ecl_process_env(); |
| 323 |
int e, s = 1; |
| 324 |
|
| 325 |
switch (ecl_t_of(x)) { |
| 326 |
case t_longfloat: { |
| 327 |
long double d = ecl_long_float(x); |
| 328 |
if (signbit(d)) { |
| 329 |
s = -1; |
| 330 |
d = -d; |
| 331 |
} |
| 332 |
if (d == 0.0) { |
| 333 |
e = 0; |
| 334 |
x = ecl_make_fixnum(0); |
| 335 |
} else { |
| 336 |
d = frexpl(d, &e); |
| 337 |
x = _ecl_long_double_to_integer(ldexpl(d, LDBL_MANT_DIG)); |
| 338 |
e -= LDBL_MANT_DIG; |
| 339 |
} |
| 340 |
break; |
| 341 |
} |
| 342 |
case t_doublefloat: { |
| 343 |
double d = ecl_double_float(x); |
| 344 |
if (signbit(d)) { |
| 345 |
s = -1; |
| 346 |
d = -d; |
| 347 |
} |
| 348 |
if (d == 0.0) { |
| 349 |
e = 0; |
| 350 |
x = ecl_make_fixnum(0); |
| 351 |
} else { |
| 352 |
d = frexp(d, &e); |
| 353 |
x = _ecl_double_to_integer(ldexp(d, DBL_MANT_DIG)); |
| 354 |
e -= DBL_MANT_DIG; |
| 355 |
} |
| 356 |
break; |
| 357 |
} |
| 358 |
case t_singlefloat: { |
| 359 |
float d = ecl_single_float(x); |
| 360 |
if (signbit(d)) { |
| 361 |
s = -1; |
| 362 |
d = -d; |
| 363 |
} |
| 364 |
if (d == 0.0) { |
| 365 |
e = 0; |
| 366 |
x = ecl_make_fixnum(0); |
| 367 |
} else { |
| 368 |
d = frexpf(d, &e); |
| 369 |
x = _ecl_double_to_integer(ldexp(d, FLT_MANT_DIG)); |
| 370 |
e -= FLT_MANT_DIG; |
| 371 |
} |
| 372 |
break; |
| 373 |
} |
| 374 |
default: |
| 375 |
FEwrong_type_only_arg(@[integer-decode-float],x,@[float]); |
| 376 |
} |
| 377 |
ecl_return3(the_env, x, ecl_make_fixnum(e), ecl_make_fixnum(s)); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
@(defun complex (r &optional (i ecl_make_fixnum(0))) |
| 382 |
@ /* INV: ecl_make_complex() checks types */ |
| 383 |
@(return ecl_make_complex(r, i)) |
| 384 |
@) |
| 385 |
|
| 386 |
cl_object |
| 387 |
cl_realpart(cl_object x) |
| 388 |
{ |
| 389 |
switch (ecl_t_of(x)) { |
| 390 |
case t_fixnum: |
| 391 |
case t_bignum: |
| 392 |
case t_ratio: |
| 393 |
case t_singlefloat: |
| 394 |
case t_doublefloat: |
| 395 |
case t_longfloat: |
| 396 |
break; |
| 397 |
case t_complex: |
| 398 |
x = x->gencomplex.real; |
| 399 |
break; |
| 400 |
#ifdef ECL_COMPLEX_FLOAT |
| 401 |
case t_csfloat: { |
| 402 |
float f = crealf(ecl_csfloat(x)); |
| 403 |
x = ecl_make_single_float(f); |
| 404 |
break; |
| 405 |
} |
| 406 |
case t_cdfloat: { |
| 407 |
double f = creal(ecl_cdfloat(x)); |
| 408 |
x = ecl_make_double_float(f); |
| 409 |
break; |
| 410 |
} |
| 411 |
case t_clfloat: { |
| 412 |
long double f = creall(ecl_clfloat(x)); |
| 413 |
x = ecl_make_long_float(f); |
| 414 |
break; |
| 415 |
} |
| 416 |
#endif |
| 417 |
default: |
| 418 |
FEwrong_type_only_arg(@[realpart],x,@[number]); |
| 419 |
} |
| 420 |
@(return x); |
| 421 |
} |
| 422 |
|
| 423 |
cl_object |
| 424 |
cl_imagpart(cl_object x) |
| 425 |
{ |
| 426 |
switch (ecl_t_of(x)) { |
| 427 |
case t_fixnum: |
| 428 |
case t_bignum: |
| 429 |
case t_ratio: |
| 430 |
x = ecl_make_fixnum(0); |
| 431 |
break; |
| 432 |
case t_singlefloat: |
| 433 |
if (signbit(ecl_single_float(x))) |
| 434 |
x = cl_core.singlefloat_minus_zero; |
| 435 |
else |
| 436 |
x = cl_core.singlefloat_zero; |
| 437 |
break; |
| 438 |
case t_doublefloat: |
| 439 |
if (signbit(ecl_double_float(x))) |
| 440 |
x = cl_core.doublefloat_minus_zero; |
| 441 |
else |
| 442 |
x = cl_core.doublefloat_zero; |
| 443 |
break; |
| 444 |
case t_longfloat: |
| 445 |
if (signbit(ecl_long_float(x))) |
| 446 |
x = cl_core.longfloat_minus_zero; |
| 447 |
else |
| 448 |
x = cl_core.longfloat_zero; |
| 449 |
break; |
| 450 |
case t_complex: |
| 451 |
x = x->gencomplex.imag; |
| 452 |
break; |
| 453 |
#ifdef ECL_COMPLEX_FLOAT |
| 454 |
case t_csfloat: { |
| 455 |
float f = cimagf(ecl_csfloat(x)); |
| 456 |
x = ecl_make_single_float(f); |
| 457 |
break; |
| 458 |
} |
| 459 |
case t_cdfloat: { |
| 460 |
double f = cimag(ecl_cdfloat(x)); |
| 461 |
x = ecl_make_double_float(f); |
| 462 |
break; |
| 463 |
} |
| 464 |
case t_clfloat: { |
| 465 |
long double f = cimagl(ecl_clfloat(x)); |
| 466 |
x = ecl_make_long_float(f); |
| 467 |
break; |
| 468 |
} |
| 469 |
#endif |
| 470 |
default: |
| 471 |
FEwrong_type_only_arg(@[imagpart],x,@[number]); |
| 472 |
} |
| 473 |
@(return x); |
| 474 |
} |
| 475 |
|
| 476 |
uint32_t |
| 477 |
ecl_float_bits(float num) |
| 478 |
{ |
| 479 |
union { float f; uint32_t u; } fu = { .f = num }; |
| 480 |
return fu.u; |
| 481 |
} |
| 482 |
|
| 483 |
uint64_t |
| 484 |
ecl_double_bits(double num) |
| 485 |
{ |
| 486 |
union { double f; uint64_t u; } fu = { .f = num }; |
| 487 |
return fu.u; |
| 488 |
} |
| 489 |
|
| 490 |
float |
| 491 |
ecl_bits_float(uint32_t num) |
| 492 |
{ |
| 493 |
union { float f; uint32_t u; } fu = { .u = num }; |
| 494 |
return fu.f; |
| 495 |
} |
| 496 |
|
| 497 |
double |
| 498 |
ecl_bits_double(uint64_t num) |
| 499 |
{ |
| 500 |
union { double f; uint64_t u; } fu = { .u = num }; |
| 501 |
return fu.f; |
| 502 |
} |