1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
4 Contributed by Andy Vaught & Katherine Holcomb
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
31 #include "intrinsic.h"
33 gfc_expr gfc_bad_expr;
36 /* Note that 'simplification' is not just transforming expressions.
37 For functions that are not simplified at compile time, range
38 checking is done if possible.
40 The return convention is that each simplification function returns:
42 A new expression node corresponding to the simplified arguments.
43 The original arguments are destroyed by the caller, and must not
44 be a part of the new expression.
46 NULL pointer indicating that no simplification was possible and
47 the original expression should remain intact. If the
48 simplification function sets the type and/or the function name
49 via the pointer gfc_simple_expression, then this type is
52 An expression pointer to gfc_bad_expr (a static placeholder)
53 indicating that some error has prevented simplification. For
54 example, sqrt(-1.0). The error is generated within the function
55 and should be propagated upwards
57 By the time a simplification function gets control, it has been
58 decided that the function call is really supposed to be the
59 intrinsic. No type checking is strictly necessary, since only
60 valid types will be passed on. On the other hand, a simplification
61 subroutine may have to look at the type of an argument as part of
64 Array arguments are never passed to these subroutines.
66 The functions in this file don't have much comment with them, but
67 everything is reasonably straight-forward. The Standard, chapter 13
68 is the best comment you'll find for this file anyway. */
70 /* Static table for converting non-ascii character sets to ascii.
71 The xascii_table[] is the inverse table. */
73 static int ascii_table[256] = {
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
76 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
77 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
78 ' ', '!', '\'', '#', '$', '%', '&', '\'',
79 '(', ')', '*', '+', ',', '-', '.', '/',
80 '0', '1', '2', '3', '4', '5', '6', '7',
81 '8', '9', ':', ';', '<', '=', '>', '?',
82 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
83 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
84 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
85 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
86 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
87 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
88 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
89 'x', 'y', 'z', '{', '|', '}', '~', '\?'
92 static int xascii_table[256];
95 /* Range checks an expression node. If all goes well, returns the
96 node, otherwise returns &gfc_bad_expr and frees the node. */
99 range_check (gfc_expr * result, const char *name)
102 if (gfc_range_check (result) == ARITH_OK)
105 gfc_error ("Result of %s overflows its kind at %L", name, &result->where);
106 gfc_free_expr (result);
107 return &gfc_bad_expr;
111 /* A helper function that gets an optional and possibly missing
112 kind parameter. Returns the kind, -1 if something went wrong. */
115 get_kind (bt type, gfc_expr * k, const char *name, int default_kind)
122 if (k->expr_type != EXPR_CONSTANT)
124 gfc_error ("KIND parameter of %s at %L must be an initialization "
125 "expression", name, &k->where);
130 if (gfc_extract_int (k, &kind) != NULL
131 || gfc_validate_kind (type, kind, true) < 0)
134 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
142 /********************** Simplification functions *****************************/
145 gfc_simplify_abs (gfc_expr * e)
150 if (e->expr_type != EXPR_CONSTANT)
156 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
158 mpz_abs (result->value.integer, e->value.integer);
160 result = range_check (result, "IABS");
164 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
166 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
168 result = range_check (result, "ABS");
172 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
174 gfc_set_model_kind (e->ts.kind);
177 /* FIXME: Possible numerical problems. */
178 mpfr_mul (a, e->value.complex.r, e->value.complex.r, GFC_RND_MODE);
179 mpfr_mul (b, e->value.complex.i, e->value.complex.i, GFC_RND_MODE);
180 mpfr_add (a, a, b, GFC_RND_MODE);
181 mpfr_sqrt (result->value.real, a, GFC_RND_MODE);
186 result = range_check (result, "CABS");
190 gfc_internal_error ("gfc_simplify_abs(): Bad type");
198 gfc_simplify_achar (gfc_expr * e)
203 if (e->expr_type != EXPR_CONSTANT)
206 /* We cannot assume that the native character set is ASCII in this
208 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
210 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
211 "must be between 0 and 127", &e->where);
212 return &gfc_bad_expr;
215 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
218 result->value.character.string = gfc_getmem (2);
220 result->value.character.length = 1;
221 result->value.character.string[0] = ascii_table[index];
222 result->value.character.string[1] = '\0'; /* For debugger */
228 gfc_simplify_acos (gfc_expr * x)
232 if (x->expr_type != EXPR_CONSTANT)
235 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
237 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
239 return &gfc_bad_expr;
242 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
244 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
246 return range_check (result, "ACOS");
251 gfc_simplify_adjustl (gfc_expr * e)
257 if (e->expr_type != EXPR_CONSTANT)
260 len = e->value.character.length;
262 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
264 result->value.character.length = len;
265 result->value.character.string = gfc_getmem (len + 1);
267 for (count = 0, i = 0; i < len; ++i)
269 ch = e->value.character.string[i];
275 for (i = 0; i < len - count; ++i)
277 result->value.character.string[i] =
278 e->value.character.string[count + i];
281 for (i = len - count; i < len; ++i)
283 result->value.character.string[i] = ' ';
286 result->value.character.string[len] = '\0'; /* For debugger */
293 gfc_simplify_adjustr (gfc_expr * e)
299 if (e->expr_type != EXPR_CONSTANT)
302 len = e->value.character.length;
304 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
306 result->value.character.length = len;
307 result->value.character.string = gfc_getmem (len + 1);
309 for (count = 0, i = len - 1; i >= 0; --i)
311 ch = e->value.character.string[i];
317 for (i = 0; i < count; ++i)
319 result->value.character.string[i] = ' ';
322 for (i = count; i < len; ++i)
324 result->value.character.string[i] =
325 e->value.character.string[i - count];
328 result->value.character.string[len] = '\0'; /* For debugger */
335 gfc_simplify_aimag (gfc_expr * e)
339 if (e->expr_type != EXPR_CONSTANT)
342 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
343 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
345 return range_check (result, "AIMAG");
350 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
352 gfc_expr *rtrunc, *result;
355 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
357 return &gfc_bad_expr;
359 if (e->expr_type != EXPR_CONSTANT)
362 rtrunc = gfc_copy_expr (e);
364 mpfr_trunc (rtrunc->value.real, e->value.real);
366 result = gfc_real2real (rtrunc, kind);
367 gfc_free_expr (rtrunc);
369 return range_check (result, "AINT");
374 gfc_simplify_dint (gfc_expr * e)
376 gfc_expr *rtrunc, *result;
378 if (e->expr_type != EXPR_CONSTANT)
381 rtrunc = gfc_copy_expr (e);
383 mpfr_trunc (rtrunc->value.real, e->value.real);
385 result = gfc_real2real (rtrunc, gfc_default_double_kind ());
386 gfc_free_expr (rtrunc);
388 return range_check (result, "DINT");
394 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
396 gfc_expr *rtrunc, *result;
400 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
402 return &gfc_bad_expr;
404 if (e->expr_type != EXPR_CONSTANT)
407 result = gfc_constant_result (e->ts.type, kind, &e->where);
409 rtrunc = gfc_copy_expr (e);
411 cmp = mpfr_cmp_ui (e->value.real, 0);
413 gfc_set_model_kind (kind);
415 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
419 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
420 mpfr_trunc (result->value.real, rtrunc->value.real);
424 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
425 mpfr_trunc (result->value.real, rtrunc->value.real);
428 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
430 gfc_free_expr (rtrunc);
433 return range_check (result, "ANINT");
438 gfc_simplify_dnint (gfc_expr * e)
440 gfc_expr *rtrunc, *result;
444 if (e->expr_type != EXPR_CONSTANT)
448 gfc_constant_result (BT_REAL, gfc_default_double_kind (), &e->where);
450 rtrunc = gfc_copy_expr (e);
452 cmp = mpfr_cmp_ui (e->value.real, 0);
454 gfc_set_model_kind (gfc_default_double_kind ());
456 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
460 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
461 mpfr_trunc (result->value.real, rtrunc->value.real);
465 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
466 mpfr_trunc (result->value.real, rtrunc->value.real);
469 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
471 gfc_free_expr (rtrunc);
474 return range_check (result, "DNINT");
479 gfc_simplify_asin (gfc_expr * x)
483 if (x->expr_type != EXPR_CONSTANT)
486 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
488 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
490 return &gfc_bad_expr;
493 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
495 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
497 return range_check (result, "ASIN");
502 gfc_simplify_atan (gfc_expr * x)
506 if (x->expr_type != EXPR_CONSTANT)
509 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
511 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
513 return range_check (result, "ATAN");
519 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
523 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
526 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
528 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
531 ("If first argument of ATAN2 %L is zero, then the second argument "
532 "must not be zero", &x->where);
533 gfc_free_expr (result);
534 return &gfc_bad_expr;
537 arctangent2 (y->value.real, x->value.real, result->value.real);
539 return range_check (result, "ATAN2");
545 gfc_simplify_bit_size (gfc_expr * e)
550 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
551 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
552 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
559 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
563 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
566 if (gfc_extract_int (bit, &b) != NULL || b < 0)
567 return gfc_logical_expr (0, &e->where);
569 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
574 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
576 gfc_expr *ceil, *result;
579 kind = get_kind (BT_REAL, k, "CEILING", gfc_default_real_kind ());
581 return &gfc_bad_expr;
583 if (e->expr_type != EXPR_CONSTANT)
586 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
588 ceil = gfc_copy_expr (e);
590 mpfr_ceil (ceil->value.real, e->value.real);
591 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
593 gfc_free_expr (ceil);
595 return range_check (result, "CEILING");
600 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
605 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind ());
607 return &gfc_bad_expr;
609 if (e->expr_type != EXPR_CONSTANT)
612 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > 255)
614 gfc_error ("Bad character in CHAR function at %L", &e->where);
615 return &gfc_bad_expr;
618 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
620 result->value.character.length = 1;
621 result->value.character.string = gfc_getmem (2);
623 result->value.character.string[0] = c;
624 result->value.character.string[1] = '\0'; /* For debugger */
630 /* Common subroutine for simplifying CMPLX and DCMPLX. */
633 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
637 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
639 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
644 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
648 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
652 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
653 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
657 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
665 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
669 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
673 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
677 return range_check (result, name);
682 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
686 if (x->expr_type != EXPR_CONSTANT
687 || (y != NULL && y->expr_type != EXPR_CONSTANT))
690 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind ());
692 return &gfc_bad_expr;
694 return simplify_cmplx ("CMPLX", x, y, kind);
699 gfc_simplify_conjg (gfc_expr * e)
703 if (e->expr_type != EXPR_CONSTANT)
706 result = gfc_copy_expr (e);
707 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
709 return range_check (result, "CONJG");
714 gfc_simplify_cos (gfc_expr * x)
719 if (x->expr_type != EXPR_CONSTANT)
722 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
727 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
730 gfc_set_model_kind (x->ts.kind);
734 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
735 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
736 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
738 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
739 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
740 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
741 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
747 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
750 return range_check (result, "COS");
756 gfc_simplify_cosh (gfc_expr * x)
760 if (x->expr_type != EXPR_CONSTANT)
763 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
765 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
767 return range_check (result, "COSH");
772 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
775 if (x->expr_type != EXPR_CONSTANT
776 || (y != NULL && y->expr_type != EXPR_CONSTANT))
779 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind ());
784 gfc_simplify_dble (gfc_expr * e)
788 if (e->expr_type != EXPR_CONSTANT)
794 result = gfc_int2real (e, gfc_default_double_kind ());
798 result = gfc_real2real (e, gfc_default_double_kind ());
802 result = gfc_complex2real (e, gfc_default_double_kind ());
806 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
809 return range_check (result, "DBLE");
814 gfc_simplify_digits (gfc_expr * x)
818 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
822 digits = gfc_integer_kinds[i].digits;
827 digits = gfc_real_kinds[i].digits;
834 return gfc_int_expr (digits);
839 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
843 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
846 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
851 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
852 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
854 mpz_set_ui (result->value.integer, 0);
859 if (mpfr_cmp (x->value.real, y->value.real) > 0)
860 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
862 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
867 gfc_internal_error ("gfc_simplify_dim(): Bad type");
870 return range_check (result, "DIM");
875 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
877 gfc_expr *a1, *a2, *result;
879 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
883 gfc_constant_result (BT_REAL, gfc_default_double_kind (), &x->where);
885 a1 = gfc_real2real (x, gfc_default_double_kind ());
886 a2 = gfc_real2real (y, gfc_default_double_kind ());
888 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
893 return range_check (result, "DPROD");
898 gfc_simplify_epsilon (gfc_expr * e)
903 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
905 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
907 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
909 return range_check (result, "EPSILON");
914 gfc_simplify_exp (gfc_expr * x)
919 if (x->expr_type != EXPR_CONSTANT)
922 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
927 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
931 gfc_set_model_kind (x->ts.kind);
934 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
935 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
936 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
937 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
938 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
944 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
947 return range_check (result, "EXP");
950 /* FIXME: MPFR should be able to do this better */
952 gfc_simplify_exponent (gfc_expr * x)
954 mpfr_t i2, absv, ln2, lnx, zero;
957 if (x->expr_type != EXPR_CONSTANT)
960 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
963 gfc_set_model (x->value.real);
965 mpfr_set_ui (zero, 0, GFC_RND_MODE);
967 if (mpfr_cmp (x->value.real, zero) == 0)
969 mpz_set_ui (result->value.integer, 0);
979 mpfr_set_ui (i2, 2, GFC_RND_MODE);
981 mpfr_log (ln2, i2, GFC_RND_MODE);
982 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
983 mpfr_log (lnx, absv, GFC_RND_MODE);
985 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
986 mpfr_trunc (lnx, lnx);
987 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
989 gfc_mpfr_to_mpz (result->value.integer, lnx);
997 return range_check (result, "EXPONENT");
1002 gfc_simplify_float (gfc_expr * a)
1006 if (a->expr_type != EXPR_CONSTANT)
1009 result = gfc_int2real (a, gfc_default_real_kind ());
1010 return range_check (result, "FLOAT");
1015 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1021 kind = get_kind (BT_REAL, k, "FLOOR", gfc_default_real_kind ());
1023 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1025 if (e->expr_type != EXPR_CONSTANT)
1028 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1030 gfc_set_model_kind (kind);
1032 mpfr_floor (floor, e->value.real);
1034 gfc_mpfr_to_mpz (result->value.integer, floor);
1038 return range_check (result, "FLOOR");
1043 gfc_simplify_fraction (gfc_expr * x)
1046 mpfr_t i2, absv, ln2, lnx, pow2, zero;
1049 if (x->expr_type != EXPR_CONSTANT)
1052 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1054 gfc_set_model_kind (x->ts.kind);
1056 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1058 if (mpfr_cmp (x->value.real, zero) == 0)
1060 mpfr_set (result->value.real, zero, GFC_RND_MODE);
1071 mpfr_set_ui (i2, 2, GFC_RND_MODE);
1073 mpfr_log (ln2, i2, GFC_RND_MODE);
1074 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1075 mpfr_log (lnx, absv, GFC_RND_MODE);
1077 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
1078 mpfr_trunc (lnx, lnx);
1079 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
1081 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
1082 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
1084 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1093 return range_check (result, "FRACTION");
1098 gfc_simplify_huge (gfc_expr * e)
1103 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1105 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1110 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1114 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1126 gfc_simplify_iachar (gfc_expr * e)
1131 if (e->expr_type != EXPR_CONSTANT)
1134 if (e->value.character.length != 1)
1136 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1137 return &gfc_bad_expr;
1140 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1142 result = gfc_int_expr (index);
1143 result->where = e->where;
1145 return range_check (result, "IACHAR");
1150 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1154 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1157 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1159 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1161 return range_check (result, "IAND");
1166 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1171 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1174 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1176 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1177 return &gfc_bad_expr;
1180 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1182 if (pos > gfc_integer_kinds[k].bit_size)
1184 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1186 return &gfc_bad_expr;
1189 result = gfc_copy_expr (x);
1191 mpz_clrbit (result->value.integer, pos);
1192 return range_check (result, "IBCLR");
1197 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1204 if (x->expr_type != EXPR_CONSTANT
1205 || y->expr_type != EXPR_CONSTANT
1206 || z->expr_type != EXPR_CONSTANT)
1209 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1211 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1212 return &gfc_bad_expr;
1215 if (gfc_extract_int (z, &len) != NULL || len < 0)
1217 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1218 return &gfc_bad_expr;
1221 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1223 bitsize = gfc_integer_kinds[k].bit_size;
1225 if (pos + len > bitsize)
1228 ("Sum of second and third arguments of IBITS exceeds bit size "
1229 "at %L", &y->where);
1230 return &gfc_bad_expr;
1233 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1235 bits = gfc_getmem (bitsize * sizeof (int));
1237 for (i = 0; i < bitsize; i++)
1240 for (i = 0; i < len; i++)
1241 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1243 for (i = 0; i < bitsize; i++)
1247 mpz_clrbit (result->value.integer, i);
1249 else if (bits[i] == 1)
1251 mpz_setbit (result->value.integer, i);
1255 gfc_internal_error ("IBITS: Bad bit");
1261 return range_check (result, "IBITS");
1266 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1271 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1274 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1276 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1277 return &gfc_bad_expr;
1280 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1282 if (pos > gfc_integer_kinds[k].bit_size)
1284 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1286 return &gfc_bad_expr;
1289 result = gfc_copy_expr (x);
1291 mpz_setbit (result->value.integer, pos);
1292 return range_check (result, "IBSET");
1297 gfc_simplify_ichar (gfc_expr * e)
1302 if (e->expr_type != EXPR_CONSTANT)
1305 if (e->value.character.length != 1)
1307 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1308 return &gfc_bad_expr;
1311 index = (int) e->value.character.string[0];
1313 if (index < CHAR_MIN || index > CHAR_MAX)
1315 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1317 return &gfc_bad_expr;
1320 result = gfc_int_expr (index);
1321 result->where = e->where;
1322 return range_check (result, "ICHAR");
1327 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1331 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1334 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1336 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1338 return range_check (result, "IEOR");
1343 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1346 int back, len, lensub;
1347 int i, j, k, count, index = 0, start;
1349 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1352 if (b != NULL && b->value.logical != 0)
1357 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1360 len = x->value.character.length;
1361 lensub = y->value.character.length;
1365 mpz_set_si (result->value.integer, 0);
1374 mpz_set_si (result->value.integer, 1);
1377 else if (lensub == 1)
1379 for (i = 0; i < len; i++)
1381 for (j = 0; j < lensub; j++)
1383 if (y->value.character.string[j] ==
1384 x->value.character.string[i])
1394 for (i = 0; i < len; i++)
1396 for (j = 0; j < lensub; j++)
1398 if (y->value.character.string[j] ==
1399 x->value.character.string[i])
1404 for (k = 0; k < lensub; k++)
1406 if (y->value.character.string[k] ==
1407 x->value.character.string[k + start])
1411 if (count == lensub)
1427 mpz_set_si (result->value.integer, len + 1);
1430 else if (lensub == 1)
1432 for (i = 0; i < len; i++)
1434 for (j = 0; j < lensub; j++)
1436 if (y->value.character.string[j] ==
1437 x->value.character.string[len - i])
1439 index = len - i + 1;
1447 for (i = 0; i < len; i++)
1449 for (j = 0; j < lensub; j++)
1451 if (y->value.character.string[j] ==
1452 x->value.character.string[len - i])
1455 if (start <= len - lensub)
1458 for (k = 0; k < lensub; k++)
1459 if (y->value.character.string[k] ==
1460 x->value.character.string[k + start])
1463 if (count == lensub)
1480 mpz_set_si (result->value.integer, index);
1481 return range_check (result, "INDEX");
1486 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1488 gfc_expr *rpart, *rtrunc, *result;
1491 kind = get_kind (BT_REAL, k, "INT", gfc_default_real_kind ());
1493 return &gfc_bad_expr;
1495 if (e->expr_type != EXPR_CONSTANT)
1498 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1503 mpz_set (result->value.integer, e->value.integer);
1507 rtrunc = gfc_copy_expr (e);
1508 mpfr_trunc (rtrunc->value.real, e->value.real);
1509 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1510 gfc_free_expr (rtrunc);
1514 rpart = gfc_complex2real (e, kind);
1515 rtrunc = gfc_copy_expr (rpart);
1516 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1517 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1518 gfc_free_expr (rpart);
1519 gfc_free_expr (rtrunc);
1523 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1524 gfc_free_expr (result);
1525 return &gfc_bad_expr;
1528 return range_check (result, "INT");
1533 gfc_simplify_ifix (gfc_expr * e)
1535 gfc_expr *rtrunc, *result;
1537 if (e->expr_type != EXPR_CONSTANT)
1540 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1543 rtrunc = gfc_copy_expr (e);
1545 mpfr_trunc (rtrunc->value.real, e->value.real);
1546 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1548 gfc_free_expr (rtrunc);
1549 return range_check (result, "IFIX");
1554 gfc_simplify_idint (gfc_expr * e)
1556 gfc_expr *rtrunc, *result;
1558 if (e->expr_type != EXPR_CONSTANT)
1561 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1564 rtrunc = gfc_copy_expr (e);
1566 mpfr_trunc (rtrunc->value.real, e->value.real);
1567 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1569 gfc_free_expr (rtrunc);
1570 return range_check (result, "IDINT");
1575 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1579 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1582 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1584 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1585 return range_check (result, "IOR");
1590 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1593 int shift, ashift, isize, k;
1596 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1599 if (gfc_extract_int (s, &shift) != NULL)
1601 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1602 return &gfc_bad_expr;
1605 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1607 isize = gfc_integer_kinds[k].bit_size;
1617 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1619 return &gfc_bad_expr;
1622 e_int = mpz_get_si (e->value.integer);
1623 if (e_int > INT_MAX || e_int < INT_MIN)
1624 gfc_internal_error ("ISHFT: unable to extract integer");
1626 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1630 mpz_set (result->value.integer, e->value.integer);
1631 return range_check (result, "ISHFT");
1635 mpz_set_si (result->value.integer, e_int << shift);
1637 mpz_set_si (result->value.integer, e_int >> ashift);
1639 return range_check (result, "ISHFT");
1644 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1647 int shift, ashift, isize, delta, k;
1650 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1653 if (gfc_extract_int (s, &shift) != NULL)
1655 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1656 return &gfc_bad_expr;
1659 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1663 if (gfc_extract_int (sz, &isize) != NULL || isize < 0)
1665 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1666 return &gfc_bad_expr;
1670 isize = gfc_integer_kinds[k].bit_size;
1680 ("Magnitude of second argument of ISHFTC exceeds third argument "
1681 "at %L", &s->where);
1682 return &gfc_bad_expr;
1685 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1687 bits = gfc_getmem (isize * sizeof (int));
1689 for (i = 0; i < isize; i++)
1690 bits[i] = mpz_tstbit (e->value.integer, i);
1692 delta = isize - ashift;
1696 mpz_set (result->value.integer, e->value.integer);
1698 return range_check (result, "ISHFTC");
1703 for (i = 0; i < delta; i++)
1706 mpz_clrbit (result->value.integer, i + shift);
1708 mpz_setbit (result->value.integer, i + shift);
1711 for (i = delta; i < isize; i++)
1714 mpz_clrbit (result->value.integer, i - delta);
1716 mpz_setbit (result->value.integer, i - delta);
1720 return range_check (result, "ISHFTC");
1724 for (i = 0; i < ashift; i++)
1727 mpz_clrbit (result->value.integer, i + delta);
1729 mpz_setbit (result->value.integer, i + delta);
1732 for (i = ashift; i < isize; i++)
1735 mpz_clrbit (result->value.integer, i + shift);
1737 mpz_setbit (result->value.integer, i + shift);
1741 return range_check (result, "ISHFTC");
1747 gfc_simplify_kind (gfc_expr * e)
1750 if (e->ts.type == BT_DERIVED)
1752 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1753 return &gfc_bad_expr;
1756 return gfc_int_expr (e->ts.kind);
1761 gfc_simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1767 if (array->expr_type != EXPR_VARIABLE)
1773 if (dim->expr_type != EXPR_CONSTANT)
1776 /* Follow any component references. */
1777 as = array->symtree->n.sym->as;
1779 while (ref->next != NULL)
1781 if (ref->type == REF_COMPONENT)
1782 as = ref->u.c.sym->as;
1786 if (ref->type != REF_ARRAY || ref->u.ar.type != AR_FULL)
1789 i = mpz_get_si (dim->value.integer);
1791 return gfc_copy_expr (as->upper[i-1]);
1793 return gfc_copy_expr (as->lower[i-1]);
1798 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
1800 return gfc_simplify_bound (array, dim, 0);
1805 gfc_simplify_len (gfc_expr * e)
1809 if (e->expr_type != EXPR_CONSTANT)
1812 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1815 mpz_set_si (result->value.integer, e->value.character.length);
1816 return range_check (result, "LEN");
1821 gfc_simplify_len_trim (gfc_expr * e)
1824 int count, len, lentrim, i;
1826 if (e->expr_type != EXPR_CONSTANT)
1829 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1832 len = e->value.character.length;
1834 for (count = 0, i = 1; i <= len; i++)
1835 if (e->value.character.string[len - i] == ' ')
1840 lentrim = len - count;
1842 mpz_set_si (result->value.integer, lentrim);
1843 return range_check (result, "LEN_TRIM");
1848 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
1851 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1854 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
1860 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
1863 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1866 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
1872 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
1875 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1878 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
1884 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
1887 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1890 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
1896 gfc_simplify_log (gfc_expr * x)
1899 mpfr_t xr, xi, zero;
1901 if (x->expr_type != EXPR_CONSTANT)
1904 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1906 gfc_set_model_kind (x->ts.kind);
1908 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1913 if (mpfr_cmp (x->value.real, zero) <= 0)
1916 ("Argument of LOG at %L cannot be less than or equal to zero",
1918 gfc_free_expr (result);
1920 return &gfc_bad_expr;
1923 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
1928 if ((mpfr_cmp (x->value.complex.r, zero) == 0)
1929 && (mpfr_cmp (x->value.complex.i, zero) == 0))
1931 gfc_error ("Complex argument of LOG at %L cannot be zero",
1933 gfc_free_expr (result);
1935 return &gfc_bad_expr;
1941 arctangent2 (x->value.complex.i, x->value.complex.r,
1942 result->value.complex.i);
1944 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1945 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1946 mpfr_add (xr, xr, xi, GFC_RND_MODE);
1947 mpfr_sqrt (xr, xr, GFC_RND_MODE);
1948 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
1957 gfc_internal_error ("gfc_simplify_log: bad type");
1960 return range_check (result, "LOG");
1965 gfc_simplify_log10 (gfc_expr * x)
1970 if (x->expr_type != EXPR_CONSTANT)
1973 gfc_set_model_kind (x->ts.kind);
1975 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1977 if (mpfr_cmp (x->value.real, zero) <= 0)
1980 ("Argument of LOG10 at %L cannot be less than or equal to zero",
1983 return &gfc_bad_expr;
1986 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1988 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
1991 return range_check (result, "LOG10");
1996 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
2001 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind ());
2003 return &gfc_bad_expr;
2005 if (e->expr_type != EXPR_CONSTANT)
2008 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2010 result->value.logical = e->value.logical;
2016 /* This function is special since MAX() can take any number of
2017 arguments. The simplified expression is a rewritten version of the
2018 argument list containing at most one constant element. Other
2019 constant elements are deleted. Because the argument list has
2020 already been checked, this function always succeeds. sign is 1 for
2021 MAX(), -1 for MIN(). */
2024 simplify_min_max (gfc_expr * expr, int sign)
2026 gfc_actual_arglist *arg, *last, *extremum;
2027 gfc_intrinsic_sym * specific;
2031 specific = expr->value.function.isym;
2033 arg = expr->value.function.actual;
2035 for (; arg; last = arg, arg = arg->next)
2037 if (arg->expr->expr_type != EXPR_CONSTANT)
2040 if (extremum == NULL)
2046 switch (arg->expr->ts.type)
2049 if (mpz_cmp (arg->expr->value.integer,
2050 extremum->expr->value.integer) * sign > 0)
2051 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2056 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2058 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2064 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2067 /* Delete the extra constant argument. */
2069 expr->value.function.actual = arg->next;
2071 last->next = arg->next;
2074 gfc_free_actual_arglist (arg);
2078 /* If there is one value left, replace the function call with the
2080 if (expr->value.function.actual->next != NULL)
2083 /* Convert to the correct type and kind. */
2084 if (expr->ts.type != BT_UNKNOWN)
2085 return gfc_convert_constant (expr->value.function.actual->expr,
2086 expr->ts.type, expr->ts.kind);
2088 if (specific->ts.type != BT_UNKNOWN)
2089 return gfc_convert_constant (expr->value.function.actual->expr,
2090 specific->ts.type, specific->ts.kind);
2092 return gfc_copy_expr (expr->value.function.actual->expr);
2097 gfc_simplify_min (gfc_expr * e)
2100 return simplify_min_max (e, -1);
2105 gfc_simplify_max (gfc_expr * e)
2108 return simplify_min_max (e, 1);
2113 gfc_simplify_maxexponent (gfc_expr * x)
2118 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2120 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2121 result->where = x->where;
2128 gfc_simplify_minexponent (gfc_expr * x)
2133 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2135 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2136 result->where = x->where;
2143 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2146 mpfr_t quot, iquot, term;
2148 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2151 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2156 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2158 /* Result is processor-dependent. */
2159 gfc_error ("Second argument MOD at %L is zero", &a->where);
2160 gfc_free_expr (result);
2161 return &gfc_bad_expr;
2163 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2167 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2169 /* Result is processor-dependent. */
2170 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2171 gfc_free_expr (result);
2172 return &gfc_bad_expr;
2175 gfc_set_model_kind (a->ts.kind);
2180 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2181 mpfr_trunc (iquot, quot);
2182 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2183 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2191 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2194 return range_check (result, "MOD");
2199 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2202 mpfr_t quot, iquot, term;
2204 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2207 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2212 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2214 /* Result is processor-dependent. This processor just opts
2215 to not handle it at all. */
2216 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2217 gfc_free_expr (result);
2218 return &gfc_bad_expr;
2220 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2225 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2227 /* Result is processor-dependent. */
2228 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2229 gfc_free_expr (result);
2230 return &gfc_bad_expr;
2233 gfc_set_model_kind (a->ts.kind);
2238 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2239 mpfr_floor (iquot, quot);
2240 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2246 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2250 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2253 return range_check (result, "MODULO");
2257 /* Exists for the sole purpose of consistency with other intrinsics. */
2259 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2260 gfc_expr * fp ATTRIBUTE_UNUSED,
2261 gfc_expr * l ATTRIBUTE_UNUSED,
2262 gfc_expr * to ATTRIBUTE_UNUSED,
2263 gfc_expr * tp ATTRIBUTE_UNUSED)
2270 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2275 int p, i, k, match_float;
2277 /* FIXME: This implementation is dopey and probably not quite right,
2278 but it's a start. */
2280 if (x->expr_type != EXPR_CONSTANT)
2283 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2285 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2287 val = mpfr_get_d (x->value.real, GFC_RND_MODE);
2288 p = gfc_real_kinds[k].digits;
2291 for (i = 1; i < p; ++i)
2296 /* TODO we should make sure that 'float' matches kind 4 */
2297 match_float = gfc_real_kinds[k].kind == 4;
2298 if (mpfr_cmp_ui (s->value.real, 0) > 0)
2304 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2309 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2312 else if (mpfr_cmp_ui (s->value.real, 0) < 0)
2318 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2323 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2328 gfc_error ("Invalid second argument of NEAREST at %L", &s->where);
2330 return &gfc_bad_expr;
2333 return range_check (result, "NEAREST");
2339 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2341 gfc_expr *rtrunc, *itrunc, *result;
2345 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind ());
2347 return &gfc_bad_expr;
2349 if (e->expr_type != EXPR_CONSTANT)
2352 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2354 rtrunc = gfc_copy_expr (e);
2355 itrunc = gfc_copy_expr (e);
2357 cmp = mpfr_cmp_ui (e->value.real, 0);
2359 gfc_set_model (e->value.real);
2361 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
2365 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2366 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2370 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2371 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2374 mpfr_set_ui (itrunc->value.real, 0, GFC_RND_MODE);
2376 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2378 gfc_free_expr (itrunc);
2379 gfc_free_expr (rtrunc);
2382 return range_check (result, name);
2387 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2390 return simplify_nint ("NINT", e, k);
2395 gfc_simplify_idnint (gfc_expr * e)
2398 return simplify_nint ("IDNINT", e, NULL);
2403 gfc_simplify_not (gfc_expr * e)
2408 if (e->expr_type != EXPR_CONSTANT)
2411 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2413 mpz_com (result->value.integer, e->value.integer);
2415 /* Because of how GMP handles numbers, the result must be ANDed with
2416 the max_int mask. For radices <> 2, this will require change. */
2418 i = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2420 mpz_and (result->value.integer, result->value.integer,
2421 gfc_integer_kinds[i].max_int);
2423 return range_check (result, "NOT");
2428 gfc_simplify_null (gfc_expr * mold)
2432 result = gfc_get_expr ();
2433 result->expr_type = EXPR_NULL;
2436 result->ts.type = BT_UNKNOWN;
2439 result->ts = mold->ts;
2440 result->where = mold->where;
2448 gfc_simplify_precision (gfc_expr * e)
2453 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2455 result = gfc_int_expr (gfc_real_kinds[i].precision);
2456 result->where = e->where;
2463 gfc_simplify_radix (gfc_expr * e)
2468 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2472 i = gfc_integer_kinds[i].radix;
2476 i = gfc_real_kinds[i].radix;
2483 result = gfc_int_expr (i);
2484 result->where = e->where;
2491 gfc_simplify_range (gfc_expr * e)
2497 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2502 j = gfc_integer_kinds[i].range;
2507 j = gfc_real_kinds[i].range;
2514 result = gfc_int_expr (j);
2515 result->where = e->where;
2522 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2527 if (e->ts.type == BT_COMPLEX)
2528 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2530 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind ());
2533 return &gfc_bad_expr;
2535 if (e->expr_type != EXPR_CONSTANT)
2541 result = gfc_int2real (e, kind);
2545 result = gfc_real2real (e, kind);
2549 result = gfc_complex2real (e, kind);
2553 gfc_internal_error ("bad type in REAL");
2557 return range_check (result, "REAL");
2561 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2564 int i, j, len, ncopies, nlen;
2566 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2569 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2571 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2572 return &gfc_bad_expr;
2575 len = e->value.character.length;
2576 nlen = ncopies * len;
2578 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2582 result->value.character.string = gfc_getmem (1);
2583 result->value.character.length = 0;
2584 result->value.character.string[0] = '\0';
2588 result->value.character.length = nlen;
2589 result->value.character.string = gfc_getmem (nlen + 1);
2591 for (i = 0; i < ncopies; i++)
2592 for (j = 0; j < len; j++)
2593 result->value.character.string[j + i * len] =
2594 e->value.character.string[j];
2596 result->value.character.string[nlen] = '\0'; /* For debugger */
2601 /* This one is a bear, but mainly has to do with shuffling elements. */
2604 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2605 gfc_expr * pad, gfc_expr * order_exp)
2608 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2609 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2610 gfc_constructor *head, *tail;
2616 /* Unpack the shape array. */
2617 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2620 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2624 && (pad->expr_type != EXPR_ARRAY
2625 || !gfc_is_constant_expr (pad)))
2628 if (order_exp != NULL
2629 && (order_exp->expr_type != EXPR_ARRAY
2630 || !gfc_is_constant_expr (order_exp)))
2639 e = gfc_get_array_element (shape_exp, rank);
2643 if (gfc_extract_int (e, &shape[rank]) != NULL)
2645 gfc_error ("Integer too large in shape specification at %L",
2653 if (rank >= GFC_MAX_DIMENSIONS)
2655 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2656 "at %L", &e->where);
2661 if (shape[rank] < 0)
2663 gfc_error ("Shape specification at %L cannot be negative",
2673 gfc_error ("Shape specification at %L cannot be the null array",
2678 /* Now unpack the order array if present. */
2679 if (order_exp == NULL)
2681 for (i = 0; i < rank; i++)
2688 for (i = 0; i < rank; i++)
2691 for (i = 0; i < rank; i++)
2693 e = gfc_get_array_element (order_exp, i);
2697 ("ORDER parameter of RESHAPE at %L is not the same size "
2698 "as SHAPE parameter", &order_exp->where);
2702 if (gfc_extract_int (e, &order[i]) != NULL)
2704 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2712 if (order[i] < 1 || order[i] > rank)
2714 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2723 gfc_error ("Invalid permutation in ORDER parameter at %L",
2732 /* Count the elements in the source and padding arrays. */
2737 gfc_array_size (pad, &size);
2738 npad = mpz_get_ui (size);
2742 gfc_array_size (source, &size);
2743 nsource = mpz_get_ui (size);
2746 /* If it weren't for that pesky permutation we could just loop
2747 through the source and round out any shortage with pad elements.
2748 But no, someone just had to have the compiler do something the
2749 user should be doing. */
2751 for (i = 0; i < rank; i++)
2756 /* Figure out which element to extract. */
2757 mpz_set_ui (index, 0);
2759 for (i = rank - 1; i >= 0; i--)
2761 mpz_add_ui (index, index, x[order[i]]);
2763 mpz_mul_ui (index, index, shape[order[i - 1]]);
2766 if (mpz_cmp_ui (index, INT_MAX) > 0)
2767 gfc_internal_error ("Reshaped array too large at %L", &e->where);
2769 j = mpz_get_ui (index);
2772 e = gfc_get_array_element (source, j);
2780 ("PAD parameter required for short SOURCE parameter at %L",
2786 e = gfc_get_array_element (pad, j);
2790 head = tail = gfc_get_constructor ();
2793 tail->next = gfc_get_constructor ();
2800 tail->where = e->where;
2803 /* Calculate the next element. */
2807 if (++x[i] < shape[i])
2818 e = gfc_get_expr ();
2819 e->where = source->where;
2820 e->expr_type = EXPR_ARRAY;
2821 e->value.constructor = head;
2822 e->shape = gfc_get_shape (rank);
2824 for (i = 0; i < rank; i++)
2825 mpz_init_set_ui (e->shape[i], shape[order[i]]);
2827 e->ts = head->expr->ts;
2833 gfc_free_constructor (head);
2835 return &gfc_bad_expr;
2840 gfc_simplify_rrspacing (gfc_expr * x)
2843 mpfr_t i2, absv, ln2, lnx, frac, pow2, zero;
2847 if (x->expr_type != EXPR_CONSTANT)
2850 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2852 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2854 p = gfc_real_kinds[i].digits;
2856 gfc_set_model_kind (x->ts.kind);
2858 mpfr_set_ui (zero, 0, GFC_RND_MODE);
2860 if (mpfr_cmp (x->value.real, zero) == 0)
2862 mpfr_ui_div (result->value.real, 1, gfc_real_kinds[i].tiny, GFC_RND_MODE);
2874 mpfr_set_ui (i2, 2, GFC_RND_MODE);
2876 mpfr_log (ln2, i2, GFC_RND_MODE);
2877 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2878 mpfr_log (lnx, absv, GFC_RND_MODE);
2880 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
2881 mpfr_trunc (lnx, lnx);
2882 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
2884 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
2885 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
2886 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
2888 exp2 = (unsigned long) p;
2889 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
2899 return range_check (result, "RRSPACING");
2904 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
2906 int k, neg_flag, power, exp_range;
2907 mpfr_t scale, radix;
2910 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
2913 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2915 if (mpfr_sgn (x->value.real) == 0)
2917 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2921 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2923 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
2925 /* This check filters out values of i that would overflow an int. */
2926 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
2927 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
2929 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
2930 return &gfc_bad_expr;
2933 /* Compute scale = radix ** power. */
2934 power = mpz_get_si (i->value.integer);
2944 gfc_set_model_kind (x->ts.kind);
2947 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
2948 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
2951 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
2953 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
2958 return range_check (result, "SCALE");
2963 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
2968 size_t indx, len, lenc;
2970 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
2973 if (b != NULL && b->value.logical != 0)
2978 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
2981 len = e->value.character.length;
2982 lenc = c->value.character.length;
2984 if (len == 0 || lenc == 0)
2993 strcspn (e->value.character.string, c->value.character.string) + 1;
3000 for (indx = len; indx > 0; indx--)
3002 for (i = 0; i < lenc; i++)
3004 if (c->value.character.string[i]
3005 == e->value.character.string[indx - 1])
3013 mpz_set_ui (result->value.integer, indx);
3014 return range_check (result, "SCAN");
3019 gfc_simplify_selected_int_kind (gfc_expr * e)
3024 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3029 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3030 if (gfc_integer_kinds[i].range >= range
3031 && gfc_integer_kinds[i].kind < kind)
3032 kind = gfc_integer_kinds[i].kind;
3034 if (kind == INT_MAX)
3037 result = gfc_int_expr (kind);
3038 result->where = e->where;
3045 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
3047 int range, precision, i, kind, found_precision, found_range;
3054 if (p->expr_type != EXPR_CONSTANT
3055 || gfc_extract_int (p, &precision) != NULL)
3063 if (q->expr_type != EXPR_CONSTANT
3064 || gfc_extract_int (q, &range) != NULL)
3069 found_precision = 0;
3072 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3074 if (gfc_real_kinds[i].precision >= precision)
3075 found_precision = 1;
3077 if (gfc_real_kinds[i].range >= range)
3080 if (gfc_real_kinds[i].precision >= precision
3081 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3082 kind = gfc_real_kinds[i].kind;
3085 if (kind == INT_MAX)
3089 if (!found_precision)
3095 result = gfc_int_expr (kind);
3096 result->where = (p != NULL) ? p->where : q->where;
3103 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3106 mpfr_t i2, ln2, absv, lnx, pow2, frac, zero;
3109 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3112 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3114 gfc_set_model_kind (x->ts.kind);
3116 mpfr_set_ui (zero, 0, GFC_RND_MODE);
3118 if (mpfr_cmp (x->value.real, zero) == 0)
3120 mpfr_set (result->value.real, zero, GFC_RND_MODE);
3132 mpfr_set_ui (i2, 2, GFC_RND_MODE);
3133 mpfr_log (ln2, i2, GFC_RND_MODE);
3135 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3136 mpfr_log (lnx, absv, GFC_RND_MODE);
3138 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
3139 mpfr_trunc (lnx, lnx);
3140 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
3142 /* Old exponent value, and fraction. */
3143 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
3144 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
3146 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3149 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3150 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3160 return range_check (result, "SET_EXPONENT");
3165 gfc_simplify_shape (gfc_expr * source)
3167 mpz_t shape[GFC_MAX_DIMENSIONS];
3168 gfc_expr *result, *e, *f;
3173 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3176 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind (),
3179 ar = gfc_find_array_ref (source);
3181 t = gfc_array_ref_shape (ar, shape);
3183 for (n = 0; n < source->rank; n++)
3185 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3190 mpz_set (e->value.integer, shape[n]);
3191 mpz_clear (shape[n]);
3195 mpz_set_ui (e->value.integer, n + 1);
3197 f = gfc_simplify_size (source, e);
3201 gfc_free_expr (result);
3210 gfc_append_constructor (result, e);
3218 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3226 if (gfc_array_size (array, &size) == FAILURE)
3231 if (dim->expr_type != EXPR_CONSTANT)
3234 d = mpz_get_ui (dim->value.integer) - 1;
3235 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3239 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3242 mpz_set (result->value.integer, size);
3249 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3253 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3256 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3261 mpz_abs (result->value.integer, x->value.integer);
3262 if (mpz_sgn (y->value.integer) < 0)
3263 mpz_neg (result->value.integer, result->value.integer);
3268 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3270 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3271 if (mpfr_sgn (y->value.real) < 0)
3272 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3277 gfc_internal_error ("Bad type in gfc_simplify_sign");
3285 gfc_simplify_sin (gfc_expr * x)
3290 if (x->expr_type != EXPR_CONSTANT)
3293 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3298 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3302 gfc_set_model (x->value.real);
3306 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3307 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3308 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3310 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3311 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3312 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3319 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3322 return range_check (result, "SIN");
3327 gfc_simplify_sinh (gfc_expr * x)
3331 if (x->expr_type != EXPR_CONSTANT)
3334 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3336 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3338 return range_check (result, "SINH");
3342 /* The argument is always a double precision real that is converted to
3343 single precision. TODO: Rounding! */
3346 gfc_simplify_sngl (gfc_expr * a)
3350 if (a->expr_type != EXPR_CONSTANT)
3353 result = gfc_real2real (a, gfc_default_real_kind ());
3354 return range_check (result, "SNGL");
3359 gfc_simplify_spacing (gfc_expr * x)
3362 mpfr_t i1, i2, ln2, absv, lnx, zero;
3367 if (x->expr_type != EXPR_CONSTANT)
3370 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3372 p = gfc_real_kinds[i].digits;
3374 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3376 gfc_set_model_kind (x->ts.kind);
3378 mpfr_set_ui (zero, 0, GFC_RND_MODE);
3380 if (mpfr_cmp (x->value.real, zero) == 0)
3382 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3393 mpfr_set_ui (i1, 1, GFC_RND_MODE);
3394 mpfr_set_ui (i2, 2, GFC_RND_MODE);
3396 mpfr_log (ln2, i2, GFC_RND_MODE);
3397 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3398 mpfr_log (lnx, absv, GFC_RND_MODE);
3400 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
3401 mpfr_trunc (lnx, lnx);
3402 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
3404 diff = (long) mpfr_get_d (lnx, GFC_RND_MODE) - (long) p;
3407 exp2 = (unsigned) diff;
3408 mpfr_mul_2exp (result->value.real, i1, exp2, GFC_RND_MODE);
3413 exp2 = (unsigned) diff;
3414 mpfr_div_2exp (result->value.real, i1, exp2, GFC_RND_MODE);
3424 if (mpfr_cmp (result->value.real, gfc_real_kinds[i].tiny) < 0)
3425 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3427 return range_check (result, "SPACING");
3432 gfc_simplify_sqrt (gfc_expr * e)
3435 mpfr_t ac, ad, s, t, w;
3437 if (e->expr_type != EXPR_CONSTANT)
3440 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3445 if (mpfr_cmp_si (e->value.real, 0) < 0)
3447 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3452 /* Formula taken from Numerical Recipes to avoid over- and
3455 gfc_set_model (e->value.real);
3462 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3463 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3466 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3467 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3471 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3472 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3474 if (mpfr_cmp (ac, ad) >= 0)
3476 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3477 mpfr_mul (t, t, t, GFC_RND_MODE);
3478 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3479 mpfr_sqrt (t, t, GFC_RND_MODE);
3480 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3481 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3482 mpfr_sqrt (t, t, GFC_RND_MODE);
3483 mpfr_sqrt (s, ac, GFC_RND_MODE);
3484 mpfr_mul (w, s, t, GFC_RND_MODE);
3488 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3489 mpfr_mul (t, s, s, GFC_RND_MODE);
3490 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3491 mpfr_sqrt (t, t, GFC_RND_MODE);
3492 mpfr_abs (s, s, GFC_RND_MODE);
3493 mpfr_add (t, t, s, GFC_RND_MODE);
3494 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3495 mpfr_sqrt (t, t, GFC_RND_MODE);
3496 mpfr_sqrt (s, ad, GFC_RND_MODE);
3497 mpfr_mul (w, s, t, GFC_RND_MODE);
3500 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3502 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3503 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3504 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3506 else if (mpfr_cmp_ui (w, 0) != 0
3507 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3508 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3510 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3511 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3512 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3514 else if (mpfr_cmp_ui (w, 0) != 0
3515 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3516 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3518 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3519 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3520 mpfr_neg (w, w, GFC_RND_MODE);
3521 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3524 gfc_internal_error ("invalid complex argument of SQRT at %L",
3536 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3539 return range_check (result, "SQRT");
3542 gfc_free_expr (result);
3543 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3544 return &gfc_bad_expr;
3549 gfc_simplify_tan (gfc_expr * x)
3554 if (x->expr_type != EXPR_CONSTANT)
3557 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3559 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3561 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3563 return range_check (result, "TAN");
3568 gfc_simplify_tanh (gfc_expr * x)
3572 if (x->expr_type != EXPR_CONSTANT)
3575 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3577 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3579 return range_check (result, "TANH");
3585 gfc_simplify_tiny (gfc_expr * e)
3590 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3592 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3593 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3600 gfc_simplify_trim (gfc_expr * e)
3603 int count, i, len, lentrim;
3605 if (e->expr_type != EXPR_CONSTANT)
3608 len = e->value.character.length;
3610 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3612 for (count = 0, i = 1; i <= len; ++i)
3614 if (e->value.character.string[len - i] == ' ')
3620 lentrim = len - count;
3622 result->value.character.length = lentrim;
3623 result->value.character.string = gfc_getmem (lentrim + 1);
3625 for (i = 0; i < lentrim; i++)
3626 result->value.character.string[i] = e->value.character.string[i];
3628 result->value.character.string[lentrim] = '\0'; /* For debugger */
3635 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3637 return gfc_simplify_bound (array, dim, 1);
3642 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3646 size_t index, len, lenset;
3649 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3652 if (b != NULL && b->value.logical != 0)
3657 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3660 len = s->value.character.length;
3661 lenset = set->value.character.length;
3665 mpz_set_ui (result->value.integer, 0);
3673 mpz_set_ui (result->value.integer, len);
3678 strspn (s->value.character.string, set->value.character.string) + 1;
3687 mpz_set_ui (result->value.integer, 1);
3690 for (index = len; index > 0; index --)
3692 for (i = 0; i < lenset; i++)
3694 if (s->value.character.string[index - 1]
3695 == set->value.character.string[i])
3703 mpz_set_ui (result->value.integer, index);
3707 /****************** Constant simplification *****************/
3709 /* Master function to convert one constant to another. While this is
3710 used as a simplification function, it requires the destination type
3711 and kind information which is supplied by a special case in
3715 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3717 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3718 gfc_constructor *head, *c, *tail = NULL;
3732 f = gfc_int2complex;
3749 f = gfc_real2complex;
3760 f = gfc_complex2int;
3763 f = gfc_complex2real;
3766 f = gfc_complex2complex;
3775 if (type != BT_LOGICAL)
3782 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3787 switch (e->expr_type)
3790 result = f (e, kind);
3792 return &gfc_bad_expr;
3796 if (!gfc_is_constant_expr (e))
3801 for (c = e->value.constructor; c; c = c->next)
3804 head = tail = gfc_get_constructor ();
3807 tail->next = gfc_get_constructor ();
3811 tail->where = c->where;
3813 if (c->iterator == NULL)
3814 tail->expr = f (c->expr, kind);
3817 g = gfc_convert_constant (c->expr, type, kind);
3818 if (g == &gfc_bad_expr)
3823 if (tail->expr == NULL)
3825 gfc_free_constructor (head);
3830 result = gfc_get_expr ();
3831 result->ts.type = type;
3832 result->ts.kind = kind;
3833 result->expr_type = EXPR_ARRAY;
3834 result->value.constructor = head;
3835 result->shape = gfc_copy_shape (e->shape, e->rank);
3836 result->where = e->where;
3837 result->rank = e->rank;
3848 /****************** Helper functions ***********************/
3850 /* Given a collating table, create the inverse table. */
3853 invert_table (const int *table, int *xtable)
3857 for (i = 0; i < 256; i++)
3860 for (i = 0; i < 256; i++)
3861 xtable[table[i]] = i;
3866 gfc_simplify_init_1 (void)
3869 invert_table (ascii_table, xascii_table);