* target.h (struct gcc_target): Add new field to struct cxx: import_export_class.
[official-gcc.git] / gcc / fortran / arith.c
blobb6aec5b951ddb4aa9c54078def2e2bfb263000c2
1 /* Compiler arithmetic
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
3 Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* Since target arithmetic must be done on the host, there has to
24 be some way of evaluating arithmetic expressions as the host
25 would evaluate them. We use the GNU MP library to do arithmetic,
26 and this file provides the interface. */
28 #include "config.h"
30 #include <string.h>
32 #include "gfortran.h"
33 #include "arith.h"
35 mpf_t pi, half_pi, two_pi, e;
37 /* The gfc_(integer|real)_kinds[] structures have everything the front
38 end needs to know about integers and real numbers on the target.
39 Other entries of the structure are calculated from these values.
40 The first entry is the default kind, the second entry of the real
41 structure is the default double kind. */
43 #define MPZ_NULL {{0,0,0}}
44 #define MPF_NULL {{0,0,0,0}}
46 #define DEF_GFC_INTEGER_KIND(KIND,RADIX,DIGITS,BIT_SIZE) \
47 {KIND, RADIX, DIGITS, BIT_SIZE, 0, MPZ_NULL, MPZ_NULL, MPZ_NULL}
49 #define DEF_GFC_LOGICAL_KIND(KIND,BIT_SIZE) \
50 {KIND, BIT_SIZE}
52 #define DEF_GFC_REAL_KIND(KIND,RADIX,DIGITS,MIN_EXP, MAX_EXP) \
53 {KIND, RADIX, DIGITS, MIN_EXP, MAX_EXP, \
54 0, 0, MPF_NULL, MPF_NULL, MPF_NULL}
56 gfc_integer_info gfc_integer_kinds[] = {
57 DEF_GFC_INTEGER_KIND (4, 2, 31, 32),
58 DEF_GFC_INTEGER_KIND (8, 2, 63, 64),
59 DEF_GFC_INTEGER_KIND (2, 2, 15, 16),
60 DEF_GFC_INTEGER_KIND (1, 2, 7, 8),
61 DEF_GFC_INTEGER_KIND (0, 0, 0, 0)
64 gfc_logical_info gfc_logical_kinds[] = {
65 DEF_GFC_LOGICAL_KIND (4, 32),
66 DEF_GFC_LOGICAL_KIND (8, 64),
67 DEF_GFC_LOGICAL_KIND (2, 16),
68 DEF_GFC_LOGICAL_KIND (1, 8),
69 DEF_GFC_LOGICAL_KIND (0, 0)
72 gfc_real_info gfc_real_kinds[] = {
73 DEF_GFC_REAL_KIND (4, 2, 24, -125, 128),
74 DEF_GFC_REAL_KIND (8, 2, 53, -1021, 1024),
75 DEF_GFC_REAL_KIND (0, 0, 0, 0, 0)
79 /* The integer kind to use for array indices. This will be set to the
80 proper value based on target information from the backend. */
82 int gfc_index_integer_kind;
85 /* Compute the natural log of arg.
87 We first get the argument into the range 0.5 to 1.5 by successive
88 multiplications or divisions by e. Then we use the series:
90 ln(x) = (x-1) - (x-1)^2/2 + (x-1)^3/3 - (x-1)^4/4 + ...
92 Because we are expanding in powers of (x-1), and 0.5 < x < 1.5, we
93 have -0.5 < (x-1) < 0.5. Ignoring the harmonic term, this means
94 that each term is at most 1/(2^i), meaning one bit is gained per
95 iteration.
97 Not very efficient, but it doesn't have to be. */
99 void
100 natural_logarithm (mpf_t * arg, mpf_t * result)
102 mpf_t x, xp, t, log;
103 int i, p;
105 mpf_init_set (x, *arg);
106 mpf_init (t);
108 p = 0;
110 mpf_set_str (t, "0.5", 10);
111 while (mpf_cmp (x, t) < 0)
113 mpf_mul (x, x, e);
114 p--;
117 mpf_set_str (t, "1.5", 10);
118 while (mpf_cmp (x, t) > 0)
120 mpf_div (x, x, e);
121 p++;
124 mpf_sub_ui (x, x, 1);
125 mpf_init_set_ui (log, 0);
126 mpf_init_set_ui (xp, 1);
128 for (i = 1; i < GFC_REAL_BITS; i++)
130 mpf_mul (xp, xp, x);
131 mpf_div_ui (t, xp, i);
133 if (i % 2 == 0)
134 mpf_sub (log, log, t);
135 else
136 mpf_add (log, log, t);
139 /* Add in the log (e^p) = p */
141 if (p < 0)
142 mpf_sub_ui (log, log, -p);
143 else
144 mpf_add_ui (log, log, p);
146 mpf_clear (x);
147 mpf_clear (xp);
148 mpf_clear (t);
150 mpf_set (*result, log);
151 mpf_clear (log);
155 /* Calculate the common logarithm of arg. We use the natural
156 logarithm of arg and of 10:
158 log10(arg) = log(arg)/log(10) */
160 void
161 common_logarithm (mpf_t * arg, mpf_t * result)
163 mpf_t i10, log10;
165 natural_logarithm (arg, result);
167 mpf_init_set_ui (i10, 10);
168 mpf_init (log10);
169 natural_logarithm (&i10, &log10);
171 mpf_div (*result, *result, log10);
172 mpf_clear (i10);
173 mpf_clear (log10);
176 /* Calculate exp(arg).
178 We use a reduction of the form
180 x = Nln2 + r
182 Then we obtain exp(r) from the Maclaurin series.
183 exp(x) is then recovered from the identity
185 exp(x) = 2^N*exp(r). */
187 void
188 exponential (mpf_t * arg, mpf_t * result)
190 mpf_t two, ln2, power, q, r, num, denom, term, x, xp;
191 int i;
192 long n;
193 unsigned long p, mp;
196 mpf_init_set (x, *arg);
198 if (mpf_cmp_ui (x, 0) == 0)
200 mpf_set_ui (*result, 1);
202 else if (mpf_cmp_ui (x, 1) == 0)
204 mpf_set (*result, e);
206 else
208 mpf_init_set_ui (two, 2);
209 mpf_init (ln2);
210 mpf_init (q);
211 mpf_init (r);
212 mpf_init (power);
213 mpf_init (term);
215 natural_logarithm (&two, &ln2);
217 mpf_div (q, x, ln2);
218 mpf_floor (power, q);
219 mpf_mul (q, power, ln2);
220 mpf_sub (r, x, q);
222 mpf_init_set_ui (xp, 1);
223 mpf_init_set_ui (num, 1);
224 mpf_init_set_ui (denom, 1);
226 for (i = 1; i <= GFC_REAL_BITS + 10; i++)
228 mpf_mul (num, num, r);
229 mpf_mul_ui (denom, denom, i);
230 mpf_div (term, num, denom);
231 mpf_add (xp, xp, term);
234 /* Reconstruction step */
235 n = (long) mpf_get_d (power);
237 if (n > 0)
239 p = (unsigned int) n;
240 mpf_mul_2exp (*result, xp, p);
242 else
244 mp = (unsigned int) (-n);
245 mpf_div_2exp (*result, xp, mp);
248 mpf_clear (two);
249 mpf_clear (ln2);
250 mpf_clear (q);
251 mpf_clear (r);
252 mpf_clear (power);
253 mpf_clear (num);
254 mpf_clear (denom);
255 mpf_clear (term);
256 mpf_clear (xp);
259 mpf_clear (x);
263 /* Calculate sin(arg).
265 We use a reduction of the form
267 x= N*2pi + r
269 Then we obtain sin(r) from the Maclaurin series. */
271 void
272 sine (mpf_t * arg, mpf_t * result)
274 mpf_t factor, q, r, num, denom, term, x, xp;
275 int i, sign;
277 mpf_init_set (x, *arg);
279 /* Special case (we do not treat multiples of pi due to roundoff issues) */
280 if (mpf_cmp_ui (x, 0) == 0)
282 mpf_set_ui (*result, 0);
284 else
286 mpf_init (q);
287 mpf_init (r);
288 mpf_init (factor);
289 mpf_init (term);
291 mpf_div (q, x, two_pi);
292 mpf_floor (factor, q);
293 mpf_mul (q, factor, two_pi);
294 mpf_sub (r, x, q);
296 mpf_init_set_ui (xp, 0);
297 mpf_init_set_ui (num, 1);
298 mpf_init_set_ui (denom, 1);
300 sign = -1;
301 for (i = 1; i < GFC_REAL_BITS + 10; i++)
303 mpf_mul (num, num, r);
304 mpf_mul_ui (denom, denom, i);
305 if (i % 2 == 0)
306 continue;
308 sign = -sign;
309 mpf_div (term, num, denom);
310 if (sign > 0)
311 mpf_add (xp, xp, term);
312 else
313 mpf_sub (xp, xp, term);
316 mpf_set (*result, xp);
318 mpf_clear (q);
319 mpf_clear (r);
320 mpf_clear (factor);
321 mpf_clear (num);
322 mpf_clear (denom);
323 mpf_clear (term);
324 mpf_clear (xp);
327 mpf_clear (x);
331 /* Calculate cos(arg).
333 Similar to sine. */
335 void
336 cosine (mpf_t * arg, mpf_t * result)
338 mpf_t factor, q, r, num, denom, term, x, xp;
339 int i, sign;
341 mpf_init_set (x, *arg);
343 /* Special case (we do not treat multiples of pi due to roundoff issues) */
344 if (mpf_cmp_ui (x, 0) == 0)
346 mpf_set_ui (*result, 1);
348 else
350 mpf_init (q);
351 mpf_init (r);
352 mpf_init (factor);
353 mpf_init (term);
355 mpf_div (q, x, two_pi);
356 mpf_floor (factor, q);
357 mpf_mul (q, factor, two_pi);
358 mpf_sub (r, x, q);
360 mpf_init_set_ui (xp, 1);
361 mpf_init_set_ui (num, 1);
362 mpf_init_set_ui (denom, 1);
364 sign = 1;
365 for (i = 1; i < GFC_REAL_BITS + 10; i++)
367 mpf_mul (num, num, r);
368 mpf_mul_ui (denom, denom, i);
369 if (i % 2 != 0)
370 continue;
372 sign = -sign;
373 mpf_div (term, num, denom);
374 if (sign > 0)
375 mpf_add (xp, xp, term);
376 else
377 mpf_sub (xp, xp, term);
379 mpf_set (*result, xp);
381 mpf_clear (q);
382 mpf_clear (r);
383 mpf_clear (factor);
384 mpf_clear (num);
385 mpf_clear (denom);
386 mpf_clear (term);
387 mpf_clear (xp);
390 mpf_clear (x);
394 /* Calculate atan(arg).
396 Similar to sine but requires special handling for x near 1. */
398 void
399 arctangent (mpf_t * arg, mpf_t * result)
401 mpf_t absval, convgu, convgl, num, term, x, xp;
402 int i, sign;
404 mpf_init_set (x, *arg);
406 /* Special cases */
407 if (mpf_cmp_ui (x, 0) == 0)
409 mpf_set_ui (*result, 0);
411 else if (mpf_cmp_ui (x, 1) == 0)
413 mpf_init (num);
414 mpf_div_ui (num, half_pi, 2);
415 mpf_set (*result, num);
416 mpf_clear (num);
418 else if (mpf_cmp_si (x, -1) == 0)
420 mpf_init (num);
421 mpf_div_ui (num, half_pi, 2);
422 mpf_neg (*result, num);
423 mpf_clear (num);
425 else
426 { /* General cases */
428 mpf_init (absval);
429 mpf_abs (absval, x);
431 mpf_init_set_d (convgu, 1.5);
432 mpf_init_set_d (convgl, 0.5);
433 mpf_init_set_ui (num, 1);
434 mpf_init (term);
436 if (mpf_cmp (absval, convgl) < 0)
438 mpf_init_set_ui (xp, 0);
439 sign = -1;
440 for (i = 1; i < GFC_REAL_BITS + 10; i++)
442 mpf_mul (num, num, absval);
443 if (i % 2 == 0)
444 continue;
446 sign = -sign;
447 mpf_div_ui (term, num, i);
448 if (sign > 0)
449 mpf_add (xp, xp, term);
450 else
451 mpf_sub (xp, xp, term);
454 else if (mpf_cmp (absval, convgu) >= 0)
456 mpf_init_set (xp, half_pi);
457 sign = 1;
458 for (i = 1; i < GFC_REAL_BITS + 10; i++)
460 mpf_div (num, num, absval);
461 if (i % 2 == 0)
462 continue;
464 sign = -sign;
465 mpf_div_ui (term, num, i);
466 if (sign > 0)
467 mpf_add (xp, xp, term);
468 else
469 mpf_sub (xp, xp, term);
472 else
474 mpf_init_set_ui (xp, 0);
476 mpf_sub_ui (num, absval, 1);
477 mpf_add_ui (term, absval, 1);
478 mpf_div (absval, num, term);
480 mpf_set_ui (num, 1);
482 sign = -1;
483 for (i = 1; i < GFC_REAL_BITS + 10; i++)
485 mpf_mul (num, num, absval);
486 if (i % 2 == 0)
487 continue;
488 sign = -sign;
489 mpf_div_ui (term, num, i);
490 if (sign > 0)
491 mpf_add (xp, xp, term);
492 else
493 mpf_sub (xp, xp, term);
496 mpf_div_ui (term, half_pi, 2);
497 mpf_add (xp, term, xp);
500 /* This makes sure to preserve the identity arctan(-x) = -arctan(x)
501 and improves accuracy to boot. */
503 if (mpf_cmp_ui (x, 0) > 0)
504 mpf_set (*result, xp);
505 else
506 mpf_neg (*result, xp);
508 mpf_clear (absval);
509 mpf_clear (convgl);
510 mpf_clear (convgu);
511 mpf_clear (num);
512 mpf_clear (term);
513 mpf_clear (xp);
515 mpf_clear (x);
519 /* Calculate atan2 (y, x)
521 atan2(y, x) = atan(y/x) if x > 0,
522 sign(y)*(pi - atan(|y/x|)) if x < 0,
523 0 if x = 0 && y == 0,
524 sign(y)*pi/2 if x = 0 && y != 0.
527 void
528 arctangent2 (mpf_t * y, mpf_t * x, mpf_t * result)
530 mpf_t t;
532 mpf_init (t);
534 switch (mpf_sgn (*x))
536 case 1:
537 mpf_div (t, *y, *x);
538 arctangent (&t, result);
539 break;
540 case -1:
541 mpf_div (t, *y, *x);
542 mpf_abs (t, t);
543 arctangent (&t, &t);
544 mpf_sub (*result, pi, t);
545 if (mpf_sgn (*y) == -1)
546 mpf_neg (*result, *result);
547 break;
548 case 0:
549 if (mpf_sgn (*y) == 0)
550 mpf_set_ui (*result, 0);
551 else
553 mpf_set (*result, half_pi);
554 if (mpf_sgn (*y) == -1)
555 mpf_neg (*result, *result);
557 break;
559 mpf_clear (t);
562 /* Calculate cosh(arg). */
564 void
565 hypercos (mpf_t * arg, mpf_t * result)
567 mpf_t neg, term1, term2, x, xp;
569 mpf_init_set (x, *arg);
571 mpf_init (neg);
572 mpf_init (term1);
573 mpf_init (term2);
574 mpf_init (xp);
576 mpf_neg (neg, x);
578 exponential (&x, &term1);
579 exponential (&neg, &term2);
581 mpf_add (xp, term1, term2);
582 mpf_div_ui (*result, xp, 2);
584 mpf_clear (neg);
585 mpf_clear (term1);
586 mpf_clear (term2);
587 mpf_clear (x);
588 mpf_clear (xp);
592 /* Calculate sinh(arg). */
594 void
595 hypersine (mpf_t * arg, mpf_t * result)
597 mpf_t neg, term1, term2, x, xp;
599 mpf_init_set (x, *arg);
601 mpf_init (neg);
602 mpf_init (term1);
603 mpf_init (term2);
604 mpf_init (xp);
606 mpf_neg (neg, x);
608 exponential (&x, &term1);
609 exponential (&neg, &term2);
611 mpf_sub (xp, term1, term2);
612 mpf_div_ui (*result, xp, 2);
614 mpf_clear (neg);
615 mpf_clear (term1);
616 mpf_clear (term2);
617 mpf_clear (x);
618 mpf_clear (xp);
622 /* Given an arithmetic error code, return a pointer to a string that
623 explains the error. */
625 static const char *
626 gfc_arith_error (arith code)
628 const char *p;
630 switch (code)
632 case ARITH_OK:
633 p = "Arithmetic OK";
634 break;
635 case ARITH_OVERFLOW:
636 p = "Arithmetic overflow";
637 break;
638 case ARITH_UNDERFLOW:
639 p = "Arithmetic underflow";
640 break;
641 case ARITH_DIV0:
642 p = "Division by zero";
643 break;
644 case ARITH_0TO0:
645 p = "Indeterminate form 0 ** 0";
646 break;
647 case ARITH_INCOMMENSURATE:
648 p = "Array operands are incommensurate";
649 break;
650 default:
651 gfc_internal_error ("gfc_arith_error(): Bad error code");
654 return p;
658 /* Get things ready to do math. */
660 void
661 gfc_arith_init_1 (void)
663 gfc_integer_info *int_info;
664 gfc_real_info *real_info;
665 mpf_t a, b;
666 mpz_t r;
667 int i, n, limit;
669 /* Set the default precision for GMP computations. */
670 mpf_set_default_prec (GFC_REAL_BITS + 30);
672 /* Calculate e, needed by the natural_logarithm() subroutine. */
673 mpf_init (b);
674 mpf_init_set_ui (e, 0);
675 mpf_init_set_ui (a, 1);
677 for (i = 1; i < 100; i++)
679 mpf_add (e, e, a);
680 mpf_div_ui (a, a, i); /* 1/(i!) */
683 /* Calculate pi, 2pi, pi/2, and -pi/2, needed for trigonometric
684 functions.
686 We use the Bailey, Borwein and Plouffe formula:
688 pi = \sum{n=0}^\infty (1/16)^n [4/(8n+1) - 2/(8n+4) - 1/(8n+5) - 1/(8n+6)]
690 which gives about four bits per iteration. */
692 mpf_init_set_ui (pi, 0);
694 mpf_init (two_pi);
695 mpf_init (half_pi);
697 limit = (GFC_REAL_BITS / 4) + 10; /* (1/16)^n gives 4 bits per iteration */
699 for (n = 0; n < limit; n++)
701 mpf_set_ui (b, 4);
702 mpf_div_ui (b, b, 8 * n + 1); /* 4/(8n+1) */
704 mpf_set_ui (a, 2);
705 mpf_div_ui (a, a, 8 * n + 4); /* 2/(8n+4) */
706 mpf_sub (b, b, a);
708 mpf_set_ui (a, 1);
709 mpf_div_ui (a, a, 8 * n + 5); /* 1/(8n+5) */
710 mpf_sub (b, b, a);
712 mpf_set_ui (a, 1);
713 mpf_div_ui (a, a, 8 * n + 6); /* 1/(8n+6) */
714 mpf_sub (b, b, a);
716 mpf_set_ui (a, 16);
717 mpf_pow_ui (a, a, n); /* 16^n */
719 mpf_div (b, b, a);
721 mpf_add (pi, pi, b);
724 mpf_mul_ui (two_pi, pi, 2);
725 mpf_div_ui (half_pi, pi, 2);
727 /* Convert the minimum/maximum values for each kind into their
728 GNU MP representation. */
729 mpz_init (r);
731 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
733 /* Huge */
734 mpz_set_ui (r, int_info->radix);
735 mpz_pow_ui (r, r, int_info->digits);
737 mpz_init (int_info->huge);
738 mpz_sub_ui (int_info->huge, r, 1);
740 /* These are the numbers that are actually representable by the
741 target. For bases other than two, this needs to be changed. */
742 if (int_info->radix != 2)
743 gfc_internal_error ("Fix min_int, max_int calculation");
745 mpz_init (int_info->min_int);
746 mpz_neg (int_info->min_int, int_info->huge);
747 /* No -1 here, because the representation is symmetric. */
749 mpz_init (int_info->max_int);
750 mpz_add (int_info->max_int, int_info->huge, int_info->huge);
751 mpz_add_ui (int_info->max_int, int_info->max_int, 1);
753 /* Range */
754 mpf_set_z (a, int_info->huge);
755 common_logarithm (&a, &a);
756 mpf_trunc (a, a);
757 mpz_set_f (r, a);
758 int_info->range = mpz_get_si (r);
761 /* mpf_set_default_prec(GFC_REAL_BITS); */
762 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
764 /* Huge */
765 mpf_set_ui (a, real_info->radix);
766 mpf_set_ui (b, real_info->radix);
768 mpf_pow_ui (a, a, real_info->max_exponent);
769 mpf_pow_ui (b, b, real_info->max_exponent - real_info->digits);
771 mpf_init (real_info->huge);
772 mpf_sub (real_info->huge, a, b);
774 /* Tiny */
775 mpf_set_ui (b, real_info->radix);
776 mpf_pow_ui (b, b, 1 - real_info->min_exponent);
778 mpf_init (real_info->tiny);
779 mpf_ui_div (real_info->tiny, 1, b);
781 /* Epsilon */
782 mpf_set_ui (b, real_info->radix);
783 mpf_pow_ui (b, b, real_info->digits - 1);
785 mpf_init (real_info->epsilon);
786 mpf_ui_div (real_info->epsilon, 1, b);
788 /* Range */
789 common_logarithm (&real_info->huge, &a);
790 common_logarithm (&real_info->tiny, &b);
791 mpf_neg (b, b);
793 if (mpf_cmp (a, b) > 0)
794 mpf_set (a, b); /* a = min(a, b) */
796 mpf_trunc (a, a);
797 mpz_set_f (r, a);
798 real_info->range = mpz_get_si (r);
800 /* Precision */
801 mpf_set_ui (a, real_info->radix);
802 common_logarithm (&a, &a);
804 mpf_mul_ui (a, a, real_info->digits - 1);
805 mpf_trunc (a, a);
806 mpz_set_f (r, a);
807 real_info->precision = mpz_get_si (r);
809 /* If the radix is an integral power of 10, add one to the
810 precision. */
811 for (i = 10; i <= real_info->radix; i *= 10)
812 if (i == real_info->radix)
813 real_info->precision++;
816 mpz_clear (r);
817 mpf_clear (a);
818 mpf_clear (b);
822 /* Clean up, get rid of numeric constants. */
824 void
825 gfc_arith_done_1 (void)
827 gfc_integer_info *ip;
828 gfc_real_info *rp;
830 mpf_clear (e);
832 mpf_clear (pi);
833 mpf_clear (half_pi);
834 mpf_clear (two_pi);
836 for (ip = gfc_integer_kinds; ip->kind; ip++)
838 mpz_clear (ip->min_int);
839 mpz_clear (ip->max_int);
840 mpz_clear (ip->huge);
843 for (rp = gfc_real_kinds; rp->kind; rp++)
845 mpf_clear (rp->epsilon);
846 mpf_clear (rp->huge);
847 mpf_clear (rp->tiny);
852 /* Return default kinds. */
855 gfc_default_integer_kind (void)
857 return gfc_integer_kinds[gfc_option.i8 ? 1 : 0].kind;
861 gfc_default_real_kind (void)
863 return gfc_real_kinds[gfc_option.r8 ? 1 : 0].kind;
867 gfc_default_double_kind (void)
869 return gfc_real_kinds[1].kind;
873 gfc_default_character_kind (void)
875 return 1;
879 gfc_default_logical_kind (void)
881 return gfc_logical_kinds[gfc_option.i8 ? 1 : 0].kind;
885 gfc_default_complex_kind (void)
887 return gfc_default_real_kind ();
891 /* Make sure that a valid kind is present. Returns an index into the
892 gfc_integer_kinds array, -1 if the kind is not present. */
894 static int
895 validate_integer (int kind)
897 int i;
899 for (i = 0;; i++)
901 if (gfc_integer_kinds[i].kind == 0)
903 i = -1;
904 break;
906 if (gfc_integer_kinds[i].kind == kind)
907 break;
910 return i;
914 static int
915 validate_real (int kind)
917 int i;
919 for (i = 0;; i++)
921 if (gfc_real_kinds[i].kind == 0)
923 i = -1;
924 break;
926 if (gfc_real_kinds[i].kind == kind)
927 break;
930 return i;
934 static int
935 validate_logical (int kind)
937 int i;
939 for (i = 0;; i++)
941 if (gfc_logical_kinds[i].kind == 0)
943 i = -1;
944 break;
946 if (gfc_logical_kinds[i].kind == kind)
947 break;
950 return i;
954 static int
955 validate_character (int kind)
958 if (kind == gfc_default_character_kind ())
959 return 0;
960 return -1;
964 /* Validate a kind given a basic type. The return value is the same
965 for the child functions, with -1 indicating nonexistence of the
966 type. */
969 gfc_validate_kind (bt type, int kind)
971 int rc;
973 switch (type)
975 case BT_REAL: /* Fall through */
976 case BT_COMPLEX:
977 rc = validate_real (kind);
978 break;
979 case BT_INTEGER:
980 rc = validate_integer (kind);
981 break;
982 case BT_LOGICAL:
983 rc = validate_logical (kind);
984 break;
985 case BT_CHARACTER:
986 rc = validate_character (kind);
987 break;
989 default:
990 gfc_internal_error ("gfc_validate_kind(): Got bad type");
993 return rc;
997 /* Given an integer and a kind, make sure that the integer lies within
998 the range of the kind. Returns ARITH_OK or ARITH_OVERFLOW. */
1000 static arith
1001 gfc_check_integer_range (mpz_t p, int kind)
1003 arith result;
1004 int i;
1006 i = validate_integer (kind);
1007 if (i == -1)
1008 gfc_internal_error ("gfc_check_integer_range(): Bad kind");
1010 result = ARITH_OK;
1012 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
1013 || mpz_cmp (p, gfc_integer_kinds[i].max_int) > 0)
1014 result = ARITH_OVERFLOW;
1016 return result;
1020 /* Given a real and a kind, make sure that the real lies within the
1021 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
1022 ARITH_UNDERFLOW. */
1024 static arith
1025 gfc_check_real_range (mpf_t p, int kind)
1027 arith retval;
1028 mpf_t q;
1029 int i;
1031 mpf_init (q);
1032 mpf_abs (q, p);
1034 i = validate_real (kind);
1035 if (i == -1)
1036 gfc_internal_error ("gfc_check_real_range(): Bad kind");
1038 retval = ARITH_OK;
1039 if (mpf_sgn (q) == 0)
1040 goto done;
1042 if (mpf_cmp (q, gfc_real_kinds[i].huge) == 1)
1044 retval = ARITH_OVERFLOW;
1045 goto done;
1048 if (mpf_cmp (q, gfc_real_kinds[i].tiny) == -1)
1049 retval = ARITH_UNDERFLOW;
1051 done:
1052 mpf_clear (q);
1054 return retval;
1058 /* Function to return a constant expression node of a given type and
1059 kind. */
1061 gfc_expr *
1062 gfc_constant_result (bt type, int kind, locus * where)
1064 gfc_expr *result;
1066 if (!where)
1067 gfc_internal_error
1068 ("gfc_constant_result(): locus 'where' cannot be NULL");
1070 result = gfc_get_expr ();
1072 result->expr_type = EXPR_CONSTANT;
1073 result->ts.type = type;
1074 result->ts.kind = kind;
1075 result->where = *where;
1077 switch (type)
1079 case BT_INTEGER:
1080 mpz_init (result->value.integer);
1081 break;
1083 case BT_REAL:
1084 mpf_init (result->value.real);
1085 break;
1087 case BT_COMPLEX:
1088 mpf_init (result->value.complex.r);
1089 mpf_init (result->value.complex.i);
1090 break;
1092 default:
1093 break;
1096 return result;
1100 /* Low-level arithmetic functions. All of these subroutines assume
1101 that all operands are of the same type and return an operand of the
1102 same type. The other thing about these subroutines is that they
1103 can fail in various ways -- overflow, underflow, division by zero,
1104 zero raised to the zero, etc. */
1106 static arith
1107 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
1109 gfc_expr *result;
1111 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
1112 result->value.logical = !op1->value.logical;
1113 *resultp = result;
1115 return ARITH_OK;
1119 static arith
1120 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1122 gfc_expr *result;
1124 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1125 &op1->where);
1126 result->value.logical = op1->value.logical && op2->value.logical;
1127 *resultp = result;
1129 return ARITH_OK;
1133 static arith
1134 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1136 gfc_expr *result;
1138 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1139 &op1->where);
1140 result->value.logical = op1->value.logical || op2->value.logical;
1141 *resultp = result;
1143 return ARITH_OK;
1147 static arith
1148 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1150 gfc_expr *result;
1152 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1153 &op1->where);
1154 result->value.logical = op1->value.logical == op2->value.logical;
1155 *resultp = result;
1157 return ARITH_OK;
1161 static arith
1162 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1164 gfc_expr *result;
1166 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1167 &op1->where);
1168 result->value.logical = op1->value.logical != op2->value.logical;
1169 *resultp = result;
1171 return ARITH_OK;
1175 /* Make sure a constant numeric expression is within the range for
1176 its type and kind. GMP is doing 130 bit arithmetic, so an UNDERFLOW
1177 is numerically zero for REAL(4) and REAL(8) types. Reset the value(s)
1178 to exactly 0 for UNDERFLOW. Note that there's also a gfc_check_range(),
1179 but that one deals with the intrinsic RANGE function. */
1181 arith
1182 gfc_range_check (gfc_expr * e)
1184 arith rc;
1186 switch (e->ts.type)
1188 case BT_INTEGER:
1189 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
1190 break;
1192 case BT_REAL:
1193 rc = gfc_check_real_range (e->value.real, e->ts.kind);
1194 if (rc == ARITH_UNDERFLOW)
1195 mpf_set_ui (e->value.real, 0);
1196 break;
1198 case BT_COMPLEX:
1199 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
1200 if (rc == ARITH_UNDERFLOW)
1201 mpf_set_ui (e->value.complex.r, 0);
1202 if (rc == ARITH_OK || rc == ARITH_UNDERFLOW)
1204 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
1205 if (rc == ARITH_UNDERFLOW)
1206 mpf_set_ui (e->value.complex.i, 0);
1209 break;
1211 default:
1212 gfc_internal_error ("gfc_range_check(): Bad type");
1215 return rc;
1219 /* It may seem silly to have a subroutine that actually computes the
1220 unary plus of a constant, but it prevents us from making exceptions
1221 in the code elsewhere. */
1223 static arith
1224 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
1227 *resultp = gfc_copy_expr (op1);
1228 return ARITH_OK;
1232 static arith
1233 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
1235 gfc_expr *result;
1236 arith rc;
1238 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1240 switch (op1->ts.type)
1242 case BT_INTEGER:
1243 mpz_neg (result->value.integer, op1->value.integer);
1244 break;
1246 case BT_REAL:
1247 mpf_neg (result->value.real, op1->value.real);
1248 break;
1250 case BT_COMPLEX:
1251 mpf_neg (result->value.complex.r, op1->value.complex.r);
1252 mpf_neg (result->value.complex.i, op1->value.complex.i);
1253 break;
1255 default:
1256 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
1259 rc = gfc_range_check (result);
1261 if (rc == ARITH_UNDERFLOW)
1263 if (gfc_option.warn_underflow)
1264 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1265 rc = ARITH_OK;
1266 *resultp = result;
1268 else if (rc != ARITH_OK)
1269 gfc_free_expr (result);
1270 else
1271 *resultp = result;
1273 return rc;
1277 static arith
1278 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1280 gfc_expr *result;
1281 arith rc;
1283 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1285 switch (op1->ts.type)
1287 case BT_INTEGER:
1288 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
1289 break;
1291 case BT_REAL:
1292 mpf_add (result->value.real, op1->value.real, op2->value.real);
1293 break;
1295 case BT_COMPLEX:
1296 mpf_add (result->value.complex.r, op1->value.complex.r,
1297 op2->value.complex.r);
1299 mpf_add (result->value.complex.i, op1->value.complex.i,
1300 op2->value.complex.i);
1301 break;
1303 default:
1304 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
1307 rc = gfc_range_check (result);
1309 if (rc == ARITH_UNDERFLOW)
1311 if (gfc_option.warn_underflow)
1312 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1313 rc = ARITH_OK;
1314 *resultp = result;
1316 else if (rc != ARITH_OK)
1317 gfc_free_expr (result);
1318 else
1319 *resultp = result;
1321 return rc;
1325 static arith
1326 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1328 gfc_expr *result;
1329 arith rc;
1331 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1333 switch (op1->ts.type)
1335 case BT_INTEGER:
1336 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
1337 break;
1339 case BT_REAL:
1340 mpf_sub (result->value.real, op1->value.real, op2->value.real);
1341 break;
1343 case BT_COMPLEX:
1344 mpf_sub (result->value.complex.r, op1->value.complex.r,
1345 op2->value.complex.r);
1347 mpf_sub (result->value.complex.i, op1->value.complex.i,
1348 op2->value.complex.i);
1350 break;
1352 default:
1353 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
1356 rc = gfc_range_check (result);
1358 if (rc == ARITH_UNDERFLOW)
1360 if (gfc_option.warn_underflow)
1361 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1362 rc = ARITH_OK;
1363 *resultp = result;
1365 else if (rc != ARITH_OK)
1366 gfc_free_expr (result);
1367 else
1368 *resultp = result;
1370 return rc;
1374 static arith
1375 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1377 gfc_expr *result;
1378 mpf_t x, y;
1379 arith rc;
1381 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1383 switch (op1->ts.type)
1385 case BT_INTEGER:
1386 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
1387 break;
1389 case BT_REAL:
1390 mpf_mul (result->value.real, op1->value.real, op2->value.real);
1391 break;
1393 case BT_COMPLEX:
1394 mpf_init (x);
1395 mpf_init (y);
1397 mpf_mul (x, op1->value.complex.r, op2->value.complex.r);
1398 mpf_mul (y, op1->value.complex.i, op2->value.complex.i);
1399 mpf_sub (result->value.complex.r, x, y);
1401 mpf_mul (x, op1->value.complex.r, op2->value.complex.i);
1402 mpf_mul (y, op1->value.complex.i, op2->value.complex.r);
1403 mpf_add (result->value.complex.i, x, y);
1405 mpf_clear (x);
1406 mpf_clear (y);
1408 break;
1410 default:
1411 gfc_internal_error ("gfc_arith_times(): Bad basic type");
1414 rc = gfc_range_check (result);
1416 if (rc == ARITH_UNDERFLOW)
1418 if (gfc_option.warn_underflow)
1419 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1420 rc = ARITH_OK;
1421 *resultp = result;
1423 else if (rc != ARITH_OK)
1424 gfc_free_expr (result);
1425 else
1426 *resultp = result;
1428 return rc;
1432 static arith
1433 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1435 gfc_expr *result;
1436 mpf_t x, y, div;
1437 arith rc;
1439 rc = ARITH_OK;
1441 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1443 switch (op1->ts.type)
1445 case BT_INTEGER:
1446 if (mpz_sgn (op2->value.integer) == 0)
1448 rc = ARITH_DIV0;
1449 break;
1452 mpz_tdiv_q (result->value.integer, op1->value.integer,
1453 op2->value.integer);
1454 break;
1456 case BT_REAL:
1457 if (mpf_sgn (op2->value.real) == 0)
1459 rc = ARITH_DIV0;
1460 break;
1463 mpf_div (result->value.real, op1->value.real, op2->value.real);
1464 break;
1466 case BT_COMPLEX:
1467 if (mpf_sgn (op2->value.complex.r) == 0
1468 && mpf_sgn (op2->value.complex.i) == 0)
1470 rc = ARITH_DIV0;
1471 break;
1474 mpf_init (x);
1475 mpf_init (y);
1476 mpf_init (div);
1478 mpf_mul (x, op2->value.complex.r, op2->value.complex.r);
1479 mpf_mul (y, op2->value.complex.i, op2->value.complex.i);
1480 mpf_add (div, x, y);
1482 mpf_mul (x, op1->value.complex.r, op2->value.complex.r);
1483 mpf_mul (y, op1->value.complex.i, op2->value.complex.i);
1484 mpf_add (result->value.complex.r, x, y);
1485 mpf_div (result->value.complex.r, result->value.complex.r, div);
1487 mpf_mul (x, op1->value.complex.i, op2->value.complex.r);
1488 mpf_mul (y, op1->value.complex.r, op2->value.complex.i);
1489 mpf_sub (result->value.complex.i, x, y);
1490 mpf_div (result->value.complex.i, result->value.complex.i, div);
1492 mpf_clear (x);
1493 mpf_clear (y);
1494 mpf_clear (div);
1496 break;
1498 default:
1499 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
1502 if (rc == ARITH_OK)
1503 rc = gfc_range_check (result);
1505 if (rc == ARITH_UNDERFLOW)
1507 if (gfc_option.warn_underflow)
1508 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1509 rc = ARITH_OK;
1510 *resultp = result;
1512 else if (rc != ARITH_OK)
1513 gfc_free_expr (result);
1514 else
1515 *resultp = result;
1517 return rc;
1521 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
1523 static void
1524 complex_reciprocal (gfc_expr * op)
1526 mpf_t mod, a, result_r, result_i;
1528 mpf_init (mod);
1529 mpf_init (a);
1531 mpf_mul (mod, op->value.complex.r, op->value.complex.r);
1532 mpf_mul (a, op->value.complex.i, op->value.complex.i);
1533 mpf_add (mod, mod, a);
1535 mpf_init (result_r);
1536 mpf_div (result_r, op->value.complex.r, mod);
1538 mpf_init (result_i);
1539 mpf_neg (result_i, op->value.complex.i);
1540 mpf_div (result_i, result_i, mod);
1542 mpf_set (op->value.complex.r, result_r);
1543 mpf_set (op->value.complex.i, result_i);
1545 mpf_clear (result_r);
1546 mpf_clear (result_i);
1548 mpf_clear (mod);
1549 mpf_clear (a);
1553 /* Raise a complex number to positive power. */
1555 static void
1556 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
1558 mpf_t temp_r, temp_i, a;
1560 mpf_set_ui (result->value.complex.r, 1);
1561 mpf_set_ui (result->value.complex.i, 0);
1563 mpf_init (temp_r);
1564 mpf_init (temp_i);
1565 mpf_init (a);
1567 for (; power > 0; power--)
1569 mpf_mul (temp_r, base->value.complex.r, result->value.complex.r);
1570 mpf_mul (a, base->value.complex.i, result->value.complex.i);
1571 mpf_sub (temp_r, temp_r, a);
1573 mpf_mul (temp_i, base->value.complex.r, result->value.complex.i);
1574 mpf_mul (a, base->value.complex.i, result->value.complex.r);
1575 mpf_add (temp_i, temp_i, a);
1577 mpf_set (result->value.complex.r, temp_r);
1578 mpf_set (result->value.complex.i, temp_i);
1581 mpf_clear (temp_r);
1582 mpf_clear (temp_i);
1583 mpf_clear (a);
1587 /* Raise a number to an integer power. */
1589 static arith
1590 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1592 int power, apower;
1593 gfc_expr *result;
1594 mpz_t unity_z;
1595 mpf_t unity_f;
1596 arith rc;
1598 rc = ARITH_OK;
1600 if (gfc_extract_int (op2, &power) != NULL)
1601 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1603 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1605 if (power == 0)
1606 { /* Handle something to the zeroth power */
1607 switch (op1->ts.type)
1609 case BT_INTEGER:
1610 if (mpz_sgn (op1->value.integer) == 0)
1611 rc = ARITH_0TO0;
1612 else
1613 mpz_set_ui (result->value.integer, 1);
1615 break;
1617 case BT_REAL:
1618 if (mpf_sgn (op1->value.real) == 0)
1619 rc = ARITH_0TO0;
1620 else
1621 mpf_set_ui (result->value.real, 1);
1623 break;
1625 case BT_COMPLEX:
1626 if (mpf_sgn (op1->value.complex.r) == 0
1627 && mpf_sgn (op1->value.complex.i) == 0)
1628 rc = ARITH_0TO0;
1629 else
1631 mpf_set_ui (result->value.complex.r, 1);
1632 mpf_set_ui (result->value.complex.i, 0);
1635 break;
1637 default:
1638 gfc_internal_error ("gfc_arith_power(): Bad base");
1642 if (power != 0)
1644 apower = power;
1645 if (power < 0)
1646 apower = -power;
1648 switch (op1->ts.type)
1650 case BT_INTEGER:
1651 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1653 if (power < 0)
1655 mpz_init_set_ui (unity_z, 1);
1656 mpz_tdiv_q (result->value.integer, unity_z,
1657 result->value.integer);
1658 mpz_clear (unity_z);
1661 break;
1663 case BT_REAL:
1664 mpf_pow_ui (result->value.real, op1->value.real, apower);
1666 if (power < 0)
1668 mpf_init_set_ui (unity_f, 1);
1669 mpf_div (result->value.real, unity_f, result->value.real);
1670 mpf_clear (unity_f);
1673 break;
1675 case BT_COMPLEX:
1676 complex_pow_ui (op1, apower, result);
1677 if (power < 0)
1678 complex_reciprocal (result);
1680 break;
1682 default:
1683 break;
1687 if (rc == ARITH_OK)
1688 rc = gfc_range_check (result);
1690 if (rc == ARITH_UNDERFLOW)
1692 if (gfc_option.warn_underflow)
1693 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1694 rc = ARITH_OK;
1695 *resultp = result;
1697 else if (rc != ARITH_OK)
1698 gfc_free_expr (result);
1699 else
1700 *resultp = result;
1702 return rc;
1706 /* Concatenate two string constants. */
1708 static arith
1709 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1711 gfc_expr *result;
1712 int len;
1714 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
1715 &op1->where);
1717 len = op1->value.character.length + op2->value.character.length;
1719 result->value.character.string = gfc_getmem (len + 1);
1720 result->value.character.length = len;
1722 memcpy (result->value.character.string, op1->value.character.string,
1723 op1->value.character.length);
1725 memcpy (result->value.character.string + op1->value.character.length,
1726 op2->value.character.string, op2->value.character.length);
1728 result->value.character.string[len] = '\0';
1730 *resultp = result;
1732 return ARITH_OK;
1736 /* Comparison operators. Assumes that the two expression nodes
1737 contain two constants of the same type. */
1740 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1742 int rc;
1744 switch (op1->ts.type)
1746 case BT_INTEGER:
1747 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1748 break;
1750 case BT_REAL:
1751 rc = mpf_cmp (op1->value.real, op2->value.real);
1752 break;
1754 case BT_CHARACTER:
1755 rc = gfc_compare_string (op1, op2, NULL);
1756 break;
1758 case BT_LOGICAL:
1759 rc = ((!op1->value.logical && op2->value.logical)
1760 || (op1->value.logical && !op2->value.logical));
1761 break;
1763 default:
1764 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1767 return rc;
1771 /* Compare a pair of complex numbers. Naturally, this is only for
1772 equality/nonequality. */
1774 static int
1775 compare_complex (gfc_expr * op1, gfc_expr * op2)
1778 return (mpf_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1779 && mpf_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1783 /* Given two constant strings and the inverse collating sequence,
1784 compare the strings. We return -1 for a<b, 0 for a==b and 1 for
1785 a>b. If the xcoll_table is NULL, we use the processor's default
1786 collating sequence. */
1789 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
1791 int len, alen, blen, i, ac, bc;
1793 alen = a->value.character.length;
1794 blen = b->value.character.length;
1796 len = (alen > blen) ? alen : blen;
1798 for (i = 0; i < len; i++)
1800 ac = (i < alen) ? a->value.character.string[i] : ' ';
1801 bc = (i < blen) ? b->value.character.string[i] : ' ';
1803 if (xcoll_table != NULL)
1805 ac = xcoll_table[ac];
1806 bc = xcoll_table[bc];
1809 if (ac < bc)
1810 return -1;
1811 if (ac > bc)
1812 return 1;
1815 /* Strings are equal */
1817 return 0;
1821 /* Specific comparison subroutines. */
1823 static arith
1824 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1826 gfc_expr *result;
1828 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1829 &op1->where);
1830 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1831 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1833 *resultp = result;
1834 return ARITH_OK;
1838 static arith
1839 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1841 gfc_expr *result;
1843 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1844 &op1->where);
1845 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1846 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1848 *resultp = result;
1849 return ARITH_OK;
1853 static arith
1854 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1856 gfc_expr *result;
1858 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1859 &op1->where);
1860 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1861 *resultp = result;
1863 return ARITH_OK;
1867 static arith
1868 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1870 gfc_expr *result;
1872 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1873 &op1->where);
1874 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1875 *resultp = result;
1877 return ARITH_OK;
1881 static arith
1882 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1884 gfc_expr *result;
1886 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1887 &op1->where);
1888 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1889 *resultp = result;
1891 return ARITH_OK;
1895 static arith
1896 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1898 gfc_expr *result;
1900 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1901 &op1->where);
1902 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1903 *resultp = result;
1905 return ARITH_OK;
1909 static arith
1910 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1911 gfc_expr ** result)
1913 gfc_constructor *c, *head;
1914 gfc_expr *r;
1915 arith rc;
1917 if (op->expr_type == EXPR_CONSTANT)
1918 return eval (op, result);
1920 rc = ARITH_OK;
1921 head = gfc_copy_constructor (op->value.constructor);
1923 for (c = head; c; c = c->next)
1925 rc = eval (c->expr, &r);
1926 if (rc != ARITH_OK)
1927 break;
1929 gfc_replace_expr (c->expr, r);
1932 if (rc != ARITH_OK)
1933 gfc_free_constructor (head);
1934 else
1936 r = gfc_get_expr ();
1937 r->expr_type = EXPR_ARRAY;
1938 r->value.constructor = head;
1939 r->shape = gfc_copy_shape (op->shape, op->rank);
1941 r->ts = head->expr->ts;
1942 r->where = op->where;
1943 r->rank = op->rank;
1945 *result = r;
1948 return rc;
1952 static arith
1953 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1954 gfc_expr * op1, gfc_expr * op2,
1955 gfc_expr ** result)
1957 gfc_constructor *c, *head;
1958 gfc_expr *r;
1959 arith rc;
1961 head = gfc_copy_constructor (op1->value.constructor);
1962 rc = ARITH_OK;
1964 for (c = head; c; c = c->next)
1966 rc = eval (c->expr, op2, &r);
1967 if (rc != ARITH_OK)
1968 break;
1970 gfc_replace_expr (c->expr, r);
1973 if (rc != ARITH_OK)
1974 gfc_free_constructor (head);
1975 else
1977 r = gfc_get_expr ();
1978 r->expr_type = EXPR_ARRAY;
1979 r->value.constructor = head;
1980 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1982 r->ts = head->expr->ts;
1983 r->where = op1->where;
1984 r->rank = op1->rank;
1986 *result = r;
1989 return rc;
1993 static arith
1994 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1995 gfc_expr * op1, gfc_expr * op2,
1996 gfc_expr ** result)
1998 gfc_constructor *c, *head;
1999 gfc_expr *r;
2000 arith rc;
2002 head = gfc_copy_constructor (op2->value.constructor);
2003 rc = ARITH_OK;
2005 for (c = head; c; c = c->next)
2007 rc = eval (op1, c->expr, &r);
2008 if (rc != ARITH_OK)
2009 break;
2011 gfc_replace_expr (c->expr, r);
2014 if (rc != ARITH_OK)
2015 gfc_free_constructor (head);
2016 else
2018 r = gfc_get_expr ();
2019 r->expr_type = EXPR_ARRAY;
2020 r->value.constructor = head;
2021 r->shape = gfc_copy_shape (op2->shape, op2->rank);
2023 r->ts = head->expr->ts;
2024 r->where = op2->where;
2025 r->rank = op2->rank;
2027 *result = r;
2030 return rc;
2034 static arith
2035 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2036 gfc_expr * op1, gfc_expr * op2,
2037 gfc_expr ** result)
2039 gfc_constructor *c, *d, *head;
2040 gfc_expr *r;
2041 arith rc;
2043 head = gfc_copy_constructor (op1->value.constructor);
2045 rc = ARITH_OK;
2046 d = op2->value.constructor;
2048 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
2049 != SUCCESS)
2050 rc = ARITH_INCOMMENSURATE;
2051 else
2054 for (c = head; c; c = c->next, d = d->next)
2056 if (d == NULL)
2058 rc = ARITH_INCOMMENSURATE;
2059 break;
2062 rc = eval (c->expr, d->expr, &r);
2063 if (rc != ARITH_OK)
2064 break;
2066 gfc_replace_expr (c->expr, r);
2069 if (d != NULL)
2070 rc = ARITH_INCOMMENSURATE;
2073 if (rc != ARITH_OK)
2074 gfc_free_constructor (head);
2075 else
2077 r = gfc_get_expr ();
2078 r->expr_type = EXPR_ARRAY;
2079 r->value.constructor = head;
2080 r->shape = gfc_copy_shape (op1->shape, op1->rank);
2082 r->ts = head->expr->ts;
2083 r->where = op1->where;
2084 r->rank = op1->rank;
2086 *result = r;
2089 return rc;
2093 static arith
2094 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2095 gfc_expr * op1, gfc_expr * op2,
2096 gfc_expr ** result)
2099 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
2100 return eval (op1, op2, result);
2102 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
2103 return reduce_binary_ca (eval, op1, op2, result);
2105 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
2106 return reduce_binary_ac (eval, op1, op2, result);
2108 return reduce_binary_aa (eval, op1, op2, result);
2112 typedef union
2114 arith (*f2)(gfc_expr *, gfc_expr **);
2115 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
2117 eval_f;
2119 /* High level arithmetic subroutines. These subroutines go into
2120 eval_intrinsic(), which can do one of several things to its
2121 operands. If the operands are incompatible with the intrinsic
2122 operation, we return a node pointing to the operands and hope that
2123 an operator interface is found during resolution.
2125 If the operands are compatible and are constants, then we try doing
2126 the arithmetic. We also handle the cases where either or both
2127 operands are array constructors. */
2129 static gfc_expr *
2130 eval_intrinsic (gfc_intrinsic_op operator,
2131 eval_f eval, gfc_expr * op1, gfc_expr * op2)
2133 gfc_expr temp, *result;
2134 int unary;
2135 arith rc;
2137 gfc_clear_ts (&temp.ts);
2139 switch (operator)
2141 case INTRINSIC_NOT: /* Logical unary */
2142 if (op1->ts.type != BT_LOGICAL)
2143 goto runtime;
2145 temp.ts.type = BT_LOGICAL;
2146 temp.ts.kind = gfc_default_logical_kind ();
2148 unary = 1;
2149 break;
2151 /* Logical binary operators */
2152 case INTRINSIC_OR:
2153 case INTRINSIC_AND:
2154 case INTRINSIC_NEQV:
2155 case INTRINSIC_EQV:
2156 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
2157 goto runtime;
2159 temp.ts.type = BT_LOGICAL;
2160 temp.ts.kind = gfc_default_logical_kind ();
2162 unary = 0;
2163 break;
2165 case INTRINSIC_UPLUS:
2166 case INTRINSIC_UMINUS: /* Numeric unary */
2167 if (!gfc_numeric_ts (&op1->ts))
2168 goto runtime;
2170 temp.ts = op1->ts;
2172 unary = 1;
2173 break;
2175 case INTRINSIC_GE:
2176 case INTRINSIC_LT: /* Additional restrictions */
2177 case INTRINSIC_LE: /* for ordering relations. */
2178 case INTRINSIC_GT:
2179 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2181 temp.ts.type = BT_LOGICAL;
2182 temp.ts.kind = gfc_default_logical_kind();
2183 goto runtime;
2186 /* else fall through */
2188 case INTRINSIC_EQ:
2189 case INTRINSIC_NE:
2190 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2192 unary = 0;
2193 temp.ts.type = BT_LOGICAL;
2194 temp.ts.kind = gfc_default_logical_kind();
2195 break;
2198 /* else fall through */
2200 case INTRINSIC_PLUS:
2201 case INTRINSIC_MINUS:
2202 case INTRINSIC_TIMES:
2203 case INTRINSIC_DIVIDE:
2204 case INTRINSIC_POWER: /* Numeric binary */
2205 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
2206 goto runtime;
2208 /* Insert any necessary type conversions to make the operands compatible. */
2210 temp.expr_type = EXPR_OP;
2211 gfc_clear_ts (&temp.ts);
2212 temp.operator = operator;
2214 temp.op1 = op1;
2215 temp.op2 = op2;
2217 gfc_type_convert_binary (&temp);
2219 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
2220 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
2221 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
2223 temp.ts.type = BT_LOGICAL;
2224 temp.ts.kind = gfc_default_logical_kind ();
2227 unary = 0;
2228 break;
2230 case INTRINSIC_CONCAT: /* Character binary */
2231 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
2232 goto runtime;
2234 temp.ts.type = BT_CHARACTER;
2235 temp.ts.kind = gfc_default_character_kind ();
2237 unary = 0;
2238 break;
2240 case INTRINSIC_USER:
2241 goto runtime;
2243 default:
2244 gfc_internal_error ("eval_intrinsic(): Bad operator");
2247 /* Try to combine the operators. */
2248 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
2249 goto runtime;
2251 if (op1->expr_type != EXPR_CONSTANT
2252 && (op1->expr_type != EXPR_ARRAY
2253 || !gfc_is_constant_expr (op1)
2254 || !gfc_expanded_ac (op1)))
2255 goto runtime;
2257 if (op2 != NULL
2258 && op2->expr_type != EXPR_CONSTANT
2259 && (op2->expr_type != EXPR_ARRAY
2260 || !gfc_is_constant_expr (op2)
2261 || !gfc_expanded_ac (op2)))
2262 goto runtime;
2264 if (unary)
2265 rc = reduce_unary (eval.f2, op1, &result);
2266 else
2267 rc = reduce_binary (eval.f3, op1, op2, &result);
2269 if (rc != ARITH_OK)
2270 { /* Something went wrong */
2271 gfc_error ("%s at %L", gfc_arith_error (rc), &op1->where);
2272 return NULL;
2275 gfc_free_expr (op1);
2276 gfc_free_expr (op2);
2277 return result;
2279 runtime:
2280 /* Create a run-time expression */
2281 result = gfc_get_expr ();
2282 result->ts = temp.ts;
2284 result->expr_type = EXPR_OP;
2285 result->operator = operator;
2287 result->op1 = op1;
2288 result->op2 = op2;
2290 result->where = op1->where;
2292 return result;
2296 /* Modify type of expression for zero size array. */
2297 static gfc_expr *
2298 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
2300 if (op == NULL)
2301 gfc_internal_error("eval_type_intrinsic0(): op NULL");
2303 switch(operator)
2305 case INTRINSIC_GE:
2306 case INTRINSIC_LT:
2307 case INTRINSIC_LE:
2308 case INTRINSIC_GT:
2309 case INTRINSIC_EQ:
2310 case INTRINSIC_NE:
2311 op->ts.type = BT_LOGICAL;
2312 op->ts.kind = gfc_default_logical_kind();
2313 break;
2315 default:
2316 break;
2319 return op;
2323 /* Return nonzero if the expression is a zero size array. */
2325 static int
2326 gfc_zero_size_array (gfc_expr * e)
2329 if (e->expr_type != EXPR_ARRAY)
2330 return 0;
2332 return e->value.constructor == NULL;
2336 /* Reduce a binary expression where at least one of the operands
2337 involves a zero-length array. Returns NULL if neither of the
2338 operands is a zero-length array. */
2340 static gfc_expr *
2341 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
2344 if (gfc_zero_size_array (op1))
2346 gfc_free_expr (op2);
2347 return op1;
2350 if (gfc_zero_size_array (op2))
2352 gfc_free_expr (op1);
2353 return op2;
2356 return NULL;
2360 static gfc_expr *
2361 eval_intrinsic_f2 (gfc_intrinsic_op operator,
2362 arith (*eval) (gfc_expr *, gfc_expr **),
2363 gfc_expr * op1, gfc_expr * op2)
2365 gfc_expr *result;
2366 eval_f f;
2368 if (op2 == NULL)
2370 if (gfc_zero_size_array (op1))
2371 return eval_type_intrinsic0(operator, op1);
2373 else
2375 result = reduce_binary0 (op1, op2);
2376 if (result != NULL)
2377 return eval_type_intrinsic0(operator, result);
2380 f.f2 = eval;
2381 return eval_intrinsic (operator, f, op1, op2);
2385 static gfc_expr *
2386 eval_intrinsic_f3 (gfc_intrinsic_op operator,
2387 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2388 gfc_expr * op1, gfc_expr * op2)
2390 gfc_expr *result;
2391 eval_f f;
2393 result = reduce_binary0 (op1, op2);
2394 if (result != NULL)
2395 return eval_type_intrinsic0(operator, result);
2397 f.f3 = eval;
2398 return eval_intrinsic (operator, f, op1, op2);
2403 gfc_expr *
2404 gfc_uplus (gfc_expr * op)
2406 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
2409 gfc_expr *
2410 gfc_uminus (gfc_expr * op)
2412 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
2415 gfc_expr *
2416 gfc_add (gfc_expr * op1, gfc_expr * op2)
2418 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
2421 gfc_expr *
2422 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
2424 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
2427 gfc_expr *
2428 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
2430 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
2433 gfc_expr *
2434 gfc_divide (gfc_expr * op1, gfc_expr * op2)
2436 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
2439 gfc_expr *
2440 gfc_power (gfc_expr * op1, gfc_expr * op2)
2442 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
2445 gfc_expr *
2446 gfc_concat (gfc_expr * op1, gfc_expr * op2)
2448 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
2451 gfc_expr *
2452 gfc_and (gfc_expr * op1, gfc_expr * op2)
2454 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
2457 gfc_expr *
2458 gfc_or (gfc_expr * op1, gfc_expr * op2)
2460 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
2463 gfc_expr *
2464 gfc_not (gfc_expr * op1)
2466 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
2469 gfc_expr *
2470 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
2472 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
2475 gfc_expr *
2476 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
2478 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
2481 gfc_expr *
2482 gfc_eq (gfc_expr * op1, gfc_expr * op2)
2484 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
2487 gfc_expr *
2488 gfc_ne (gfc_expr * op1, gfc_expr * op2)
2490 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
2493 gfc_expr *
2494 gfc_gt (gfc_expr * op1, gfc_expr * op2)
2496 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
2499 gfc_expr *
2500 gfc_ge (gfc_expr * op1, gfc_expr * op2)
2502 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
2505 gfc_expr *
2506 gfc_lt (gfc_expr * op1, gfc_expr * op2)
2508 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
2511 gfc_expr *
2512 gfc_le (gfc_expr * op1, gfc_expr * op2)
2514 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
2518 /* Convert an integer string to an expression node. */
2520 gfc_expr *
2521 gfc_convert_integer (const char *buffer, int kind, int radix, locus * where)
2523 gfc_expr *e;
2524 const char *t;
2526 e = gfc_constant_result (BT_INTEGER, kind, where);
2527 /* a leading plus is allowed, but not by mpz_set_str */
2528 if (buffer[0] == '+')
2529 t = buffer + 1;
2530 else
2531 t = buffer;
2532 mpz_set_str (e->value.integer, t, radix);
2534 return e;
2538 /* Convert a real string to an expression node. */
2540 gfc_expr *
2541 gfc_convert_real (const char *buffer, int kind, locus * where)
2543 gfc_expr *e;
2544 const char *t;
2546 e = gfc_constant_result (BT_REAL, kind, where);
2547 /* a leading plus is allowed, but not by mpf_set_str */
2548 if (buffer[0] == '+')
2549 t = buffer + 1;
2550 else
2551 t = buffer;
2552 mpf_set_str (e->value.real, t, 10);
2554 return e;
2558 /* Convert a pair of real, constant expression nodes to a single
2559 complex expression node. */
2561 gfc_expr *
2562 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
2564 gfc_expr *e;
2566 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
2567 mpf_set (e->value.complex.r, real->value.real);
2568 mpf_set (e->value.complex.i, imag->value.real);
2570 return e;
2574 /******* Simplification of intrinsic functions with constant arguments *****/
2577 /* Deal with an arithmetic error. */
2579 static void
2580 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
2583 gfc_error ("%s converting %s to %s at %L", gfc_arith_error (rc),
2584 gfc_typename (from), gfc_typename (to), where);
2586 /* TODO: Do something about the error, ie, throw exception, return
2587 NaN, etc. */
2590 /* Convert integers to integers. */
2592 gfc_expr *
2593 gfc_int2int (gfc_expr * src, int kind)
2595 gfc_expr *result;
2596 arith rc;
2598 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2600 mpz_set (result->value.integer, src->value.integer);
2602 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2603 != ARITH_OK)
2605 arith_error (rc, &src->ts, &result->ts, &src->where);
2606 gfc_free_expr (result);
2607 return NULL;
2610 return result;
2614 /* Convert integers to reals. */
2616 gfc_expr *
2617 gfc_int2real (gfc_expr * src, int kind)
2619 gfc_expr *result;
2620 arith rc;
2622 result = gfc_constant_result (BT_REAL, kind, &src->where);
2624 mpf_set_z (result->value.real, src->value.integer);
2626 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2628 arith_error (rc, &src->ts, &result->ts, &src->where);
2629 gfc_free_expr (result);
2630 return NULL;
2633 return result;
2637 /* Convert default integer to default complex. */
2639 gfc_expr *
2640 gfc_int2complex (gfc_expr * src, int kind)
2642 gfc_expr *result;
2643 arith rc;
2645 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2647 mpf_set_z (result->value.complex.r, src->value.integer);
2648 mpf_set_ui (result->value.complex.i, 0);
2650 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2652 arith_error (rc, &src->ts, &result->ts, &src->where);
2653 gfc_free_expr (result);
2654 return NULL;
2657 return result;
2661 /* Convert default real to default integer. */
2663 gfc_expr *
2664 gfc_real2int (gfc_expr * src, int kind)
2666 gfc_expr *result;
2667 arith rc;
2669 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2671 mpz_set_f (result->value.integer, src->value.real);
2673 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2674 != ARITH_OK)
2676 arith_error (rc, &src->ts, &result->ts, &src->where);
2677 gfc_free_expr (result);
2678 return NULL;
2681 return result;
2685 /* Convert real to real. */
2687 gfc_expr *
2688 gfc_real2real (gfc_expr * src, int kind)
2690 gfc_expr *result;
2691 arith rc;
2693 result = gfc_constant_result (BT_REAL, kind, &src->where);
2695 mpf_set (result->value.real, src->value.real);
2697 rc = gfc_check_real_range (result->value.real, kind);
2699 if (rc == ARITH_UNDERFLOW)
2701 if (gfc_option.warn_underflow)
2702 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2703 mpf_set_ui(result->value.real, 0);
2705 else if (rc != ARITH_OK)
2707 arith_error (rc, &src->ts, &result->ts, &src->where);
2708 gfc_free_expr (result);
2709 return NULL;
2712 return result;
2716 /* Convert real to complex. */
2718 gfc_expr *
2719 gfc_real2complex (gfc_expr * src, int kind)
2721 gfc_expr *result;
2722 arith rc;
2724 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2726 mpf_set (result->value.complex.r, src->value.real);
2727 mpf_set_ui (result->value.complex.i, 0);
2729 rc = gfc_check_real_range (result->value.complex.r, kind);
2731 if (rc == ARITH_UNDERFLOW)
2733 if (gfc_option.warn_underflow)
2734 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2735 mpf_set_ui(result->value.complex.r, 0);
2737 else if (rc != ARITH_OK)
2739 arith_error (rc, &src->ts, &result->ts, &src->where);
2740 gfc_free_expr (result);
2741 return NULL;
2744 return result;
2748 /* Convert complex to integer. */
2750 gfc_expr *
2751 gfc_complex2int (gfc_expr * src, int kind)
2753 gfc_expr *result;
2754 arith rc;
2756 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2758 mpz_set_f (result->value.integer, src->value.complex.r);
2760 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2761 != ARITH_OK)
2763 arith_error (rc, &src->ts, &result->ts, &src->where);
2764 gfc_free_expr (result);
2765 return NULL;
2768 return result;
2772 /* Convert complex to real. */
2774 gfc_expr *
2775 gfc_complex2real (gfc_expr * src, int kind)
2777 gfc_expr *result;
2778 arith rc;
2780 result = gfc_constant_result (BT_REAL, kind, &src->where);
2782 mpf_set (result->value.real, src->value.complex.r);
2784 rc = gfc_check_real_range (result->value.real, kind);
2786 if (rc == ARITH_UNDERFLOW)
2788 if (gfc_option.warn_underflow)
2789 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2790 mpf_set_ui(result->value.real, 0);
2792 if (rc != ARITH_OK)
2794 arith_error (rc, &src->ts, &result->ts, &src->where);
2795 gfc_free_expr (result);
2796 return NULL;
2799 return result;
2803 /* Convert complex to complex. */
2805 gfc_expr *
2806 gfc_complex2complex (gfc_expr * src, int kind)
2808 gfc_expr *result;
2809 arith rc;
2811 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2813 mpf_set (result->value.complex.r, src->value.complex.r);
2814 mpf_set (result->value.complex.i, src->value.complex.i);
2816 rc = gfc_check_real_range (result->value.complex.r, kind);
2818 if (rc == ARITH_UNDERFLOW)
2820 if (gfc_option.warn_underflow)
2821 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2822 mpf_set_ui(result->value.complex.r, 0);
2824 else if (rc != ARITH_OK)
2826 arith_error (rc, &src->ts, &result->ts, &src->where);
2827 gfc_free_expr (result);
2828 return NULL;
2831 rc = gfc_check_real_range (result->value.complex.i, kind);
2833 if (rc == ARITH_UNDERFLOW)
2835 if (gfc_option.warn_underflow)
2836 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2837 mpf_set_ui(result->value.complex.i, 0);
2839 else if (rc != ARITH_OK)
2841 arith_error (rc, &src->ts, &result->ts, &src->where);
2842 gfc_free_expr (result);
2843 return NULL;
2846 return result;
2850 /* Logical kind conversion. */
2852 gfc_expr *
2853 gfc_log2log (gfc_expr * src, int kind)
2855 gfc_expr *result;
2857 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2858 result->value.logical = src->value.logical;
2860 return result;