2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, 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
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
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, 51 Franklin Street, Fifth Floor, Boston, MA
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 and the MPFR
26 library to do arithmetic, and this file provides the interface. */
34 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
35 It's easily implemented with a few calls though. */
38 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x)
42 e = mpfr_get_z_exp (z, x);
43 /* MPFR 2.0.1 (included with GMP 4.1) has a bug whereby mpfr_get_z_exp
44 may set the sign of z incorrectly. Work around that here. */
45 if (mpfr_sgn (x) != mpz_sgn (z))
49 mpz_mul_2exp (z, z, e);
51 mpz_tdiv_q_2exp (z, z, -e);
55 /* Set the model number precision by the requested KIND. */
58 gfc_set_model_kind (int kind)
60 int index = gfc_validate_kind (BT_REAL, kind, false);
63 base2prec = gfc_real_kinds[index].digits;
64 if (gfc_real_kinds[index].radix != 2)
65 base2prec *= gfc_real_kinds[index].radix / 2;
66 mpfr_set_default_prec (base2prec);
70 /* Set the model number precision from mpfr_t x. */
73 gfc_set_model (mpfr_t x)
75 mpfr_set_default_prec (mpfr_get_prec (x));
78 #if defined(GFC_MPFR_TOO_OLD)
79 /* Calculate atan2 (y, x)
81 atan2(y, x) = atan(y/x) if x > 0,
82 sign(y)*(pi - atan(|y/x|)) if x < 0,
84 sign(y)*pi/2 if x = 0 && y != 0.
88 arctangent2 (mpfr_t y, mpfr_t x, mpfr_t result)
100 mpfr_div (t, y, x, GFC_RND_MODE);
101 mpfr_atan (result, t, GFC_RND_MODE);
105 mpfr_const_pi (result, GFC_RND_MODE);
106 mpfr_div (t, y, x, GFC_RND_MODE);
107 mpfr_abs (t, t, GFC_RND_MODE);
108 mpfr_atan (t, t, GFC_RND_MODE);
109 mpfr_sub (result, result, t, GFC_RND_MODE);
110 if (mpfr_sgn (y) < 0)
111 mpfr_neg (result, result, GFC_RND_MODE);
115 if (mpfr_sgn (y) == 0)
116 mpfr_set_ui (result, 0, GFC_RND_MODE);
119 mpfr_const_pi (result, GFC_RND_MODE);
120 mpfr_div_ui (result, result, 2, GFC_RND_MODE);
121 if (mpfr_sgn (y) < 0)
122 mpfr_neg (result, result, GFC_RND_MODE);
130 /* Given an arithmetic error code, return a pointer to a string that
131 explains the error. */
134 gfc_arith_error (arith code)
141 p = _("Arithmetic OK at %L");
144 p = _("Arithmetic overflow at %L");
146 case ARITH_UNDERFLOW:
147 p = _("Arithmetic underflow at %L");
150 p = _("Arithmetic NaN at %L");
153 p = _("Division by zero at %L");
155 case ARITH_INCOMMENSURATE:
156 p = _("Array operands are incommensurate at %L");
158 case ARITH_ASYMMETRIC:
160 _("Integer outside symmetric range implied by Standard Fortran at %L");
163 gfc_internal_error ("gfc_arith_error(): Bad error code");
170 /* Get things ready to do math. */
173 gfc_arith_init_1 (void)
175 gfc_integer_info *int_info;
176 gfc_real_info *real_info;
181 mpfr_set_default_prec (128);
185 /* Convert the minimum and maximum values for each kind into their
186 GNU MP representation. */
187 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
190 mpz_set_ui (r, int_info->radix);
191 mpz_pow_ui (r, r, int_info->digits);
193 mpz_init (int_info->huge);
194 mpz_sub_ui (int_info->huge, r, 1);
196 /* These are the numbers that are actually representable by the
197 target. For bases other than two, this needs to be changed. */
198 if (int_info->radix != 2)
199 gfc_internal_error ("Fix min_int calculation");
201 /* See PRs 13490 and 17912, related to integer ranges.
202 The pedantic_min_int exists for range checking when a program
203 is compiled with -pedantic, and reflects the belief that
204 Standard Fortran requires integers to be symmetrical, i.e.
205 every negative integer must have a representable positive
206 absolute value, and vice versa. */
208 mpz_init (int_info->pedantic_min_int);
209 mpz_neg (int_info->pedantic_min_int, int_info->huge);
211 mpz_init (int_info->min_int);
212 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
215 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
216 mpfr_log10 (a, a, GFC_RND_MODE);
218 gfc_mpfr_to_mpz (r, a);
219 int_info->range = mpz_get_si (r);
224 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
226 gfc_set_model_kind (real_info->kind);
232 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
233 /* a = 1 - b**(-p) */
234 mpfr_set_ui (a, 1, GFC_RND_MODE);
235 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
236 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
237 mpfr_sub (a, a, b, GFC_RND_MODE);
239 /* c = b**(emax-1) */
240 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
241 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
243 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
244 mpfr_mul (a, a, c, GFC_RND_MODE);
246 /* a = (1 - b**(-p)) * b**(emax-1) * b */
247 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
249 mpfr_init (real_info->huge);
250 mpfr_set (real_info->huge, a, GFC_RND_MODE);
252 /* tiny(x) = b**(emin-1) */
253 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
254 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
256 mpfr_init (real_info->tiny);
257 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
259 /* subnormal (x) = b**(emin - digit) */
260 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
261 mpfr_pow_si (b, b, real_info->min_exponent - real_info->digits,
264 mpfr_init (real_info->subnormal);
265 mpfr_set (real_info->subnormal, b, GFC_RND_MODE);
267 /* epsilon(x) = b**(1-p) */
268 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
269 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
271 mpfr_init (real_info->epsilon);
272 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
274 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
275 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
276 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
277 mpfr_neg (b, b, GFC_RND_MODE);
280 if (mpfr_cmp (a, b) > 0)
281 mpfr_set (a, b, GFC_RND_MODE);
284 gfc_mpfr_to_mpz (r, a);
285 real_info->range = mpz_get_si (r);
287 /* precision(x) = int((p - 1) * log10(b)) + k */
288 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
289 mpfr_log10 (a, a, GFC_RND_MODE);
291 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
293 gfc_mpfr_to_mpz (r, a);
294 real_info->precision = mpz_get_si (r);
296 /* If the radix is an integral power of 10, add one to the precision. */
297 for (i = 10; i <= real_info->radix; i *= 10)
298 if (i == real_info->radix)
299 real_info->precision++;
310 /* Clean up, get rid of numeric constants. */
313 gfc_arith_done_1 (void)
315 gfc_integer_info *ip;
318 for (ip = gfc_integer_kinds; ip->kind; ip++)
320 mpz_clear (ip->min_int);
321 mpz_clear (ip->pedantic_min_int);
322 mpz_clear (ip->huge);
325 for (rp = gfc_real_kinds; rp->kind; rp++)
327 mpfr_clear (rp->epsilon);
328 mpfr_clear (rp->huge);
329 mpfr_clear (rp->tiny);
330 mpfr_clear (rp->subnormal);
335 /* Given an integer and a kind, make sure that the integer lies within
336 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
340 gfc_check_integer_range (mpz_t p, int kind)
345 i = gfc_validate_kind (BT_INTEGER, kind, false);
350 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
351 result = ARITH_ASYMMETRIC;
355 if (gfc_option.flag_range_check == 0)
358 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
359 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
360 result = ARITH_OVERFLOW;
366 /* Given a real and a kind, make sure that the real lies within the
367 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
371 gfc_check_real_range (mpfr_t p, int kind)
377 i = gfc_validate_kind (BT_REAL, kind, false);
381 mpfr_abs (q, p, GFC_RND_MODE);
385 if (gfc_option.flag_range_check == 0)
388 retval = ARITH_OVERFLOW;
390 else if (mpfr_nan_p (p))
392 if (gfc_option.flag_range_check == 0)
397 else if (mpfr_sgn (q) == 0)
399 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
401 if (gfc_option.flag_range_check == 0)
404 retval = ARITH_OVERFLOW;
406 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
408 if (gfc_option.flag_range_check == 0)
411 retval = ARITH_UNDERFLOW;
413 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
415 #if defined(GFC_MPFR_TOO_OLD)
416 /* MPFR operates on a number with a given precision and enormous
417 exponential range. To represent subnormal numbers, the exponent is
418 allowed to become smaller than emin, but always retains the full
419 precision. This code resets unused bits to 0 to alleviate
420 rounding problems. Note, a future version of MPFR will have a
421 mpfr_subnormalize() function, which handles this truncation in a
422 more efficient and robust way. */
428 bin = mpfr_get_str (NULL, &e, gfc_real_kinds[i].radix, 0, q, GMP_RNDN);
429 k = gfc_real_kinds[i].digits - (gfc_real_kinds[i].min_exponent - e);
430 for (j = k; j < gfc_real_kinds[i].digits; j++)
432 /* Need space for '0.', bin, 'E', and e */
433 s = (char *) gfc_getmem (strlen(bin) + 10);
434 sprintf (s, "0.%sE%d", bin, (int) e);
435 mpfr_set_str (q, s, gfc_real_kinds[i].radix, GMP_RNDN);
443 /* Save current values of emin and emax. */
444 emin = mpfr_get_emin ();
445 emax = mpfr_get_emax ();
447 /* Set emin and emax for the current model number. */
448 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
449 mpfr_set_emin ((mp_exp_t) en);
450 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
451 mpfr_subnormalize (q, 0, GFC_RND_MODE);
453 /* Reset emin and emax. */
454 mpfr_set_emin (emin);
455 mpfr_set_emax (emax);
458 /* Copy sign if needed. */
459 if (mpfr_sgn (p) < 0)
460 mpfr_neg (p, q, GMP_RNDN);
462 mpfr_set (p, q, GMP_RNDN);
475 /* Function to return a constant expression node of a given type and kind. */
478 gfc_constant_result (bt type, int kind, locus * where)
484 ("gfc_constant_result(): locus 'where' cannot be NULL");
486 result = gfc_get_expr ();
488 result->expr_type = EXPR_CONSTANT;
489 result->ts.type = type;
490 result->ts.kind = kind;
491 result->where = *where;
496 mpz_init (result->value.integer);
500 gfc_set_model_kind (kind);
501 mpfr_init (result->value.real);
505 gfc_set_model_kind (kind);
506 mpfr_init (result->value.complex.r);
507 mpfr_init (result->value.complex.i);
518 /* Low-level arithmetic functions. All of these subroutines assume
519 that all operands are of the same type and return an operand of the
520 same type. The other thing about these subroutines is that they
521 can fail in various ways -- overflow, underflow, division by zero,
522 zero raised to the zero, etc. */
525 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
529 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
530 result->value.logical = !op1->value.logical;
538 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
542 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
544 result->value.logical = op1->value.logical && op2->value.logical;
552 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
556 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
558 result->value.logical = op1->value.logical || op2->value.logical;
566 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
570 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
572 result->value.logical = op1->value.logical == op2->value.logical;
580 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
584 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
586 result->value.logical = op1->value.logical != op2->value.logical;
593 /* Make sure a constant numeric expression is within the range for
594 its type and kind. Note that there's also a gfc_check_range(),
595 but that one deals with the intrinsic RANGE function. */
598 gfc_range_check (gfc_expr * e)
605 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
609 rc = gfc_check_real_range (e->value.real, e->ts.kind);
610 if (rc == ARITH_UNDERFLOW)
611 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
612 if (rc == ARITH_OVERFLOW)
613 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
615 mpfr_set_nan (e->value.real);
619 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
620 if (rc == ARITH_UNDERFLOW)
621 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
622 if (rc == ARITH_OVERFLOW)
623 mpfr_set_inf (e->value.complex.r, mpfr_sgn (e->value.complex.r));
625 mpfr_set_nan (e->value.complex.r);
627 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
628 if (rc == ARITH_UNDERFLOW)
629 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
630 if (rc == ARITH_OVERFLOW)
631 mpfr_set_inf (e->value.complex.i, mpfr_sgn (e->value.complex.i));
633 mpfr_set_nan (e->value.complex.i);
637 gfc_internal_error ("gfc_range_check(): Bad type");
644 /* Several of the following routines use the same set of statements to
645 check the validity of the result. Encapsulate the checking here. */
648 check_result (arith rc, gfc_expr * x, gfc_expr * r, gfc_expr ** rp)
652 if (val == ARITH_UNDERFLOW)
654 if (gfc_option.warn_underflow)
655 gfc_warning (gfc_arith_error (val), &x->where);
659 if (val == ARITH_ASYMMETRIC)
661 gfc_warning (gfc_arith_error (val), &x->where);
674 /* It may seem silly to have a subroutine that actually computes the
675 unary plus of a constant, but it prevents us from making exceptions
676 in the code elsewhere. */
679 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
681 *resultp = gfc_copy_expr (op1);
687 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
692 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
694 switch (op1->ts.type)
697 mpz_neg (result->value.integer, op1->value.integer);
701 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
705 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
706 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
710 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
713 rc = gfc_range_check (result);
715 return check_result (rc, op1, result, resultp);
720 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
725 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
727 switch (op1->ts.type)
730 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
734 mpfr_add (result->value.real, op1->value.real, op2->value.real,
739 mpfr_add (result->value.complex.r, op1->value.complex.r,
740 op2->value.complex.r, GFC_RND_MODE);
742 mpfr_add (result->value.complex.i, op1->value.complex.i,
743 op2->value.complex.i, GFC_RND_MODE);
747 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
750 rc = gfc_range_check (result);
752 return check_result (rc, op1, result, resultp);
757 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
762 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
764 switch (op1->ts.type)
767 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
771 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
776 mpfr_sub (result->value.complex.r, op1->value.complex.r,
777 op2->value.complex.r, GFC_RND_MODE);
779 mpfr_sub (result->value.complex.i, op1->value.complex.i,
780 op2->value.complex.i, GFC_RND_MODE);
784 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
787 rc = gfc_range_check (result);
789 return check_result (rc, op1, result, resultp);
794 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
800 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
802 switch (op1->ts.type)
805 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
809 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
814 gfc_set_model (op1->value.complex.r);
818 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
819 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
820 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
822 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
823 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
824 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
831 gfc_internal_error ("gfc_arith_times(): Bad basic type");
834 rc = gfc_range_check (result);
836 return check_result (rc, op1, result, resultp);
841 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
849 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
851 switch (op1->ts.type)
854 if (mpz_sgn (op2->value.integer) == 0)
860 mpz_tdiv_q (result->value.integer, op1->value.integer,
865 if (mpfr_sgn (op2->value.real) == 0
866 && gfc_option.flag_range_check == 1)
872 mpfr_div (result->value.real, op1->value.real, op2->value.real,
877 if (mpfr_sgn (op2->value.complex.r) == 0
878 && mpfr_sgn (op2->value.complex.i) == 0
879 && gfc_option.flag_range_check == 1)
885 gfc_set_model (op1->value.complex.r);
890 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
891 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
892 mpfr_add (div, x, y, GFC_RND_MODE);
894 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
895 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
896 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
897 mpfr_div (result->value.complex.r, result->value.complex.r, div,
900 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
901 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
902 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
903 mpfr_div (result->value.complex.i, result->value.complex.i, div,
912 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
916 rc = gfc_range_check (result);
918 return check_result (rc, op1, result, resultp);
922 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
925 complex_reciprocal (gfc_expr * op)
927 mpfr_t mod, a, re, im;
929 gfc_set_model (op->value.complex.r);
935 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
936 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
937 mpfr_add (mod, mod, a, GFC_RND_MODE);
939 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
941 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
942 mpfr_div (im, im, mod, GFC_RND_MODE);
944 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
945 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
954 /* Raise a complex number to positive power. */
957 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
961 gfc_set_model (base->value.complex.r);
966 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
967 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
969 for (; power > 0; power--)
971 mpfr_mul (re, base->value.complex.r, result->value.complex.r,
973 mpfr_mul (a, base->value.complex.i, result->value.complex.i,
975 mpfr_sub (re, re, a, GFC_RND_MODE);
977 mpfr_mul (im, base->value.complex.r, result->value.complex.i,
979 mpfr_mul (a, base->value.complex.i, result->value.complex.r,
981 mpfr_add (im, im, a, GFC_RND_MODE);
983 mpfr_set (result->value.complex.r, re, GFC_RND_MODE);
984 mpfr_set (result->value.complex.i, im, GFC_RND_MODE);
993 /* Raise a number to an integer power. */
996 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1006 if (gfc_extract_int (op2, &power) != NULL)
1007 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1009 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1013 /* Handle something to the zeroth power. Since we're dealing
1014 with integral exponents, there is no ambiguity in the
1015 limiting procedure used to determine the value of 0**0. */
1016 switch (op1->ts.type)
1019 mpz_set_ui (result->value.integer, 1);
1023 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
1027 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1028 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1032 gfc_internal_error ("gfc_arith_power(): Bad base");
1041 switch (op1->ts.type)
1044 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1048 mpz_init_set_ui (unity_z, 1);
1049 mpz_tdiv_q (result->value.integer, unity_z,
1050 result->value.integer);
1051 mpz_clear (unity_z);
1056 mpfr_pow_ui (result->value.real, op1->value.real, apower,
1061 gfc_set_model (op1->value.real);
1062 mpfr_init (unity_f);
1063 mpfr_set_ui (unity_f, 1, GFC_RND_MODE);
1064 mpfr_div (result->value.real, unity_f, result->value.real,
1066 mpfr_clear (unity_f);
1071 complex_pow_ui (op1, apower, result);
1073 complex_reciprocal (result);
1082 rc = gfc_range_check (result);
1084 return check_result (rc, op1, result, resultp);
1088 /* Concatenate two string constants. */
1091 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1096 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
1099 len = op1->value.character.length + op2->value.character.length;
1101 result->value.character.string = gfc_getmem (len + 1);
1102 result->value.character.length = len;
1104 memcpy (result->value.character.string, op1->value.character.string,
1105 op1->value.character.length);
1107 memcpy (result->value.character.string + op1->value.character.length,
1108 op2->value.character.string, op2->value.character.length);
1110 result->value.character.string[len] = '\0';
1118 /* Comparison operators. Assumes that the two expression nodes
1119 contain two constants of the same type. */
1122 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1126 switch (op1->ts.type)
1129 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1133 rc = mpfr_cmp (op1->value.real, op2->value.real);
1137 rc = gfc_compare_string (op1, op2, NULL);
1141 rc = ((!op1->value.logical && op2->value.logical)
1142 || (op1->value.logical && !op2->value.logical));
1146 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1153 /* Compare a pair of complex numbers. Naturally, this is only for
1154 equality and nonequality. */
1157 compare_complex (gfc_expr * op1, gfc_expr * op2)
1159 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1160 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1164 /* Given two constant strings and the inverse collating sequence, compare the
1165 strings. We return -1 for a < b, 0 for a == b and 1 for a > b. If the
1166 xcoll_table is NULL, we use the processor's default collating sequence. */
1169 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int * xcoll_table)
1171 int len, alen, blen, i, ac, bc;
1173 alen = a->value.character.length;
1174 blen = b->value.character.length;
1176 len = (alen > blen) ? alen : blen;
1178 for (i = 0; i < len; i++)
1180 /* We cast to unsigned char because default char, if it is signed,
1181 would lead to ac < 0 for string[i] > 127. */
1182 ac = (unsigned char) ((i < alen) ? a->value.character.string[i] : ' ');
1183 bc = (unsigned char) ((i < blen) ? b->value.character.string[i] : ' ');
1185 if (xcoll_table != NULL)
1187 ac = xcoll_table[ac];
1188 bc = xcoll_table[bc];
1197 /* Strings are equal */
1203 /* Specific comparison subroutines. */
1206 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1210 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1212 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1213 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1221 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1225 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1227 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1228 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1236 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1240 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1242 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1250 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1254 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1256 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1264 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1268 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1270 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1278 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1282 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1284 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1292 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1295 gfc_constructor *c, *head;
1299 if (op->expr_type == EXPR_CONSTANT)
1300 return eval (op, result);
1303 head = gfc_copy_constructor (op->value.constructor);
1305 for (c = head; c; c = c->next)
1307 rc = eval (c->expr, &r);
1311 gfc_replace_expr (c->expr, r);
1315 gfc_free_constructor (head);
1318 r = gfc_get_expr ();
1319 r->expr_type = EXPR_ARRAY;
1320 r->value.constructor = head;
1321 r->shape = gfc_copy_shape (op->shape, op->rank);
1323 r->ts = head->expr->ts;
1324 r->where = op->where;
1335 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1336 gfc_expr * op1, gfc_expr * op2,
1339 gfc_constructor *c, *head;
1343 head = gfc_copy_constructor (op1->value.constructor);
1346 for (c = head; c; c = c->next)
1348 rc = eval (c->expr, op2, &r);
1352 gfc_replace_expr (c->expr, r);
1356 gfc_free_constructor (head);
1359 r = gfc_get_expr ();
1360 r->expr_type = EXPR_ARRAY;
1361 r->value.constructor = head;
1362 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1364 r->ts = head->expr->ts;
1365 r->where = op1->where;
1366 r->rank = op1->rank;
1376 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1377 gfc_expr * op1, gfc_expr * op2,
1380 gfc_constructor *c, *head;
1384 head = gfc_copy_constructor (op2->value.constructor);
1387 for (c = head; c; c = c->next)
1389 rc = eval (op1, c->expr, &r);
1393 gfc_replace_expr (c->expr, r);
1397 gfc_free_constructor (head);
1400 r = gfc_get_expr ();
1401 r->expr_type = EXPR_ARRAY;
1402 r->value.constructor = head;
1403 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1405 r->ts = head->expr->ts;
1406 r->where = op2->where;
1407 r->rank = op2->rank;
1417 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1418 gfc_expr * op1, gfc_expr * op2,
1421 gfc_constructor *c, *d, *head;
1425 head = gfc_copy_constructor (op1->value.constructor);
1428 d = op2->value.constructor;
1430 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1432 rc = ARITH_INCOMMENSURATE;
1436 for (c = head; c; c = c->next, d = d->next)
1440 rc = ARITH_INCOMMENSURATE;
1444 rc = eval (c->expr, d->expr, &r);
1448 gfc_replace_expr (c->expr, r);
1452 rc = ARITH_INCOMMENSURATE;
1456 gfc_free_constructor (head);
1459 r = gfc_get_expr ();
1460 r->expr_type = EXPR_ARRAY;
1461 r->value.constructor = head;
1462 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1464 r->ts = head->expr->ts;
1465 r->where = op1->where;
1466 r->rank = op1->rank;
1476 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1477 gfc_expr * op1, gfc_expr * op2,
1480 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1481 return eval (op1, op2, result);
1483 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1484 return reduce_binary_ca (eval, op1, op2, result);
1486 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1487 return reduce_binary_ac (eval, op1, op2, result);
1489 return reduce_binary_aa (eval, op1, op2, result);
1495 arith (*f2)(gfc_expr *, gfc_expr **);
1496 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1500 /* High level arithmetic subroutines. These subroutines go into
1501 eval_intrinsic(), which can do one of several things to its
1502 operands. If the operands are incompatible with the intrinsic
1503 operation, we return a node pointing to the operands and hope that
1504 an operator interface is found during resolution.
1506 If the operands are compatible and are constants, then we try doing
1507 the arithmetic. We also handle the cases where either or both
1508 operands are array constructors. */
1511 eval_intrinsic (gfc_intrinsic_op operator,
1512 eval_f eval, gfc_expr * op1, gfc_expr * op2)
1514 gfc_expr temp, *result;
1518 gfc_clear_ts (&temp.ts);
1524 if (op1->ts.type != BT_LOGICAL)
1527 temp.ts.type = BT_LOGICAL;
1528 temp.ts.kind = gfc_default_logical_kind;
1533 /* Logical binary operators */
1536 case INTRINSIC_NEQV:
1538 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1541 temp.ts.type = BT_LOGICAL;
1542 temp.ts.kind = gfc_default_logical_kind;
1548 case INTRINSIC_UPLUS:
1549 case INTRINSIC_UMINUS:
1550 if (!gfc_numeric_ts (&op1->ts))
1558 case INTRINSIC_PARENTHESES:
1564 /* Additional restrictions for ordering relations. */
1569 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1571 temp.ts.type = BT_LOGICAL;
1572 temp.ts.kind = gfc_default_logical_kind;
1579 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1582 temp.ts.type = BT_LOGICAL;
1583 temp.ts.kind = gfc_default_logical_kind;
1588 /* Numeric binary */
1589 case INTRINSIC_PLUS:
1590 case INTRINSIC_MINUS:
1591 case INTRINSIC_TIMES:
1592 case INTRINSIC_DIVIDE:
1593 case INTRINSIC_POWER:
1594 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1597 /* Insert any necessary type conversions to make the operands
1600 temp.expr_type = EXPR_OP;
1601 gfc_clear_ts (&temp.ts);
1602 temp.value.op.operator = operator;
1604 temp.value.op.op1 = op1;
1605 temp.value.op.op2 = op2;
1607 gfc_type_convert_binary (&temp);
1609 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1610 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1611 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
1613 temp.ts.type = BT_LOGICAL;
1614 temp.ts.kind = gfc_default_logical_kind;
1620 /* Character binary */
1621 case INTRINSIC_CONCAT:
1622 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1625 temp.ts.type = BT_CHARACTER;
1626 temp.ts.kind = gfc_default_character_kind;
1631 case INTRINSIC_USER:
1635 gfc_internal_error ("eval_intrinsic(): Bad operator");
1638 /* Try to combine the operators. */
1639 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1643 || (op1->expr_type != EXPR_CONSTANT
1644 && (op1->expr_type != EXPR_ARRAY
1645 || !gfc_is_constant_expr (op1)
1646 || !gfc_expanded_ac (op1))))
1651 || (op2->expr_type != EXPR_CONSTANT
1652 && (op2->expr_type != EXPR_ARRAY
1653 || !gfc_is_constant_expr (op2)
1654 || !gfc_expanded_ac (op2)))))
1658 rc = reduce_unary (eval.f2, op1, &result);
1660 rc = reduce_binary (eval.f3, op1, op2, &result);
1663 { /* Something went wrong. */
1664 gfc_error (gfc_arith_error (rc), &op1->where);
1668 gfc_free_expr (op1);
1669 gfc_free_expr (op2);
1673 /* Create a run-time expression. */
1674 result = gfc_get_expr ();
1675 result->ts = temp.ts;
1677 result->expr_type = EXPR_OP;
1678 result->value.op.operator = operator;
1680 result->value.op.op1 = op1;
1681 result->value.op.op2 = op2;
1683 result->where = op1->where;
1689 /* Modify type of expression for zero size array. */
1692 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr * op)
1695 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1705 op->ts.type = BT_LOGICAL;
1706 op->ts.kind = gfc_default_logical_kind;
1717 /* Return nonzero if the expression is a zero size array. */
1720 gfc_zero_size_array (gfc_expr * e)
1722 if (e->expr_type != EXPR_ARRAY)
1725 return e->value.constructor == NULL;
1729 /* Reduce a binary expression where at least one of the operands
1730 involves a zero-length array. Returns NULL if neither of the
1731 operands is a zero-length array. */
1734 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
1736 if (gfc_zero_size_array (op1))
1738 gfc_free_expr (op2);
1742 if (gfc_zero_size_array (op2))
1744 gfc_free_expr (op1);
1753 eval_intrinsic_f2 (gfc_intrinsic_op operator,
1754 arith (*eval) (gfc_expr *, gfc_expr **),
1755 gfc_expr * op1, gfc_expr * op2)
1762 if (gfc_zero_size_array (op1))
1763 return eval_type_intrinsic0 (operator, op1);
1767 result = reduce_binary0 (op1, op2);
1769 return eval_type_intrinsic0 (operator, result);
1773 return eval_intrinsic (operator, f, op1, op2);
1778 eval_intrinsic_f3 (gfc_intrinsic_op operator,
1779 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1780 gfc_expr * op1, gfc_expr * op2)
1785 result = reduce_binary0 (op1, op2);
1787 return eval_type_intrinsic0(operator, result);
1790 return eval_intrinsic (operator, f, op1, op2);
1795 gfc_uplus (gfc_expr * op)
1797 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
1802 gfc_uminus (gfc_expr * op)
1804 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1809 gfc_add (gfc_expr * op1, gfc_expr * op2)
1811 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1816 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
1818 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1823 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
1825 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1830 gfc_divide (gfc_expr * op1, gfc_expr * op2)
1832 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1837 gfc_power (gfc_expr * op1, gfc_expr * op2)
1839 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
1844 gfc_concat (gfc_expr * op1, gfc_expr * op2)
1846 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1851 gfc_and (gfc_expr * op1, gfc_expr * op2)
1853 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1858 gfc_or (gfc_expr * op1, gfc_expr * op2)
1860 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1865 gfc_not (gfc_expr * op1)
1867 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1872 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
1874 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1879 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
1881 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1886 gfc_eq (gfc_expr * op1, gfc_expr * op2)
1888 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
1893 gfc_ne (gfc_expr * op1, gfc_expr * op2)
1895 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
1900 gfc_gt (gfc_expr * op1, gfc_expr * op2)
1902 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
1907 gfc_ge (gfc_expr * op1, gfc_expr * op2)
1909 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
1914 gfc_lt (gfc_expr * op1, gfc_expr * op2)
1916 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
1921 gfc_le (gfc_expr * op1, gfc_expr * op2)
1923 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
1927 /* Convert an integer string to an expression node. */
1930 gfc_convert_integer (const char * buffer, int kind, int radix, locus * where)
1935 e = gfc_constant_result (BT_INTEGER, kind, where);
1936 /* A leading plus is allowed, but not by mpz_set_str. */
1937 if (buffer[0] == '+')
1941 mpz_set_str (e->value.integer, t, radix);
1947 /* Convert a real string to an expression node. */
1950 gfc_convert_real (const char * buffer, int kind, locus * where)
1954 e = gfc_constant_result (BT_REAL, kind, where);
1955 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1961 /* Convert a pair of real, constant expression nodes to a single
1962 complex expression node. */
1965 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
1969 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
1970 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
1971 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
1977 /******* Simplification of intrinsic functions with constant arguments *****/
1980 /* Deal with an arithmetic error. */
1983 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
1988 gfc_error ("Arithmetic OK converting %s to %s at %L",
1989 gfc_typename (from), gfc_typename (to), where);
1991 case ARITH_OVERFLOW:
1992 gfc_error ("Arithmetic overflow converting %s to %s at %L",
1993 gfc_typename (from), gfc_typename (to), where);
1995 case ARITH_UNDERFLOW:
1996 gfc_error ("Arithmetic underflow converting %s to %s at %L",
1997 gfc_typename (from), gfc_typename (to), where);
2000 gfc_error ("Arithmetic NaN converting %s to %s at %L",
2001 gfc_typename (from), gfc_typename (to), where);
2004 gfc_error ("Division by zero converting %s to %s at %L",
2005 gfc_typename (from), gfc_typename (to), where);
2007 case ARITH_INCOMMENSURATE:
2008 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
2009 gfc_typename (from), gfc_typename (to), where);
2011 case ARITH_ASYMMETRIC:
2012 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
2013 " converting %s to %s at %L",
2014 gfc_typename (from), gfc_typename (to), where);
2017 gfc_internal_error ("gfc_arith_error(): Bad error code");
2020 /* TODO: Do something about the error, ie, throw exception, return
2025 /* Convert integers to integers. */
2028 gfc_int2int (gfc_expr * src, int kind)
2033 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2035 mpz_set (result->value.integer, src->value.integer);
2037 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2040 if (rc == ARITH_ASYMMETRIC)
2042 gfc_warning (gfc_arith_error (rc), &src->where);
2046 arith_error (rc, &src->ts, &result->ts, &src->where);
2047 gfc_free_expr (result);
2056 /* Convert integers to reals. */
2059 gfc_int2real (gfc_expr * src, int kind)
2064 result = gfc_constant_result (BT_REAL, kind, &src->where);
2066 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2068 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2070 arith_error (rc, &src->ts, &result->ts, &src->where);
2071 gfc_free_expr (result);
2079 /* Convert default integer to default complex. */
2082 gfc_int2complex (gfc_expr * src, int kind)
2087 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2089 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2090 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2092 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2094 arith_error (rc, &src->ts, &result->ts, &src->where);
2095 gfc_free_expr (result);
2103 /* Convert default real to default integer. */
2106 gfc_real2int (gfc_expr * src, int kind)
2111 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2113 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2115 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2118 arith_error (rc, &src->ts, &result->ts, &src->where);
2119 gfc_free_expr (result);
2127 /* Convert real to real. */
2130 gfc_real2real (gfc_expr * src, int kind)
2135 result = gfc_constant_result (BT_REAL, kind, &src->where);
2137 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2139 rc = gfc_check_real_range (result->value.real, kind);
2141 if (rc == ARITH_UNDERFLOW)
2143 if (gfc_option.warn_underflow)
2144 gfc_warning (gfc_arith_error (rc), &src->where);
2145 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2147 else if (rc != ARITH_OK)
2149 arith_error (rc, &src->ts, &result->ts, &src->where);
2150 gfc_free_expr (result);
2158 /* Convert real to complex. */
2161 gfc_real2complex (gfc_expr * src, int kind)
2166 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2168 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2169 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2171 rc = gfc_check_real_range (result->value.complex.r, kind);
2173 if (rc == ARITH_UNDERFLOW)
2175 if (gfc_option.warn_underflow)
2176 gfc_warning (gfc_arith_error (rc), &src->where);
2177 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2179 else if (rc != ARITH_OK)
2181 arith_error (rc, &src->ts, &result->ts, &src->where);
2182 gfc_free_expr (result);
2190 /* Convert complex to integer. */
2193 gfc_complex2int (gfc_expr * src, int kind)
2198 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2200 gfc_mpfr_to_mpz (result->value.integer, src->value.complex.r);
2202 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2205 arith_error (rc, &src->ts, &result->ts, &src->where);
2206 gfc_free_expr (result);
2214 /* Convert complex to real. */
2217 gfc_complex2real (gfc_expr * src, int kind)
2222 result = gfc_constant_result (BT_REAL, kind, &src->where);
2224 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2226 rc = gfc_check_real_range (result->value.real, kind);
2228 if (rc == ARITH_UNDERFLOW)
2230 if (gfc_option.warn_underflow)
2231 gfc_warning (gfc_arith_error (rc), &src->where);
2232 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2236 arith_error (rc, &src->ts, &result->ts, &src->where);
2237 gfc_free_expr (result);
2245 /* Convert complex to complex. */
2248 gfc_complex2complex (gfc_expr * src, int kind)
2253 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2255 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2256 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2258 rc = gfc_check_real_range (result->value.complex.r, kind);
2260 if (rc == ARITH_UNDERFLOW)
2262 if (gfc_option.warn_underflow)
2263 gfc_warning (gfc_arith_error (rc), &src->where);
2264 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2266 else if (rc != ARITH_OK)
2268 arith_error (rc, &src->ts, &result->ts, &src->where);
2269 gfc_free_expr (result);
2273 rc = gfc_check_real_range (result->value.complex.i, kind);
2275 if (rc == ARITH_UNDERFLOW)
2277 if (gfc_option.warn_underflow)
2278 gfc_warning (gfc_arith_error (rc), &src->where);
2279 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2281 else if (rc != ARITH_OK)
2283 arith_error (rc, &src->ts, &result->ts, &src->where);
2284 gfc_free_expr (result);
2292 /* Logical kind conversion. */
2295 gfc_log2log (gfc_expr * src, int kind)
2299 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2300 result->value.logical = src->value.logical;
2306 /* Convert logical to integer. */
2309 gfc_log2int (gfc_expr *src, int kind)
2313 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2314 mpz_set_si (result->value.integer, src->value.logical);
2320 /* Convert integer to logical. */
2323 gfc_int2log (gfc_expr *src, int kind)
2327 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2328 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2334 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2337 gfc_hollerith2int (gfc_expr * src, int kind)
2342 len = src->value.character.length;
2344 result = gfc_get_expr ();
2345 result->expr_type = EXPR_CONSTANT;
2346 result->ts.type = BT_INTEGER;
2347 result->ts.kind = kind;
2348 result->where = src->where;
2353 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2354 &src->where, gfc_typename(&result->ts));
2356 result->value.character.string = gfc_getmem (kind + 1);
2357 memcpy (result->value.character.string, src->value.character.string,
2361 memset (&result->value.character.string[len], ' ', kind - len);
2363 result->value.character.string[kind] = '\0'; /* For debugger */
2364 result->value.character.length = kind;
2370 /* Convert Hollerith to real. The constant will be padded or truncated. */
2373 gfc_hollerith2real (gfc_expr * src, int kind)
2378 len = src->value.character.length;
2380 result = gfc_get_expr ();
2381 result->expr_type = EXPR_CONSTANT;
2382 result->ts.type = BT_REAL;
2383 result->ts.kind = kind;
2384 result->where = src->where;
2389 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2390 &src->where, gfc_typename(&result->ts));
2392 result->value.character.string = gfc_getmem (kind + 1);
2393 memcpy (result->value.character.string, src->value.character.string,
2397 memset (&result->value.character.string[len], ' ', kind - len);
2399 result->value.character.string[kind] = '\0'; /* For debugger. */
2400 result->value.character.length = kind;
2406 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2409 gfc_hollerith2complex (gfc_expr * src, int kind)
2414 len = src->value.character.length;
2416 result = gfc_get_expr ();
2417 result->expr_type = EXPR_CONSTANT;
2418 result->ts.type = BT_COMPLEX;
2419 result->ts.kind = kind;
2420 result->where = src->where;
2427 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2428 &src->where, gfc_typename(&result->ts));
2430 result->value.character.string = gfc_getmem (kind + 1);
2431 memcpy (result->value.character.string, src->value.character.string,
2435 memset (&result->value.character.string[len], ' ', kind - len);
2437 result->value.character.string[kind] = '\0'; /* For debugger */
2438 result->value.character.length = kind;
2444 /* Convert Hollerith to character. */
2447 gfc_hollerith2character (gfc_expr * src, int kind)
2451 result = gfc_copy_expr (src);
2452 result->ts.type = BT_CHARACTER;
2453 result->ts.kind = kind;
2460 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2463 gfc_hollerith2logical (gfc_expr * src, int kind)
2468 len = src->value.character.length;
2470 result = gfc_get_expr ();
2471 result->expr_type = EXPR_CONSTANT;
2472 result->ts.type = BT_LOGICAL;
2473 result->ts.kind = kind;
2474 result->where = src->where;
2479 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2480 &src->where, gfc_typename(&result->ts));
2482 result->value.character.string = gfc_getmem (kind + 1);
2483 memcpy (result->value.character.string, src->value.character.string,
2487 memset (&result->value.character.string[len], ' ', kind - len);
2489 result->value.character.string[kind] = '\0'; /* For debugger */
2490 result->value.character.length = kind;
2496 /* Returns an initializer whose value is one higher than the value of the
2497 LAST_INITIALIZER argument. If the argument is NULL, the
2498 initializers value will be set to zero. The initializer's kind
2499 will be set to gfc_c_int_kind.
2501 If -fshort-enums is given, the appropriate kind will be selected
2502 later after all enumerators have been parsed. A warning is issued
2503 here if an initializer exceeds gfc_c_int_kind. */
2506 gfc_enum_initializer (gfc_expr * last_initializer, locus where)
2510 result = gfc_get_expr ();
2511 result->expr_type = EXPR_CONSTANT;
2512 result->ts.type = BT_INTEGER;
2513 result->ts.kind = gfc_c_int_kind;
2514 result->where = where;
2516 mpz_init (result->value.integer);
2518 if (last_initializer != NULL)
2520 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
2521 result->where = last_initializer->where;
2523 if (gfc_check_integer_range (result->value.integer,
2524 gfc_c_int_kind) != ARITH_OK)
2526 gfc_error ("Enumerator exceeds the C integer type at %C");
2532 /* Control comes here, if it's the very first enumerator and no
2533 initializer has been given. It will be initialized to zero. */
2534 mpz_set_si (result->value.integer, 0);