1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
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, 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "intrinsic.h"
30 gfc_expr gfc_bad_expr;
33 /* Note that 'simplification' is not just transforming expressions.
34 For functions that are not simplified at compile time, range
35 checking is done if possible.
37 The return convention is that each simplification function returns:
39 A new expression node corresponding to the simplified arguments.
40 The original arguments are destroyed by the caller, and must not
41 be a part of the new expression.
43 NULL pointer indicating that no simplification was possible and
44 the original expression should remain intact. If the
45 simplification function sets the type and/or the function name
46 via the pointer gfc_simple_expression, then this type is
49 An expression pointer to gfc_bad_expr (a static placeholder)
50 indicating that some error has prevented simplification. For
51 example, sqrt(-1.0). The error is generated within the function
52 and should be propagated upwards
54 By the time a simplification function gets control, it has been
55 decided that the function call is really supposed to be the
56 intrinsic. No type checking is strictly necessary, since only
57 valid types will be passed on. On the other hand, a simplification
58 subroutine may have to look at the type of an argument as part of
61 Array arguments are never passed to these subroutines.
63 The functions in this file don't have much comment with them, but
64 everything is reasonably straight-forward. The Standard, chapter 13
65 is the best comment you'll find for this file anyway. */
67 /* Static table for converting non-ascii character sets to ascii.
68 The xascii_table[] is the inverse table. */
70 static int ascii_table[256] = {
71 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
72 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
73 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 ' ', '!', '"', '#', '$', '%', '&', '\'',
76 '(', ')', '*', '+', ',', '-', '.', '/',
77 '0', '1', '2', '3', '4', '5', '6', '7',
78 '8', '9', ':', ';', '<', '=', '>', '?',
79 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
80 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
81 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
82 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
83 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
84 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
85 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
86 'x', 'y', 'z', '{', '|', '}', '~', '\?'
89 static int xascii_table[256];
92 /* Range checks an expression node. If all goes well, returns the
93 node, otherwise returns &gfc_bad_expr and frees the node. */
96 range_check (gfc_expr * result, const char *name)
99 switch (gfc_range_check (result))
105 gfc_error ("Result of %s overflows its kind at %L", name, &result->where);
108 case ARITH_UNDERFLOW:
109 gfc_error ("Result of %s underflows its kind at %L", name, &result->where);
113 gfc_error ("Result of %s is NaN at %L", name, &result->where);
117 gfc_error ("Result of %s gives range error for its kind at %L", name, &result->where);
121 gfc_free_expr (result);
122 return &gfc_bad_expr;
126 /* A helper function that gets an optional and possibly missing
127 kind parameter. Returns the kind, -1 if something went wrong. */
130 get_kind (bt type, gfc_expr * k, const char *name, int default_kind)
137 if (k->expr_type != EXPR_CONSTANT)
139 gfc_error ("KIND parameter of %s at %L must be an initialization "
140 "expression", name, &k->where);
145 if (gfc_extract_int (k, &kind) != NULL
146 || gfc_validate_kind (type, kind, true) < 0)
149 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
157 /* Checks if X, which is assumed to represent a two's complement
158 integer of binary width BITSIZE, has the signbit set. If so, makes
159 X the corresponding negative number. */
162 twos_complement (mpz_t x, int bitsize)
166 if (mpz_tstbit (x, bitsize - 1) == 1)
168 mpz_init_set_ui(mask, 1);
169 mpz_mul_2exp(mask, mask, bitsize);
170 mpz_sub_ui(mask, mask, 1);
172 /* We negate the number by hand, zeroing the high bits, that is
173 make it the corresponding positive number, and then have it
174 negated by GMP, giving the correct representation of the
177 mpz_add_ui (x, x, 1);
178 mpz_and (x, x, mask);
187 /********************** Simplification functions *****************************/
190 gfc_simplify_abs (gfc_expr * e)
194 if (e->expr_type != EXPR_CONSTANT)
200 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
202 mpz_abs (result->value.integer, e->value.integer);
204 result = range_check (result, "IABS");
208 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
210 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
212 result = range_check (result, "ABS");
216 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
218 gfc_set_model_kind (e->ts.kind);
220 mpfr_hypot (result->value.real, e->value.complex.r,
221 e->value.complex.i, GFC_RND_MODE);
222 result = range_check (result, "CABS");
226 gfc_internal_error ("gfc_simplify_abs(): Bad type");
234 gfc_simplify_achar (gfc_expr * e)
239 if (e->expr_type != EXPR_CONSTANT)
242 /* We cannot assume that the native character set is ASCII in this
244 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
246 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
247 "must be between 0 and 127", &e->where);
248 return &gfc_bad_expr;
251 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
254 result->value.character.string = gfc_getmem (2);
256 result->value.character.length = 1;
257 result->value.character.string[0] = ascii_table[index];
258 result->value.character.string[1] = '\0'; /* For debugger */
264 gfc_simplify_acos (gfc_expr * x)
268 if (x->expr_type != EXPR_CONSTANT)
271 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
273 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
275 return &gfc_bad_expr;
278 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
280 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
282 return range_check (result, "ACOS");
286 gfc_simplify_acosh (gfc_expr * x)
290 if (x->expr_type != EXPR_CONSTANT)
293 if (mpfr_cmp_si (x->value.real, 1) < 0)
295 gfc_error ("Argument of ACOSH at %L must not be less than 1",
297 return &gfc_bad_expr;
300 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
302 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
304 return range_check (result, "ACOSH");
308 gfc_simplify_adjustl (gfc_expr * e)
314 if (e->expr_type != EXPR_CONSTANT)
317 len = e->value.character.length;
319 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
321 result->value.character.length = len;
322 result->value.character.string = gfc_getmem (len + 1);
324 for (count = 0, i = 0; i < len; ++i)
326 ch = e->value.character.string[i];
332 for (i = 0; i < len - count; ++i)
334 result->value.character.string[i] =
335 e->value.character.string[count + i];
338 for (i = len - count; i < len; ++i)
340 result->value.character.string[i] = ' ';
343 result->value.character.string[len] = '\0'; /* For debugger */
350 gfc_simplify_adjustr (gfc_expr * e)
356 if (e->expr_type != EXPR_CONSTANT)
359 len = e->value.character.length;
361 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
363 result->value.character.length = len;
364 result->value.character.string = gfc_getmem (len + 1);
366 for (count = 0, i = len - 1; i >= 0; --i)
368 ch = e->value.character.string[i];
374 for (i = 0; i < count; ++i)
376 result->value.character.string[i] = ' ';
379 for (i = count; i < len; ++i)
381 result->value.character.string[i] =
382 e->value.character.string[i - count];
385 result->value.character.string[len] = '\0'; /* For debugger */
392 gfc_simplify_aimag (gfc_expr * e)
397 if (e->expr_type != EXPR_CONSTANT)
400 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
401 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
403 return range_check (result, "AIMAG");
408 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
410 gfc_expr *rtrunc, *result;
413 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
415 return &gfc_bad_expr;
417 if (e->expr_type != EXPR_CONSTANT)
420 rtrunc = gfc_copy_expr (e);
422 mpfr_trunc (rtrunc->value.real, e->value.real);
424 result = gfc_real2real (rtrunc, kind);
425 gfc_free_expr (rtrunc);
427 return range_check (result, "AINT");
432 gfc_simplify_dint (gfc_expr * e)
434 gfc_expr *rtrunc, *result;
436 if (e->expr_type != EXPR_CONSTANT)
439 rtrunc = gfc_copy_expr (e);
441 mpfr_trunc (rtrunc->value.real, e->value.real);
443 result = gfc_real2real (rtrunc, gfc_default_double_kind);
444 gfc_free_expr (rtrunc);
446 return range_check (result, "DINT");
451 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
456 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
458 return &gfc_bad_expr;
460 if (e->expr_type != EXPR_CONSTANT)
463 result = gfc_constant_result (e->ts.type, kind, &e->where);
465 mpfr_round (result->value.real, e->value.real);
467 return range_check (result, "ANINT");
472 gfc_simplify_and (gfc_expr * x, gfc_expr * y)
477 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
480 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
481 if (x->ts.type == BT_INTEGER)
483 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
484 mpz_and (result->value.integer, x->value.integer, y->value.integer);
486 else /* BT_LOGICAL */
488 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
489 result->value.logical = x->value.logical && y->value.logical;
492 return range_check (result, "AND");
497 gfc_simplify_dnint (gfc_expr * e)
501 if (e->expr_type != EXPR_CONSTANT)
504 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
506 mpfr_round (result->value.real, e->value.real);
508 return range_check (result, "DNINT");
513 gfc_simplify_asin (gfc_expr * x)
517 if (x->expr_type != EXPR_CONSTANT)
520 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
522 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
524 return &gfc_bad_expr;
527 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
529 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
531 return range_check (result, "ASIN");
536 gfc_simplify_asinh (gfc_expr * x)
540 if (x->expr_type != EXPR_CONSTANT)
543 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
545 mpfr_asinh(result->value.real, x->value.real, GFC_RND_MODE);
547 return range_check (result, "ASINH");
552 gfc_simplify_atan (gfc_expr * x)
556 if (x->expr_type != EXPR_CONSTANT)
559 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
561 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
563 return range_check (result, "ATAN");
568 gfc_simplify_atanh (gfc_expr * x)
572 if (x->expr_type != EXPR_CONSTANT)
575 if (mpfr_cmp_si (x->value.real, 1) >= 0 ||
576 mpfr_cmp_si (x->value.real, -1) <= 0)
578 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
580 return &gfc_bad_expr;
583 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
585 mpfr_atanh(result->value.real, x->value.real, GFC_RND_MODE);
587 return range_check (result, "ATANH");
592 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
596 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
599 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
601 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
604 ("If first argument of ATAN2 %L is zero, then the second argument "
605 "must not be zero", &x->where);
606 gfc_free_expr (result);
607 return &gfc_bad_expr;
610 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
611 arctangent2 (y->value.real, x->value.real, result->value.real);
613 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
616 return range_check (result, "ATAN2");
621 gfc_simplify_bit_size (gfc_expr * e)
626 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
627 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
628 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
635 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
639 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
642 if (gfc_extract_int (bit, &b) != NULL || b < 0)
643 return gfc_logical_expr (0, &e->where);
645 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
650 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
652 gfc_expr *ceil, *result;
655 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
657 return &gfc_bad_expr;
659 if (e->expr_type != EXPR_CONSTANT)
662 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
664 ceil = gfc_copy_expr (e);
666 mpfr_ceil (ceil->value.real, e->value.real);
667 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
669 gfc_free_expr (ceil);
671 return range_check (result, "CEILING");
676 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
681 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
683 return &gfc_bad_expr;
685 if (e->expr_type != EXPR_CONSTANT)
688 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > UCHAR_MAX)
690 gfc_error ("Bad character in CHAR function at %L", &e->where);
691 return &gfc_bad_expr;
694 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
696 result->value.character.length = 1;
697 result->value.character.string = gfc_getmem (2);
699 result->value.character.string[0] = c;
700 result->value.character.string[1] = '\0'; /* For debugger */
706 /* Common subroutine for simplifying CMPLX and DCMPLX. */
709 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
713 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
715 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
720 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
724 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
728 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
729 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
733 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
741 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
745 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
749 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
753 return range_check (result, name);
758 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
762 if (x->expr_type != EXPR_CONSTANT
763 || (y != NULL && y->expr_type != EXPR_CONSTANT))
766 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
768 return &gfc_bad_expr;
770 return simplify_cmplx ("CMPLX", x, y, kind);
775 gfc_simplify_complex (gfc_expr * x, gfc_expr * y)
779 if (x->expr_type != EXPR_CONSTANT
780 || (y != NULL && y->expr_type != EXPR_CONSTANT))
783 if (x->ts.type == BT_INTEGER)
785 if (y->ts.type == BT_INTEGER)
786 kind = gfc_default_real_kind;
792 if (y->ts.type == BT_REAL)
793 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
798 return simplify_cmplx ("COMPLEX", x, y, kind);
803 gfc_simplify_conjg (gfc_expr * e)
807 if (e->expr_type != EXPR_CONSTANT)
810 result = gfc_copy_expr (e);
811 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
813 return range_check (result, "CONJG");
818 gfc_simplify_cos (gfc_expr * x)
823 if (x->expr_type != EXPR_CONSTANT)
826 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
831 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
834 gfc_set_model_kind (x->ts.kind);
838 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
839 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
840 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
842 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
843 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
844 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
845 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
851 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
854 return range_check (result, "COS");
860 gfc_simplify_cosh (gfc_expr * x)
864 if (x->expr_type != EXPR_CONSTANT)
867 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
869 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
871 return range_check (result, "COSH");
876 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
879 if (x->expr_type != EXPR_CONSTANT
880 || (y != NULL && y->expr_type != EXPR_CONSTANT))
883 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
888 gfc_simplify_dble (gfc_expr * e)
892 if (e->expr_type != EXPR_CONSTANT)
898 result = gfc_int2real (e, gfc_default_double_kind);
902 result = gfc_real2real (e, gfc_default_double_kind);
906 result = gfc_complex2real (e, gfc_default_double_kind);
910 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
913 return range_check (result, "DBLE");
918 gfc_simplify_digits (gfc_expr * x)
922 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
926 digits = gfc_integer_kinds[i].digits;
931 digits = gfc_real_kinds[i].digits;
938 return gfc_int_expr (digits);
943 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
948 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
951 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
952 result = gfc_constant_result (x->ts.type, kind, &x->where);
957 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
958 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
960 mpz_set_ui (result->value.integer, 0);
965 if (mpfr_cmp (x->value.real, y->value.real) > 0)
966 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
968 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
973 gfc_internal_error ("gfc_simplify_dim(): Bad type");
976 return range_check (result, "DIM");
981 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
983 gfc_expr *a1, *a2, *result;
985 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
989 gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
991 a1 = gfc_real2real (x, gfc_default_double_kind);
992 a2 = gfc_real2real (y, gfc_default_double_kind);
994 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
999 return range_check (result, "DPROD");
1004 gfc_simplify_epsilon (gfc_expr * e)
1009 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1011 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1013 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1015 return range_check (result, "EPSILON");
1020 gfc_simplify_exp (gfc_expr * x)
1025 if (x->expr_type != EXPR_CONSTANT)
1028 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1033 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
1037 gfc_set_model_kind (x->ts.kind);
1040 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1041 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1042 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1043 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1044 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1050 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1053 return range_check (result, "EXP");
1056 /* FIXME: MPFR should be able to do this better */
1058 gfc_simplify_exponent (gfc_expr * x)
1063 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
1067 if (x->expr_type != EXPR_CONSTANT)
1070 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1073 gfc_set_model (x->value.real);
1075 if (mpfr_sgn (x->value.real) == 0)
1077 mpz_set_ui (result->value.integer, 0);
1081 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
1082 /* PR fortran/28276 suffers from a buggy MPFR, and this block of code
1083 does not function correctly. */
1086 mpfr_abs (tmp, x->value.real, GFC_RND_MODE);
1087 mpfr_log2 (tmp, tmp, GFC_RND_MODE);
1089 gfc_mpfr_to_mpz (result->value.integer, tmp);
1091 /* The model number for tiny(x) is b**(emin - 1) where b is the base and emin
1092 is the smallest exponent value. So, we need to add 1 if x is tiny(x). */
1093 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1094 if (mpfr_cmp (x->value.real, gfc_real_kinds[i].tiny) == 0)
1095 mpz_add_ui (result->value.integer,result->value.integer, 1);
1099 /* Requires MPFR 2.2.0 or newer. */
1100 i = (int) mpfr_get_exp (x->value.real);
1101 mpz_set_si (result->value.integer, i);
1104 return range_check (result, "EXPONENT");
1109 gfc_simplify_float (gfc_expr * a)
1113 if (a->expr_type != EXPR_CONSTANT)
1116 result = gfc_int2real (a, gfc_default_real_kind);
1117 return range_check (result, "FLOAT");
1122 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1128 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1130 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1132 if (e->expr_type != EXPR_CONSTANT)
1135 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1137 gfc_set_model_kind (kind);
1139 mpfr_floor (floor, e->value.real);
1141 gfc_mpfr_to_mpz (result->value.integer, floor);
1145 return range_check (result, "FLOOR");
1150 gfc_simplify_fraction (gfc_expr * x)
1153 mpfr_t absv, exp, pow2;
1155 if (x->expr_type != EXPR_CONSTANT)
1158 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1160 gfc_set_model_kind (x->ts.kind);
1162 if (mpfr_sgn (x->value.real) == 0)
1164 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1172 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1173 mpfr_log2 (exp, absv, GFC_RND_MODE);
1175 mpfr_trunc (exp, exp);
1176 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1178 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1180 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1186 return range_check (result, "FRACTION");
1191 gfc_simplify_huge (gfc_expr * e)
1196 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1198 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1203 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1207 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1219 gfc_simplify_iachar (gfc_expr * e)
1224 if (e->expr_type != EXPR_CONSTANT)
1227 if (e->value.character.length != 1)
1229 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1230 return &gfc_bad_expr;
1233 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1235 result = gfc_int_expr (index);
1236 result->where = e->where;
1238 return range_check (result, "IACHAR");
1243 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1247 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1250 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1252 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1254 return range_check (result, "IAND");
1259 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1264 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1267 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1269 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1270 return &gfc_bad_expr;
1273 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1275 if (pos > gfc_integer_kinds[k].bit_size)
1277 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1279 return &gfc_bad_expr;
1282 result = gfc_copy_expr (x);
1284 mpz_clrbit (result->value.integer, pos);
1285 return range_check (result, "IBCLR");
1290 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1297 if (x->expr_type != EXPR_CONSTANT
1298 || y->expr_type != EXPR_CONSTANT
1299 || z->expr_type != EXPR_CONSTANT)
1302 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1304 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1305 return &gfc_bad_expr;
1308 if (gfc_extract_int (z, &len) != NULL || len < 0)
1310 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1311 return &gfc_bad_expr;
1314 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1316 bitsize = gfc_integer_kinds[k].bit_size;
1318 if (pos + len > bitsize)
1321 ("Sum of second and third arguments of IBITS exceeds bit size "
1322 "at %L", &y->where);
1323 return &gfc_bad_expr;
1326 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1328 bits = gfc_getmem (bitsize * sizeof (int));
1330 for (i = 0; i < bitsize; i++)
1333 for (i = 0; i < len; i++)
1334 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1336 for (i = 0; i < bitsize; i++)
1340 mpz_clrbit (result->value.integer, i);
1342 else if (bits[i] == 1)
1344 mpz_setbit (result->value.integer, i);
1348 gfc_internal_error ("IBITS: Bad bit");
1354 return range_check (result, "IBITS");
1359 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1364 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1367 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1369 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1370 return &gfc_bad_expr;
1373 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1375 if (pos > gfc_integer_kinds[k].bit_size)
1377 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1379 return &gfc_bad_expr;
1382 result = gfc_copy_expr (x);
1384 mpz_setbit (result->value.integer, pos);
1386 twos_complement (result->value.integer, gfc_integer_kinds[k].bit_size);
1388 return range_check (result, "IBSET");
1393 gfc_simplify_ichar (gfc_expr * e)
1398 if (e->expr_type != EXPR_CONSTANT)
1401 if (e->value.character.length != 1)
1403 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1404 return &gfc_bad_expr;
1407 index = (unsigned char) e->value.character.string[0];
1409 if (index < 0 || index > UCHAR_MAX)
1411 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1413 return &gfc_bad_expr;
1416 result = gfc_int_expr (index);
1417 result->where = e->where;
1418 return range_check (result, "ICHAR");
1423 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1427 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1430 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1432 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1434 return range_check (result, "IEOR");
1439 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1442 int back, len, lensub;
1443 int i, j, k, count, index = 0, start;
1445 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1448 if (b != NULL && b->value.logical != 0)
1453 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1456 len = x->value.character.length;
1457 lensub = y->value.character.length;
1461 mpz_set_si (result->value.integer, 0);
1470 mpz_set_si (result->value.integer, 1);
1473 else if (lensub == 1)
1475 for (i = 0; i < len; i++)
1477 for (j = 0; j < lensub; j++)
1479 if (y->value.character.string[j] ==
1480 x->value.character.string[i])
1490 for (i = 0; i < len; i++)
1492 for (j = 0; j < lensub; j++)
1494 if (y->value.character.string[j] ==
1495 x->value.character.string[i])
1500 for (k = 0; k < lensub; k++)
1502 if (y->value.character.string[k] ==
1503 x->value.character.string[k + start])
1507 if (count == lensub)
1523 mpz_set_si (result->value.integer, len + 1);
1526 else if (lensub == 1)
1528 for (i = 0; i < len; i++)
1530 for (j = 0; j < lensub; j++)
1532 if (y->value.character.string[j] ==
1533 x->value.character.string[len - i])
1535 index = len - i + 1;
1543 for (i = 0; i < len; i++)
1545 for (j = 0; j < lensub; j++)
1547 if (y->value.character.string[j] ==
1548 x->value.character.string[len - i])
1551 if (start <= len - lensub)
1554 for (k = 0; k < lensub; k++)
1555 if (y->value.character.string[k] ==
1556 x->value.character.string[k + start])
1559 if (count == lensub)
1576 mpz_set_si (result->value.integer, index);
1577 return range_check (result, "INDEX");
1582 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1584 gfc_expr *rpart, *rtrunc, *result;
1587 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
1589 return &gfc_bad_expr;
1591 if (e->expr_type != EXPR_CONSTANT)
1594 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1599 mpz_set (result->value.integer, e->value.integer);
1603 rtrunc = gfc_copy_expr (e);
1604 mpfr_trunc (rtrunc->value.real, e->value.real);
1605 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1606 gfc_free_expr (rtrunc);
1610 rpart = gfc_complex2real (e, kind);
1611 rtrunc = gfc_copy_expr (rpart);
1612 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1613 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1614 gfc_free_expr (rpart);
1615 gfc_free_expr (rtrunc);
1619 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1620 gfc_free_expr (result);
1621 return &gfc_bad_expr;
1624 return range_check (result, "INT");
1629 gfc_simplify_intconv (gfc_expr * e, int kind, const char *name)
1631 gfc_expr *rpart, *rtrunc, *result;
1633 if (e->expr_type != EXPR_CONSTANT)
1636 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1641 mpz_set (result->value.integer, e->value.integer);
1645 rtrunc = gfc_copy_expr (e);
1646 mpfr_trunc (rtrunc->value.real, e->value.real);
1647 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1648 gfc_free_expr (rtrunc);
1652 rpart = gfc_complex2real (e, kind);
1653 rtrunc = gfc_copy_expr (rpart);
1654 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1655 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1656 gfc_free_expr (rpart);
1657 gfc_free_expr (rtrunc);
1661 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
1662 gfc_free_expr (result);
1663 return &gfc_bad_expr;
1666 return range_check (result, name);
1670 gfc_simplify_int2 (gfc_expr * e)
1672 return gfc_simplify_intconv (e, 2, "INT2");
1676 gfc_simplify_int8 (gfc_expr * e)
1678 return gfc_simplify_intconv (e, 8, "INT8");
1682 gfc_simplify_long (gfc_expr * e)
1684 return gfc_simplify_intconv (e, 4, "LONG");
1689 gfc_simplify_ifix (gfc_expr * e)
1691 gfc_expr *rtrunc, *result;
1693 if (e->expr_type != EXPR_CONSTANT)
1696 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1699 rtrunc = gfc_copy_expr (e);
1701 mpfr_trunc (rtrunc->value.real, e->value.real);
1702 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1704 gfc_free_expr (rtrunc);
1705 return range_check (result, "IFIX");
1710 gfc_simplify_idint (gfc_expr * e)
1712 gfc_expr *rtrunc, *result;
1714 if (e->expr_type != EXPR_CONSTANT)
1717 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1720 rtrunc = gfc_copy_expr (e);
1722 mpfr_trunc (rtrunc->value.real, e->value.real);
1723 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1725 gfc_free_expr (rtrunc);
1726 return range_check (result, "IDINT");
1731 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1735 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1738 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1740 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1741 return range_check (result, "IOR");
1746 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1749 int shift, ashift, isize, k, *bits, i;
1751 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1754 if (gfc_extract_int (s, &shift) != NULL)
1756 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1757 return &gfc_bad_expr;
1760 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1762 isize = gfc_integer_kinds[k].bit_size;
1772 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1774 return &gfc_bad_expr;
1777 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1781 mpz_set (result->value.integer, e->value.integer);
1782 return range_check (result, "ISHFT");
1785 bits = gfc_getmem (isize * sizeof (int));
1787 for (i = 0; i < isize; i++)
1788 bits[i] = mpz_tstbit (e->value.integer, i);
1792 for (i = 0; i < shift; i++)
1793 mpz_clrbit (result->value.integer, i);
1795 for (i = 0; i < isize - shift; i++)
1798 mpz_clrbit (result->value.integer, i + shift);
1800 mpz_setbit (result->value.integer, i + shift);
1805 for (i = isize - 1; i >= isize - ashift; i--)
1806 mpz_clrbit (result->value.integer, i);
1808 for (i = isize - 1; i >= ashift; i--)
1811 mpz_clrbit (result->value.integer, i - ashift);
1813 mpz_setbit (result->value.integer, i - ashift);
1817 twos_complement (result->value.integer, isize);
1825 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1828 int shift, ashift, isize, delta, k;
1831 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1834 if (gfc_extract_int (s, &shift) != NULL)
1836 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1837 return &gfc_bad_expr;
1840 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1844 if (gfc_extract_int (sz, &isize) != NULL || isize < 0)
1846 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1847 return &gfc_bad_expr;
1851 isize = gfc_integer_kinds[k].bit_size;
1861 ("Magnitude of second argument of ISHFTC exceeds third argument "
1862 "at %L", &s->where);
1863 return &gfc_bad_expr;
1866 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1870 mpz_set (result->value.integer, e->value.integer);
1874 bits = gfc_getmem (isize * sizeof (int));
1876 for (i = 0; i < isize; i++)
1877 bits[i] = mpz_tstbit (e->value.integer, i);
1879 delta = isize - ashift;
1883 for (i = 0; i < delta; i++)
1886 mpz_clrbit (result->value.integer, i + shift);
1888 mpz_setbit (result->value.integer, i + shift);
1891 for (i = delta; i < isize; i++)
1894 mpz_clrbit (result->value.integer, i - delta);
1896 mpz_setbit (result->value.integer, i - delta);
1901 for (i = 0; i < ashift; i++)
1904 mpz_clrbit (result->value.integer, i + delta);
1906 mpz_setbit (result->value.integer, i + delta);
1909 for (i = ashift; i < isize; i++)
1912 mpz_clrbit (result->value.integer, i + shift);
1914 mpz_setbit (result->value.integer, i + shift);
1918 twos_complement (result->value.integer, isize);
1926 gfc_simplify_kind (gfc_expr * e)
1929 if (e->ts.type == BT_DERIVED)
1931 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1932 return &gfc_bad_expr;
1935 return gfc_int_expr (e->ts.kind);
1940 simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1947 if (array->expr_type != EXPR_VARIABLE)
1951 /* TODO: Simplify constant multi-dimensional bounds. */
1954 if (dim->expr_type != EXPR_CONSTANT)
1957 /* Follow any component references. */
1958 as = array->symtree->n.sym->as;
1959 for (ref = array->ref; ref; ref = ref->next)
1964 switch (ref->u.ar.type)
1971 /* We're done because 'as' has already been set in the
1972 previous iteration. */
1983 as = ref->u.c.component->as;
1994 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
1997 d = mpz_get_si (dim->value.integer);
1999 if (d < 1 || d > as->rank
2000 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
2002 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
2003 return &gfc_bad_expr;
2006 e = upper ? as->upper[d-1] : as->lower[d-1];
2008 if (e->expr_type != EXPR_CONSTANT)
2011 return gfc_copy_expr (e);
2016 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
2018 return simplify_bound (array, dim, 0);
2023 gfc_simplify_len (gfc_expr * e)
2027 if (e->expr_type == EXPR_CONSTANT)
2029 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2031 mpz_set_si (result->value.integer, e->value.character.length);
2032 return range_check (result, "LEN");
2035 if (e->ts.cl != NULL && e->ts.cl->length != NULL
2036 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
2038 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2040 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
2041 return range_check (result, "LEN");
2049 gfc_simplify_len_trim (gfc_expr * e)
2052 int count, len, lentrim, i;
2054 if (e->expr_type != EXPR_CONSTANT)
2057 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2060 len = e->value.character.length;
2062 for (count = 0, i = 1; i <= len; i++)
2063 if (e->value.character.string[len - i] == ' ')
2068 lentrim = len - count;
2070 mpz_set_si (result->value.integer, lentrim);
2071 return range_check (result, "LEN_TRIM");
2076 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
2079 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2082 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
2088 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
2091 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2094 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
2100 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
2103 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2106 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
2112 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
2115 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2118 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
2124 gfc_simplify_log (gfc_expr * x)
2129 if (x->expr_type != EXPR_CONSTANT)
2132 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2134 gfc_set_model_kind (x->ts.kind);
2139 if (mpfr_sgn (x->value.real) <= 0)
2142 ("Argument of LOG at %L cannot be less than or equal to zero",
2144 gfc_free_expr (result);
2145 return &gfc_bad_expr;
2148 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
2152 if ((mpfr_sgn (x->value.complex.r) == 0)
2153 && (mpfr_sgn (x->value.complex.i) == 0))
2155 gfc_error ("Complex argument of LOG at %L cannot be zero",
2157 gfc_free_expr (result);
2158 return &gfc_bad_expr;
2164 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
2165 arctangent2 (x->value.complex.i, x->value.complex.r, result->value.complex.i);
2167 mpfr_atan2 (result->value.complex.i, x->value.complex.i, x->value.complex.r,
2172 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
2173 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
2174 mpfr_add (xr, xr, xi, GFC_RND_MODE);
2175 mpfr_sqrt (xr, xr, GFC_RND_MODE);
2176 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
2184 gfc_internal_error ("gfc_simplify_log: bad type");
2187 return range_check (result, "LOG");
2192 gfc_simplify_log10 (gfc_expr * x)
2196 if (x->expr_type != EXPR_CONSTANT)
2199 gfc_set_model_kind (x->ts.kind);
2201 if (mpfr_sgn (x->value.real) <= 0)
2204 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2206 return &gfc_bad_expr;
2209 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2211 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2213 return range_check (result, "LOG10");
2218 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
2223 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
2225 return &gfc_bad_expr;
2227 if (e->expr_type != EXPR_CONSTANT)
2230 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2232 result->value.logical = e->value.logical;
2238 /* This function is special since MAX() can take any number of
2239 arguments. The simplified expression is a rewritten version of the
2240 argument list containing at most one constant element. Other
2241 constant elements are deleted. Because the argument list has
2242 already been checked, this function always succeeds. sign is 1 for
2243 MAX(), -1 for MIN(). */
2246 simplify_min_max (gfc_expr * expr, int sign)
2248 gfc_actual_arglist *arg, *last, *extremum;
2249 gfc_intrinsic_sym * specific;
2253 specific = expr->value.function.isym;
2255 arg = expr->value.function.actual;
2257 for (; arg; last = arg, arg = arg->next)
2259 if (arg->expr->expr_type != EXPR_CONSTANT)
2262 if (extremum == NULL)
2268 switch (arg->expr->ts.type)
2271 if (mpz_cmp (arg->expr->value.integer,
2272 extremum->expr->value.integer) * sign > 0)
2273 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2278 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2280 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2286 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2289 /* Delete the extra constant argument. */
2291 expr->value.function.actual = arg->next;
2293 last->next = arg->next;
2296 gfc_free_actual_arglist (arg);
2300 /* If there is one value left, replace the function call with the
2302 if (expr->value.function.actual->next != NULL)
2305 /* Convert to the correct type and kind. */
2306 if (expr->ts.type != BT_UNKNOWN)
2307 return gfc_convert_constant (expr->value.function.actual->expr,
2308 expr->ts.type, expr->ts.kind);
2310 if (specific->ts.type != BT_UNKNOWN)
2311 return gfc_convert_constant (expr->value.function.actual->expr,
2312 specific->ts.type, specific->ts.kind);
2314 return gfc_copy_expr (expr->value.function.actual->expr);
2319 gfc_simplify_min (gfc_expr * e)
2321 return simplify_min_max (e, -1);
2326 gfc_simplify_max (gfc_expr * e)
2328 return simplify_min_max (e, 1);
2333 gfc_simplify_maxexponent (gfc_expr * x)
2338 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2340 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2341 result->where = x->where;
2348 gfc_simplify_minexponent (gfc_expr * x)
2353 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2355 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2356 result->where = x->where;
2363 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2366 mpfr_t quot, iquot, term;
2369 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2372 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2373 result = gfc_constant_result (a->ts.type, kind, &a->where);
2378 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2380 /* Result is processor-dependent. */
2381 gfc_error ("Second argument MOD at %L is zero", &a->where);
2382 gfc_free_expr (result);
2383 return &gfc_bad_expr;
2385 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2389 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2391 /* Result is processor-dependent. */
2392 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2393 gfc_free_expr (result);
2394 return &gfc_bad_expr;
2397 gfc_set_model_kind (kind);
2402 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2403 mpfr_trunc (iquot, quot);
2404 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2405 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2413 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2416 return range_check (result, "MOD");
2421 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2424 mpfr_t quot, iquot, term;
2427 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2430 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2431 result = gfc_constant_result (a->ts.type, kind, &a->where);
2436 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2438 /* Result is processor-dependent. This processor just opts
2439 to not handle it at all. */
2440 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2441 gfc_free_expr (result);
2442 return &gfc_bad_expr;
2444 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2449 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2451 /* Result is processor-dependent. */
2452 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2453 gfc_free_expr (result);
2454 return &gfc_bad_expr;
2457 gfc_set_model_kind (kind);
2462 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2463 mpfr_floor (iquot, quot);
2464 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2465 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2473 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2476 return range_check (result, "MODULO");
2480 /* Exists for the sole purpose of consistency with other intrinsics. */
2482 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2483 gfc_expr * fp ATTRIBUTE_UNUSED,
2484 gfc_expr * l ATTRIBUTE_UNUSED,
2485 gfc_expr * to ATTRIBUTE_UNUSED,
2486 gfc_expr * tp ATTRIBUTE_UNUSED)
2493 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2498 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
2501 mp_exp_t emin, emax;
2504 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2507 if (mpfr_sgn (s->value.real) == 0)
2509 gfc_error ("Second argument of NEAREST at %L shall not be zero", &s->where);
2510 return &gfc_bad_expr;
2513 gfc_set_model_kind (x->ts.kind);
2514 result = gfc_copy_expr (x);
2516 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
2518 direction = mpfr_sgn (s->value.real);
2519 sgn = mpfr_sgn (x->value.real);
2523 int k = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
2526 mpfr_add (result->value.real,
2527 x->value.real, gfc_real_kinds[k].subnormal, GFC_RND_MODE);
2529 mpfr_sub (result->value.real,
2530 x->value.real, gfc_real_kinds[k].subnormal, GFC_RND_MODE);
2536 direction = -direction;
2537 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
2541 mpfr_add_one_ulp (result->value.real, GFC_RND_MODE);
2544 /* In this case the exponent can shrink, which makes us skip
2545 over one number because we subtract one ulp with the
2546 larger exponent. Thus we need to compensate for this. */
2547 mpfr_init_set (tmp, result->value.real, GFC_RND_MODE);
2549 mpfr_sub_one_ulp (result->value.real, GFC_RND_MODE);
2550 mpfr_add_one_ulp (result->value.real, GFC_RND_MODE);
2552 /* If we're back to where we started, the spacing is one
2553 ulp, and we get the correct result by subtracting. */
2554 if (mpfr_cmp (tmp, result->value.real) == 0)
2555 mpfr_sub_one_ulp (result->value.real, GFC_RND_MODE);
2561 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
2565 /* Save current values of emin and emax. */
2566 emin = mpfr_get_emin ();
2567 emax = mpfr_get_emax ();
2569 /* Set emin and emax for the current model number. */
2570 sgn = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
2571 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[sgn].min_exponent - 1);
2572 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[sgn].max_exponent - 1);
2574 sgn = mpfr_sgn (s->value.real);
2576 mpfr_set_inf (tmp, sgn);
2577 mpfr_nexttoward (result->value.real, tmp);
2578 mpfr_subnormalize (result->value.real, 0, GFC_RND_MODE);
2580 mpfr_set_emin (emin);
2581 mpfr_set_emax (emax);
2586 return range_check (result, "NEAREST");
2591 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2593 gfc_expr *itrunc, *result;
2596 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2598 return &gfc_bad_expr;
2600 if (e->expr_type != EXPR_CONSTANT)
2603 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2605 itrunc = gfc_copy_expr (e);
2607 mpfr_round(itrunc->value.real, e->value.real);
2609 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2611 gfc_free_expr (itrunc);
2613 return range_check (result, name);
2618 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2620 return simplify_nint ("NINT", e, k);
2625 gfc_simplify_idnint (gfc_expr * e)
2627 return simplify_nint ("IDNINT", e, NULL);
2632 gfc_simplify_not (gfc_expr * e)
2638 if (e->expr_type != EXPR_CONSTANT)
2641 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2643 mpz_com (result->value.integer, e->value.integer);
2645 /* Because of how GMP handles numbers, the result must be ANDed with
2646 a mask. For radices <> 2, this will require change. */
2648 i = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2651 mpz_add (mask, gfc_integer_kinds[i].huge, gfc_integer_kinds[i].huge);
2652 mpz_add_ui (mask, mask, 1);
2654 mpz_and (result->value.integer, result->value.integer, mask);
2656 twos_complement (result->value.integer, gfc_integer_kinds[i].bit_size);
2660 return range_check (result, "NOT");
2665 gfc_simplify_null (gfc_expr * mold)
2671 result = gfc_get_expr ();
2672 result->ts.type = BT_UNKNOWN;
2675 result = gfc_copy_expr (mold);
2676 result->expr_type = EXPR_NULL;
2683 gfc_simplify_or (gfc_expr * x, gfc_expr * y)
2688 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2691 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2692 if (x->ts.type == BT_INTEGER)
2694 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
2695 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2697 else /* BT_LOGICAL */
2699 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
2700 result->value.logical = x->value.logical || y->value.logical;
2703 return range_check (result, "OR");
2708 gfc_simplify_precision (gfc_expr * e)
2713 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2715 result = gfc_int_expr (gfc_real_kinds[i].precision);
2716 result->where = e->where;
2723 gfc_simplify_radix (gfc_expr * e)
2728 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2732 i = gfc_integer_kinds[i].radix;
2736 i = gfc_real_kinds[i].radix;
2743 result = gfc_int_expr (i);
2744 result->where = e->where;
2751 gfc_simplify_range (gfc_expr * e)
2757 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2762 j = gfc_integer_kinds[i].range;
2767 j = gfc_real_kinds[i].range;
2774 result = gfc_int_expr (j);
2775 result->where = e->where;
2782 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2787 if (e->ts.type == BT_COMPLEX)
2788 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2790 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2793 return &gfc_bad_expr;
2795 if (e->expr_type != EXPR_CONSTANT)
2801 result = gfc_int2real (e, kind);
2805 result = gfc_real2real (e, kind);
2809 result = gfc_complex2real (e, kind);
2813 gfc_internal_error ("bad type in REAL");
2817 return range_check (result, "REAL");
2822 gfc_simplify_realpart (gfc_expr * e)
2826 if (e->expr_type != EXPR_CONSTANT)
2829 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
2830 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
2832 return range_check (result, "REALPART");
2836 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2839 int i, j, len, ncopies, nlen;
2841 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2844 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2846 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2847 return &gfc_bad_expr;
2850 len = e->value.character.length;
2851 nlen = ncopies * len;
2853 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2857 result->value.character.string = gfc_getmem (1);
2858 result->value.character.length = 0;
2859 result->value.character.string[0] = '\0';
2863 result->value.character.length = nlen;
2864 result->value.character.string = gfc_getmem (nlen + 1);
2866 for (i = 0; i < ncopies; i++)
2867 for (j = 0; j < len; j++)
2868 result->value.character.string[j + i * len] =
2869 e->value.character.string[j];
2871 result->value.character.string[nlen] = '\0'; /* For debugger */
2876 /* This one is a bear, but mainly has to do with shuffling elements. */
2879 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2880 gfc_expr * pad, gfc_expr * order_exp)
2883 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2884 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2885 gfc_constructor *head, *tail;
2891 /* Unpack the shape array. */
2892 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2895 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2899 && (pad->expr_type != EXPR_ARRAY
2900 || !gfc_is_constant_expr (pad)))
2903 if (order_exp != NULL
2904 && (order_exp->expr_type != EXPR_ARRAY
2905 || !gfc_is_constant_expr (order_exp)))
2914 e = gfc_get_array_element (shape_exp, rank);
2918 if (gfc_extract_int (e, &shape[rank]) != NULL)
2920 gfc_error ("Integer too large in shape specification at %L",
2928 if (rank >= GFC_MAX_DIMENSIONS)
2930 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2931 "at %L", &e->where);
2936 if (shape[rank] < 0)
2938 gfc_error ("Shape specification at %L cannot be negative",
2948 gfc_error ("Shape specification at %L cannot be the null array",
2953 /* Now unpack the order array if present. */
2954 if (order_exp == NULL)
2956 for (i = 0; i < rank; i++)
2963 for (i = 0; i < rank; i++)
2966 for (i = 0; i < rank; i++)
2968 e = gfc_get_array_element (order_exp, i);
2972 ("ORDER parameter of RESHAPE at %L is not the same size "
2973 "as SHAPE parameter", &order_exp->where);
2977 if (gfc_extract_int (e, &order[i]) != NULL)
2979 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2987 if (order[i] < 1 || order[i] > rank)
2989 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2998 gfc_error ("Invalid permutation in ORDER parameter at %L",
3007 /* Count the elements in the source and padding arrays. */
3012 gfc_array_size (pad, &size);
3013 npad = mpz_get_ui (size);
3017 gfc_array_size (source, &size);
3018 nsource = mpz_get_ui (size);
3021 /* If it weren't for that pesky permutation we could just loop
3022 through the source and round out any shortage with pad elements.
3023 But no, someone just had to have the compiler do something the
3024 user should be doing. */
3026 for (i = 0; i < rank; i++)
3031 /* Figure out which element to extract. */
3032 mpz_set_ui (index, 0);
3034 for (i = rank - 1; i >= 0; i--)
3036 mpz_add_ui (index, index, x[order[i]]);
3038 mpz_mul_ui (index, index, shape[order[i - 1]]);
3041 if (mpz_cmp_ui (index, INT_MAX) > 0)
3042 gfc_internal_error ("Reshaped array too large at %L", &e->where);
3044 j = mpz_get_ui (index);
3047 e = gfc_get_array_element (source, j);
3055 ("PAD parameter required for short SOURCE parameter at %L",
3061 e = gfc_get_array_element (pad, j);
3065 head = tail = gfc_get_constructor ();
3068 tail->next = gfc_get_constructor ();
3075 tail->where = e->where;
3078 /* Calculate the next element. */
3082 if (++x[i] < shape[i])
3093 e = gfc_get_expr ();
3094 e->where = source->where;
3095 e->expr_type = EXPR_ARRAY;
3096 e->value.constructor = head;
3097 e->shape = gfc_get_shape (rank);
3099 for (i = 0; i < rank; i++)
3100 mpz_init_set_ui (e->shape[i], shape[i]);
3108 gfc_free_constructor (head);
3110 return &gfc_bad_expr;
3115 gfc_simplify_rrspacing (gfc_expr * x)
3118 mpfr_t absv, log2, exp, frac, pow2;
3121 if (x->expr_type != EXPR_CONSTANT)
3124 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3126 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3128 p = gfc_real_kinds[i].digits;
3130 gfc_set_model_kind (x->ts.kind);
3132 if (mpfr_sgn (x->value.real) == 0)
3134 mpfr_ui_div (result->value.real, 1, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3144 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3145 mpfr_log2 (log2, absv, GFC_RND_MODE);
3147 mpfr_trunc (log2, log2);
3148 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3150 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3151 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3153 mpfr_mul_2exp (result->value.real, frac, (unsigned long)p, GFC_RND_MODE);
3161 return range_check (result, "RRSPACING");
3166 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
3168 int k, neg_flag, power, exp_range;
3169 mpfr_t scale, radix;
3172 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3175 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3177 if (mpfr_sgn (x->value.real) == 0)
3179 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3183 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3185 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
3187 /* This check filters out values of i that would overflow an int. */
3188 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
3189 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
3191 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
3192 return &gfc_bad_expr;
3195 /* Compute scale = radix ** power. */
3196 power = mpz_get_si (i->value.integer);
3206 gfc_set_model_kind (x->ts.kind);
3209 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
3210 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
3213 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
3215 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3220 return range_check (result, "SCALE");
3225 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
3230 size_t indx, len, lenc;
3232 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3235 if (b != NULL && b->value.logical != 0)
3240 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3243 len = e->value.character.length;
3244 lenc = c->value.character.length;
3246 if (len == 0 || lenc == 0)
3255 strcspn (e->value.character.string, c->value.character.string) + 1;
3262 for (indx = len; indx > 0; indx--)
3264 for (i = 0; i < lenc; i++)
3266 if (c->value.character.string[i]
3267 == e->value.character.string[indx - 1])
3275 mpz_set_ui (result->value.integer, indx);
3276 return range_check (result, "SCAN");
3281 gfc_simplify_selected_int_kind (gfc_expr * e)
3286 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3291 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3292 if (gfc_integer_kinds[i].range >= range
3293 && gfc_integer_kinds[i].kind < kind)
3294 kind = gfc_integer_kinds[i].kind;
3296 if (kind == INT_MAX)
3299 result = gfc_int_expr (kind);
3300 result->where = e->where;
3307 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
3309 int range, precision, i, kind, found_precision, found_range;
3316 if (p->expr_type != EXPR_CONSTANT
3317 || gfc_extract_int (p, &precision) != NULL)
3325 if (q->expr_type != EXPR_CONSTANT
3326 || gfc_extract_int (q, &range) != NULL)
3331 found_precision = 0;
3334 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3336 if (gfc_real_kinds[i].precision >= precision)
3337 found_precision = 1;
3339 if (gfc_real_kinds[i].range >= range)
3342 if (gfc_real_kinds[i].precision >= precision
3343 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3344 kind = gfc_real_kinds[i].kind;
3347 if (kind == INT_MAX)
3351 if (!found_precision)
3357 result = gfc_int_expr (kind);
3358 result->where = (p != NULL) ? p->where : q->where;
3365 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3368 mpfr_t exp, absv, log2, pow2, frac;
3371 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3374 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3376 gfc_set_model_kind (x->ts.kind);
3378 if (mpfr_sgn (x->value.real) == 0)
3380 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3390 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3391 mpfr_log2 (log2, absv, GFC_RND_MODE);
3393 mpfr_trunc (log2, log2);
3394 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3396 /* Old exponent value, and fraction. */
3397 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3399 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3402 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3403 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3410 return range_check (result, "SET_EXPONENT");
3415 gfc_simplify_shape (gfc_expr * source)
3417 mpz_t shape[GFC_MAX_DIMENSIONS];
3418 gfc_expr *result, *e, *f;
3423 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3426 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3429 ar = gfc_find_array_ref (source);
3431 t = gfc_array_ref_shape (ar, shape);
3433 for (n = 0; n < source->rank; n++)
3435 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3440 mpz_set (e->value.integer, shape[n]);
3441 mpz_clear (shape[n]);
3445 mpz_set_ui (e->value.integer, n + 1);
3447 f = gfc_simplify_size (source, e);
3451 gfc_free_expr (result);
3460 gfc_append_constructor (result, e);
3468 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3476 if (gfc_array_size (array, &size) == FAILURE)
3481 if (dim->expr_type != EXPR_CONSTANT)
3484 d = mpz_get_ui (dim->value.integer) - 1;
3485 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3489 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3492 mpz_set (result->value.integer, size);
3499 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3503 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3506 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3511 mpz_abs (result->value.integer, x->value.integer);
3512 if (mpz_sgn (y->value.integer) < 0)
3513 mpz_neg (result->value.integer, result->value.integer);
3518 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3520 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3521 if (mpfr_sgn (y->value.real) < 0)
3522 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3527 gfc_internal_error ("Bad type in gfc_simplify_sign");
3535 gfc_simplify_sin (gfc_expr * x)
3540 if (x->expr_type != EXPR_CONSTANT)
3543 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3548 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3552 gfc_set_model (x->value.real);
3556 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3557 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3558 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3560 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3561 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3562 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3569 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3572 return range_check (result, "SIN");
3577 gfc_simplify_sinh (gfc_expr * x)
3581 if (x->expr_type != EXPR_CONSTANT)
3584 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3586 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3588 return range_check (result, "SINH");
3592 /* The argument is always a double precision real that is converted to
3593 single precision. TODO: Rounding! */
3596 gfc_simplify_sngl (gfc_expr * a)
3600 if (a->expr_type != EXPR_CONSTANT)
3603 result = gfc_real2real (a, gfc_default_real_kind);
3604 return range_check (result, "SNGL");
3609 gfc_simplify_spacing (gfc_expr * x)
3616 if (x->expr_type != EXPR_CONSTANT)
3619 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3621 p = gfc_real_kinds[i].digits;
3623 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3625 gfc_set_model_kind (x->ts.kind);
3627 if (mpfr_sgn (x->value.real) == 0)
3629 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3636 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3637 mpfr_log2 (log2, absv, GFC_RND_MODE);
3638 mpfr_trunc (log2, log2);
3640 mpfr_add_ui (log2, log2, 1, GFC_RND_MODE);
3642 /* FIXME: We should be using mpfr_get_si here, but this function is
3643 not available with the version of mpfr distributed with gmp (as of
3644 2004-09-17). Replace once mpfr has been imported into the gcc cvs
3646 diff = (long)mpfr_get_d (log2, GFC_RND_MODE) - (long)p;
3647 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3648 mpfr_mul_2si (result->value.real, result->value.real, diff, GFC_RND_MODE);
3653 if (mpfr_cmp (result->value.real, gfc_real_kinds[i].tiny) < 0)
3654 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3656 return range_check (result, "SPACING");
3661 gfc_simplify_sqrt (gfc_expr * e)
3664 mpfr_t ac, ad, s, t, w;
3666 if (e->expr_type != EXPR_CONSTANT)
3669 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3674 if (mpfr_cmp_si (e->value.real, 0) < 0)
3676 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3681 /* Formula taken from Numerical Recipes to avoid over- and
3684 gfc_set_model (e->value.real);
3691 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3692 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3695 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3696 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3700 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3701 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3703 if (mpfr_cmp (ac, ad) >= 0)
3705 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3706 mpfr_mul (t, t, t, GFC_RND_MODE);
3707 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3708 mpfr_sqrt (t, t, GFC_RND_MODE);
3709 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3710 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3711 mpfr_sqrt (t, t, GFC_RND_MODE);
3712 mpfr_sqrt (s, ac, GFC_RND_MODE);
3713 mpfr_mul (w, s, t, GFC_RND_MODE);
3717 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3718 mpfr_mul (t, s, s, GFC_RND_MODE);
3719 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3720 mpfr_sqrt (t, t, GFC_RND_MODE);
3721 mpfr_abs (s, s, GFC_RND_MODE);
3722 mpfr_add (t, t, s, GFC_RND_MODE);
3723 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3724 mpfr_sqrt (t, t, GFC_RND_MODE);
3725 mpfr_sqrt (s, ad, GFC_RND_MODE);
3726 mpfr_mul (w, s, t, GFC_RND_MODE);
3729 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3731 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3732 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3733 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3735 else if (mpfr_cmp_ui (w, 0) != 0
3736 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3737 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3739 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3740 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3741 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3743 else if (mpfr_cmp_ui (w, 0) != 0
3744 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3745 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3747 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3748 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3749 mpfr_neg (w, w, GFC_RND_MODE);
3750 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3753 gfc_internal_error ("invalid complex argument of SQRT at %L",
3765 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3768 return range_check (result, "SQRT");
3771 gfc_free_expr (result);
3772 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3773 return &gfc_bad_expr;
3778 gfc_simplify_tan (gfc_expr * x)
3783 if (x->expr_type != EXPR_CONSTANT)
3786 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3788 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3790 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3792 return range_check (result, "TAN");
3797 gfc_simplify_tanh (gfc_expr * x)
3801 if (x->expr_type != EXPR_CONSTANT)
3804 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3806 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3808 return range_check (result, "TANH");
3814 gfc_simplify_tiny (gfc_expr * e)
3819 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3821 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3822 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3829 gfc_simplify_transfer (gfc_expr * source, gfc_expr *mold, gfc_expr * size)
3832 /* Reference mold and size to suppress warning. */
3833 if (gfc_init_expr && (mold || size))
3834 gfc_error ("TRANSFER intrinsic not implemented for initialization at %L",
3842 gfc_simplify_trim (gfc_expr * e)
3845 int count, i, len, lentrim;
3847 if (e->expr_type != EXPR_CONSTANT)
3850 len = e->value.character.length;
3852 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3854 for (count = 0, i = 1; i <= len; ++i)
3856 if (e->value.character.string[len - i] == ' ')
3862 lentrim = len - count;
3864 result->value.character.length = lentrim;
3865 result->value.character.string = gfc_getmem (lentrim + 1);
3867 for (i = 0; i < lentrim; i++)
3868 result->value.character.string[i] = e->value.character.string[i];
3870 result->value.character.string[lentrim] = '\0'; /* For debugger */
3877 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3879 return simplify_bound (array, dim, 1);
3884 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3888 size_t index, len, lenset;
3891 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3894 if (b != NULL && b->value.logical != 0)
3899 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3902 len = s->value.character.length;
3903 lenset = set->value.character.length;
3907 mpz_set_ui (result->value.integer, 0);
3915 mpz_set_ui (result->value.integer, 1);
3920 strspn (s->value.character.string, set->value.character.string) + 1;
3929 mpz_set_ui (result->value.integer, len);
3932 for (index = len; index > 0; index --)
3934 for (i = 0; i < lenset; i++)
3936 if (s->value.character.string[index - 1]
3937 == set->value.character.string[i])
3945 mpz_set_ui (result->value.integer, index);
3951 gfc_simplify_xor (gfc_expr * x, gfc_expr * y)
3956 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3959 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3960 if (x->ts.type == BT_INTEGER)
3962 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3963 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3965 else /* BT_LOGICAL */
3967 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3968 result->value.logical = (x->value.logical && ! y->value.logical)
3969 || (! x->value.logical && y->value.logical);
3972 return range_check (result, "XOR");
3977 /****************** Constant simplification *****************/
3979 /* Master function to convert one constant to another. While this is
3980 used as a simplification function, it requires the destination type
3981 and kind information which is supplied by a special case in
3985 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3987 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3988 gfc_constructor *head, *c, *tail = NULL;
4002 f = gfc_int2complex;
4022 f = gfc_real2complex;
4033 f = gfc_complex2int;
4036 f = gfc_complex2real;
4039 f = gfc_complex2complex;
4065 f = gfc_hollerith2int;
4069 f = gfc_hollerith2real;
4073 f = gfc_hollerith2complex;
4077 f = gfc_hollerith2character;
4081 f = gfc_hollerith2logical;
4091 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
4096 switch (e->expr_type)
4099 result = f (e, kind);
4101 return &gfc_bad_expr;
4105 if (!gfc_is_constant_expr (e))
4110 for (c = e->value.constructor; c; c = c->next)
4113 head = tail = gfc_get_constructor ();
4116 tail->next = gfc_get_constructor ();
4120 tail->where = c->where;
4122 if (c->iterator == NULL)
4123 tail->expr = f (c->expr, kind);
4126 g = gfc_convert_constant (c->expr, type, kind);
4127 if (g == &gfc_bad_expr)
4132 if (tail->expr == NULL)
4134 gfc_free_constructor (head);
4139 result = gfc_get_expr ();
4140 result->ts.type = type;
4141 result->ts.kind = kind;
4142 result->expr_type = EXPR_ARRAY;
4143 result->value.constructor = head;
4144 result->shape = gfc_copy_shape (e->shape, e->rank);
4145 result->where = e->where;
4146 result->rank = e->rank;
4157 /****************** Helper functions ***********************/
4159 /* Given a collating table, create the inverse table. */
4162 invert_table (const int *table, int *xtable)
4166 for (i = 0; i < 256; i++)
4169 for (i = 0; i < 256; i++)
4170 xtable[table[i]] = i;
4175 gfc_simplify_init_1 (void)
4178 invert_table (ascii_table, xascii_table);