2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
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, 59 Temple Place - Suite 330, 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 to do arithmetic,
26 and this file provides the interface. */
35 /* The gfc_(integer|real)_kinds[] structures have everything the front
36 end needs to know about integers and real numbers on the target.
37 Other entries of the structure are calculated from these values.
38 The first entry is the default kind, the second entry of the real
39 structure is the default double kind. */
41 #define MPZ_NULL {{0,0,0}}
42 #define MPF_NULL {{0,0,0,0}}
44 #define DEF_GFC_INTEGER_KIND(KIND,RADIX,DIGITS,BIT_SIZE) \
45 {KIND, RADIX, DIGITS, BIT_SIZE, 0, MPZ_NULL, MPZ_NULL, MPZ_NULL}
47 #define DEF_GFC_LOGICAL_KIND(KIND,BIT_SIZE) \
50 #define DEF_GFC_REAL_KIND(KIND,RADIX,DIGITS,MIN_EXP, MAX_EXP) \
51 {KIND, RADIX, DIGITS, MIN_EXP, MAX_EXP, \
52 0, 0, MPF_NULL, MPF_NULL, MPF_NULL}
54 gfc_integer_info gfc_integer_kinds[] = {
55 DEF_GFC_INTEGER_KIND (4, 2, 31, 32),
56 DEF_GFC_INTEGER_KIND (8, 2, 63, 64),
57 DEF_GFC_INTEGER_KIND (2, 2, 15, 16),
58 DEF_GFC_INTEGER_KIND (1, 2, 7, 8),
59 DEF_GFC_INTEGER_KIND (0, 0, 0, 0)
62 gfc_logical_info gfc_logical_kinds[] = {
63 DEF_GFC_LOGICAL_KIND (4, 32),
64 DEF_GFC_LOGICAL_KIND (8, 64),
65 DEF_GFC_LOGICAL_KIND (2, 16),
66 DEF_GFC_LOGICAL_KIND (1, 8),
67 DEF_GFC_LOGICAL_KIND (0, 0)
71 /* IEEE-754 uses 1.xEe representation whereas the fortran standard
72 uses 0.xEe representation. Hence the exponents below are biased
76 #define GFC_SP_PREC 24 /* p = 24, IEEE-754 */
77 #define GFC_SP_EMIN -125 /* emin = -126, IEEE-754 */
78 #define GFC_SP_EMAX 128 /* emin = 127, IEEE-754 */
80 /* Double precision model numbers. */
82 #define GFC_DP_PREC 53 /* p = 53, IEEE-754 */
83 #define GFC_DP_EMIN -1021 /* emin = -1022, IEEE-754 */
84 #define GFC_DP_EMAX 1024 /* emin = 1023, IEEE-754 */
86 /* Quad precision model numbers. Not used. */
87 #define GFC_QP_KIND 16
88 #define GFC_QP_PREC 113 /* p = 113, IEEE-754 */
89 #define GFC_QP_EMIN -16381 /* emin = -16382, IEEE-754 */
90 #define GFC_QP_EMAX 16384 /* emin = 16383, IEEE-754 */
92 gfc_real_info gfc_real_kinds[] = {
93 DEF_GFC_REAL_KIND (GFC_SP_KIND, 2, GFC_SP_PREC, GFC_SP_EMIN, GFC_SP_EMAX),
94 DEF_GFC_REAL_KIND (GFC_DP_KIND, 2, GFC_DP_PREC, GFC_DP_EMIN, GFC_DP_EMAX),
95 DEF_GFC_REAL_KIND (0, 0, 0, 0, 0)
99 /* The integer kind to use for array indices. This will be set to the
100 proper value based on target information from the backend. */
102 int gfc_index_integer_kind;
105 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
106 It's easily implemented with a few calls though. */
109 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x)
113 e = mpfr_get_z_exp (z, x);
114 /* MPFR 2.0.1 (included with GMP 4.1) has a bug whereby mpfr_get_z_exp
115 may set the sign of z incorrectly. Work around that here. */
116 if (mpfr_sgn (x) != mpz_sgn (z))
120 mpz_mul_2exp (z, z, e);
122 mpz_tdiv_q_2exp (z, z, -e);
126 /* Set the model number precision by the requested KIND. */
129 gfc_set_model_kind (int kind)
134 mpfr_set_default_prec (GFC_SP_PREC);
137 mpfr_set_default_prec (GFC_DP_PREC);
140 mpfr_set_default_prec (GFC_QP_PREC);
143 gfc_internal_error ("gfc_set_model_kind(): Bad model number");
148 /* Set the model number precision from mpfr_t x. */
151 gfc_set_model (mpfr_t x)
153 switch (mpfr_get_prec (x))
156 mpfr_set_default_prec (GFC_SP_PREC);
159 mpfr_set_default_prec (GFC_DP_PREC);
162 mpfr_set_default_prec (GFC_QP_PREC);
165 gfc_internal_error ("gfc_set_model(): Bad model number");
169 /* Calculate atan2 (y, x)
171 atan2(y, x) = atan(y/x) if x > 0,
172 sign(y)*(pi - atan(|y/x|)) if x < 0,
173 0 if x = 0 && y == 0,
174 sign(y)*pi/2 if x = 0 && y != 0.
178 arctangent2 (mpfr_t y, mpfr_t x, mpfr_t result)
190 mpfr_div (t, y, x, GFC_RND_MODE);
191 mpfr_atan (result, t, GFC_RND_MODE);
195 mpfr_const_pi (result, GFC_RND_MODE);
196 mpfr_div (t, y, x, GFC_RND_MODE);
197 mpfr_abs (t, t, GFC_RND_MODE);
198 mpfr_atan (t, t, GFC_RND_MODE);
199 mpfr_sub (result, result, t, GFC_RND_MODE);
200 if (mpfr_sgn (y) < 0)
201 mpfr_neg (result, result, GFC_RND_MODE);
205 if (mpfr_sgn (y) == 0)
206 mpfr_set_ui (result, 0, GFC_RND_MODE);
209 mpfr_const_pi (result, GFC_RND_MODE);
210 mpfr_div_ui (result, result, 2, GFC_RND_MODE);
211 if (mpfr_sgn (y) < 0)
212 mpfr_neg (result, result, GFC_RND_MODE);
221 /* Given an arithmetic error code, return a pointer to a string that
222 explains the error. */
225 gfc_arith_error (arith code)
235 p = "Arithmetic overflow";
237 case ARITH_UNDERFLOW:
238 p = "Arithmetic underflow";
241 p = "Arithmetic NaN";
244 p = "Division by zero";
247 p = "Indeterminate form 0 ** 0";
249 case ARITH_INCOMMENSURATE:
250 p = "Array operands are incommensurate";
253 gfc_internal_error ("gfc_arith_error(): Bad error code");
260 /* Get things ready to do math. */
263 gfc_arith_init_1 (void)
265 gfc_integer_info *int_info;
266 gfc_real_info *real_info;
271 gfc_set_model_kind (GFC_QP_KIND);
276 /* Convert the minimum/maximum values for each kind into their
277 GNU MP representation. */
278 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
281 mpz_set_ui (r, int_info->radix);
282 mpz_pow_ui (r, r, int_info->digits);
284 mpz_init (int_info->huge);
285 mpz_sub_ui (int_info->huge, r, 1);
287 /* These are the numbers that are actually representable by the
288 target. For bases other than two, this needs to be changed. */
289 if (int_info->radix != 2)
290 gfc_internal_error ("Fix min_int, max_int calculation");
292 mpz_init (int_info->min_int);
293 mpz_neg (int_info->min_int, int_info->huge);
294 /* No -1 here, because the representation is symmetric. */
296 mpz_init (int_info->max_int);
297 mpz_add (int_info->max_int, int_info->huge, int_info->huge);
298 mpz_add_ui (int_info->max_int, int_info->max_int, 1);
301 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
302 mpfr_log10 (a, a, GFC_RND_MODE);
304 gfc_mpfr_to_mpz (r, a);
305 int_info->range = mpz_get_si (r);
310 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
312 gfc_set_model_kind (real_info->kind);
318 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
319 /* a = 1 - b**(-p) */
320 mpfr_set_ui (a, 1, GFC_RND_MODE);
321 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
322 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
323 mpfr_sub (a, a, b, GFC_RND_MODE);
325 /* c = b**(emax-1) */
326 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
327 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
329 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
330 mpfr_mul (a, a, c, GFC_RND_MODE);
332 /* a = (1 - b**(-p)) * b**(emax-1) * b */
333 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
335 mpfr_init (real_info->huge);
336 mpfr_set (real_info->huge, a, GFC_RND_MODE);
338 /* tiny(x) = b**(emin-1) */
339 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
340 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
342 mpfr_init (real_info->tiny);
343 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
345 /* epsilon(x) = b**(1-p) */
346 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
347 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
349 mpfr_init (real_info->epsilon);
350 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
352 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
353 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
354 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
355 mpfr_neg (b, b, GFC_RND_MODE);
357 if (mpfr_cmp (a, b) > 0)
358 mpfr_set (a, b, GFC_RND_MODE); /* a = min(a, b) */
361 gfc_mpfr_to_mpz (r, a);
362 real_info->range = mpz_get_si (r);
364 /* precision(x) = int((p - 1) * log10(b)) + k */
365 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
366 mpfr_log10 (a, a, GFC_RND_MODE);
368 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
370 gfc_mpfr_to_mpz (r, a);
371 real_info->precision = mpz_get_si (r);
373 /* If the radix is an integral power of 10, add one to the
375 for (i = 10; i <= real_info->radix; i *= 10)
376 if (i == real_info->radix)
377 real_info->precision++;
388 /* Clean up, get rid of numeric constants. */
391 gfc_arith_done_1 (void)
393 gfc_integer_info *ip;
396 for (ip = gfc_integer_kinds; ip->kind; ip++)
398 mpz_clear (ip->min_int);
399 mpz_clear (ip->max_int);
400 mpz_clear (ip->huge);
403 for (rp = gfc_real_kinds; rp->kind; rp++)
405 mpfr_clear (rp->epsilon);
406 mpfr_clear (rp->huge);
407 mpfr_clear (rp->tiny);
412 /* Return default kinds. */
415 gfc_default_integer_kind (void)
417 return gfc_integer_kinds[gfc_option.i8 ? 1 : 0].kind;
421 gfc_default_real_kind (void)
423 return gfc_real_kinds[gfc_option.r8 ? 1 : 0].kind;
427 gfc_default_double_kind (void)
429 return gfc_real_kinds[1].kind;
433 gfc_default_character_kind (void)
439 gfc_default_logical_kind (void)
441 return gfc_logical_kinds[gfc_option.i8 ? 1 : 0].kind;
445 gfc_default_complex_kind (void)
447 return gfc_default_real_kind ();
451 /* Make sure that a valid kind is present. Returns an index into the
452 gfc_integer_kinds array, -1 if the kind is not present. */
455 validate_integer (int kind)
461 if (gfc_integer_kinds[i].kind == 0)
466 if (gfc_integer_kinds[i].kind == kind)
475 validate_real (int kind)
481 if (gfc_real_kinds[i].kind == 0)
486 if (gfc_real_kinds[i].kind == kind)
495 validate_logical (int kind)
501 if (gfc_logical_kinds[i].kind == 0)
506 if (gfc_logical_kinds[i].kind == kind)
515 validate_character (int kind)
518 if (kind == gfc_default_character_kind ())
524 /* Validate a kind given a basic type. The return value is the same
525 for the child functions, with -1 indicating nonexistence of the
529 gfc_validate_kind (bt type, int kind)
535 case BT_REAL: /* Fall through */
537 rc = validate_real (kind);
540 rc = validate_integer (kind);
543 rc = validate_logical (kind);
546 rc = validate_character (kind);
550 gfc_internal_error ("gfc_validate_kind(): Got bad type");
557 /* Given an integer and a kind, make sure that the integer lies within
558 the range of the kind. Returns ARITH_OK or ARITH_OVERFLOW. */
561 gfc_check_integer_range (mpz_t p, int kind)
566 i = validate_integer (kind);
568 gfc_internal_error ("gfc_check_integer_range(): Bad kind");
572 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
573 || mpz_cmp (p, gfc_integer_kinds[i].max_int) > 0)
574 result = ARITH_OVERFLOW;
580 /* Given a real and a kind, make sure that the real lies within the
581 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
585 gfc_check_real_range (mpfr_t p, int kind)
591 i = validate_real (kind);
593 gfc_internal_error ("gfc_check_real_range(): Bad kind");
597 mpfr_abs (q, p, GFC_RND_MODE);
600 if (mpfr_sgn (q) == 0)
603 if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
605 retval = ARITH_OVERFLOW;
609 if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
610 retval = ARITH_UNDERFLOW;
619 /* Function to return a constant expression node of a given type and
623 gfc_constant_result (bt type, int kind, locus * where)
629 ("gfc_constant_result(): locus 'where' cannot be NULL");
631 result = gfc_get_expr ();
633 result->expr_type = EXPR_CONSTANT;
634 result->ts.type = type;
635 result->ts.kind = kind;
636 result->where = *where;
641 mpz_init (result->value.integer);
645 gfc_set_model_kind (kind);
646 mpfr_init (result->value.real);
650 gfc_set_model_kind (kind);
651 mpfr_init (result->value.complex.r);
652 mpfr_init (result->value.complex.i);
663 /* Low-level arithmetic functions. All of these subroutines assume
664 that all operands are of the same type and return an operand of the
665 same type. The other thing about these subroutines is that they
666 can fail in various ways -- overflow, underflow, division by zero,
667 zero raised to the zero, etc. */
670 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
674 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
675 result->value.logical = !op1->value.logical;
683 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
687 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
689 result->value.logical = op1->value.logical && op2->value.logical;
697 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
701 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
703 result->value.logical = op1->value.logical || op2->value.logical;
711 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
715 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
717 result->value.logical = op1->value.logical == op2->value.logical;
725 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
729 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
731 result->value.logical = op1->value.logical != op2->value.logical;
738 /* Make sure a constant numeric expression is within the range for
739 its type and kind. Note that there's also a gfc_check_range(),
740 but that one deals with the intrinsic RANGE function. */
743 gfc_range_check (gfc_expr * e)
750 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
754 rc = gfc_check_real_range (e->value.real, e->ts.kind);
755 if (rc == ARITH_UNDERFLOW)
756 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
760 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
761 if (rc == ARITH_UNDERFLOW)
762 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
763 if (rc == ARITH_OK || rc == ARITH_UNDERFLOW)
765 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
766 if (rc == ARITH_UNDERFLOW)
767 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
773 gfc_internal_error ("gfc_range_check(): Bad type");
780 /* It may seem silly to have a subroutine that actually computes the
781 unary plus of a constant, but it prevents us from making exceptions
782 in the code elsewhere. */
785 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
788 *resultp = gfc_copy_expr (op1);
794 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
799 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
801 switch (op1->ts.type)
804 mpz_neg (result->value.integer, op1->value.integer);
808 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
812 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
813 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
817 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
820 rc = gfc_range_check (result);
822 if (rc == ARITH_UNDERFLOW)
824 if (gfc_option.warn_underflow)
825 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
829 else if (rc != ARITH_OK)
830 gfc_free_expr (result);
839 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
844 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
846 switch (op1->ts.type)
849 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
853 mpfr_add (result->value.real, op1->value.real, op2->value.real,
858 mpfr_add (result->value.complex.r, op1->value.complex.r,
859 op2->value.complex.r, GFC_RND_MODE);
861 mpfr_add (result->value.complex.i, op1->value.complex.i,
862 op2->value.complex.i, GFC_RND_MODE);
866 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
869 rc = gfc_range_check (result);
871 if (rc == ARITH_UNDERFLOW)
873 if (gfc_option.warn_underflow)
874 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
878 else if (rc != ARITH_OK)
879 gfc_free_expr (result);
888 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
893 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
895 switch (op1->ts.type)
898 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
902 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
907 mpfr_sub (result->value.complex.r, op1->value.complex.r,
908 op2->value.complex.r, GFC_RND_MODE);
910 mpfr_sub (result->value.complex.i, op1->value.complex.i,
911 op2->value.complex.i, GFC_RND_MODE);
915 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
918 rc = gfc_range_check (result);
920 if (rc == ARITH_UNDERFLOW)
922 if (gfc_option.warn_underflow)
923 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
927 else if (rc != ARITH_OK)
928 gfc_free_expr (result);
937 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
943 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
945 switch (op1->ts.type)
948 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
952 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
958 /* FIXME: possible numericals problem. */
960 gfc_set_model (op1->value.complex.r);
964 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
965 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
966 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
968 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
969 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
970 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
978 gfc_internal_error ("gfc_arith_times(): Bad basic type");
981 rc = gfc_range_check (result);
983 if (rc == ARITH_UNDERFLOW)
985 if (gfc_option.warn_underflow)
986 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
990 else if (rc != ARITH_OK)
991 gfc_free_expr (result);
1000 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1008 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1010 switch (op1->ts.type)
1013 if (mpz_sgn (op2->value.integer) == 0)
1019 mpz_tdiv_q (result->value.integer, op1->value.integer,
1020 op2->value.integer);
1024 /* FIXME: MPFR correctly generates NaN. This may not be needed. */
1025 if (mpfr_sgn (op2->value.real) == 0)
1031 mpfr_div (result->value.real, op1->value.real, op2->value.real,
1036 /* FIXME: MPFR correctly generates NaN. This may not be needed. */
1037 if (mpfr_sgn (op2->value.complex.r) == 0
1038 && mpfr_sgn (op2->value.complex.i) == 0)
1044 gfc_set_model (op1->value.complex.r);
1049 /* FIXME: possible numerical problems. */
1050 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
1051 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
1052 mpfr_add (div, x, y, GFC_RND_MODE);
1054 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
1055 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
1056 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
1057 mpfr_div (result->value.complex.r, result->value.complex.r, div,
1060 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
1061 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
1062 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
1063 mpfr_div (result->value.complex.i, result->value.complex.i, div,
1073 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
1077 rc = gfc_range_check (result);
1079 if (rc == ARITH_UNDERFLOW)
1081 if (gfc_option.warn_underflow)
1082 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1086 else if (rc != ARITH_OK)
1087 gfc_free_expr (result);
1095 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
1098 complex_reciprocal (gfc_expr * op)
1100 mpfr_t mod, a, re, im;
1102 gfc_set_model (op->value.complex.r);
1108 /* FIXME: another possible numerical problem. */
1109 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
1110 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
1111 mpfr_add (mod, mod, a, GFC_RND_MODE);
1113 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
1115 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
1116 mpfr_div (im, im, mod, GFC_RND_MODE);
1118 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
1119 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
1128 /* Raise a complex number to positive power. */
1131 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
1135 gfc_set_model (base->value.complex.r);
1140 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1141 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1143 for (; power > 0; power--)
1145 mpfr_mul (re, base->value.complex.r, result->value.complex.r,
1147 mpfr_mul (a, base->value.complex.i, result->value.complex.i,
1149 mpfr_sub (re, re, a, GFC_RND_MODE);
1151 mpfr_mul (im, base->value.complex.r, result->value.complex.i,
1153 mpfr_mul (a, base->value.complex.i, result->value.complex.r,
1155 mpfr_add (im, im, a, GFC_RND_MODE);
1157 mpfr_set (result->value.complex.r, re, GFC_RND_MODE);
1158 mpfr_set (result->value.complex.i, im, GFC_RND_MODE);
1167 /* Raise a number to an integer power. */
1170 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1180 if (gfc_extract_int (op2, &power) != NULL)
1181 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1183 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1186 { /* Handle something to the zeroth power */
1187 switch (op1->ts.type)
1190 if (mpz_sgn (op1->value.integer) == 0)
1193 mpz_set_ui (result->value.integer, 1);
1197 if (mpfr_sgn (op1->value.real) == 0)
1200 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
1204 if (mpfr_sgn (op1->value.complex.r) == 0
1205 && mpfr_sgn (op1->value.complex.i) == 0)
1209 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1210 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1216 gfc_internal_error ("gfc_arith_power(): Bad base");
1225 switch (op1->ts.type)
1228 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1232 mpz_init_set_ui (unity_z, 1);
1233 mpz_tdiv_q (result->value.integer, unity_z,
1234 result->value.integer);
1235 mpz_clear (unity_z);
1241 mpfr_pow_ui (result->value.real, op1->value.real, apower,
1246 gfc_set_model (op1->value.real);
1247 mpfr_init (unity_f);
1248 mpfr_set_ui (unity_f, 1, GFC_RND_MODE);
1249 mpfr_div (result->value.real, unity_f, result->value.real,
1251 mpfr_clear (unity_f);
1256 complex_pow_ui (op1, apower, result);
1258 complex_reciprocal (result);
1267 rc = gfc_range_check (result);
1269 if (rc == ARITH_UNDERFLOW)
1271 if (gfc_option.warn_underflow)
1272 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1276 else if (rc != ARITH_OK)
1277 gfc_free_expr (result);
1285 /* Concatenate two string constants. */
1288 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1293 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
1296 len = op1->value.character.length + op2->value.character.length;
1298 result->value.character.string = gfc_getmem (len + 1);
1299 result->value.character.length = len;
1301 memcpy (result->value.character.string, op1->value.character.string,
1302 op1->value.character.length);
1304 memcpy (result->value.character.string + op1->value.character.length,
1305 op2->value.character.string, op2->value.character.length);
1307 result->value.character.string[len] = '\0';
1315 /* Comparison operators. Assumes that the two expression nodes
1316 contain two constants of the same type. */
1319 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1323 switch (op1->ts.type)
1326 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1330 rc = mpfr_cmp (op1->value.real, op2->value.real);
1334 rc = gfc_compare_string (op1, op2, NULL);
1338 rc = ((!op1->value.logical && op2->value.logical)
1339 || (op1->value.logical && !op2->value.logical));
1343 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1350 /* Compare a pair of complex numbers. Naturally, this is only for
1351 equality/nonequality. */
1354 compare_complex (gfc_expr * op1, gfc_expr * op2)
1357 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1358 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1362 /* Given two constant strings and the inverse collating sequence,
1363 compare the strings. We return -1 for a<b, 0 for a==b and 1 for
1364 a>b. If the xcoll_table is NULL, we use the processor's default
1365 collating sequence. */
1368 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
1370 int len, alen, blen, i, ac, bc;
1372 alen = a->value.character.length;
1373 blen = b->value.character.length;
1375 len = (alen > blen) ? alen : blen;
1377 for (i = 0; i < len; i++)
1379 ac = (i < alen) ? a->value.character.string[i] : ' ';
1380 bc = (i < blen) ? b->value.character.string[i] : ' ';
1382 if (xcoll_table != NULL)
1384 ac = xcoll_table[ac];
1385 bc = xcoll_table[bc];
1394 /* Strings are equal */
1400 /* Specific comparison subroutines. */
1403 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1407 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1409 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1410 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1418 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1422 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1424 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1425 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1433 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1437 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1439 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1447 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1451 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1453 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1461 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1465 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1467 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1475 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1479 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1481 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1489 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1492 gfc_constructor *c, *head;
1496 if (op->expr_type == EXPR_CONSTANT)
1497 return eval (op, result);
1500 head = gfc_copy_constructor (op->value.constructor);
1502 for (c = head; c; c = c->next)
1504 rc = eval (c->expr, &r);
1508 gfc_replace_expr (c->expr, r);
1512 gfc_free_constructor (head);
1515 r = gfc_get_expr ();
1516 r->expr_type = EXPR_ARRAY;
1517 r->value.constructor = head;
1518 r->shape = gfc_copy_shape (op->shape, op->rank);
1520 r->ts = head->expr->ts;
1521 r->where = op->where;
1532 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1533 gfc_expr * op1, gfc_expr * op2,
1536 gfc_constructor *c, *head;
1540 head = gfc_copy_constructor (op1->value.constructor);
1543 for (c = head; c; c = c->next)
1545 rc = eval (c->expr, op2, &r);
1549 gfc_replace_expr (c->expr, r);
1553 gfc_free_constructor (head);
1556 r = gfc_get_expr ();
1557 r->expr_type = EXPR_ARRAY;
1558 r->value.constructor = head;
1559 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1561 r->ts = head->expr->ts;
1562 r->where = op1->where;
1563 r->rank = op1->rank;
1573 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1574 gfc_expr * op1, gfc_expr * op2,
1577 gfc_constructor *c, *head;
1581 head = gfc_copy_constructor (op2->value.constructor);
1584 for (c = head; c; c = c->next)
1586 rc = eval (op1, c->expr, &r);
1590 gfc_replace_expr (c->expr, r);
1594 gfc_free_constructor (head);
1597 r = gfc_get_expr ();
1598 r->expr_type = EXPR_ARRAY;
1599 r->value.constructor = head;
1600 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1602 r->ts = head->expr->ts;
1603 r->where = op2->where;
1604 r->rank = op2->rank;
1614 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1615 gfc_expr * op1, gfc_expr * op2,
1618 gfc_constructor *c, *d, *head;
1622 head = gfc_copy_constructor (op1->value.constructor);
1625 d = op2->value.constructor;
1627 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1629 rc = ARITH_INCOMMENSURATE;
1633 for (c = head; c; c = c->next, d = d->next)
1637 rc = ARITH_INCOMMENSURATE;
1641 rc = eval (c->expr, d->expr, &r);
1645 gfc_replace_expr (c->expr, r);
1649 rc = ARITH_INCOMMENSURATE;
1653 gfc_free_constructor (head);
1656 r = gfc_get_expr ();
1657 r->expr_type = EXPR_ARRAY;
1658 r->value.constructor = head;
1659 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1661 r->ts = head->expr->ts;
1662 r->where = op1->where;
1663 r->rank = op1->rank;
1673 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1674 gfc_expr * op1, gfc_expr * op2,
1678 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1679 return eval (op1, op2, result);
1681 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1682 return reduce_binary_ca (eval, op1, op2, result);
1684 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1685 return reduce_binary_ac (eval, op1, op2, result);
1687 return reduce_binary_aa (eval, op1, op2, result);
1693 arith (*f2)(gfc_expr *, gfc_expr **);
1694 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1698 /* High level arithmetic subroutines. These subroutines go into
1699 eval_intrinsic(), which can do one of several things to its
1700 operands. If the operands are incompatible with the intrinsic
1701 operation, we return a node pointing to the operands and hope that
1702 an operator interface is found during resolution.
1704 If the operands are compatible and are constants, then we try doing
1705 the arithmetic. We also handle the cases where either or both
1706 operands are array constructors. */
1709 eval_intrinsic (gfc_intrinsic_op operator,
1710 eval_f eval, gfc_expr * op1, gfc_expr * op2)
1712 gfc_expr temp, *result;
1716 gfc_clear_ts (&temp.ts);
1720 case INTRINSIC_NOT: /* Logical unary */
1721 if (op1->ts.type != BT_LOGICAL)
1724 temp.ts.type = BT_LOGICAL;
1725 temp.ts.kind = gfc_default_logical_kind ();
1730 /* Logical binary operators */
1733 case INTRINSIC_NEQV:
1735 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1738 temp.ts.type = BT_LOGICAL;
1739 temp.ts.kind = gfc_default_logical_kind ();
1744 case INTRINSIC_UPLUS:
1745 case INTRINSIC_UMINUS: /* Numeric unary */
1746 if (!gfc_numeric_ts (&op1->ts))
1755 case INTRINSIC_LT: /* Additional restrictions */
1756 case INTRINSIC_LE: /* for ordering relations. */
1758 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1760 temp.ts.type = BT_LOGICAL;
1761 temp.ts.kind = gfc_default_logical_kind();
1765 /* else fall through */
1769 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1772 temp.ts.type = BT_LOGICAL;
1773 temp.ts.kind = gfc_default_logical_kind();
1777 /* else fall through */
1779 case INTRINSIC_PLUS:
1780 case INTRINSIC_MINUS:
1781 case INTRINSIC_TIMES:
1782 case INTRINSIC_DIVIDE:
1783 case INTRINSIC_POWER: /* Numeric binary */
1784 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1787 /* Insert any necessary type conversions to make the operands compatible. */
1789 temp.expr_type = EXPR_OP;
1790 gfc_clear_ts (&temp.ts);
1791 temp.operator = operator;
1796 gfc_type_convert_binary (&temp);
1798 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1799 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1800 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
1802 temp.ts.type = BT_LOGICAL;
1803 temp.ts.kind = gfc_default_logical_kind ();
1809 case INTRINSIC_CONCAT: /* Character binary */
1810 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1813 temp.ts.type = BT_CHARACTER;
1814 temp.ts.kind = gfc_default_character_kind ();
1819 case INTRINSIC_USER:
1823 gfc_internal_error ("eval_intrinsic(): Bad operator");
1826 /* Try to combine the operators. */
1827 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1830 if (op1->expr_type != EXPR_CONSTANT
1831 && (op1->expr_type != EXPR_ARRAY
1832 || !gfc_is_constant_expr (op1)
1833 || !gfc_expanded_ac (op1)))
1837 && op2->expr_type != EXPR_CONSTANT
1838 && (op2->expr_type != EXPR_ARRAY
1839 || !gfc_is_constant_expr (op2)
1840 || !gfc_expanded_ac (op2)))
1844 rc = reduce_unary (eval.f2, op1, &result);
1846 rc = reduce_binary (eval.f3, op1, op2, &result);
1849 { /* Something went wrong */
1850 gfc_error ("%s at %L", gfc_arith_error (rc), &op1->where);
1854 gfc_free_expr (op1);
1855 gfc_free_expr (op2);
1859 /* Create a run-time expression */
1860 result = gfc_get_expr ();
1861 result->ts = temp.ts;
1863 result->expr_type = EXPR_OP;
1864 result->operator = operator;
1869 result->where = op1->where;
1875 /* Modify type of expression for zero size array. */
1877 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
1880 gfc_internal_error("eval_type_intrinsic0(): op NULL");
1890 op->ts.type = BT_LOGICAL;
1891 op->ts.kind = gfc_default_logical_kind();
1902 /* Return nonzero if the expression is a zero size array. */
1905 gfc_zero_size_array (gfc_expr * e)
1908 if (e->expr_type != EXPR_ARRAY)
1911 return e->value.constructor == NULL;
1915 /* Reduce a binary expression where at least one of the operands
1916 involves a zero-length array. Returns NULL if neither of the
1917 operands is a zero-length array. */
1920 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
1923 if (gfc_zero_size_array (op1))
1925 gfc_free_expr (op2);
1929 if (gfc_zero_size_array (op2))
1931 gfc_free_expr (op1);
1940 eval_intrinsic_f2 (gfc_intrinsic_op operator,
1941 arith (*eval) (gfc_expr *, gfc_expr **),
1942 gfc_expr * op1, gfc_expr * op2)
1949 if (gfc_zero_size_array (op1))
1950 return eval_type_intrinsic0(operator, op1);
1954 result = reduce_binary0 (op1, op2);
1956 return eval_type_intrinsic0(operator, result);
1960 return eval_intrinsic (operator, f, op1, op2);
1965 eval_intrinsic_f3 (gfc_intrinsic_op operator,
1966 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1967 gfc_expr * op1, gfc_expr * op2)
1972 result = reduce_binary0 (op1, op2);
1974 return eval_type_intrinsic0(operator, result);
1977 return eval_intrinsic (operator, f, op1, op2);
1983 gfc_uplus (gfc_expr * op)
1985 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
1989 gfc_uminus (gfc_expr * op)
1991 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1995 gfc_add (gfc_expr * op1, gfc_expr * op2)
1997 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
2001 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
2003 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
2007 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
2009 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
2013 gfc_divide (gfc_expr * op1, gfc_expr * op2)
2015 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
2019 gfc_power (gfc_expr * op1, gfc_expr * op2)
2021 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
2025 gfc_concat (gfc_expr * op1, gfc_expr * op2)
2027 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
2031 gfc_and (gfc_expr * op1, gfc_expr * op2)
2033 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
2037 gfc_or (gfc_expr * op1, gfc_expr * op2)
2039 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
2043 gfc_not (gfc_expr * op1)
2045 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
2049 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
2051 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
2055 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
2057 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
2061 gfc_eq (gfc_expr * op1, gfc_expr * op2)
2063 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
2067 gfc_ne (gfc_expr * op1, gfc_expr * op2)
2069 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
2073 gfc_gt (gfc_expr * op1, gfc_expr * op2)
2075 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
2079 gfc_ge (gfc_expr * op1, gfc_expr * op2)
2081 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
2085 gfc_lt (gfc_expr * op1, gfc_expr * op2)
2087 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
2091 gfc_le (gfc_expr * op1, gfc_expr * op2)
2093 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
2097 /* Convert an integer string to an expression node. */
2100 gfc_convert_integer (const char *buffer, int kind, int radix, locus * where)
2105 e = gfc_constant_result (BT_INTEGER, kind, where);
2106 /* a leading plus is allowed, but not by mpz_set_str */
2107 if (buffer[0] == '+')
2111 mpz_set_str (e->value.integer, t, radix);
2117 /* Convert a real string to an expression node. */
2120 gfc_convert_real (const char *buffer, int kind, locus * where)
2125 e = gfc_constant_result (BT_REAL, kind, where);
2126 /* A leading plus is allowed in Fortran, but not by mpfr_set_str */
2127 if (buffer[0] == '+')
2131 mpfr_set_str (e->value.real, t, 10, GFC_RND_MODE);
2137 /* Convert a pair of real, constant expression nodes to a single
2138 complex expression node. */
2141 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
2145 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
2146 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
2147 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
2153 /******* Simplification of intrinsic functions with constant arguments *****/
2156 /* Deal with an arithmetic error. */
2159 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
2162 gfc_error ("%s converting %s to %s at %L", gfc_arith_error (rc),
2163 gfc_typename (from), gfc_typename (to), where);
2165 /* TODO: Do something about the error, ie, throw exception, return
2169 /* Convert integers to integers. */
2172 gfc_int2int (gfc_expr * src, int kind)
2177 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2179 mpz_set (result->value.integer, src->value.integer);
2181 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2184 arith_error (rc, &src->ts, &result->ts, &src->where);
2185 gfc_free_expr (result);
2193 /* Convert integers to reals. */
2196 gfc_int2real (gfc_expr * src, int kind)
2201 result = gfc_constant_result (BT_REAL, kind, &src->where);
2203 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2205 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2207 arith_error (rc, &src->ts, &result->ts, &src->where);
2208 gfc_free_expr (result);
2216 /* Convert default integer to default complex. */
2219 gfc_int2complex (gfc_expr * src, int kind)
2224 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2226 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2227 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2229 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2231 arith_error (rc, &src->ts, &result->ts, &src->where);
2232 gfc_free_expr (result);
2240 /* Convert default real to default integer. */
2243 gfc_real2int (gfc_expr * src, int kind)
2248 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2250 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2252 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2255 arith_error (rc, &src->ts, &result->ts, &src->where);
2256 gfc_free_expr (result);
2264 /* Convert real to real. */
2267 gfc_real2real (gfc_expr * src, int kind)
2272 result = gfc_constant_result (BT_REAL, kind, &src->where);
2274 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2276 rc = gfc_check_real_range (result->value.real, kind);
2278 if (rc == ARITH_UNDERFLOW)
2280 if (gfc_option.warn_underflow)
2281 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2282 mpfr_set_ui(result->value.real, 0, GFC_RND_MODE);
2284 else if (rc != ARITH_OK)
2286 arith_error (rc, &src->ts, &result->ts, &src->where);
2287 gfc_free_expr (result);
2295 /* Convert real to complex. */
2298 gfc_real2complex (gfc_expr * src, int kind)
2303 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2305 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2306 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2308 rc = gfc_check_real_range (result->value.complex.r, kind);
2310 if (rc == ARITH_UNDERFLOW)
2312 if (gfc_option.warn_underflow)
2313 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2314 mpfr_set_ui(result->value.complex.r, 0, GFC_RND_MODE);
2316 else if (rc != ARITH_OK)
2318 arith_error (rc, &src->ts, &result->ts, &src->where);
2319 gfc_free_expr (result);
2327 /* Convert complex to integer. */
2330 gfc_complex2int (gfc_expr * src, int kind)
2335 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2337 gfc_mpfr_to_mpz(result->value.integer, src->value.complex.r);
2339 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2342 arith_error (rc, &src->ts, &result->ts, &src->where);
2343 gfc_free_expr (result);
2351 /* Convert complex to real. */
2354 gfc_complex2real (gfc_expr * src, int kind)
2359 result = gfc_constant_result (BT_REAL, kind, &src->where);
2361 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2363 rc = gfc_check_real_range (result->value.real, kind);
2365 if (rc == ARITH_UNDERFLOW)
2367 if (gfc_option.warn_underflow)
2368 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2369 mpfr_set_ui(result->value.real, 0, GFC_RND_MODE);
2373 arith_error (rc, &src->ts, &result->ts, &src->where);
2374 gfc_free_expr (result);
2382 /* Convert complex to complex. */
2385 gfc_complex2complex (gfc_expr * src, int kind)
2390 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2392 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2393 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2395 rc = gfc_check_real_range (result->value.complex.r, kind);
2397 if (rc == ARITH_UNDERFLOW)
2399 if (gfc_option.warn_underflow)
2400 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2401 mpfr_set_ui(result->value.complex.r, 0, GFC_RND_MODE);
2403 else if (rc != ARITH_OK)
2405 arith_error (rc, &src->ts, &result->ts, &src->where);
2406 gfc_free_expr (result);
2410 rc = gfc_check_real_range (result->value.complex.i, kind);
2412 if (rc == ARITH_UNDERFLOW)
2414 if (gfc_option.warn_underflow)
2415 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2416 mpfr_set_ui(result->value.complex.i, 0, GFC_RND_MODE);
2418 else if (rc != ARITH_OK)
2420 arith_error (rc, &src->ts, &result->ts, &src->where);
2421 gfc_free_expr (result);
2429 /* Logical kind conversion. */
2432 gfc_log2log (gfc_expr * src, int kind)
2436 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2437 result->value.logical = src->value.logical;