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);
115 mpz_mul_2exp (z, z, e);
117 mpz_tdiv_q_2exp (z, z, -e);
118 if (mpfr_sgn (x) < 0)
123 /* Set the model number precision by the requested KIND. */
126 gfc_set_model_kind (int kind)
131 mpfr_set_default_prec (GFC_SP_PREC);
134 mpfr_set_default_prec (GFC_DP_PREC);
137 mpfr_set_default_prec (GFC_QP_PREC);
140 gfc_internal_error ("gfc_set_model_kind(): Bad model number");
145 /* Set the model number precision from mpfr_t x. */
148 gfc_set_model (mpfr_t x)
150 switch (mpfr_get_prec (x))
153 mpfr_set_default_prec (GFC_SP_PREC);
156 mpfr_set_default_prec (GFC_DP_PREC);
159 mpfr_set_default_prec (GFC_QP_PREC);
162 gfc_internal_error ("gfc_set_model(): Bad model number");
166 /* Calculate atan2 (y, x)
168 atan2(y, x) = atan(y/x) if x > 0,
169 sign(y)*(pi - atan(|y/x|)) if x < 0,
170 0 if x = 0 && y == 0,
171 sign(y)*pi/2 if x = 0 && y != 0.
175 arctangent2 (mpfr_t y, mpfr_t x, mpfr_t result)
187 mpfr_div (t, y, x, GFC_RND_MODE);
188 mpfr_atan (result, t, GFC_RND_MODE);
192 mpfr_const_pi (result, GFC_RND_MODE);
193 mpfr_div (t, y, x, GFC_RND_MODE);
194 mpfr_abs (t, t, GFC_RND_MODE);
195 mpfr_atan (t, t, GFC_RND_MODE);
196 mpfr_sub (result, result, t, GFC_RND_MODE);
197 if (mpfr_sgn (y) < 0)
198 mpfr_neg (result, result, GFC_RND_MODE);
202 if (mpfr_sgn (y) == 0)
203 mpfr_set_ui (result, 0, GFC_RND_MODE);
206 mpfr_const_pi (result, GFC_RND_MODE);
207 mpfr_div_ui (result, result, 2, GFC_RND_MODE);
208 if (mpfr_sgn (y) < 0)
209 mpfr_neg (result, result, GFC_RND_MODE);
218 /* Given an arithmetic error code, return a pointer to a string that
219 explains the error. */
222 gfc_arith_error (arith code)
232 p = "Arithmetic overflow";
234 case ARITH_UNDERFLOW:
235 p = "Arithmetic underflow";
238 p = "Arithmetic NaN";
241 p = "Division by zero";
244 p = "Indeterminate form 0 ** 0";
246 case ARITH_INCOMMENSURATE:
247 p = "Array operands are incommensurate";
250 gfc_internal_error ("gfc_arith_error(): Bad error code");
257 /* Get things ready to do math. */
260 gfc_arith_init_1 (void)
262 gfc_integer_info *int_info;
263 gfc_real_info *real_info;
268 gfc_set_model_kind (GFC_QP_KIND);
273 /* Convert the minimum/maximum values for each kind into their
274 GNU MP representation. */
275 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
278 mpz_set_ui (r, int_info->radix);
279 mpz_pow_ui (r, r, int_info->digits);
281 mpz_init (int_info->huge);
282 mpz_sub_ui (int_info->huge, r, 1);
284 /* These are the numbers that are actually representable by the
285 target. For bases other than two, this needs to be changed. */
286 if (int_info->radix != 2)
287 gfc_internal_error ("Fix min_int, max_int calculation");
289 mpz_init (int_info->min_int);
290 mpz_neg (int_info->min_int, int_info->huge);
291 /* No -1 here, because the representation is symmetric. */
293 mpz_init (int_info->max_int);
294 mpz_add (int_info->max_int, int_info->huge, int_info->huge);
295 mpz_add_ui (int_info->max_int, int_info->max_int, 1);
298 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
299 mpfr_log10 (a, a, GFC_RND_MODE);
301 gfc_mpfr_to_mpz (r, a);
302 int_info->range = mpz_get_si (r);
307 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
309 gfc_set_model_kind (real_info->kind);
315 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
316 /* a = 1 - b**(-p) */
317 mpfr_set_ui (a, 1, GFC_RND_MODE);
318 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
319 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
320 mpfr_sub (a, a, b, GFC_RND_MODE);
322 /* c = b**(emax-1) */
323 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
324 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
326 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
327 mpfr_mul (a, a, c, GFC_RND_MODE);
329 /* a = (1 - b**(-p)) * b**(emax-1) * b */
330 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
332 mpfr_init (real_info->huge);
333 mpfr_set (real_info->huge, a, GFC_RND_MODE);
335 /* tiny(x) = b**(emin-1) */
336 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
337 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
339 mpfr_init (real_info->tiny);
340 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
342 /* epsilon(x) = b**(1-p) */
343 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
344 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
346 mpfr_init (real_info->epsilon);
347 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
349 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
350 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
351 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
352 mpfr_neg (b, b, GFC_RND_MODE);
354 if (mpfr_cmp (a, b) > 0)
355 mpfr_set (a, b, GFC_RND_MODE); /* a = min(a, b) */
358 gfc_mpfr_to_mpz (r, a);
359 real_info->range = mpz_get_si (r);
361 /* precision(x) = int((p - 1) * log10(b)) + k */
362 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
363 mpfr_log10 (a, a, GFC_RND_MODE);
365 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
367 gfc_mpfr_to_mpz (r, a);
368 real_info->precision = mpz_get_si (r);
370 /* If the radix is an integral power of 10, add one to the
372 for (i = 10; i <= real_info->radix; i *= 10)
373 if (i == real_info->radix)
374 real_info->precision++;
385 /* Clean up, get rid of numeric constants. */
388 gfc_arith_done_1 (void)
390 gfc_integer_info *ip;
393 for (ip = gfc_integer_kinds; ip->kind; ip++)
395 mpz_clear (ip->min_int);
396 mpz_clear (ip->max_int);
397 mpz_clear (ip->huge);
400 for (rp = gfc_real_kinds; rp->kind; rp++)
402 mpfr_clear (rp->epsilon);
403 mpfr_clear (rp->huge);
404 mpfr_clear (rp->tiny);
409 /* Return default kinds. */
412 gfc_default_integer_kind (void)
414 return gfc_integer_kinds[gfc_option.i8 ? 1 : 0].kind;
418 gfc_default_real_kind (void)
420 return gfc_real_kinds[gfc_option.r8 ? 1 : 0].kind;
424 gfc_default_double_kind (void)
426 return gfc_real_kinds[1].kind;
430 gfc_default_character_kind (void)
436 gfc_default_logical_kind (void)
438 return gfc_logical_kinds[gfc_option.i8 ? 1 : 0].kind;
442 gfc_default_complex_kind (void)
444 return gfc_default_real_kind ();
448 /* Make sure that a valid kind is present. Returns an index into the
449 gfc_integer_kinds array, -1 if the kind is not present. */
452 validate_integer (int kind)
458 if (gfc_integer_kinds[i].kind == 0)
463 if (gfc_integer_kinds[i].kind == kind)
472 validate_real (int kind)
478 if (gfc_real_kinds[i].kind == 0)
483 if (gfc_real_kinds[i].kind == kind)
492 validate_logical (int kind)
498 if (gfc_logical_kinds[i].kind == 0)
503 if (gfc_logical_kinds[i].kind == kind)
512 validate_character (int kind)
515 if (kind == gfc_default_character_kind ())
521 /* Validate a kind given a basic type. The return value is the same
522 for the child functions, with -1 indicating nonexistence of the
526 gfc_validate_kind (bt type, int kind)
532 case BT_REAL: /* Fall through */
534 rc = validate_real (kind);
537 rc = validate_integer (kind);
540 rc = validate_logical (kind);
543 rc = validate_character (kind);
547 gfc_internal_error ("gfc_validate_kind(): Got bad type");
554 /* Given an integer and a kind, make sure that the integer lies within
555 the range of the kind. Returns ARITH_OK or ARITH_OVERFLOW. */
558 gfc_check_integer_range (mpz_t p, int kind)
563 i = validate_integer (kind);
565 gfc_internal_error ("gfc_check_integer_range(): Bad kind");
569 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
570 || mpz_cmp (p, gfc_integer_kinds[i].max_int) > 0)
571 result = ARITH_OVERFLOW;
577 /* Given a real and a kind, make sure that the real lies within the
578 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
582 gfc_check_real_range (mpfr_t p, int kind)
588 i = validate_real (kind);
590 gfc_internal_error ("gfc_check_real_range(): Bad kind");
594 mpfr_abs (q, p, GFC_RND_MODE);
597 if (mpfr_sgn (q) == 0)
600 if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
602 retval = ARITH_OVERFLOW;
606 if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
607 retval = ARITH_UNDERFLOW;
616 /* Function to return a constant expression node of a given type and
620 gfc_constant_result (bt type, int kind, locus * where)
626 ("gfc_constant_result(): locus 'where' cannot be NULL");
628 result = gfc_get_expr ();
630 result->expr_type = EXPR_CONSTANT;
631 result->ts.type = type;
632 result->ts.kind = kind;
633 result->where = *where;
638 mpz_init (result->value.integer);
642 gfc_set_model_kind (kind);
643 mpfr_init (result->value.real);
647 gfc_set_model_kind (kind);
648 mpfr_init (result->value.complex.r);
649 mpfr_init (result->value.complex.i);
660 /* Low-level arithmetic functions. All of these subroutines assume
661 that all operands are of the same type and return an operand of the
662 same type. The other thing about these subroutines is that they
663 can fail in various ways -- overflow, underflow, division by zero,
664 zero raised to the zero, etc. */
667 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
671 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
672 result->value.logical = !op1->value.logical;
680 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
684 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
686 result->value.logical = op1->value.logical && op2->value.logical;
694 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
698 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
700 result->value.logical = op1->value.logical || op2->value.logical;
708 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
712 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
714 result->value.logical = op1->value.logical == op2->value.logical;
722 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
726 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
728 result->value.logical = op1->value.logical != op2->value.logical;
735 /* Make sure a constant numeric expression is within the range for
736 its type and kind. Note that there's also a gfc_check_range(),
737 but that one deals with the intrinsic RANGE function. */
740 gfc_range_check (gfc_expr * e)
747 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
751 rc = gfc_check_real_range (e->value.real, e->ts.kind);
752 if (rc == ARITH_UNDERFLOW)
753 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
757 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
758 if (rc == ARITH_UNDERFLOW)
759 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
760 if (rc == ARITH_OK || rc == ARITH_UNDERFLOW)
762 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
763 if (rc == ARITH_UNDERFLOW)
764 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
770 gfc_internal_error ("gfc_range_check(): Bad type");
777 /* It may seem silly to have a subroutine that actually computes the
778 unary plus of a constant, but it prevents us from making exceptions
779 in the code elsewhere. */
782 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
785 *resultp = gfc_copy_expr (op1);
791 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
796 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
798 switch (op1->ts.type)
801 mpz_neg (result->value.integer, op1->value.integer);
805 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
809 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
810 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
814 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
817 rc = gfc_range_check (result);
819 if (rc == ARITH_UNDERFLOW)
821 if (gfc_option.warn_underflow)
822 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
826 else if (rc != ARITH_OK)
827 gfc_free_expr (result);
836 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
841 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
843 switch (op1->ts.type)
846 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
850 mpfr_add (result->value.real, op1->value.real, op2->value.real,
855 mpfr_add (result->value.complex.r, op1->value.complex.r,
856 op2->value.complex.r, GFC_RND_MODE);
858 mpfr_add (result->value.complex.i, op1->value.complex.i,
859 op2->value.complex.i, GFC_RND_MODE);
863 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
866 rc = gfc_range_check (result);
868 if (rc == ARITH_UNDERFLOW)
870 if (gfc_option.warn_underflow)
871 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
875 else if (rc != ARITH_OK)
876 gfc_free_expr (result);
885 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
890 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
892 switch (op1->ts.type)
895 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
899 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
904 mpfr_sub (result->value.complex.r, op1->value.complex.r,
905 op2->value.complex.r, GFC_RND_MODE);
907 mpfr_sub (result->value.complex.i, op1->value.complex.i,
908 op2->value.complex.i, GFC_RND_MODE);
912 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
915 rc = gfc_range_check (result);
917 if (rc == ARITH_UNDERFLOW)
919 if (gfc_option.warn_underflow)
920 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
924 else if (rc != ARITH_OK)
925 gfc_free_expr (result);
934 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
940 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
942 switch (op1->ts.type)
945 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
949 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
955 /* FIXME: possible numericals problem. */
957 gfc_set_model (op1->value.complex.r);
961 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
962 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
963 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
965 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
966 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
967 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
975 gfc_internal_error ("gfc_arith_times(): Bad basic type");
978 rc = gfc_range_check (result);
980 if (rc == ARITH_UNDERFLOW)
982 if (gfc_option.warn_underflow)
983 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
987 else if (rc != ARITH_OK)
988 gfc_free_expr (result);
997 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1005 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1007 switch (op1->ts.type)
1010 if (mpz_sgn (op2->value.integer) == 0)
1016 mpz_tdiv_q (result->value.integer, op1->value.integer,
1017 op2->value.integer);
1021 /* FIXME: MPFR correctly generates NaN. This may not be needed. */
1022 if (mpfr_sgn (op2->value.real) == 0)
1028 mpfr_div (result->value.real, op1->value.real, op2->value.real,
1033 /* FIXME: MPFR correctly generates NaN. This may not be needed. */
1034 if (mpfr_sgn (op2->value.complex.r) == 0
1035 && mpfr_sgn (op2->value.complex.i) == 0)
1041 gfc_set_model (op1->value.complex.r);
1046 /* FIXME: possible numerical problems. */
1047 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
1048 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
1049 mpfr_add (div, x, y, GFC_RND_MODE);
1051 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
1052 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
1053 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
1054 mpfr_div (result->value.complex.r, result->value.complex.r, div,
1057 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
1058 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
1059 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
1060 mpfr_div (result->value.complex.i, result->value.complex.i, div,
1070 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
1074 rc = gfc_range_check (result);
1076 if (rc == ARITH_UNDERFLOW)
1078 if (gfc_option.warn_underflow)
1079 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1083 else if (rc != ARITH_OK)
1084 gfc_free_expr (result);
1092 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
1095 complex_reciprocal (gfc_expr * op)
1097 mpfr_t mod, a, re, im;
1099 gfc_set_model (op->value.complex.r);
1105 /* FIXME: another possible numerical problem. */
1106 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
1107 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
1108 mpfr_add (mod, mod, a, GFC_RND_MODE);
1110 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
1112 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
1113 mpfr_div (im, im, mod, GFC_RND_MODE);
1115 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
1116 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
1125 /* Raise a complex number to positive power. */
1128 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
1132 gfc_set_model (base->value.complex.r);
1137 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1138 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1140 for (; power > 0; power--)
1142 mpfr_mul (re, base->value.complex.r, result->value.complex.r,
1144 mpfr_mul (a, base->value.complex.i, result->value.complex.i,
1146 mpfr_sub (re, re, a, GFC_RND_MODE);
1148 mpfr_mul (im, base->value.complex.r, result->value.complex.i,
1150 mpfr_mul (a, base->value.complex.i, result->value.complex.r,
1152 mpfr_add (im, im, a, GFC_RND_MODE);
1154 mpfr_set (result->value.complex.r, re, GFC_RND_MODE);
1155 mpfr_set (result->value.complex.i, im, GFC_RND_MODE);
1164 /* Raise a number to an integer power. */
1167 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1177 if (gfc_extract_int (op2, &power) != NULL)
1178 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1180 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1183 { /* Handle something to the zeroth power */
1184 switch (op1->ts.type)
1187 if (mpz_sgn (op1->value.integer) == 0)
1190 mpz_set_ui (result->value.integer, 1);
1194 if (mpfr_sgn (op1->value.real) == 0)
1197 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
1201 if (mpfr_sgn (op1->value.complex.r) == 0
1202 && mpfr_sgn (op1->value.complex.i) == 0)
1206 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1207 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1213 gfc_internal_error ("gfc_arith_power(): Bad base");
1222 switch (op1->ts.type)
1225 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1229 mpz_init_set_ui (unity_z, 1);
1230 mpz_tdiv_q (result->value.integer, unity_z,
1231 result->value.integer);
1232 mpz_clear (unity_z);
1238 mpfr_pow_ui (result->value.real, op1->value.real, apower,
1243 gfc_set_model (op1->value.real);
1244 mpfr_init (unity_f);
1245 mpfr_set_ui (unity_f, 1, GFC_RND_MODE);
1246 mpfr_div (result->value.real, unity_f, result->value.real,
1248 mpfr_clear (unity_f);
1253 complex_pow_ui (op1, apower, result);
1255 complex_reciprocal (result);
1264 rc = gfc_range_check (result);
1266 if (rc == ARITH_UNDERFLOW)
1268 if (gfc_option.warn_underflow)
1269 gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1273 else if (rc != ARITH_OK)
1274 gfc_free_expr (result);
1282 /* Concatenate two string constants. */
1285 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1290 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
1293 len = op1->value.character.length + op2->value.character.length;
1295 result->value.character.string = gfc_getmem (len + 1);
1296 result->value.character.length = len;
1298 memcpy (result->value.character.string, op1->value.character.string,
1299 op1->value.character.length);
1301 memcpy (result->value.character.string + op1->value.character.length,
1302 op2->value.character.string, op2->value.character.length);
1304 result->value.character.string[len] = '\0';
1312 /* Comparison operators. Assumes that the two expression nodes
1313 contain two constants of the same type. */
1316 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1320 switch (op1->ts.type)
1323 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1327 rc = mpfr_cmp (op1->value.real, op2->value.real);
1331 rc = gfc_compare_string (op1, op2, NULL);
1335 rc = ((!op1->value.logical && op2->value.logical)
1336 || (op1->value.logical && !op2->value.logical));
1340 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1347 /* Compare a pair of complex numbers. Naturally, this is only for
1348 equality/nonequality. */
1351 compare_complex (gfc_expr * op1, gfc_expr * op2)
1354 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1355 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1359 /* Given two constant strings and the inverse collating sequence,
1360 compare the strings. We return -1 for a<b, 0 for a==b and 1 for
1361 a>b. If the xcoll_table is NULL, we use the processor's default
1362 collating sequence. */
1365 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
1367 int len, alen, blen, i, ac, bc;
1369 alen = a->value.character.length;
1370 blen = b->value.character.length;
1372 len = (alen > blen) ? alen : blen;
1374 for (i = 0; i < len; i++)
1376 ac = (i < alen) ? a->value.character.string[i] : ' ';
1377 bc = (i < blen) ? b->value.character.string[i] : ' ';
1379 if (xcoll_table != NULL)
1381 ac = xcoll_table[ac];
1382 bc = xcoll_table[bc];
1391 /* Strings are equal */
1397 /* Specific comparison subroutines. */
1400 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1404 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1406 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1407 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1415 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1419 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1421 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1422 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1430 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1434 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1436 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1444 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1448 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1450 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1458 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1462 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1464 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1472 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1476 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1478 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1486 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1489 gfc_constructor *c, *head;
1493 if (op->expr_type == EXPR_CONSTANT)
1494 return eval (op, result);
1497 head = gfc_copy_constructor (op->value.constructor);
1499 for (c = head; c; c = c->next)
1501 rc = eval (c->expr, &r);
1505 gfc_replace_expr (c->expr, r);
1509 gfc_free_constructor (head);
1512 r = gfc_get_expr ();
1513 r->expr_type = EXPR_ARRAY;
1514 r->value.constructor = head;
1515 r->shape = gfc_copy_shape (op->shape, op->rank);
1517 r->ts = head->expr->ts;
1518 r->where = op->where;
1529 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1530 gfc_expr * op1, gfc_expr * op2,
1533 gfc_constructor *c, *head;
1537 head = gfc_copy_constructor (op1->value.constructor);
1540 for (c = head; c; c = c->next)
1542 rc = eval (c->expr, op2, &r);
1546 gfc_replace_expr (c->expr, r);
1550 gfc_free_constructor (head);
1553 r = gfc_get_expr ();
1554 r->expr_type = EXPR_ARRAY;
1555 r->value.constructor = head;
1556 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1558 r->ts = head->expr->ts;
1559 r->where = op1->where;
1560 r->rank = op1->rank;
1570 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1571 gfc_expr * op1, gfc_expr * op2,
1574 gfc_constructor *c, *head;
1578 head = gfc_copy_constructor (op2->value.constructor);
1581 for (c = head; c; c = c->next)
1583 rc = eval (op1, c->expr, &r);
1587 gfc_replace_expr (c->expr, r);
1591 gfc_free_constructor (head);
1594 r = gfc_get_expr ();
1595 r->expr_type = EXPR_ARRAY;
1596 r->value.constructor = head;
1597 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1599 r->ts = head->expr->ts;
1600 r->where = op2->where;
1601 r->rank = op2->rank;
1611 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1612 gfc_expr * op1, gfc_expr * op2,
1615 gfc_constructor *c, *d, *head;
1619 head = gfc_copy_constructor (op1->value.constructor);
1622 d = op2->value.constructor;
1624 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1626 rc = ARITH_INCOMMENSURATE;
1630 for (c = head; c; c = c->next, d = d->next)
1634 rc = ARITH_INCOMMENSURATE;
1638 rc = eval (c->expr, d->expr, &r);
1642 gfc_replace_expr (c->expr, r);
1646 rc = ARITH_INCOMMENSURATE;
1650 gfc_free_constructor (head);
1653 r = gfc_get_expr ();
1654 r->expr_type = EXPR_ARRAY;
1655 r->value.constructor = head;
1656 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1658 r->ts = head->expr->ts;
1659 r->where = op1->where;
1660 r->rank = op1->rank;
1670 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1671 gfc_expr * op1, gfc_expr * op2,
1675 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1676 return eval (op1, op2, result);
1678 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1679 return reduce_binary_ca (eval, op1, op2, result);
1681 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1682 return reduce_binary_ac (eval, op1, op2, result);
1684 return reduce_binary_aa (eval, op1, op2, result);
1690 arith (*f2)(gfc_expr *, gfc_expr **);
1691 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1695 /* High level arithmetic subroutines. These subroutines go into
1696 eval_intrinsic(), which can do one of several things to its
1697 operands. If the operands are incompatible with the intrinsic
1698 operation, we return a node pointing to the operands and hope that
1699 an operator interface is found during resolution.
1701 If the operands are compatible and are constants, then we try doing
1702 the arithmetic. We also handle the cases where either or both
1703 operands are array constructors. */
1706 eval_intrinsic (gfc_intrinsic_op operator,
1707 eval_f eval, gfc_expr * op1, gfc_expr * op2)
1709 gfc_expr temp, *result;
1713 gfc_clear_ts (&temp.ts);
1717 case INTRINSIC_NOT: /* Logical unary */
1718 if (op1->ts.type != BT_LOGICAL)
1721 temp.ts.type = BT_LOGICAL;
1722 temp.ts.kind = gfc_default_logical_kind ();
1727 /* Logical binary operators */
1730 case INTRINSIC_NEQV:
1732 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1735 temp.ts.type = BT_LOGICAL;
1736 temp.ts.kind = gfc_default_logical_kind ();
1741 case INTRINSIC_UPLUS:
1742 case INTRINSIC_UMINUS: /* Numeric unary */
1743 if (!gfc_numeric_ts (&op1->ts))
1752 case INTRINSIC_LT: /* Additional restrictions */
1753 case INTRINSIC_LE: /* for ordering relations. */
1755 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1757 temp.ts.type = BT_LOGICAL;
1758 temp.ts.kind = gfc_default_logical_kind();
1762 /* else fall through */
1766 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1769 temp.ts.type = BT_LOGICAL;
1770 temp.ts.kind = gfc_default_logical_kind();
1774 /* else fall through */
1776 case INTRINSIC_PLUS:
1777 case INTRINSIC_MINUS:
1778 case INTRINSIC_TIMES:
1779 case INTRINSIC_DIVIDE:
1780 case INTRINSIC_POWER: /* Numeric binary */
1781 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1784 /* Insert any necessary type conversions to make the operands compatible. */
1786 temp.expr_type = EXPR_OP;
1787 gfc_clear_ts (&temp.ts);
1788 temp.operator = operator;
1793 gfc_type_convert_binary (&temp);
1795 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1796 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1797 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
1799 temp.ts.type = BT_LOGICAL;
1800 temp.ts.kind = gfc_default_logical_kind ();
1806 case INTRINSIC_CONCAT: /* Character binary */
1807 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1810 temp.ts.type = BT_CHARACTER;
1811 temp.ts.kind = gfc_default_character_kind ();
1816 case INTRINSIC_USER:
1820 gfc_internal_error ("eval_intrinsic(): Bad operator");
1823 /* Try to combine the operators. */
1824 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1827 if (op1->expr_type != EXPR_CONSTANT
1828 && (op1->expr_type != EXPR_ARRAY
1829 || !gfc_is_constant_expr (op1)
1830 || !gfc_expanded_ac (op1)))
1834 && op2->expr_type != EXPR_CONSTANT
1835 && (op2->expr_type != EXPR_ARRAY
1836 || !gfc_is_constant_expr (op2)
1837 || !gfc_expanded_ac (op2)))
1841 rc = reduce_unary (eval.f2, op1, &result);
1843 rc = reduce_binary (eval.f3, op1, op2, &result);
1846 { /* Something went wrong */
1847 gfc_error ("%s at %L", gfc_arith_error (rc), &op1->where);
1851 gfc_free_expr (op1);
1852 gfc_free_expr (op2);
1856 /* Create a run-time expression */
1857 result = gfc_get_expr ();
1858 result->ts = temp.ts;
1860 result->expr_type = EXPR_OP;
1861 result->operator = operator;
1866 result->where = op1->where;
1872 /* Modify type of expression for zero size array. */
1874 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
1877 gfc_internal_error("eval_type_intrinsic0(): op NULL");
1887 op->ts.type = BT_LOGICAL;
1888 op->ts.kind = gfc_default_logical_kind();
1899 /* Return nonzero if the expression is a zero size array. */
1902 gfc_zero_size_array (gfc_expr * e)
1905 if (e->expr_type != EXPR_ARRAY)
1908 return e->value.constructor == NULL;
1912 /* Reduce a binary expression where at least one of the operands
1913 involves a zero-length array. Returns NULL if neither of the
1914 operands is a zero-length array. */
1917 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
1920 if (gfc_zero_size_array (op1))
1922 gfc_free_expr (op2);
1926 if (gfc_zero_size_array (op2))
1928 gfc_free_expr (op1);
1937 eval_intrinsic_f2 (gfc_intrinsic_op operator,
1938 arith (*eval) (gfc_expr *, gfc_expr **),
1939 gfc_expr * op1, gfc_expr * op2)
1946 if (gfc_zero_size_array (op1))
1947 return eval_type_intrinsic0(operator, op1);
1951 result = reduce_binary0 (op1, op2);
1953 return eval_type_intrinsic0(operator, result);
1957 return eval_intrinsic (operator, f, op1, op2);
1962 eval_intrinsic_f3 (gfc_intrinsic_op operator,
1963 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1964 gfc_expr * op1, gfc_expr * op2)
1969 result = reduce_binary0 (op1, op2);
1971 return eval_type_intrinsic0(operator, result);
1974 return eval_intrinsic (operator, f, op1, op2);
1980 gfc_uplus (gfc_expr * op)
1982 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
1986 gfc_uminus (gfc_expr * op)
1988 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1992 gfc_add (gfc_expr * op1, gfc_expr * op2)
1994 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1998 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
2000 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
2004 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
2006 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
2010 gfc_divide (gfc_expr * op1, gfc_expr * op2)
2012 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
2016 gfc_power (gfc_expr * op1, gfc_expr * op2)
2018 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
2022 gfc_concat (gfc_expr * op1, gfc_expr * op2)
2024 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
2028 gfc_and (gfc_expr * op1, gfc_expr * op2)
2030 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
2034 gfc_or (gfc_expr * op1, gfc_expr * op2)
2036 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
2040 gfc_not (gfc_expr * op1)
2042 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
2046 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
2048 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
2052 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
2054 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
2058 gfc_eq (gfc_expr * op1, gfc_expr * op2)
2060 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
2064 gfc_ne (gfc_expr * op1, gfc_expr * op2)
2066 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
2070 gfc_gt (gfc_expr * op1, gfc_expr * op2)
2072 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
2076 gfc_ge (gfc_expr * op1, gfc_expr * op2)
2078 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
2082 gfc_lt (gfc_expr * op1, gfc_expr * op2)
2084 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
2088 gfc_le (gfc_expr * op1, gfc_expr * op2)
2090 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
2094 /* Convert an integer string to an expression node. */
2097 gfc_convert_integer (const char *buffer, int kind, int radix, locus * where)
2102 e = gfc_constant_result (BT_INTEGER, kind, where);
2103 /* a leading plus is allowed, but not by mpz_set_str */
2104 if (buffer[0] == '+')
2108 mpz_set_str (e->value.integer, t, radix);
2114 /* Convert a real string to an expression node. */
2117 gfc_convert_real (const char *buffer, int kind, locus * where)
2122 e = gfc_constant_result (BT_REAL, kind, where);
2123 /* A leading plus is allowed in Fortran, but not by mpfr_set_str */
2124 if (buffer[0] == '+')
2128 mpfr_set_str (e->value.real, t, 10, GFC_RND_MODE);
2134 /* Convert a pair of real, constant expression nodes to a single
2135 complex expression node. */
2138 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
2142 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
2143 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
2144 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
2150 /******* Simplification of intrinsic functions with constant arguments *****/
2153 /* Deal with an arithmetic error. */
2156 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
2159 gfc_error ("%s converting %s to %s at %L", gfc_arith_error (rc),
2160 gfc_typename (from), gfc_typename (to), where);
2162 /* TODO: Do something about the error, ie, throw exception, return
2166 /* Convert integers to integers. */
2169 gfc_int2int (gfc_expr * src, int kind)
2174 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2176 mpz_set (result->value.integer, src->value.integer);
2178 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2181 arith_error (rc, &src->ts, &result->ts, &src->where);
2182 gfc_free_expr (result);
2190 /* Convert integers to reals. */
2193 gfc_int2real (gfc_expr * src, int kind)
2198 result = gfc_constant_result (BT_REAL, kind, &src->where);
2200 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2202 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2204 arith_error (rc, &src->ts, &result->ts, &src->where);
2205 gfc_free_expr (result);
2213 /* Convert default integer to default complex. */
2216 gfc_int2complex (gfc_expr * src, int kind)
2221 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2223 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2224 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2226 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2228 arith_error (rc, &src->ts, &result->ts, &src->where);
2229 gfc_free_expr (result);
2237 /* Convert default real to default integer. */
2240 gfc_real2int (gfc_expr * src, int kind)
2245 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2247 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2249 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2252 arith_error (rc, &src->ts, &result->ts, &src->where);
2253 gfc_free_expr (result);
2261 /* Convert real to real. */
2264 gfc_real2real (gfc_expr * src, int kind)
2269 result = gfc_constant_result (BT_REAL, kind, &src->where);
2271 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2273 rc = gfc_check_real_range (result->value.real, kind);
2275 if (rc == ARITH_UNDERFLOW)
2277 if (gfc_option.warn_underflow)
2278 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2279 mpfr_set_ui(result->value.real, 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 /* Convert real to complex. */
2295 gfc_real2complex (gfc_expr * src, int kind)
2300 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2302 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2303 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2305 rc = gfc_check_real_range (result->value.complex.r, kind);
2307 if (rc == ARITH_UNDERFLOW)
2309 if (gfc_option.warn_underflow)
2310 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2311 mpfr_set_ui(result->value.complex.r, 0, GFC_RND_MODE);
2313 else if (rc != ARITH_OK)
2315 arith_error (rc, &src->ts, &result->ts, &src->where);
2316 gfc_free_expr (result);
2324 /* Convert complex to integer. */
2327 gfc_complex2int (gfc_expr * src, int kind)
2332 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2334 gfc_mpfr_to_mpz(result->value.integer, src->value.complex.r);
2336 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2339 arith_error (rc, &src->ts, &result->ts, &src->where);
2340 gfc_free_expr (result);
2348 /* Convert complex to real. */
2351 gfc_complex2real (gfc_expr * src, int kind)
2356 result = gfc_constant_result (BT_REAL, kind, &src->where);
2358 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2360 rc = gfc_check_real_range (result->value.real, kind);
2362 if (rc == ARITH_UNDERFLOW)
2364 if (gfc_option.warn_underflow)
2365 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2366 mpfr_set_ui(result->value.real, 0, GFC_RND_MODE);
2370 arith_error (rc, &src->ts, &result->ts, &src->where);
2371 gfc_free_expr (result);
2379 /* Convert complex to complex. */
2382 gfc_complex2complex (gfc_expr * src, int kind)
2387 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2389 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2390 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2392 rc = gfc_check_real_range (result->value.complex.r, kind);
2394 if (rc == ARITH_UNDERFLOW)
2396 if (gfc_option.warn_underflow)
2397 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2398 mpfr_set_ui(result->value.complex.r, 0, GFC_RND_MODE);
2400 else if (rc != ARITH_OK)
2402 arith_error (rc, &src->ts, &result->ts, &src->where);
2403 gfc_free_expr (result);
2407 rc = gfc_check_real_range (result->value.complex.i, kind);
2409 if (rc == ARITH_UNDERFLOW)
2411 if (gfc_option.warn_underflow)
2412 gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2413 mpfr_set_ui(result->value.complex.i, 0, GFC_RND_MODE);
2415 else if (rc != ARITH_OK)
2417 arith_error (rc, &src->ts, &result->ts, &src->where);
2418 gfc_free_expr (result);
2426 /* Logical kind conversion. */
2429 gfc_log2log (gfc_expr * src, int kind)
2433 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2434 result->value.logical = src->value.logical;