Debian Codegraph @

src/c/number.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * number.d - constructing 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 #include <float.h>
16 #include <complex.h>
17 #include <limits.h>
18 #include <signal.h>
19 #define ECL_INCLUDE_MATH_H
20 #include <ecl/ecl.h>
21 #include <ecl/ecl-inl.h>
22 #include <ecl/internal.h>
23 #include <ecl/impl/math_fenv.h>
24
25 #if defined(ECL_IEEE_FP)
26 # if defined(ECL_AVOID_FPE_H)
27 /*
28 * We don't check for floating point exceptions
29 */
30 # define DO_DETECT_FPE(f)
31 # define DO_DETECT_FPE2(f1,f2)
32 # elif defined(HAVE_FEENABLEEXCEPT)
33 /*
34 * We are using IEEE arithmetics and can rely on FPE exceptions
35 * to be raised when invalid operations are performed.
36 */
37 # define DO_DETECT_FPE(f) ecl_detect_fpe()
38 # define DO_DETECT_FPE2(f1,f2) DO_DETECT_FPE(f1)
39 # else
40 /*
41 * We need explicit checks for floating point exception bits being
42 * set; ECL_MATHERR_TEST handles this for us, so nothing to do here.
43 */
44 # define DO_DETECT_FPE(f)
45 # define DO_DETECT_FPE2(f1,f2)
46 # endif
47 #else
48 /*
49 * We do not want IEEE NaNs and infinities
50 */
51 # define DO_DETECT_FPE(f) do { \
52 unlikely_if (isnan(f)) ecl_deliver_fpe(FE_INVALID); \
53 unlikely_if (!isfinite(f)) ecl_deliver_fpe(FE_OVERFLOW); \
54 } while (0)
55 # define DO_DETECT_FPE2(f1,f2) DO_DETECT_FPE(f1); DO_DETECT_FPE(f2)
56 #endif
57
58 #if !ECL_CAN_INLINE
59 cl_fixnum
60 ecl_to_fix(cl_object f)
61 {
62 if (ecl_unlikely(!ECL_FIXNUMP(f)))
63 FEtype_error_fixnum(f);
64 return ecl_fixnum(f);
65 }
66
67 cl_index
68 ecl_to_size(cl_object f)
69 {
70 cl_fixnum aux;
71 if (ecl_likely(ECL_FIXNUMP(f))) {
72 cl_fixnum aux = ecl_fixnum(f);
73 if (ecl_likely(aux >= 0))
74 return aux;
75 }
76 FEtype_error_size(f);
77 }
78 #endif /* !ECL_CAN_INLINE */
79
80 cl_object
81 ecl_make_integer(cl_fixnum l)
82 {
83 if (l > MOST_POSITIVE_FIXNUM || l < MOST_NEGATIVE_FIXNUM) {
84 cl_object z = _ecl_big_register0();
85 _ecl_big_set_fixnum(z, l);
86 return _ecl_big_register_copy(z);
87 }
88 return ecl_make_fixnum(l);
89 }
90
91 cl_object
92 ecl_make_unsigned_integer(cl_index l)
93 {
94 if (l > MOST_POSITIVE_FIXNUM) {
95 cl_object z = _ecl_big_register0();
96 _ecl_big_set_index(z, l);
97 return _ecl_big_register_copy(z);
98 }
99 return ecl_make_fixnum(l);
100 }
101
102 int
103 ecl_to_bit(cl_object x) {
104 if (ecl_unlikely((x != ecl_make_fixnum(0)) && (x != ecl_make_fixnum(1))))
105 FEwrong_type_nth_arg(@[coerce], 1, x, @[bit]);
106 return x == ecl_make_fixnum(1);
107 }
108
109 ecl_uint8_t
110 ecl_to_uint8_t(cl_object x) {
111 if (ecl_likely(ECL_FIXNUMP(x))) {
112 cl_fixnum aux = ecl_fixnum(x);
113 if (ecl_likely(aux >= 0 && aux <= 255))
114 return (ecl_uint8_t)aux;
115 }
116 FEwrong_type_argument(cl_list(2, @'unsigned-byte', ecl_make_fixnum(8)),
117 x);
118 }
119
120 ecl_int8_t
121 ecl_to_int8_t(cl_object x) {
122 if (ecl_likely(ECL_FIXNUMP(x))) {
123 cl_fixnum aux = ecl_fixnum(x);
124 if (ecl_likely(aux >= -128 && aux <= 127))
125 return (ecl_uint8_t)aux;
126 }
127 FEwrong_type_argument(cl_list(2, @'signed-byte', ecl_make_fixnum(8)),
128 x);
129 }
130
131 unsigned short
132 ecl_to_ushort(cl_object x) {
133 const unsigned short ushort_max = USHRT_MAX;
134 if (ecl_likely(ECL_FIXNUMP(x))) {
135 cl_fixnum y = ecl_fixnum(x);
136 if (ecl_likely(y >= 0 && y <= ushort_max)) {
137 return (unsigned short)y;
138 }
139 }
140 FEwrong_type_argument(cl_list(3,@'integer',
141 ecl_make_fixnum(0),
142 ecl_make_fixnum(ushort_max)),
143 x);
144 }
145
146 short
147 ecl_to_short(cl_object x) {
148 const short short_min = SHRT_MIN;
149 const short short_max = SHRT_MAX;
150 if (ecl_likely(ECL_FIXNUMP(x))) {
151 cl_fixnum y = ecl_fixnum(x);
152 if (ecl_likely(y >= short_min && y <= short_max)) {
153 return (short)y;
154 }
155 }
156 FEwrong_type_argument(cl_list(3,@'integer',
157 ecl_make_fixnum(short_min),
158 ecl_make_fixnum(short_max)),
159 x);
160 }
161
162 #if ECL_FIXNUM_BITS < 32
163 # error "Unsupported platform with cl_fixnum < ecl_uint32_t"
164 #endif
165
166 #ifdef ecl_uint16_t
167 ecl_uint16_t
168 ecl_to_uint16_t(cl_object x) {
169 const uint16_t uint16_max = 0xFFFFL;
170 if (ecl_likely(ECL_FIXNUMP(x))) {
171 cl_fixnum y = ecl_fixnum(x);
172 if (ecl_likely(y >= 0 && y <= uint16_max)) {
173 return (ecl_uint16_t)y;
174 }
175 }
176 FEwrong_type_argument(cl_list(3,@'integer',
177 ecl_make_fixnum(0),
178 ecl_make_fixnum(uint16_max)),
179 x);
180 }
181
182 ecl_int16_t
183 ecl_to_int16_t(cl_object x) {
184 const int16_t int16_min = -0x8000;
185 const int16_t int16_max = 0x7FFF;
186 if (ecl_likely(ECL_FIXNUMP(x))) {
187 cl_fixnum y = ecl_fixnum(x);
188 if (ecl_likely(y >= int16_min && y <= int16_max)) {
189 return (ecl_int16_t)y;
190 }
191 }
192 FEwrong_type_argument(cl_list(3,@'integer',
193 ecl_make_fixnum(int16_min),
194 ecl_make_fixnum(int16_max)),
195 x);
196 }
197 #endif /* ecl_uint16_t */
198
199 #if defined(ecl_uint32_t) && (ECL_FIXNUM_BITS > 32)
200 ecl_uint32_t
201 ecl_to_uint32_t(cl_object x) {
202 const uint32_t uint32_max = 0xFFFFFFFFUL;
203 if (ecl_likely(ECL_FIXNUMP(x))) {
204 cl_fixnum y = ecl_fixnum(x);
205 if (ecl_likely(y >= 0 && y <= uint32_max)) {
206 return (ecl_uint32_t)y;
207 }
208 }
209 FEwrong_type_argument(cl_list(3,@'integer',ecl_make_fixnum(0),
210 ecl_make_unsigned_integer(uint32_max)),
211 x);
212 }
213
214 ecl_int32_t
215 ecl_to_int32_t(cl_object x) {
216 const int32_t int32_min = -0x80000000L;
217 const int32_t int32_max = 0x7FFFFFFFL;
218 if (ecl_likely(ECL_FIXNUMP(x))) {
219 cl_fixnum y = ecl_fixnum(x);
220 if (ecl_likely(y >= int32_min && y <= int32_max)) {
221 return (ecl_int32_t)y;
222 }
223 }
224 FEwrong_type_argument(cl_list(3,@'integer',
225 ecl_make_integer(int32_min),
226 ecl_make_integer(int32_max)),
227 x);
228 }
229 #endif /* ecl_uint32_t */
230
231 #if defined(ecl_uint64_t) && (ECL_FIXNUM_BITS < 64)
232 ecl_uint64_t
233 ecl_to_uint64_t(cl_object x) {
234 if (!ecl_minusp(x)) {
235 if (ECL_FIXNUMP(x)) {
236 return (ecl_uint64_t)ecl_fixnum(x);
237 } else if (!ECL_BIGNUMP(x)) {
238 (void)0;
239 } else if (_ecl_big_fits_ui(x)) {
240 return (ecl_uint64_t)_ecl_big_get_ui(x);
241 } else {
242 cl_object copy = _ecl_big_register0();
243 _ecl_big_fdiv_q_2exp(copy, x, 32);
244 if (_ecl_big_fits_ui(copy)) {
245 ecl_uint64_t output;
246 output = (ecl_uint64_t)_ecl_big_get_ui(copy);
247 output = (output << 32) + (ecl_uint64_t)_ecl_big_get_ui(x);
248 _ecl_big_register_free(copy);
249 return output;
250 }
251 _ecl_big_register_free(copy);
252 }
253 }
254 FEwrong_type_argument(cl_list(3,@'integer',ecl_make_fixnum(0),
255 ecl_one_minus(ecl_ash(ecl_make_fixnum(1), 64))),
256 x);
257 }
258
259 ecl_int64_t
260 ecl_to_int64_t(cl_object x) {
261 if (ECL_FIXNUMP(x)) {
262 return (ecl_int64_t)ecl_fixnum(x);
263 } else if (!ECL_BIGNUMP(x)) {
264 (void)0;
265 } else if (_ecl_big_fits_si(x)) {
266 return (ecl_int64_t)_ecl_big_get_si(x);
267 } else {
268 cl_object copy = _ecl_big_register0();
269 _ecl_big_fdiv_q_2exp(copy, x, 32);
270 if (_ecl_big_fits_si(copy)) {
271 ecl_int64_t output;
272 output = (ecl_int64_t)_ecl_big_get_si(copy);
273 _ecl_big_fdiv_r_2exp(copy, x, 32);
274 output = (output << 32) + _ecl_big_get_ui(copy);
275 _ecl_big_register_free(copy);
276 return output;
277 }
278 _ecl_big_register_free(copy);
279 }
280 FEwrong_type_argument(cl_list(3,@'integer',
281 ecl_negate(ecl_ash(ecl_make_fixnum(1), 63)),
282 ecl_one_minus(ecl_ash(ecl_make_fixnum(1), 63))),
283 x);
284 }
285
286 cl_object
287 ecl_make_uint64_t(ecl_uint64_t i)
288 {
289 if (i <= MOST_POSITIVE_FIXNUM) {
290 return ecl_make_fixnum(i);
291 } else if (i <= ~(ecl_uint32_t)0) {
292 return ecl_make_uint32_t(i);
293 } else {
294 cl_object aux = ecl_make_uint32_t(i >> 32);
295 return cl_logior(2, ecl_ash(aux, 32),
296 ecl_make_uint32_t((ecl_uint32_t)i));
297 }
298 }
299
300 cl_object
301 ecl_make_int64_t(ecl_int64_t i)
302 {
303 if (i >= MOST_NEGATIVE_FIXNUM && i <= MOST_POSITIVE_FIXNUM) {
304 return ecl_make_fixnum(i);
305 } else {
306 cl_object aux = ecl_make_int32_t(i >> 32);
307 return cl_logior(2, ecl_ash(aux, 32), ecl_make_uint32_t((ecl_uint32_t)i));
308 }
309 }
310 #endif /* ecl_uint64_t */
311
312 #if defined(ecl_ulong_long_t)
313 # if defined(ecl_uint32_t) && ECL_LONG_LONG_BITS == 32
314 ecl_ulong_long_t
315 ecl_to_ulong_long(cl_object x) {
316 return (ecl_ulong_long_t)ecl_to_uint32_t(x);
317 }
318
319 ecl_long_long_t
320 ecl_to_long_long(cl_object x) {
321 return (ecl_long_long_t)ecl_to_int32_t(x);
322 }
323 cl_object
324 ecl_make_ulong_long(ecl_ulong_long_t i) {
325 return ecl_make_uint32_t(i);
326 }
327 cl_object
328 ecl_make_long_long(ecl_long_long_t i) {
329 return ecl_make_int32_t(i);
330 }
331 # else
332 # if defined(ecl_uint64_t) && ECL_LONG_LONG_BITS == 64
333 ecl_ulong_long_t
334 ecl_to_ulong_long(cl_object x) {
335 return (ecl_ulong_long_t)ecl_to_uint64_t(x);
336 }
337 ecl_long_long_t
338 ecl_to_long_long(cl_object x) {
339 return (ecl_long_long_t)ecl_to_int64_t(x);
340 }
341 cl_object
342 ecl_make_ulong_long(ecl_ulong_long_t i) {
343 return ecl_make_uint64_t(i);
344 }
345 cl_object
346 ecl_make_long_long(ecl_long_long_t i) {
347 return ecl_make_int64_t(i);
348 }
349 # else
350 ecl_ulong_long_t
351 ecl_to_ulong_long(cl_object x) {
352 if (!ecl_minusp(x)) {
353 if (ECL_FIXNUMP(x)) {
354 return (ecl_ulong_long_t)ecl_fixnum(x);
355 } else if (!ECL_BIGNUMP(x)) {
356 (void)0;
357 } else if (_ecl_big_fits_ui(x)) {
358 return (ecl_ulong_long_t)_ecl_big_get_ui(x);
359 } else {
360 cl_object copy = _ecl_big_register0();
361 int i = ECL_LONG_LONG_BITS - ECL_FIXNUM_BITS;
362 _ecl_big_fdiv_q_2exp(copy, x, i);
363 if (_ecl_big_fits_ui(copy)) {
364 ecl_ulong_long_t output;
365 output = _ecl_big_get_ui(copy);
366 for (i -= ECL_FIXNUM_BITS; i;
367 i-= ECL_FIXNUM_BITS) {
368 output = (output << ECL_FIXNUM_BITS);
369 output += _ecl_big_get_ui(x);
370 }
371 _ecl_big_register_free(copy);
372 return output;
373 }
374 _ecl_big_register_free(copy);
375 }
376 }
377 FEwrong_type_argument(cl_list(3,@'integer',ecl_make_fixnum(0),
378 ecl_one_minus(ecl_ash(ecl_make_fixnum(1),
379 ECL_LONG_LONG_BITS))),
380 x);
381 }
382
383 ecl_long_long_t
384 ecl_to_long_long(cl_object x)
385 {
386 if (ECL_FIXNUMP(x)) {
387 return (ecl_long_long_t)ecl_fixnum(x);
388 } else if (!ECL_BIGNUMP(x)) {
389 (void)0;
390 } else if (_ecl_big_fits_si(x)) {
391 return (ecl_long_long_t)_ecl_big_get_si(x);
392 } else {
393 cl_object copy = _ecl_big_register0();
394 int i = ECL_LONG_LONG_BITS - ECL_FIXNUM_BITS;
395 _ecl_big_fdiv_q_2exp(copy, x, i);
396 if (_ecl_big_fits_ui(copy)) {
397 ecl_long_long_t output;
398 output = _ecl_big_get_si(copy);
399 for (i -= ECL_FIXNUM_BITS; i; i-= ECL_FIXNUM_BITS) {
400 output = (output << ECL_FIXNUM_BITS);
401 output += _ecl_big_get_ui(x);
402 }
403 _ecl_big_register_free(copy);
404 return output;
405 }
406 _ecl_big_register_free(copy);
407 }
408 FEwrong_type_argument(cl_list(3,@'integer',
409 ecl_negate(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1)),
410 ecl_one_minus(ecl_ash(ecl_make_fixnum(1), ECL_LONG_LONG_BITS-1))),
411 x);
412 }
413
414 cl_object
415 ecl_make_ulong_long(ecl_ulong_long_t i)
416 {
417 if (i <= MOST_POSITIVE_FIXNUM) {
418 return ecl_make_fixnum(i);
419 } else if (i <= ~(ecl_uint32_t)0) {
420 return ecl_make_uint32_t(i);
421 } else {
422 cl_object aux = ecl_make_uint32_t(i >> 32);
423 return cl_logior(2, ecl_ash(aux, 32),
424 ecl_make_uint32_t((ecl_uint32_t)i));
425 }
426 }
427
428 cl_object
429 ecl_make_long_long(ecl_long_long_t i)
430 {
431 if (i >= MOST_NEGATIVE_FIXNUM && i <= MOST_POSITIVE_FIXNUM) {
432 return ecl_make_fixnum(i);
433 } else {
434 cl_object aux = ecl_make_int32_t(i >> 32);
435 return cl_logior(2, ecl_ash(aux, 32), ecl_make_uint32_t((ecl_uint32_t)i));
436 }
437 }
438 # endif
439 # endif
440 #endif /* ecl_ulong_long_t */
441
442 cl_object
443 ecl_make_ratio(cl_object num, cl_object den)
444 {
445 cl_object g, r;
446
447 /* INV: the arguments NUM & DEN are integers */
448 if (den == ecl_make_fixnum(0))
449 FEdivision_by_zero(num, den);
450 if (num == ecl_make_fixnum(0) || den == ecl_make_fixnum(1))
451 return(num);
452 if (ecl_minusp(den)) {
453 num = ecl_negate(num);
454 den = ecl_negate(den);
455 }
456 g = ecl_gcd(num, den);
457 if (g != ecl_make_fixnum(1)) {
458 num = ecl_integer_divide(num, g);
459 den = ecl_integer_divide(den, g);
460 }
461 if (den == ecl_make_fixnum(1))
462 return num;
463 if (den == ecl_make_fixnum(-1))
464 return ecl_negate(num);
465 r = ecl_alloc_object(t_ratio);
466 r->ratio.num = num;
467 r->ratio.den = den;
468 return(r);
469 }
470
471 void
472 ecl_deliver_fpe(int status)
473 {
474 cl_env_ptr env = ecl_process_env();
475 int bits = status & env->trap_fpe_bits;
476 feclearexcept(FE_ALL_EXCEPT);
477 if (bits) {
478 cl_object condition;
479 if (bits & FE_DIVBYZERO)
480 condition = @'division-by-zero';
481 else if (bits & FE_INVALID)
482 condition = @'floating-point-invalid-operation';
483 else if (bits & FE_OVERFLOW)
484 condition = @'floating-point-overflow';
485 else if (bits & FE_UNDERFLOW)
486 condition = @'floating-point-underflow';
487 else if (bits & FE_INEXACT)
488 condition = @'floating-point-inexact';
489 else
490 condition = @'arithmetic-error';
491 cl_error(1, condition);
492 }
493 }
494
495 cl_object
496 ecl_make_single_float(float f)
497 {
498 cl_object x;
499
500 DO_DETECT_FPE(f);
501 if (f == (float)0.0) {
502 #if defined(ECL_SIGNED_ZERO)
503 if (signbit(f))
504 return cl_core.singlefloat_minus_zero;
505 #endif
506 return cl_core.singlefloat_zero;
507 }
508 x = ecl_alloc_object(t_singlefloat);
509 ecl_single_float(x) = f;
510 return(x);
511 }
512
513 cl_object
514 ecl_make_double_float(double f)
515 {
516 cl_object x;
517
518 DO_DETECT_FPE(f);
519 if (f == (double)0.0) {
520 #if defined(ECL_SIGNED_ZERO)
521 if (signbit(f))
522 return cl_core.doublefloat_minus_zero;
523 #endif
524 return cl_core.doublefloat_zero;
525 }
526 x = ecl_alloc_object(t_doublefloat);
527 ecl_double_float(x) = f;
528 return(x);
529 }
530
531 cl_object
532 ecl_make_long_float(long double f)
533 {
534 cl_object x;
535
536 DO_DETECT_FPE(f);
537 if (f == (long double)0.0) {
538 #if defined(ECL_SIGNED_ZERO)
539 if (signbit(f))
540 return cl_core.longfloat_minus_zero;
541 #endif
542 return cl_core.longfloat_zero;
543 }
544 x = ecl_alloc_object(t_longfloat);
545 x->longfloat.value = f;
546 return x;
547 }
548
549 cl_object
550 ecl_make_complex(cl_object r, cl_object i)
551 {
552 cl_object c = ECL_NIL;
553 cl_type tr = ecl_t_of(r);
554 cl_type ti = ecl_t_of(i);
555 if (!ECL_REAL_TYPE_P(tr)) { ecl_type_error(@'complex', "real part", r, @'real'); }
556 if (!ECL_REAL_TYPE_P(ti)) { ecl_type_error(@'complex', "imaginary part", i, @'real'); }
557 switch((tr > ti) ? tr : ti) {
558 #ifdef ECL_COMPLEX_FLOAT
559 case t_longfloat: return ecl_make_clfloat(CMPLXL(ecl_to_long_double(r), ecl_to_long_double(i)));
560 case t_doublefloat: return ecl_make_cdfloat(CMPLX(ecl_to_double(r), ecl_to_double(i)));
561 case t_singlefloat: return ecl_make_csfloat(CMPLXF(ecl_to_float(r), ecl_to_float(i)));
562 #else
563 case t_singlefloat:
564 c = ecl_alloc_object(t_complex);
565 c->gencomplex.real = ecl_make_single_float(ecl_to_float(r));
566 c->gencomplex.imag = ecl_make_single_float(ecl_to_float(i));
567 return c;
568 case t_doublefloat:
569 c = ecl_alloc_object(t_complex);
570 c->gencomplex.real = ecl_make_double_float(ecl_to_double(r));
571 c->gencomplex.imag = ecl_make_double_float(ecl_to_double(i));
572 return c;
573 case t_longfloat:
574 c = ecl_alloc_object(t_complex);
575 c->gencomplex.real = ecl_make_long_float(ecl_to_long_double(r));
576 c->gencomplex.imag = ecl_make_long_float(ecl_to_long_double(i));
577 return c;
578 #endif
579 case t_fixnum:
580 case t_bignum:
581 case t_ratio:
582 if (i == ecl_make_fixnum(0))
583 return r;
584 c = ecl_alloc_object(t_complex);
585 c->gencomplex.real = r;
586 c->gencomplex.imag = i;
587 return c;
588 default:
589 FEerror("ecl_make_complex: unexpected argument type.", 0);
590 }
591 return(c);
592 }
593
594 #ifdef ECL_COMPLEX_FLOAT
595 /* This function is safe. Still both arguments must be of the same
596 float type, otherwise a type error will be signalled. -- jd 2019-04-03 */
597
598 cl_object si_complex_float_p(cl_object f) {
599 switch(ecl_t_of(f)) {
600 case t_csfloat:
601 case t_cdfloat:
602 case t_clfloat:
603 return ECL_T;
604 default:
605 return ECL_NIL;
606 }
607 }
608
609 cl_object
610 si_complex_float(cl_object r, cl_object i)
611 {
612 cl_type tr = ecl_t_of(r);
613 cl_type ti = ecl_t_of(i);
614 cl_object result = OBJNULL;
615 switch (tr) {
616 case t_singlefloat:
617 if (ti != tr) { ecl_type_error(@'si::complex-float',"imag part", i, @'single-float'); }
618 result = ecl_alloc_object(t_csfloat);
619 ecl_csfloat(result) = CMPLXF(ecl_single_float(r), ecl_single_float(i));
620 break;
621 case t_doublefloat:
622 if (ti != tr) { ecl_type_error(@'si::complex-float',"imag part", i, @'double-float'); }
623 result = ecl_alloc_object(t_cdfloat);
624 ecl_cdfloat(result) = CMPLX(ecl_double_float(r), ecl_double_float(i));
625 break;
626 case t_longfloat:
627 if (ti != tr) { ecl_type_error(@'si::complex-float',"imag part", i, @'long-float'); }
628 result = ecl_alloc_object(t_clfloat);
629 ecl_clfloat(result) = CMPLXL(ecl_long_float(r), ecl_long_float(i));
630 break;
631 default:
632 ecl_type_error(@'si::complex-float',"real part", r, @'float');
633 }
634 return result;
635 }
636
637 cl_object ecl_make_csfloat(float _Complex x) {
638 DO_DETECT_FPE2(crealf(x), cimagf(x));
639
640 cl_object c = ecl_alloc_object(t_csfloat);
641 ecl_csfloat(c) = x;
642 return c;
643 }
644
645 cl_object ecl_make_cdfloat(double _Complex x) {
646 DO_DETECT_FPE2(creal(x), cimag(x));
647
648 cl_object c = ecl_alloc_object(t_cdfloat);
649 ecl_cdfloat(c) = x;
650 return c;
651 }
652
653 cl_object ecl_make_clfloat(long double _Complex x) {
654 DO_DETECT_FPE2(creall(x), cimagl(x));
655
656 cl_object c = ecl_alloc_object(t_clfloat);
657 ecl_clfloat(c) = x;
658 return c;
659 }
660 #endif
661
662 static cl_object
663 mantissa_and_exponent_from_ratio(cl_object num, cl_object den, int digits, cl_fixnum *exponent)
664 {
665 /* We have to cook our own routine because GMP does not round. The
666 * recipe is simple: we multiply the numerator by a large enough
667 * number so that the integer length of the division by the
668 * denominator is equal to the number of digits of the mantissa of
669 * the floating point number. The result is scaled back by the
670 * appropriate exponent.
671 */
672 bool negative = 0;
673 if (ecl_minusp(num)) {
674 negative = 1;
675 num = ecl_negate(num);
676 }
677 cl_fixnum num_digits = ecl_integer_length(num);
678 cl_fixnum den_digits = ecl_integer_length(den);
679 cl_fixnum scale = digits+1 - (num_digits - den_digits);
680 /* Scale the numerator in the correct range so that the quotient
681 * truncated to an integer has a length of digits+1 or digits+2. If
682 * scale is negative, we simply shift out unnecessary digits of num,
683 * which don't affect the quotient. */
684 num = ecl_ash(num, scale);
685 cl_object quotient = ecl_integer_divide(num, den);
686 if (ecl_integer_length(quotient) > digits+1) {
687 /* quotient is too large, shift out an unnecessary digit */
688 scale--;
689 quotient = ecl_ash(quotient, -1);
690 }
691 /* round quotient */
692 if (ecl_oddp(quotient)) {
693 quotient = ecl_one_plus(quotient);
694 }
695 /* shift out the remaining unnecessary digit of quotient */
696 quotient = ecl_ash(quotient, -1);
697 /* fix the sign */
698 if (negative) {
699 quotient = ecl_negate(quotient);
700 }
701 *exponent = 1 - scale;
702 return quotient;
703 }
704
705 static float
706 ratio_to_float(cl_object num, cl_object den)
707 {
708 cl_fixnum exponent;
709 cl_object mantissa = mantissa_and_exponent_from_ratio(num, den, FLT_MANT_DIG, &exponent);
710 #if (FIXNUM_BITS-ECL_TAG_BITS) >= FLT_MANT_DIG
711 /* The output of mantissa_and_exponent_from_ratio will always fit an integer */
712 double output = ecl_fixnum(mantissa);
713 #else
714 double output = ECL_FIXNUMP(mantissa)
715 ? ecl_fixnum(mantissa)
716 : _ecl_big_to_double(mantissa);
717 #endif
718 return ldexpf(output, exponent);
719 }
720
721 static double
722 ratio_to_double(cl_object num, cl_object den)
723 {
724 cl_fixnum exponent;
725 cl_object mantissa = mantissa_and_exponent_from_ratio(num, den, DBL_MANT_DIG, &exponent);
726 #if (FIXNUM_BITS-ECL_TAG_BITS) >= DBL_MANT_DIG
727 /* The output of mantissa_and_exponent_from_ratio will always fit an integer */
728 double output = ecl_fixnum(mantissa);
729 #else
730 double output = ECL_FIXNUMP(mantissa)
731 ? ecl_fixnum(mantissa)
732 : _ecl_big_to_double(mantissa);
733 #endif
734 return ldexp(output, exponent);
735 }
736
737 static long double
738 ratio_to_long_double(cl_object num, cl_object den)
739 {
740 cl_fixnum exponent;
741 cl_object mantissa = mantissa_and_exponent_from_ratio(num, den, LDBL_MANT_DIG, &exponent);
742 #if (FIXNUM_BITS-ECL_TAG_BITS) >= LDBL_MANT_DIG
743 /* The output of mantissa_and_exponent_from_ratio will always fit an integer */
744 long double output = ecl_fixnum(mantissa);
745 #else
746 long double output = ECL_FIXNUMP(mantissa)
747 ? ecl_fixnum(mantissa)
748 : _ecl_big_to_long_double(mantissa);
749 #endif
750 return ldexpl(output, exponent);
751 }
752
753 float
754 ecl_to_float(cl_object x)
755 {
756 if (ECL_FIXNUMP(x)) return(ecl_fixnum(x)); /* Immediate fixnum */
757
758 switch (ecl_t_of(x)) {
759 case t_fixnum:
760 return (float)ecl_fixnum(x);
761 case t_bignum:
762 return ratio_to_float(x, ecl_make_fixnum(1));
763 case t_ratio:
764 return ratio_to_float(x->ratio.num, x->ratio.den);
765 case t_singlefloat:
766 return ecl_single_float(x);
767 case t_doublefloat:
768 return (float)ecl_double_float(x);
769 case t_longfloat:
770 return (float)ecl_long_float(x);
771 default:
772 FEwrong_type_nth_arg(@[coerce], 1, x, @[real]);
773 }
774 }
775
776 double
777 ecl_to_double(cl_object x)
778 {
779 switch(ecl_t_of(x)) {
780 case t_fixnum:
781 return((double)(ecl_fixnum(x)));
782 case t_bignum:
783 return ratio_to_double(x, ecl_make_fixnum(1));
784 case t_ratio:
785 return ratio_to_double(x->ratio.num, x->ratio.den);
786 case t_singlefloat:
787 return (double)ecl_single_float(x);
788 case t_doublefloat:
789 return(ecl_double_float(x));
790 case t_longfloat:
791 return (double)ecl_long_float(x);
792 default:
793 FEwrong_type_nth_arg(@[coerce], 1, x, @[real]);
794 }
795 }
796
797 long double
798 ecl_to_long_double(cl_object x)
799 {
800 switch(ecl_t_of(x)) {
801 case t_fixnum:
802 return (long double)ecl_fixnum(x);
803 case t_bignum:
804 return ratio_to_long_double(x, ecl_make_fixnum(1));
805 case t_ratio:
806 return ratio_to_long_double(x->ratio.num, x->ratio.den);
807 case t_singlefloat:
808 return (long double)ecl_single_float(x);
809 case t_doublefloat:
810 return (long double)ecl_double_float(x);
811 case t_longfloat:
812 return ecl_long_float(x);
813 default:
814 FEwrong_type_nth_arg(@[coerce], 1, x, @[real]);
815 }
816 }
817
818 #ifdef ECL_COMPLEX_FLOAT
819 float _Complex ecl_to_csfloat(cl_object x) {
820 switch(ecl_t_of(x)) {
821 case t_fixnum:
822 case t_bignum:
823 case t_ratio:
824 case t_singlefloat:
825 case t_doublefloat:
826 case t_longfloat: {
827 return ecl_to_float(x);
828 }
829 case t_complex: {
830 return ecl_to_float(x->gencomplex.real) + I * ecl_to_float(x->gencomplex.imag);
831 }
832 case t_csfloat: return ecl_csfloat(x);
833 case t_cdfloat: return ecl_cdfloat(x);
834 case t_clfloat: return ecl_clfloat(x);
835 default:
836 FEwrong_type_nth_arg(@[coerce], 1, x, @[number]);
837 }
838 }
839
840 double _Complex ecl_to_cdfloat(cl_object x) {
841 switch(ecl_t_of(x)) {
842 case t_fixnum:
843 case t_bignum:
844 case t_ratio:
845 case t_singlefloat:
846 case t_doublefloat:
847 case t_longfloat:
848 return ecl_to_double(x);
849 case t_complex:
850 return ecl_to_double(x->gencomplex.real) + I * ecl_to_double(x->gencomplex.imag);
851 case t_csfloat: return ecl_csfloat(x);
852 case t_cdfloat: return ecl_cdfloat(x);
853 case t_clfloat: return ecl_clfloat(x);
854 default:
855 FEwrong_type_nth_arg(@[coerce], 1, x, @[number]);
856 }
857 }
858
859 long double _Complex ecl_to_clfloat(cl_object x) {
860 switch(ecl_t_of(x)) {
861 case t_fixnum:
862 case t_bignum:
863 case t_ratio:
864 case t_singlefloat:
865 case t_doublefloat:
866 case t_longfloat:
867 return ecl_to_long_double(x);
868 case t_complex:
869 return ecl_to_long_double(x->gencomplex.real) + I * ecl_to_long_double(x->gencomplex.imag);
870 case t_csfloat: return ecl_csfloat(x);
871 case t_cdfloat: return ecl_cdfloat(x);
872 case t_clfloat: return ecl_clfloat(x);
873 default:
874 FEwrong_type_nth_arg(@[coerce], 1, x, @[number]);
875 }
876 }
877 #endif
878
879 cl_object
880 cl_rational(cl_object x)
881 {
882 double d;
883 AGAIN:
884 switch (ecl_t_of(x)) {
885 case t_fixnum:
886 case t_bignum:
887 case t_ratio:
888 break;
889 case t_singlefloat:
890 d = ecl_single_float(x);
891 goto GO_ON;
892 case t_doublefloat:
893 d = ecl_double_float(x);
894 GO_ON: if (d == 0) {
895 x = ecl_make_fixnum(0);
896 } else {
897 int e;
898 d = frexp(d, &e);
899 e -= DBL_MANT_DIG;
900 x = _ecl_double_to_integer(ldexp(d, DBL_MANT_DIG));
901 if (e != 0) {
902 x = ecl_times(ecl_expt(ecl_make_fixnum(FLT_RADIX),
903 ecl_make_fixnum(e)),
904 x);
905 }
906 }
907 break;
908 case t_longfloat: {
909 long double d = ecl_long_float(x);
910 if (d == 0) {
911 x = ecl_make_fixnum(0);
912 } else {
913 int e;
914 d = frexpl(d, &e);
915 e -= LDBL_MANT_DIG;
916 d = ldexpl(d, LDBL_MANT_DIG);
917 x = _ecl_long_double_to_integer(d);
918 if (e != 0) {
919 x = ecl_times(ecl_expt(ecl_make_fixnum(FLT_RADIX),
920 ecl_make_fixnum(e)),
921 x);
922 }
923 }
924 break;
925 }
926 default:
927 x = ecl_type_error(@'rational',"argument",x,@'number');
928 goto AGAIN;
929 }
930 @(return x);
931 }
932
933 cl_object
934 _ecl_long_double_to_integer(long double d0)
935 {
936 const int fb = ECL_FIXNUM_BITS - 3;
937 int e;
938 long double d = frexpl(d0, &e);
939 if (e <= fb) {
940 return ecl_make_fixnum((cl_fixnum)d0);
941 } else if (e > LDBL_MANT_DIG) {
942 return ecl_ash(_ecl_long_double_to_integer(ldexp(d, LDBL_MANT_DIG)),
943 e - LDBL_MANT_DIG);
944 } else {
945 long double d1 = floorl(d = ldexpl(d, fb));
946 int newe = e - fb;
947 cl_object o = ecl_ash(_ecl_long_double_to_integer(d1), newe);
948 long double d2 = ldexpl(d - d1, newe);
949 if (d2) o = ecl_plus(o, _ecl_long_double_to_integer(d2));
950 return o;
951 }
952 }
953
954 cl_object
955 _ecl_double_to_integer(double d)
956 {
957 if (d <= MOST_POSITIVE_FIXNUM && d >= MOST_NEGATIVE_FIXNUM)
958 return ecl_make_fixnum((cl_fixnum)d);
959 else {
960 cl_object z = _ecl_big_register0();
961 _ecl_big_set_d(z, d);
962 return _ecl_big_register_copy(z);
963 }
964 }
965
966 cl_object
967 _ecl_float_to_integer(float d)
968 {
969 if (d <= MOST_POSITIVE_FIXNUM && d >= MOST_NEGATIVE_FIXNUM)
970 return ecl_make_fixnum((cl_fixnum)d);
971 else {
972 cl_object z = _ecl_big_register0();
973 _ecl_big_set_d(z, d);
974 return _ecl_big_register_copy(z);
975 }
976 }
977
978 #ifdef ECL_IEEE_FP
979 cl_object
980 si_nan() {
981 cl_object x = ecl_alloc_object(t_doublefloat);
982 ecl_double_float(x) = NAN;
983 return x;
984 }
985 #endif /* ECL_IEEE_FP */