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 mpf_t pi
, half_pi
, two_pi
, e
;
37 /* The gfc_(integer|real)_kinds[] structures have everything the front
38 end needs to know about integers and real numbers on the target.
39 Other entries of the structure are calculated from these values.
40 The first entry is the default kind, the second entry of the real
41 structure is the default double kind. */
43 #define MPZ_NULL {{0,0,0}}
44 #define MPF_NULL {{0,0,0,0}}
46 #define DEF_GFC_INTEGER_KIND(KIND,RADIX,DIGITS,BIT_SIZE) \
47 {KIND, RADIX, DIGITS, BIT_SIZE, 0, MPZ_NULL, MPZ_NULL, MPZ_NULL}
49 #define DEF_GFC_LOGICAL_KIND(KIND,BIT_SIZE) \
52 #define DEF_GFC_REAL_KIND(KIND,RADIX,DIGITS,MIN_EXP, MAX_EXP) \
53 {KIND, RADIX, DIGITS, MIN_EXP, MAX_EXP, \
54 0, 0, MPF_NULL, MPF_NULL, MPF_NULL}
56 gfc_integer_info gfc_integer_kinds
[] = {
57 DEF_GFC_INTEGER_KIND (4, 2, 31, 32),
58 DEF_GFC_INTEGER_KIND (8, 2, 63, 64),
59 DEF_GFC_INTEGER_KIND (2, 2, 15, 16),
60 DEF_GFC_INTEGER_KIND (1, 2, 7, 8),
61 DEF_GFC_INTEGER_KIND (0, 0, 0, 0)
64 gfc_logical_info gfc_logical_kinds
[] = {
65 DEF_GFC_LOGICAL_KIND (4, 32),
66 DEF_GFC_LOGICAL_KIND (8, 64),
67 DEF_GFC_LOGICAL_KIND (2, 16),
68 DEF_GFC_LOGICAL_KIND (1, 8),
69 DEF_GFC_LOGICAL_KIND (0, 0)
72 gfc_real_info gfc_real_kinds
[] = {
73 DEF_GFC_REAL_KIND (4, 2, 24, -125, 128),
74 DEF_GFC_REAL_KIND (8, 2, 53, -1021, 1024),
75 DEF_GFC_REAL_KIND (0, 0, 0, 0, 0)
79 /* The integer kind to use for array indices. This will be set to the
80 proper value based on target information from the backend. */
82 int gfc_index_integer_kind
;
85 /* Compute the natural log of arg.
87 We first get the argument into the range 0.5 to 1.5 by successive
88 multiplications or divisions by e. Then we use the series:
90 ln(x) = (x-1) - (x-1)^2/2 + (x-1)^3/3 - (x-1)^4/4 + ...
92 Because we are expanding in powers of (x-1), and 0.5 < x < 1.5, we
93 have -0.5 < (x-1) < 0.5. Ignoring the harmonic term, this means
94 that each term is at most 1/(2^i), meaning one bit is gained per
97 Not very efficient, but it doesn't have to be. */
100 natural_logarithm (mpf_t
* arg
, mpf_t
* result
)
105 mpf_init_set (x
, *arg
);
110 mpf_set_str (t
, "0.5", 10);
111 while (mpf_cmp (x
, t
) < 0)
117 mpf_set_str (t
, "1.5", 10);
118 while (mpf_cmp (x
, t
) > 0)
124 mpf_sub_ui (x
, x
, 1);
125 mpf_init_set_ui (log
, 0);
126 mpf_init_set_ui (xp
, 1);
128 for (i
= 1; i
< GFC_REAL_BITS
; i
++)
131 mpf_div_ui (t
, xp
, i
);
134 mpf_sub (log
, log
, t
);
136 mpf_add (log
, log
, t
);
139 /* Add in the log (e^p) = p */
142 mpf_sub_ui (log
, log
, -p
);
144 mpf_add_ui (log
, log
, p
);
150 mpf_set (*result
, log
);
155 /* Calculate the common logarithm of arg. We use the natural
156 logarithm of arg and of 10:
158 log10(arg) = log(arg)/log(10) */
161 common_logarithm (mpf_t
* arg
, mpf_t
* result
)
165 natural_logarithm (arg
, result
);
167 mpf_init_set_ui (i10
, 10);
169 natural_logarithm (&i10
, &log10
);
171 mpf_div (*result
, *result
, log10
);
176 /* Calculate exp(arg).
178 We use a reduction of the form
182 Then we obtain exp(r) from the Maclaurin series.
183 exp(x) is then recovered from the identity
185 exp(x) = 2^N*exp(r). */
188 exponential (mpf_t
* arg
, mpf_t
* result
)
190 mpf_t two
, ln2
, power
, q
, r
, num
, denom
, term
, x
, xp
;
196 mpf_init_set (x
, *arg
);
198 if (mpf_cmp_ui (x
, 0) == 0)
200 mpf_set_ui (*result
, 1);
202 else if (mpf_cmp_ui (x
, 1) == 0)
204 mpf_set (*result
, e
);
208 mpf_init_set_ui (two
, 2);
215 natural_logarithm (&two
, &ln2
);
218 mpf_floor (power
, q
);
219 mpf_mul (q
, power
, ln2
);
222 mpf_init_set_ui (xp
, 1);
223 mpf_init_set_ui (num
, 1);
224 mpf_init_set_ui (denom
, 1);
226 for (i
= 1; i
<= GFC_REAL_BITS
+ 10; i
++)
228 mpf_mul (num
, num
, r
);
229 mpf_mul_ui (denom
, denom
, i
);
230 mpf_div (term
, num
, denom
);
231 mpf_add (xp
, xp
, term
);
234 /* Reconstruction step */
235 n
= (long) mpf_get_d (power
);
239 p
= (unsigned int) n
;
240 mpf_mul_2exp (*result
, xp
, p
);
244 mp
= (unsigned int) (-n
);
245 mpf_div_2exp (*result
, xp
, mp
);
263 /* Calculate sin(arg).
265 We use a reduction of the form
269 Then we obtain sin(r) from the Maclaurin series. */
272 sine (mpf_t
* arg
, mpf_t
* result
)
274 mpf_t factor
, q
, r
, num
, denom
, term
, x
, xp
;
277 mpf_init_set (x
, *arg
);
279 /* Special case (we do not treat multiples of pi due to roundoff issues) */
280 if (mpf_cmp_ui (x
, 0) == 0)
282 mpf_set_ui (*result
, 0);
291 mpf_div (q
, x
, two_pi
);
292 mpf_floor (factor
, q
);
293 mpf_mul (q
, factor
, two_pi
);
296 mpf_init_set_ui (xp
, 0);
297 mpf_init_set_ui (num
, 1);
298 mpf_init_set_ui (denom
, 1);
301 for (i
= 1; i
< GFC_REAL_BITS
+ 10; i
++)
303 mpf_mul (num
, num
, r
);
304 mpf_mul_ui (denom
, denom
, i
);
309 mpf_div (term
, num
, denom
);
311 mpf_add (xp
, xp
, term
);
313 mpf_sub (xp
, xp
, term
);
316 mpf_set (*result
, xp
);
331 /* Calculate cos(arg).
336 cosine (mpf_t
* arg
, mpf_t
* result
)
338 mpf_t factor
, q
, r
, num
, denom
, term
, x
, xp
;
341 mpf_init_set (x
, *arg
);
343 /* Special case (we do not treat multiples of pi due to roundoff issues) */
344 if (mpf_cmp_ui (x
, 0) == 0)
346 mpf_set_ui (*result
, 1);
355 mpf_div (q
, x
, two_pi
);
356 mpf_floor (factor
, q
);
357 mpf_mul (q
, factor
, two_pi
);
360 mpf_init_set_ui (xp
, 1);
361 mpf_init_set_ui (num
, 1);
362 mpf_init_set_ui (denom
, 1);
365 for (i
= 1; i
< GFC_REAL_BITS
+ 10; i
++)
367 mpf_mul (num
, num
, r
);
368 mpf_mul_ui (denom
, denom
, i
);
373 mpf_div (term
, num
, denom
);
375 mpf_add (xp
, xp
, term
);
377 mpf_sub (xp
, xp
, term
);
379 mpf_set (*result
, xp
);
394 /* Calculate atan(arg).
396 Similar to sine but requires special handling for x near 1. */
399 arctangent (mpf_t
* arg
, mpf_t
* result
)
401 mpf_t absval
, convgu
, convgl
, num
, term
, x
, xp
;
404 mpf_init_set (x
, *arg
);
407 if (mpf_cmp_ui (x
, 0) == 0)
409 mpf_set_ui (*result
, 0);
411 else if (mpf_cmp_ui (x
, 1) == 0)
414 mpf_div_ui (num
, half_pi
, 2);
415 mpf_set (*result
, num
);
418 else if (mpf_cmp_si (x
, -1) == 0)
421 mpf_div_ui (num
, half_pi
, 2);
422 mpf_neg (*result
, num
);
426 { /* General cases */
431 mpf_init_set_d (convgu
, 1.5);
432 mpf_init_set_d (convgl
, 0.5);
433 mpf_init_set_ui (num
, 1);
436 if (mpf_cmp (absval
, convgl
) < 0)
438 mpf_init_set_ui (xp
, 0);
440 for (i
= 1; i
< GFC_REAL_BITS
+ 10; i
++)
442 mpf_mul (num
, num
, absval
);
447 mpf_div_ui (term
, num
, i
);
449 mpf_add (xp
, xp
, term
);
451 mpf_sub (xp
, xp
, term
);
454 else if (mpf_cmp (absval
, convgu
) >= 0)
456 mpf_init_set (xp
, half_pi
);
458 for (i
= 1; i
< GFC_REAL_BITS
+ 10; i
++)
460 mpf_div (num
, num
, absval
);
465 mpf_div_ui (term
, num
, i
);
467 mpf_add (xp
, xp
, term
);
469 mpf_sub (xp
, xp
, term
);
474 mpf_init_set_ui (xp
, 0);
476 mpf_sub_ui (num
, absval
, 1);
477 mpf_add_ui (term
, absval
, 1);
478 mpf_div (absval
, num
, term
);
483 for (i
= 1; i
< GFC_REAL_BITS
+ 10; i
++)
485 mpf_mul (num
, num
, absval
);
489 mpf_div_ui (term
, num
, i
);
491 mpf_add (xp
, xp
, term
);
493 mpf_sub (xp
, xp
, term
);
496 mpf_div_ui (term
, half_pi
, 2);
497 mpf_add (xp
, term
, xp
);
500 /* This makes sure to preserve the identity arctan(-x) = -arctan(x)
501 and improves accuracy to boot. */
503 if (mpf_cmp_ui (x
, 0) > 0)
504 mpf_set (*result
, xp
);
506 mpf_neg (*result
, xp
);
519 /* Calculate atan2 (y, x)
521 atan2(y, x) = atan(y/x) if x > 0,
522 sign(y)*(pi - atan(|y/x|)) if x < 0,
523 0 if x = 0 && y == 0,
524 sign(y)*pi/2 if x = 0 && y != 0.
528 arctangent2 (mpf_t
* y
, mpf_t
* x
, mpf_t
* result
)
534 switch (mpf_sgn (*x
))
538 arctangent (&t
, result
);
544 mpf_sub (*result
, pi
, t
);
545 if (mpf_sgn (*y
) == -1)
546 mpf_neg (*result
, *result
);
549 if (mpf_sgn (*y
) == 0)
550 mpf_set_ui (*result
, 0);
553 mpf_set (*result
, half_pi
);
554 if (mpf_sgn (*y
) == -1)
555 mpf_neg (*result
, *result
);
562 /* Calculate cosh(arg). */
565 hypercos (mpf_t
* arg
, mpf_t
* result
)
567 mpf_t neg
, term1
, term2
, x
, xp
;
569 mpf_init_set (x
, *arg
);
578 exponential (&x
, &term1
);
579 exponential (&neg
, &term2
);
581 mpf_add (xp
, term1
, term2
);
582 mpf_div_ui (*result
, xp
, 2);
592 /* Calculate sinh(arg). */
595 hypersine (mpf_t
* arg
, mpf_t
* result
)
597 mpf_t neg
, term1
, term2
, x
, xp
;
599 mpf_init_set (x
, *arg
);
608 exponential (&x
, &term1
);
609 exponential (&neg
, &term2
);
611 mpf_sub (xp
, term1
, term2
);
612 mpf_div_ui (*result
, xp
, 2);
622 /* Given an arithmetic error code, return a pointer to a string that
623 explains the error. */
626 gfc_arith_error (arith code
)
636 p
= "Arithmetic overflow";
638 case ARITH_UNDERFLOW
:
639 p
= "Arithmetic underflow";
642 p
= "Division by zero";
645 p
= "Indeterminate form 0 ** 0";
647 case ARITH_INCOMMENSURATE
:
648 p
= "Array operands are incommensurate";
651 gfc_internal_error ("gfc_arith_error(): Bad error code");
658 /* Get things ready to do math. */
661 gfc_arith_init_1 (void)
663 gfc_integer_info
*int_info
;
664 gfc_real_info
*real_info
;
669 /* Set the default precision for GMP computations. */
670 mpf_set_default_prec (GFC_REAL_BITS
+ 30);
672 /* Calculate e, needed by the natural_logarithm() subroutine. */
674 mpf_init_set_ui (e
, 0);
675 mpf_init_set_ui (a
, 1);
677 for (i
= 1; i
< 100; i
++)
680 mpf_div_ui (a
, a
, i
); /* 1/(i!) */
683 /* Calculate pi, 2pi, pi/2, and -pi/2, needed for trigonometric
686 We use the Bailey, Borwein and Plouffe formula:
688 pi = \sum{n=0}^\infty (1/16)^n [4/(8n+1) - 2/(8n+4) - 1/(8n+5) - 1/(8n+6)]
690 which gives about four bits per iteration. */
692 mpf_init_set_ui (pi
, 0);
697 limit
= (GFC_REAL_BITS
/ 4) + 10; /* (1/16)^n gives 4 bits per iteration */
699 for (n
= 0; n
< limit
; n
++)
702 mpf_div_ui (b
, b
, 8 * n
+ 1); /* 4/(8n+1) */
705 mpf_div_ui (a
, a
, 8 * n
+ 4); /* 2/(8n+4) */
709 mpf_div_ui (a
, a
, 8 * n
+ 5); /* 1/(8n+5) */
713 mpf_div_ui (a
, a
, 8 * n
+ 6); /* 1/(8n+6) */
717 mpf_pow_ui (a
, a
, n
); /* 16^n */
724 mpf_mul_ui (two_pi
, pi
, 2);
725 mpf_div_ui (half_pi
, pi
, 2);
727 /* Convert the minimum/maximum values for each kind into their
728 GNU MP representation. */
731 for (int_info
= gfc_integer_kinds
; int_info
->kind
!= 0; int_info
++)
734 mpz_set_ui (r
, int_info
->radix
);
735 mpz_pow_ui (r
, r
, int_info
->digits
);
737 mpz_init (int_info
->huge
);
738 mpz_sub_ui (int_info
->huge
, r
, 1);
740 /* These are the numbers that are actually representable by the
741 target. For bases other than two, this needs to be changed. */
742 if (int_info
->radix
!= 2)
743 gfc_internal_error ("Fix min_int, max_int calculation");
745 mpz_init (int_info
->min_int
);
746 mpz_neg (int_info
->min_int
, int_info
->huge
);
747 /* No -1 here, because the representation is symmetric. */
749 mpz_init (int_info
->max_int
);
750 mpz_add (int_info
->max_int
, int_info
->huge
, int_info
->huge
);
751 mpz_add_ui (int_info
->max_int
, int_info
->max_int
, 1);
754 mpf_set_z (a
, int_info
->huge
);
755 common_logarithm (&a
, &a
);
758 int_info
->range
= mpz_get_si (r
);
761 /* mpf_set_default_prec(GFC_REAL_BITS); */
762 for (real_info
= gfc_real_kinds
; real_info
->kind
!= 0; real_info
++)
765 mpf_set_ui (a
, real_info
->radix
);
766 mpf_set_ui (b
, real_info
->radix
);
768 mpf_pow_ui (a
, a
, real_info
->max_exponent
);
769 mpf_pow_ui (b
, b
, real_info
->max_exponent
- real_info
->digits
);
771 mpf_init (real_info
->huge
);
772 mpf_sub (real_info
->huge
, a
, b
);
775 mpf_set_ui (b
, real_info
->radix
);
776 mpf_pow_ui (b
, b
, 1 - real_info
->min_exponent
);
778 mpf_init (real_info
->tiny
);
779 mpf_ui_div (real_info
->tiny
, 1, b
);
782 mpf_set_ui (b
, real_info
->radix
);
783 mpf_pow_ui (b
, b
, real_info
->digits
- 1);
785 mpf_init (real_info
->epsilon
);
786 mpf_ui_div (real_info
->epsilon
, 1, b
);
789 common_logarithm (&real_info
->huge
, &a
);
790 common_logarithm (&real_info
->tiny
, &b
);
793 if (mpf_cmp (a
, b
) > 0)
794 mpf_set (a
, b
); /* a = min(a, b) */
798 real_info
->range
= mpz_get_si (r
);
801 mpf_set_ui (a
, real_info
->radix
);
802 common_logarithm (&a
, &a
);
804 mpf_mul_ui (a
, a
, real_info
->digits
- 1);
807 real_info
->precision
= mpz_get_si (r
);
809 /* If the radix is an integral power of 10, add one to the
811 for (i
= 10; i
<= real_info
->radix
; i
*= 10)
812 if (i
== real_info
->radix
)
813 real_info
->precision
++;
822 /* Clean up, get rid of numeric constants. */
825 gfc_arith_done_1 (void)
827 gfc_integer_info
*ip
;
836 for (ip
= gfc_integer_kinds
; ip
->kind
; ip
++)
838 mpz_clear (ip
->min_int
);
839 mpz_clear (ip
->max_int
);
840 mpz_clear (ip
->huge
);
843 for (rp
= gfc_real_kinds
; rp
->kind
; rp
++)
845 mpf_clear (rp
->epsilon
);
846 mpf_clear (rp
->huge
);
847 mpf_clear (rp
->tiny
);
852 /* Return default kinds. */
855 gfc_default_integer_kind (void)
857 return gfc_integer_kinds
[gfc_option
.i8
? 1 : 0].kind
;
861 gfc_default_real_kind (void)
863 return gfc_real_kinds
[gfc_option
.r8
? 1 : 0].kind
;
867 gfc_default_double_kind (void)
869 return gfc_real_kinds
[1].kind
;
873 gfc_default_character_kind (void)
879 gfc_default_logical_kind (void)
881 return gfc_logical_kinds
[gfc_option
.i8
? 1 : 0].kind
;
885 gfc_default_complex_kind (void)
887 return gfc_default_real_kind ();
891 /* Make sure that a valid kind is present. Returns an index into the
892 gfc_integer_kinds array, -1 if the kind is not present. */
895 validate_integer (int kind
)
901 if (gfc_integer_kinds
[i
].kind
== 0)
906 if (gfc_integer_kinds
[i
].kind
== kind
)
915 validate_real (int kind
)
921 if (gfc_real_kinds
[i
].kind
== 0)
926 if (gfc_real_kinds
[i
].kind
== kind
)
935 validate_logical (int kind
)
941 if (gfc_logical_kinds
[i
].kind
== 0)
946 if (gfc_logical_kinds
[i
].kind
== kind
)
955 validate_character (int kind
)
958 if (kind
== gfc_default_character_kind ())
964 /* Validate a kind given a basic type. The return value is the same
965 for the child functions, with -1 indicating nonexistence of the
969 gfc_validate_kind (bt type
, int kind
)
975 case BT_REAL
: /* Fall through */
977 rc
= validate_real (kind
);
980 rc
= validate_integer (kind
);
983 rc
= validate_logical (kind
);
986 rc
= validate_character (kind
);
990 gfc_internal_error ("gfc_validate_kind(): Got bad type");
997 /* Given an integer and a kind, make sure that the integer lies within
998 the range of the kind. Returns ARITH_OK or ARITH_OVERFLOW. */
1001 gfc_check_integer_range (mpz_t p
, int kind
)
1006 i
= validate_integer (kind
);
1008 gfc_internal_error ("gfc_check_integer_range(): Bad kind");
1012 if (mpz_cmp (p
, gfc_integer_kinds
[i
].min_int
) < 0
1013 || mpz_cmp (p
, gfc_integer_kinds
[i
].max_int
) > 0)
1014 result
= ARITH_OVERFLOW
;
1020 /* Given a real and a kind, make sure that the real lies within the
1021 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
1025 gfc_check_real_range (mpf_t p
, int kind
)
1034 i
= validate_real (kind
);
1036 gfc_internal_error ("gfc_check_real_range(): Bad kind");
1039 if (mpf_sgn (q
) == 0)
1042 if (mpf_cmp (q
, gfc_real_kinds
[i
].huge
) == 1)
1044 retval
= ARITH_OVERFLOW
;
1048 if (mpf_cmp (q
, gfc_real_kinds
[i
].tiny
) == -1)
1049 retval
= ARITH_UNDERFLOW
;
1058 /* Function to return a constant expression node of a given type and
1062 gfc_constant_result (bt type
, int kind
, locus
* where
)
1068 ("gfc_constant_result(): locus 'where' cannot be NULL");
1070 result
= gfc_get_expr ();
1072 result
->expr_type
= EXPR_CONSTANT
;
1073 result
->ts
.type
= type
;
1074 result
->ts
.kind
= kind
;
1075 result
->where
= *where
;
1080 mpz_init (result
->value
.integer
);
1084 mpf_init (result
->value
.real
);
1088 mpf_init (result
->value
.complex.r
);
1089 mpf_init (result
->value
.complex.i
);
1100 /* Low-level arithmetic functions. All of these subroutines assume
1101 that all operands are of the same type and return an operand of the
1102 same type. The other thing about these subroutines is that they
1103 can fail in various ways -- overflow, underflow, division by zero,
1104 zero raised to the zero, etc. */
1107 gfc_arith_not (gfc_expr
* op1
, gfc_expr
** resultp
)
1111 result
= gfc_constant_result (BT_LOGICAL
, op1
->ts
.kind
, &op1
->where
);
1112 result
->value
.logical
= !op1
->value
.logical
;
1120 gfc_arith_and (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1124 result
= gfc_constant_result (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
1126 result
->value
.logical
= op1
->value
.logical
&& op2
->value
.logical
;
1134 gfc_arith_or (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1138 result
= gfc_constant_result (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
1140 result
->value
.logical
= op1
->value
.logical
|| op2
->value
.logical
;
1148 gfc_arith_eqv (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1152 result
= gfc_constant_result (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
1154 result
->value
.logical
= op1
->value
.logical
== op2
->value
.logical
;
1162 gfc_arith_neqv (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1166 result
= gfc_constant_result (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
1168 result
->value
.logical
= op1
->value
.logical
!= op2
->value
.logical
;
1175 /* Make sure a constant numeric expression is within the range for
1176 its type and kind. GMP is doing 130 bit arithmetic, so an UNDERFLOW
1177 is numerically zero for REAL(4) and REAL(8) types. Reset the value(s)
1178 to exactly 0 for UNDERFLOW. Note that there's also a gfc_check_range(),
1179 but that one deals with the intrinsic RANGE function. */
1182 gfc_range_check (gfc_expr
* e
)
1189 rc
= gfc_check_integer_range (e
->value
.integer
, e
->ts
.kind
);
1193 rc
= gfc_check_real_range (e
->value
.real
, e
->ts
.kind
);
1194 if (rc
== ARITH_UNDERFLOW
)
1195 mpf_set_ui (e
->value
.real
, 0);
1199 rc
= gfc_check_real_range (e
->value
.complex.r
, e
->ts
.kind
);
1200 if (rc
== ARITH_UNDERFLOW
)
1201 mpf_set_ui (e
->value
.complex.r
, 0);
1202 if (rc
== ARITH_OK
|| rc
== ARITH_UNDERFLOW
)
1204 rc
= gfc_check_real_range (e
->value
.complex.i
, e
->ts
.kind
);
1205 if (rc
== ARITH_UNDERFLOW
)
1206 mpf_set_ui (e
->value
.complex.i
, 0);
1212 gfc_internal_error ("gfc_range_check(): Bad type");
1219 /* It may seem silly to have a subroutine that actually computes the
1220 unary plus of a constant, but it prevents us from making exceptions
1221 in the code elsewhere. */
1224 gfc_arith_uplus (gfc_expr
* op1
, gfc_expr
** resultp
)
1227 *resultp
= gfc_copy_expr (op1
);
1233 gfc_arith_uminus (gfc_expr
* op1
, gfc_expr
** resultp
)
1238 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1240 switch (op1
->ts
.type
)
1243 mpz_neg (result
->value
.integer
, op1
->value
.integer
);
1247 mpf_neg (result
->value
.real
, op1
->value
.real
);
1251 mpf_neg (result
->value
.complex.r
, op1
->value
.complex.r
);
1252 mpf_neg (result
->value
.complex.i
, op1
->value
.complex.i
);
1256 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
1259 rc
= gfc_range_check (result
);
1261 if (rc
== ARITH_UNDERFLOW
)
1263 if (gfc_option
.warn_underflow
)
1264 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1268 else if (rc
!= ARITH_OK
)
1269 gfc_free_expr (result
);
1278 gfc_arith_plus (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1283 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1285 switch (op1
->ts
.type
)
1288 mpz_add (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
1292 mpf_add (result
->value
.real
, op1
->value
.real
, op2
->value
.real
);
1296 mpf_add (result
->value
.complex.r
, op1
->value
.complex.r
,
1297 op2
->value
.complex.r
);
1299 mpf_add (result
->value
.complex.i
, op1
->value
.complex.i
,
1300 op2
->value
.complex.i
);
1304 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
1307 rc
= gfc_range_check (result
);
1309 if (rc
== ARITH_UNDERFLOW
)
1311 if (gfc_option
.warn_underflow
)
1312 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1316 else if (rc
!= ARITH_OK
)
1317 gfc_free_expr (result
);
1326 gfc_arith_minus (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1331 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1333 switch (op1
->ts
.type
)
1336 mpz_sub (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
1340 mpf_sub (result
->value
.real
, op1
->value
.real
, op2
->value
.real
);
1344 mpf_sub (result
->value
.complex.r
, op1
->value
.complex.r
,
1345 op2
->value
.complex.r
);
1347 mpf_sub (result
->value
.complex.i
, op1
->value
.complex.i
,
1348 op2
->value
.complex.i
);
1353 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
1356 rc
= gfc_range_check (result
);
1358 if (rc
== ARITH_UNDERFLOW
)
1360 if (gfc_option
.warn_underflow
)
1361 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1365 else if (rc
!= ARITH_OK
)
1366 gfc_free_expr (result
);
1375 gfc_arith_times (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1381 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1383 switch (op1
->ts
.type
)
1386 mpz_mul (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
1390 mpf_mul (result
->value
.real
, op1
->value
.real
, op2
->value
.real
);
1397 mpf_mul (x
, op1
->value
.complex.r
, op2
->value
.complex.r
);
1398 mpf_mul (y
, op1
->value
.complex.i
, op2
->value
.complex.i
);
1399 mpf_sub (result
->value
.complex.r
, x
, y
);
1401 mpf_mul (x
, op1
->value
.complex.r
, op2
->value
.complex.i
);
1402 mpf_mul (y
, op1
->value
.complex.i
, op2
->value
.complex.r
);
1403 mpf_add (result
->value
.complex.i
, x
, y
);
1411 gfc_internal_error ("gfc_arith_times(): Bad basic type");
1414 rc
= gfc_range_check (result
);
1416 if (rc
== ARITH_UNDERFLOW
)
1418 if (gfc_option
.warn_underflow
)
1419 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1423 else if (rc
!= ARITH_OK
)
1424 gfc_free_expr (result
);
1433 gfc_arith_divide (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1441 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1443 switch (op1
->ts
.type
)
1446 if (mpz_sgn (op2
->value
.integer
) == 0)
1452 mpz_tdiv_q (result
->value
.integer
, op1
->value
.integer
,
1453 op2
->value
.integer
);
1457 if (mpf_sgn (op2
->value
.real
) == 0)
1463 mpf_div (result
->value
.real
, op1
->value
.real
, op2
->value
.real
);
1467 if (mpf_sgn (op2
->value
.complex.r
) == 0
1468 && mpf_sgn (op2
->value
.complex.i
) == 0)
1478 mpf_mul (x
, op2
->value
.complex.r
, op2
->value
.complex.r
);
1479 mpf_mul (y
, op2
->value
.complex.i
, op2
->value
.complex.i
);
1480 mpf_add (div
, x
, y
);
1482 mpf_mul (x
, op1
->value
.complex.r
, op2
->value
.complex.r
);
1483 mpf_mul (y
, op1
->value
.complex.i
, op2
->value
.complex.i
);
1484 mpf_add (result
->value
.complex.r
, x
, y
);
1485 mpf_div (result
->value
.complex.r
, result
->value
.complex.r
, div
);
1487 mpf_mul (x
, op1
->value
.complex.i
, op2
->value
.complex.r
);
1488 mpf_mul (y
, op1
->value
.complex.r
, op2
->value
.complex.i
);
1489 mpf_sub (result
->value
.complex.i
, x
, y
);
1490 mpf_div (result
->value
.complex.i
, result
->value
.complex.i
, div
);
1499 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
1503 rc
= gfc_range_check (result
);
1505 if (rc
== ARITH_UNDERFLOW
)
1507 if (gfc_option
.warn_underflow
)
1508 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1512 else if (rc
!= ARITH_OK
)
1513 gfc_free_expr (result
);
1521 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
1524 complex_reciprocal (gfc_expr
* op
)
1526 mpf_t mod
, a
, result_r
, result_i
;
1531 mpf_mul (mod
, op
->value
.complex.r
, op
->value
.complex.r
);
1532 mpf_mul (a
, op
->value
.complex.i
, op
->value
.complex.i
);
1533 mpf_add (mod
, mod
, a
);
1535 mpf_init (result_r
);
1536 mpf_div (result_r
, op
->value
.complex.r
, mod
);
1538 mpf_init (result_i
);
1539 mpf_neg (result_i
, op
->value
.complex.i
);
1540 mpf_div (result_i
, result_i
, mod
);
1542 mpf_set (op
->value
.complex.r
, result_r
);
1543 mpf_set (op
->value
.complex.i
, result_i
);
1545 mpf_clear (result_r
);
1546 mpf_clear (result_i
);
1553 /* Raise a complex number to positive power. */
1556 complex_pow_ui (gfc_expr
* base
, int power
, gfc_expr
* result
)
1558 mpf_t temp_r
, temp_i
, a
;
1560 mpf_set_ui (result
->value
.complex.r
, 1);
1561 mpf_set_ui (result
->value
.complex.i
, 0);
1567 for (; power
> 0; power
--)
1569 mpf_mul (temp_r
, base
->value
.complex.r
, result
->value
.complex.r
);
1570 mpf_mul (a
, base
->value
.complex.i
, result
->value
.complex.i
);
1571 mpf_sub (temp_r
, temp_r
, a
);
1573 mpf_mul (temp_i
, base
->value
.complex.r
, result
->value
.complex.i
);
1574 mpf_mul (a
, base
->value
.complex.i
, result
->value
.complex.r
);
1575 mpf_add (temp_i
, temp_i
, a
);
1577 mpf_set (result
->value
.complex.r
, temp_r
);
1578 mpf_set (result
->value
.complex.i
, temp_i
);
1587 /* Raise a number to an integer power. */
1590 gfc_arith_power (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1600 if (gfc_extract_int (op2
, &power
) != NULL
)
1601 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1603 result
= gfc_constant_result (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
1606 { /* Handle something to the zeroth power */
1607 switch (op1
->ts
.type
)
1610 if (mpz_sgn (op1
->value
.integer
) == 0)
1613 mpz_set_ui (result
->value
.integer
, 1);
1618 if (mpf_sgn (op1
->value
.real
) == 0)
1621 mpf_set_ui (result
->value
.real
, 1);
1626 if (mpf_sgn (op1
->value
.complex.r
) == 0
1627 && mpf_sgn (op1
->value
.complex.i
) == 0)
1631 mpf_set_ui (result
->value
.complex.r
, 1);
1632 mpf_set_ui (result
->value
.complex.i
, 0);
1638 gfc_internal_error ("gfc_arith_power(): Bad base");
1648 switch (op1
->ts
.type
)
1651 mpz_pow_ui (result
->value
.integer
, op1
->value
.integer
, apower
);
1655 mpz_init_set_ui (unity_z
, 1);
1656 mpz_tdiv_q (result
->value
.integer
, unity_z
,
1657 result
->value
.integer
);
1658 mpz_clear (unity_z
);
1664 mpf_pow_ui (result
->value
.real
, op1
->value
.real
, apower
);
1668 mpf_init_set_ui (unity_f
, 1);
1669 mpf_div (result
->value
.real
, unity_f
, result
->value
.real
);
1670 mpf_clear (unity_f
);
1676 complex_pow_ui (op1
, apower
, result
);
1678 complex_reciprocal (result
);
1688 rc
= gfc_range_check (result
);
1690 if (rc
== ARITH_UNDERFLOW
)
1692 if (gfc_option
.warn_underflow
)
1693 gfc_warning ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
1697 else if (rc
!= ARITH_OK
)
1698 gfc_free_expr (result
);
1706 /* Concatenate two string constants. */
1709 gfc_arith_concat (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1714 result
= gfc_constant_result (BT_CHARACTER
, gfc_default_character_kind (),
1717 len
= op1
->value
.character
.length
+ op2
->value
.character
.length
;
1719 result
->value
.character
.string
= gfc_getmem (len
+ 1);
1720 result
->value
.character
.length
= len
;
1722 memcpy (result
->value
.character
.string
, op1
->value
.character
.string
,
1723 op1
->value
.character
.length
);
1725 memcpy (result
->value
.character
.string
+ op1
->value
.character
.length
,
1726 op2
->value
.character
.string
, op2
->value
.character
.length
);
1728 result
->value
.character
.string
[len
] = '\0';
1736 /* Comparison operators. Assumes that the two expression nodes
1737 contain two constants of the same type. */
1740 gfc_compare_expr (gfc_expr
* op1
, gfc_expr
* op2
)
1744 switch (op1
->ts
.type
)
1747 rc
= mpz_cmp (op1
->value
.integer
, op2
->value
.integer
);
1751 rc
= mpf_cmp (op1
->value
.real
, op2
->value
.real
);
1755 rc
= gfc_compare_string (op1
, op2
, NULL
);
1759 rc
= ((!op1
->value
.logical
&& op2
->value
.logical
)
1760 || (op1
->value
.logical
&& !op2
->value
.logical
));
1764 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1771 /* Compare a pair of complex numbers. Naturally, this is only for
1772 equality/nonequality. */
1775 compare_complex (gfc_expr
* op1
, gfc_expr
* op2
)
1778 return (mpf_cmp (op1
->value
.complex.r
, op2
->value
.complex.r
) == 0
1779 && mpf_cmp (op1
->value
.complex.i
, op2
->value
.complex.i
) == 0);
1783 /* Given two constant strings and the inverse collating sequence,
1784 compare the strings. We return -1 for a<b, 0 for a==b and 1 for
1785 a>b. If the xcoll_table is NULL, we use the processor's default
1786 collating sequence. */
1789 gfc_compare_string (gfc_expr
* a
, gfc_expr
* b
, const int *xcoll_table
)
1791 int len
, alen
, blen
, i
, ac
, bc
;
1793 alen
= a
->value
.character
.length
;
1794 blen
= b
->value
.character
.length
;
1796 len
= (alen
> blen
) ? alen
: blen
;
1798 for (i
= 0; i
< len
; i
++)
1800 ac
= (i
< alen
) ? a
->value
.character
.string
[i
] : ' ';
1801 bc
= (i
< blen
) ? b
->value
.character
.string
[i
] : ' ';
1803 if (xcoll_table
!= NULL
)
1805 ac
= xcoll_table
[ac
];
1806 bc
= xcoll_table
[bc
];
1815 /* Strings are equal */
1821 /* Specific comparison subroutines. */
1824 gfc_arith_eq (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1828 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1830 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
) ?
1831 compare_complex (op1
, op2
) : (gfc_compare_expr (op1
, op2
) == 0);
1839 gfc_arith_ne (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1843 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1845 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
) ?
1846 !compare_complex (op1
, op2
) : (gfc_compare_expr (op1
, op2
) != 0);
1854 gfc_arith_gt (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1858 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1860 result
->value
.logical
= (gfc_compare_expr (op1
, op2
) > 0);
1868 gfc_arith_ge (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1872 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1874 result
->value
.logical
= (gfc_compare_expr (op1
, op2
) >= 0);
1882 gfc_arith_lt (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1886 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1888 result
->value
.logical
= (gfc_compare_expr (op1
, op2
) < 0);
1896 gfc_arith_le (gfc_expr
* op1
, gfc_expr
* op2
, gfc_expr
** resultp
)
1900 result
= gfc_constant_result (BT_LOGICAL
, gfc_default_logical_kind (),
1902 result
->value
.logical
= (gfc_compare_expr (op1
, op2
) <= 0);
1910 reduce_unary (arith (*eval
) (gfc_expr
*, gfc_expr
**), gfc_expr
* op
,
1913 gfc_constructor
*c
, *head
;
1917 if (op
->expr_type
== EXPR_CONSTANT
)
1918 return eval (op
, result
);
1921 head
= gfc_copy_constructor (op
->value
.constructor
);
1923 for (c
= head
; c
; c
= c
->next
)
1925 rc
= eval (c
->expr
, &r
);
1929 gfc_replace_expr (c
->expr
, r
);
1933 gfc_free_constructor (head
);
1936 r
= gfc_get_expr ();
1937 r
->expr_type
= EXPR_ARRAY
;
1938 r
->value
.constructor
= head
;
1939 r
->shape
= gfc_copy_shape (op
->shape
, op
->rank
);
1941 r
->ts
= head
->expr
->ts
;
1942 r
->where
= op
->where
;
1953 reduce_binary_ac (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1954 gfc_expr
* op1
, gfc_expr
* op2
,
1957 gfc_constructor
*c
, *head
;
1961 head
= gfc_copy_constructor (op1
->value
.constructor
);
1964 for (c
= head
; c
; c
= c
->next
)
1966 rc
= eval (c
->expr
, op2
, &r
);
1970 gfc_replace_expr (c
->expr
, r
);
1974 gfc_free_constructor (head
);
1977 r
= gfc_get_expr ();
1978 r
->expr_type
= EXPR_ARRAY
;
1979 r
->value
.constructor
= head
;
1980 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1982 r
->ts
= head
->expr
->ts
;
1983 r
->where
= op1
->where
;
1984 r
->rank
= op1
->rank
;
1994 reduce_binary_ca (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1995 gfc_expr
* op1
, gfc_expr
* op2
,
1998 gfc_constructor
*c
, *head
;
2002 head
= gfc_copy_constructor (op2
->value
.constructor
);
2005 for (c
= head
; c
; c
= c
->next
)
2007 rc
= eval (op1
, c
->expr
, &r
);
2011 gfc_replace_expr (c
->expr
, r
);
2015 gfc_free_constructor (head
);
2018 r
= gfc_get_expr ();
2019 r
->expr_type
= EXPR_ARRAY
;
2020 r
->value
.constructor
= head
;
2021 r
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
2023 r
->ts
= head
->expr
->ts
;
2024 r
->where
= op2
->where
;
2025 r
->rank
= op2
->rank
;
2035 reduce_binary_aa (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
2036 gfc_expr
* op1
, gfc_expr
* op2
,
2039 gfc_constructor
*c
, *d
, *head
;
2043 head
= gfc_copy_constructor (op1
->value
.constructor
);
2046 d
= op2
->value
.constructor
;
2048 if (gfc_check_conformance ("Elemental binary operation", op1
, op2
)
2050 rc
= ARITH_INCOMMENSURATE
;
2054 for (c
= head
; c
; c
= c
->next
, d
= d
->next
)
2058 rc
= ARITH_INCOMMENSURATE
;
2062 rc
= eval (c
->expr
, d
->expr
, &r
);
2066 gfc_replace_expr (c
->expr
, r
);
2070 rc
= ARITH_INCOMMENSURATE
;
2074 gfc_free_constructor (head
);
2077 r
= gfc_get_expr ();
2078 r
->expr_type
= EXPR_ARRAY
;
2079 r
->value
.constructor
= head
;
2080 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
2082 r
->ts
= head
->expr
->ts
;
2083 r
->where
= op1
->where
;
2084 r
->rank
= op1
->rank
;
2094 reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
2095 gfc_expr
* op1
, gfc_expr
* op2
,
2099 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_CONSTANT
)
2100 return eval (op1
, op2
, result
);
2102 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_ARRAY
)
2103 return reduce_binary_ca (eval
, op1
, op2
, result
);
2105 if (op1
->expr_type
== EXPR_ARRAY
&& op2
->expr_type
== EXPR_CONSTANT
)
2106 return reduce_binary_ac (eval
, op1
, op2
, result
);
2108 return reduce_binary_aa (eval
, op1
, op2
, result
);
2114 arith (*f2
)(gfc_expr
*, gfc_expr
**);
2115 arith (*f3
)(gfc_expr
*, gfc_expr
*, gfc_expr
**);
2119 /* High level arithmetic subroutines. These subroutines go into
2120 eval_intrinsic(), which can do one of several things to its
2121 operands. If the operands are incompatible with the intrinsic
2122 operation, we return a node pointing to the operands and hope that
2123 an operator interface is found during resolution.
2125 If the operands are compatible and are constants, then we try doing
2126 the arithmetic. We also handle the cases where either or both
2127 operands are array constructors. */
2130 eval_intrinsic (gfc_intrinsic_op
operator,
2131 eval_f eval
, gfc_expr
* op1
, gfc_expr
* op2
)
2133 gfc_expr temp
, *result
;
2137 gfc_clear_ts (&temp
.ts
);
2141 case INTRINSIC_NOT
: /* Logical unary */
2142 if (op1
->ts
.type
!= BT_LOGICAL
)
2145 temp
.ts
.type
= BT_LOGICAL
;
2146 temp
.ts
.kind
= gfc_default_logical_kind ();
2151 /* Logical binary operators */
2154 case INTRINSIC_NEQV
:
2156 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
2159 temp
.ts
.type
= BT_LOGICAL
;
2160 temp
.ts
.kind
= gfc_default_logical_kind ();
2165 case INTRINSIC_UPLUS
:
2166 case INTRINSIC_UMINUS
: /* Numeric unary */
2167 if (!gfc_numeric_ts (&op1
->ts
))
2176 case INTRINSIC_LT
: /* Additional restrictions */
2177 case INTRINSIC_LE
: /* for ordering relations. */
2179 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
2181 temp
.ts
.type
= BT_LOGICAL
;
2182 temp
.ts
.kind
= gfc_default_logical_kind();
2186 /* else fall through */
2190 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
)
2193 temp
.ts
.type
= BT_LOGICAL
;
2194 temp
.ts
.kind
= gfc_default_logical_kind();
2198 /* else fall through */
2200 case INTRINSIC_PLUS
:
2201 case INTRINSIC_MINUS
:
2202 case INTRINSIC_TIMES
:
2203 case INTRINSIC_DIVIDE
:
2204 case INTRINSIC_POWER
: /* Numeric binary */
2205 if (!gfc_numeric_ts (&op1
->ts
) || !gfc_numeric_ts (&op2
->ts
))
2208 /* Insert any necessary type conversions to make the operands compatible. */
2210 temp
.expr_type
= EXPR_OP
;
2211 gfc_clear_ts (&temp
.ts
);
2212 temp
.operator = operator;
2217 gfc_type_convert_binary (&temp
);
2219 if (operator == INTRINSIC_EQ
|| operator == INTRINSIC_NE
2220 || operator == INTRINSIC_GE
|| operator == INTRINSIC_GT
2221 || operator == INTRINSIC_LE
|| operator == INTRINSIC_LT
)
2223 temp
.ts
.type
= BT_LOGICAL
;
2224 temp
.ts
.kind
= gfc_default_logical_kind ();
2230 case INTRINSIC_CONCAT
: /* Character binary */
2231 if (op1
->ts
.type
!= BT_CHARACTER
|| op2
->ts
.type
!= BT_CHARACTER
)
2234 temp
.ts
.type
= BT_CHARACTER
;
2235 temp
.ts
.kind
= gfc_default_character_kind ();
2240 case INTRINSIC_USER
:
2244 gfc_internal_error ("eval_intrinsic(): Bad operator");
2247 /* Try to combine the operators. */
2248 if (operator == INTRINSIC_POWER
&& op2
->ts
.type
!= BT_INTEGER
)
2251 if (op1
->expr_type
!= EXPR_CONSTANT
2252 && (op1
->expr_type
!= EXPR_ARRAY
2253 || !gfc_is_constant_expr (op1
)
2254 || !gfc_expanded_ac (op1
)))
2258 && op2
->expr_type
!= EXPR_CONSTANT
2259 && (op2
->expr_type
!= EXPR_ARRAY
2260 || !gfc_is_constant_expr (op2
)
2261 || !gfc_expanded_ac (op2
)))
2265 rc
= reduce_unary (eval
.f2
, op1
, &result
);
2267 rc
= reduce_binary (eval
.f3
, op1
, op2
, &result
);
2270 { /* Something went wrong */
2271 gfc_error ("%s at %L", gfc_arith_error (rc
), &op1
->where
);
2275 gfc_free_expr (op1
);
2276 gfc_free_expr (op2
);
2280 /* Create a run-time expression */
2281 result
= gfc_get_expr ();
2282 result
->ts
= temp
.ts
;
2284 result
->expr_type
= EXPR_OP
;
2285 result
->operator = operator;
2290 result
->where
= op1
->where
;
2296 /* Modify type of expression for zero size array. */
2298 eval_type_intrinsic0 (gfc_intrinsic_op
operator, gfc_expr
*op
)
2301 gfc_internal_error("eval_type_intrinsic0(): op NULL");
2311 op
->ts
.type
= BT_LOGICAL
;
2312 op
->ts
.kind
= gfc_default_logical_kind();
2323 /* Return nonzero if the expression is a zero size array. */
2326 gfc_zero_size_array (gfc_expr
* e
)
2329 if (e
->expr_type
!= EXPR_ARRAY
)
2332 return e
->value
.constructor
== NULL
;
2336 /* Reduce a binary expression where at least one of the operands
2337 involves a zero-length array. Returns NULL if neither of the
2338 operands is a zero-length array. */
2341 reduce_binary0 (gfc_expr
* op1
, gfc_expr
* op2
)
2344 if (gfc_zero_size_array (op1
))
2346 gfc_free_expr (op2
);
2350 if (gfc_zero_size_array (op2
))
2352 gfc_free_expr (op1
);
2361 eval_intrinsic_f2 (gfc_intrinsic_op
operator,
2362 arith (*eval
) (gfc_expr
*, gfc_expr
**),
2363 gfc_expr
* op1
, gfc_expr
* op2
)
2370 if (gfc_zero_size_array (op1
))
2371 return eval_type_intrinsic0(operator, op1
);
2375 result
= reduce_binary0 (op1
, op2
);
2377 return eval_type_intrinsic0(operator, result
);
2381 return eval_intrinsic (operator, f
, op1
, op2
);
2386 eval_intrinsic_f3 (gfc_intrinsic_op
operator,
2387 arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
2388 gfc_expr
* op1
, gfc_expr
* op2
)
2393 result
= reduce_binary0 (op1
, op2
);
2395 return eval_type_intrinsic0(operator, result
);
2398 return eval_intrinsic (operator, f
, op1
, op2
);
2404 gfc_uplus (gfc_expr
* op
)
2406 return eval_intrinsic_f2 (INTRINSIC_UPLUS
, gfc_arith_uplus
, op
, NULL
);
2410 gfc_uminus (gfc_expr
* op
)
2412 return eval_intrinsic_f2 (INTRINSIC_UMINUS
, gfc_arith_uminus
, op
, NULL
);
2416 gfc_add (gfc_expr
* op1
, gfc_expr
* op2
)
2418 return eval_intrinsic_f3 (INTRINSIC_PLUS
, gfc_arith_plus
, op1
, op2
);
2422 gfc_subtract (gfc_expr
* op1
, gfc_expr
* op2
)
2424 return eval_intrinsic_f3 (INTRINSIC_MINUS
, gfc_arith_minus
, op1
, op2
);
2428 gfc_multiply (gfc_expr
* op1
, gfc_expr
* op2
)
2430 return eval_intrinsic_f3 (INTRINSIC_TIMES
, gfc_arith_times
, op1
, op2
);
2434 gfc_divide (gfc_expr
* op1
, gfc_expr
* op2
)
2436 return eval_intrinsic_f3 (INTRINSIC_DIVIDE
, gfc_arith_divide
, op1
, op2
);
2440 gfc_power (gfc_expr
* op1
, gfc_expr
* op2
)
2442 return eval_intrinsic_f3 (INTRINSIC_POWER
, gfc_arith_power
, op1
, op2
);
2446 gfc_concat (gfc_expr
* op1
, gfc_expr
* op2
)
2448 return eval_intrinsic_f3 (INTRINSIC_CONCAT
, gfc_arith_concat
, op1
, op2
);
2452 gfc_and (gfc_expr
* op1
, gfc_expr
* op2
)
2454 return eval_intrinsic_f3 (INTRINSIC_AND
, gfc_arith_and
, op1
, op2
);
2458 gfc_or (gfc_expr
* op1
, gfc_expr
* op2
)
2460 return eval_intrinsic_f3 (INTRINSIC_OR
, gfc_arith_or
, op1
, op2
);
2464 gfc_not (gfc_expr
* op1
)
2466 return eval_intrinsic_f2 (INTRINSIC_NOT
, gfc_arith_not
, op1
, NULL
);
2470 gfc_eqv (gfc_expr
* op1
, gfc_expr
* op2
)
2472 return eval_intrinsic_f3 (INTRINSIC_EQV
, gfc_arith_eqv
, op1
, op2
);
2476 gfc_neqv (gfc_expr
* op1
, gfc_expr
* op2
)
2478 return eval_intrinsic_f3 (INTRINSIC_NEQV
, gfc_arith_neqv
, op1
, op2
);
2482 gfc_eq (gfc_expr
* op1
, gfc_expr
* op2
)
2484 return eval_intrinsic_f3 (INTRINSIC_EQ
, gfc_arith_eq
, op1
, op2
);
2488 gfc_ne (gfc_expr
* op1
, gfc_expr
* op2
)
2490 return eval_intrinsic_f3 (INTRINSIC_NE
, gfc_arith_ne
, op1
, op2
);
2494 gfc_gt (gfc_expr
* op1
, gfc_expr
* op2
)
2496 return eval_intrinsic_f3 (INTRINSIC_GT
, gfc_arith_gt
, op1
, op2
);
2500 gfc_ge (gfc_expr
* op1
, gfc_expr
* op2
)
2502 return eval_intrinsic_f3 (INTRINSIC_GE
, gfc_arith_ge
, op1
, op2
);
2506 gfc_lt (gfc_expr
* op1
, gfc_expr
* op2
)
2508 return eval_intrinsic_f3 (INTRINSIC_LT
, gfc_arith_lt
, op1
, op2
);
2512 gfc_le (gfc_expr
* op1
, gfc_expr
* op2
)
2514 return eval_intrinsic_f3 (INTRINSIC_LE
, gfc_arith_le
, op1
, op2
);
2518 /* Convert an integer string to an expression node. */
2521 gfc_convert_integer (const char *buffer
, int kind
, int radix
, locus
* where
)
2526 e
= gfc_constant_result (BT_INTEGER
, kind
, where
);
2527 /* a leading plus is allowed, but not by mpz_set_str */
2528 if (buffer
[0] == '+')
2532 mpz_set_str (e
->value
.integer
, t
, radix
);
2538 /* Convert a real string to an expression node. */
2541 gfc_convert_real (const char *buffer
, int kind
, locus
* where
)
2546 e
= gfc_constant_result (BT_REAL
, kind
, where
);
2547 /* a leading plus is allowed, but not by mpf_set_str */
2548 if (buffer
[0] == '+')
2552 mpf_set_str (e
->value
.real
, t
, 10);
2558 /* Convert a pair of real, constant expression nodes to a single
2559 complex expression node. */
2562 gfc_convert_complex (gfc_expr
* real
, gfc_expr
* imag
, int kind
)
2566 e
= gfc_constant_result (BT_COMPLEX
, kind
, &real
->where
);
2567 mpf_set (e
->value
.complex.r
, real
->value
.real
);
2568 mpf_set (e
->value
.complex.i
, imag
->value
.real
);
2574 /******* Simplification of intrinsic functions with constant arguments *****/
2577 /* Deal with an arithmetic error. */
2580 arith_error (arith rc
, gfc_typespec
* from
, gfc_typespec
* to
, locus
* where
)
2583 gfc_error ("%s converting %s to %s at %L", gfc_arith_error (rc
),
2584 gfc_typename (from
), gfc_typename (to
), where
);
2586 /* TODO: Do something about the error, ie, throw exception, return
2590 /* Convert integers to integers. */
2593 gfc_int2int (gfc_expr
* src
, int kind
)
2598 result
= gfc_constant_result (BT_INTEGER
, kind
, &src
->where
);
2600 mpz_set (result
->value
.integer
, src
->value
.integer
);
2602 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
))
2605 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2606 gfc_free_expr (result
);
2614 /* Convert integers to reals. */
2617 gfc_int2real (gfc_expr
* src
, int kind
)
2622 result
= gfc_constant_result (BT_REAL
, kind
, &src
->where
);
2624 mpf_set_z (result
->value
.real
, src
->value
.integer
);
2626 if ((rc
= gfc_check_real_range (result
->value
.real
, kind
)) != ARITH_OK
)
2628 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2629 gfc_free_expr (result
);
2637 /* Convert default integer to default complex. */
2640 gfc_int2complex (gfc_expr
* src
, int kind
)
2645 result
= gfc_constant_result (BT_COMPLEX
, kind
, &src
->where
);
2647 mpf_set_z (result
->value
.complex.r
, src
->value
.integer
);
2648 mpf_set_ui (result
->value
.complex.i
, 0);
2650 if ((rc
= gfc_check_real_range (result
->value
.complex.r
, kind
)) != ARITH_OK
)
2652 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2653 gfc_free_expr (result
);
2661 /* Convert default real to default integer. */
2664 gfc_real2int (gfc_expr
* src
, int kind
)
2669 result
= gfc_constant_result (BT_INTEGER
, kind
, &src
->where
);
2671 mpz_set_f (result
->value
.integer
, src
->value
.real
);
2673 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
))
2676 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2677 gfc_free_expr (result
);
2685 /* Convert real to real. */
2688 gfc_real2real (gfc_expr
* src
, int kind
)
2693 result
= gfc_constant_result (BT_REAL
, kind
, &src
->where
);
2695 mpf_set (result
->value
.real
, src
->value
.real
);
2697 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2699 if (rc
== ARITH_UNDERFLOW
)
2701 if (gfc_option
.warn_underflow
)
2702 gfc_warning ("%s at %L", gfc_arith_error (rc
), &src
->where
);
2703 mpf_set_ui(result
->value
.real
, 0);
2705 else if (rc
!= ARITH_OK
)
2707 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2708 gfc_free_expr (result
);
2716 /* Convert real to complex. */
2719 gfc_real2complex (gfc_expr
* src
, int kind
)
2724 result
= gfc_constant_result (BT_COMPLEX
, kind
, &src
->where
);
2726 mpf_set (result
->value
.complex.r
, src
->value
.real
);
2727 mpf_set_ui (result
->value
.complex.i
, 0);
2729 rc
= gfc_check_real_range (result
->value
.complex.r
, kind
);
2731 if (rc
== ARITH_UNDERFLOW
)
2733 if (gfc_option
.warn_underflow
)
2734 gfc_warning ("%s at %L", gfc_arith_error (rc
), &src
->where
);
2735 mpf_set_ui(result
->value
.complex.r
, 0);
2737 else if (rc
!= ARITH_OK
)
2739 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2740 gfc_free_expr (result
);
2748 /* Convert complex to integer. */
2751 gfc_complex2int (gfc_expr
* src
, int kind
)
2756 result
= gfc_constant_result (BT_INTEGER
, kind
, &src
->where
);
2758 mpz_set_f (result
->value
.integer
, src
->value
.complex.r
);
2760 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
))
2763 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2764 gfc_free_expr (result
);
2772 /* Convert complex to real. */
2775 gfc_complex2real (gfc_expr
* src
, int kind
)
2780 result
= gfc_constant_result (BT_REAL
, kind
, &src
->where
);
2782 mpf_set (result
->value
.real
, src
->value
.complex.r
);
2784 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2786 if (rc
== ARITH_UNDERFLOW
)
2788 if (gfc_option
.warn_underflow
)
2789 gfc_warning ("%s at %L", gfc_arith_error (rc
), &src
->where
);
2790 mpf_set_ui(result
->value
.real
, 0);
2794 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2795 gfc_free_expr (result
);
2803 /* Convert complex to complex. */
2806 gfc_complex2complex (gfc_expr
* src
, int kind
)
2811 result
= gfc_constant_result (BT_COMPLEX
, kind
, &src
->where
);
2813 mpf_set (result
->value
.complex.r
, src
->value
.complex.r
);
2814 mpf_set (result
->value
.complex.i
, src
->value
.complex.i
);
2816 rc
= gfc_check_real_range (result
->value
.complex.r
, kind
);
2818 if (rc
== ARITH_UNDERFLOW
)
2820 if (gfc_option
.warn_underflow
)
2821 gfc_warning ("%s at %L", gfc_arith_error (rc
), &src
->where
);
2822 mpf_set_ui(result
->value
.complex.r
, 0);
2824 else if (rc
!= ARITH_OK
)
2826 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2827 gfc_free_expr (result
);
2831 rc
= gfc_check_real_range (result
->value
.complex.i
, kind
);
2833 if (rc
== ARITH_UNDERFLOW
)
2835 if (gfc_option
.warn_underflow
)
2836 gfc_warning ("%s at %L", gfc_arith_error (rc
), &src
->where
);
2837 mpf_set_ui(result
->value
.complex.i
, 0);
2839 else if (rc
!= ARITH_OK
)
2841 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2842 gfc_free_expr (result
);
2850 /* Logical kind conversion. */
2853 gfc_log2log (gfc_expr
* src
, int kind
)
2857 result
= gfc_constant_result (BT_LOGICAL
, kind
, &src
->where
);
2858 result
->value
.logical
= src
->value
.logical
;