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)
101 if (gfc_range_check (result) == ARITH_OK)
104 gfc_error ("Result of %s overflows its kind at %L", name, &result->where);
105 gfc_free_expr (result);
106 return &gfc_bad_expr;
110 /* A helper function that gets an optional and possibly missing
111 kind parameter. Returns the kind, -1 if something went wrong. */
114 get_kind (bt type, gfc_expr * k, const char *name, int default_kind)
121 if (k->expr_type != EXPR_CONSTANT)
123 gfc_error ("KIND parameter of %s at %L must be an initialization "
124 "expression", name, &k->where);
129 if (gfc_extract_int (k, &kind) != NULL
130 || gfc_validate_kind (type, kind, true) < 0)
133 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
141 /********************** Simplification functions *****************************/
144 gfc_simplify_abs (gfc_expr * e)
149 if (e->expr_type != EXPR_CONSTANT)
155 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
157 mpz_abs (result->value.integer, e->value.integer);
159 result = range_check (result, "IABS");
163 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
165 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
167 result = range_check (result, "ABS");
171 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
173 gfc_set_model_kind (e->ts.kind);
176 /* FIXME: Possible numerical problems. */
177 mpfr_mul (a, e->value.complex.r, e->value.complex.r, GFC_RND_MODE);
178 mpfr_mul (b, e->value.complex.i, e->value.complex.i, GFC_RND_MODE);
179 mpfr_add (a, a, b, GFC_RND_MODE);
180 mpfr_sqrt (result->value.real, a, GFC_RND_MODE);
185 result = range_check (result, "CABS");
189 gfc_internal_error ("gfc_simplify_abs(): Bad type");
197 gfc_simplify_achar (gfc_expr * e)
202 if (e->expr_type != EXPR_CONSTANT)
205 /* We cannot assume that the native character set is ASCII in this
207 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
209 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
210 "must be between 0 and 127", &e->where);
211 return &gfc_bad_expr;
214 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
217 result->value.character.string = gfc_getmem (2);
219 result->value.character.length = 1;
220 result->value.character.string[0] = ascii_table[index];
221 result->value.character.string[1] = '\0'; /* For debugger */
227 gfc_simplify_acos (gfc_expr * x)
231 if (x->expr_type != EXPR_CONSTANT)
234 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
236 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
238 return &gfc_bad_expr;
241 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
243 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
245 return range_check (result, "ACOS");
250 gfc_simplify_adjustl (gfc_expr * e)
256 if (e->expr_type != EXPR_CONSTANT)
259 len = e->value.character.length;
261 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
263 result->value.character.length = len;
264 result->value.character.string = gfc_getmem (len + 1);
266 for (count = 0, i = 0; i < len; ++i)
268 ch = e->value.character.string[i];
274 for (i = 0; i < len - count; ++i)
276 result->value.character.string[i] =
277 e->value.character.string[count + i];
280 for (i = len - count; i < len; ++i)
282 result->value.character.string[i] = ' ';
285 result->value.character.string[len] = '\0'; /* For debugger */
292 gfc_simplify_adjustr (gfc_expr * e)
298 if (e->expr_type != EXPR_CONSTANT)
301 len = e->value.character.length;
303 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
305 result->value.character.length = len;
306 result->value.character.string = gfc_getmem (len + 1);
308 for (count = 0, i = len - 1; i >= 0; --i)
310 ch = e->value.character.string[i];
316 for (i = 0; i < count; ++i)
318 result->value.character.string[i] = ' ';
321 for (i = count; i < len; ++i)
323 result->value.character.string[i] =
324 e->value.character.string[i - count];
327 result->value.character.string[len] = '\0'; /* For debugger */
334 gfc_simplify_aimag (gfc_expr * e)
338 if (e->expr_type != EXPR_CONSTANT)
341 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
342 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
344 return range_check (result, "AIMAG");
349 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
351 gfc_expr *rtrunc, *result;
354 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
356 return &gfc_bad_expr;
358 if (e->expr_type != EXPR_CONSTANT)
361 rtrunc = gfc_copy_expr (e);
363 mpfr_trunc (rtrunc->value.real, e->value.real);
365 result = gfc_real2real (rtrunc, kind);
366 gfc_free_expr (rtrunc);
368 return range_check (result, "AINT");
373 gfc_simplify_dint (gfc_expr * e)
375 gfc_expr *rtrunc, *result;
377 if (e->expr_type != EXPR_CONSTANT)
380 rtrunc = gfc_copy_expr (e);
382 mpfr_trunc (rtrunc->value.real, e->value.real);
384 result = gfc_real2real (rtrunc, gfc_default_double_kind);
385 gfc_free_expr (rtrunc);
387 return range_check (result, "DINT");
392 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
394 gfc_expr *rtrunc, *result;
398 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
400 return &gfc_bad_expr;
402 if (e->expr_type != EXPR_CONSTANT)
405 result = gfc_constant_result (e->ts.type, kind, &e->where);
407 rtrunc = gfc_copy_expr (e);
409 cmp = mpfr_cmp_ui (e->value.real, 0);
411 gfc_set_model_kind (kind);
413 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
417 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
418 mpfr_trunc (result->value.real, rtrunc->value.real);
422 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
423 mpfr_trunc (result->value.real, rtrunc->value.real);
426 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
428 gfc_free_expr (rtrunc);
431 return range_check (result, "ANINT");
436 gfc_simplify_dnint (gfc_expr * e)
438 gfc_expr *rtrunc, *result;
442 if (e->expr_type != EXPR_CONSTANT)
446 gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
448 rtrunc = gfc_copy_expr (e);
450 cmp = mpfr_cmp_ui (e->value.real, 0);
452 gfc_set_model_kind (gfc_default_double_kind);
454 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
458 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
459 mpfr_trunc (result->value.real, rtrunc->value.real);
463 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
464 mpfr_trunc (result->value.real, rtrunc->value.real);
467 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
469 gfc_free_expr (rtrunc);
472 return range_check (result, "DNINT");
477 gfc_simplify_asin (gfc_expr * x)
481 if (x->expr_type != EXPR_CONSTANT)
484 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
486 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
488 return &gfc_bad_expr;
491 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
493 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
495 return range_check (result, "ASIN");
500 gfc_simplify_atan (gfc_expr * x)
504 if (x->expr_type != EXPR_CONSTANT)
507 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
509 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
511 return range_check (result, "ATAN");
517 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
521 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
524 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
526 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
529 ("If first argument of ATAN2 %L is zero, then the second argument "
530 "must not be zero", &x->where);
531 gfc_free_expr (result);
532 return &gfc_bad_expr;
535 arctangent2 (y->value.real, x->value.real, result->value.real);
537 return range_check (result, "ATAN2");
543 gfc_simplify_bit_size (gfc_expr * e)
548 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
549 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
550 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
557 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
561 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
564 if (gfc_extract_int (bit, &b) != NULL || b < 0)
565 return gfc_logical_expr (0, &e->where);
567 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
572 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
574 gfc_expr *ceil, *result;
577 kind = get_kind (BT_REAL, k, "CEILING", gfc_default_real_kind);
579 return &gfc_bad_expr;
581 if (e->expr_type != EXPR_CONSTANT)
584 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
586 ceil = gfc_copy_expr (e);
588 mpfr_ceil (ceil->value.real, e->value.real);
589 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
591 gfc_free_expr (ceil);
593 return range_check (result, "CEILING");
598 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
603 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
605 return &gfc_bad_expr;
607 if (e->expr_type != EXPR_CONSTANT)
610 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > 255)
612 gfc_error ("Bad character in CHAR function at %L", &e->where);
613 return &gfc_bad_expr;
616 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
618 result->value.character.length = 1;
619 result->value.character.string = gfc_getmem (2);
621 result->value.character.string[0] = c;
622 result->value.character.string[1] = '\0'; /* For debugger */
628 /* Common subroutine for simplifying CMPLX and DCMPLX. */
631 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
635 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
637 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
642 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
646 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
650 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
651 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
655 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
663 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
667 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
671 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
675 return range_check (result, name);
680 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
684 if (x->expr_type != EXPR_CONSTANT
685 || (y != NULL && y->expr_type != EXPR_CONSTANT))
688 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
690 return &gfc_bad_expr;
692 return simplify_cmplx ("CMPLX", x, y, kind);
697 gfc_simplify_conjg (gfc_expr * e)
701 if (e->expr_type != EXPR_CONSTANT)
704 result = gfc_copy_expr (e);
705 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
707 return range_check (result, "CONJG");
712 gfc_simplify_cos (gfc_expr * x)
717 if (x->expr_type != EXPR_CONSTANT)
720 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
725 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
728 gfc_set_model_kind (x->ts.kind);
732 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
733 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
734 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
736 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
737 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
738 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
739 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
745 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
748 return range_check (result, "COS");
754 gfc_simplify_cosh (gfc_expr * x)
758 if (x->expr_type != EXPR_CONSTANT)
761 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
763 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
765 return range_check (result, "COSH");
770 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
773 if (x->expr_type != EXPR_CONSTANT
774 || (y != NULL && y->expr_type != EXPR_CONSTANT))
777 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
782 gfc_simplify_dble (gfc_expr * e)
786 if (e->expr_type != EXPR_CONSTANT)
792 result = gfc_int2real (e, gfc_default_double_kind);
796 result = gfc_real2real (e, gfc_default_double_kind);
800 result = gfc_complex2real (e, gfc_default_double_kind);
804 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
807 return range_check (result, "DBLE");
812 gfc_simplify_digits (gfc_expr * x)
816 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
820 digits = gfc_integer_kinds[i].digits;
825 digits = gfc_real_kinds[i].digits;
832 return gfc_int_expr (digits);
837 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
841 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
844 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
849 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
850 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
852 mpz_set_ui (result->value.integer, 0);
857 if (mpfr_cmp (x->value.real, y->value.real) > 0)
858 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
860 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
865 gfc_internal_error ("gfc_simplify_dim(): Bad type");
868 return range_check (result, "DIM");
873 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
875 gfc_expr *a1, *a2, *result;
877 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
881 gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
883 a1 = gfc_real2real (x, gfc_default_double_kind);
884 a2 = gfc_real2real (y, gfc_default_double_kind);
886 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
891 return range_check (result, "DPROD");
896 gfc_simplify_epsilon (gfc_expr * e)
901 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
903 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
905 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
907 return range_check (result, "EPSILON");
912 gfc_simplify_exp (gfc_expr * x)
917 if (x->expr_type != EXPR_CONSTANT)
920 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
925 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
929 gfc_set_model_kind (x->ts.kind);
932 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
933 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
934 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
935 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
936 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
942 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
945 return range_check (result, "EXP");
948 /* FIXME: MPFR should be able to do this better */
950 gfc_simplify_exponent (gfc_expr * x)
955 if (x->expr_type != EXPR_CONSTANT)
958 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
961 gfc_set_model (x->value.real);
963 if (mpfr_sgn (x->value.real) == 0)
965 mpz_set_ui (result->value.integer, 0);
971 mpfr_abs (tmp, x->value.real, GFC_RND_MODE);
972 mpfr_log2 (tmp, tmp, GFC_RND_MODE);
974 gfc_mpfr_to_mpz (result->value.integer, tmp);
978 return range_check (result, "EXPONENT");
983 gfc_simplify_float (gfc_expr * a)
987 if (a->expr_type != EXPR_CONSTANT)
990 result = gfc_int2real (a, gfc_default_real_kind);
991 return range_check (result, "FLOAT");
996 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1002 kind = get_kind (BT_REAL, k, "FLOOR", gfc_default_real_kind);
1004 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1006 if (e->expr_type != EXPR_CONSTANT)
1009 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1011 gfc_set_model_kind (kind);
1013 mpfr_floor (floor, e->value.real);
1015 gfc_mpfr_to_mpz (result->value.integer, floor);
1019 return range_check (result, "FLOOR");
1024 gfc_simplify_fraction (gfc_expr * x)
1027 mpfr_t absv, exp, pow2;
1029 if (x->expr_type != EXPR_CONSTANT)
1032 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1034 gfc_set_model_kind (x->ts.kind);
1036 if (mpfr_sgn (x->value.real) == 0)
1038 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1046 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1047 mpfr_log2 (exp, absv, GFC_RND_MODE);
1049 mpfr_trunc (exp, exp);
1050 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1052 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1054 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1060 return range_check (result, "FRACTION");
1065 gfc_simplify_huge (gfc_expr * e)
1070 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1072 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1077 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1081 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1093 gfc_simplify_iachar (gfc_expr * e)
1098 if (e->expr_type != EXPR_CONSTANT)
1101 if (e->value.character.length != 1)
1103 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1104 return &gfc_bad_expr;
1107 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1109 result = gfc_int_expr (index);
1110 result->where = e->where;
1112 return range_check (result, "IACHAR");
1117 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1121 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1124 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1126 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1128 return range_check (result, "IAND");
1133 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1138 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1141 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1143 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1144 return &gfc_bad_expr;
1147 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1149 if (pos > gfc_integer_kinds[k].bit_size)
1151 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1153 return &gfc_bad_expr;
1156 result = gfc_copy_expr (x);
1158 mpz_clrbit (result->value.integer, pos);
1159 return range_check (result, "IBCLR");
1164 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1171 if (x->expr_type != EXPR_CONSTANT
1172 || y->expr_type != EXPR_CONSTANT
1173 || z->expr_type != EXPR_CONSTANT)
1176 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1178 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1179 return &gfc_bad_expr;
1182 if (gfc_extract_int (z, &len) != NULL || len < 0)
1184 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1185 return &gfc_bad_expr;
1188 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1190 bitsize = gfc_integer_kinds[k].bit_size;
1192 if (pos + len > bitsize)
1195 ("Sum of second and third arguments of IBITS exceeds bit size "
1196 "at %L", &y->where);
1197 return &gfc_bad_expr;
1200 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1202 bits = gfc_getmem (bitsize * sizeof (int));
1204 for (i = 0; i < bitsize; i++)
1207 for (i = 0; i < len; i++)
1208 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1210 for (i = 0; i < bitsize; i++)
1214 mpz_clrbit (result->value.integer, i);
1216 else if (bits[i] == 1)
1218 mpz_setbit (result->value.integer, i);
1222 gfc_internal_error ("IBITS: Bad bit");
1228 return range_check (result, "IBITS");
1233 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1238 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1241 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1243 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1244 return &gfc_bad_expr;
1247 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1249 if (pos > gfc_integer_kinds[k].bit_size)
1251 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1253 return &gfc_bad_expr;
1256 result = gfc_copy_expr (x);
1258 mpz_setbit (result->value.integer, pos);
1259 return range_check (result, "IBSET");
1264 gfc_simplify_ichar (gfc_expr * e)
1269 if (e->expr_type != EXPR_CONSTANT)
1272 if (e->value.character.length != 1)
1274 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1275 return &gfc_bad_expr;
1278 index = (int) e->value.character.string[0];
1280 if (index < CHAR_MIN || index > CHAR_MAX)
1282 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1284 return &gfc_bad_expr;
1287 result = gfc_int_expr (index);
1288 result->where = e->where;
1289 return range_check (result, "ICHAR");
1294 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1298 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1301 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1303 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1305 return range_check (result, "IEOR");
1310 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1313 int back, len, lensub;
1314 int i, j, k, count, index = 0, start;
1316 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1319 if (b != NULL && b->value.logical != 0)
1324 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1327 len = x->value.character.length;
1328 lensub = y->value.character.length;
1332 mpz_set_si (result->value.integer, 0);
1341 mpz_set_si (result->value.integer, 1);
1344 else if (lensub == 1)
1346 for (i = 0; i < len; i++)
1348 for (j = 0; j < lensub; j++)
1350 if (y->value.character.string[j] ==
1351 x->value.character.string[i])
1361 for (i = 0; i < len; i++)
1363 for (j = 0; j < lensub; j++)
1365 if (y->value.character.string[j] ==
1366 x->value.character.string[i])
1371 for (k = 0; k < lensub; k++)
1373 if (y->value.character.string[k] ==
1374 x->value.character.string[k + start])
1378 if (count == lensub)
1394 mpz_set_si (result->value.integer, len + 1);
1397 else if (lensub == 1)
1399 for (i = 0; i < len; i++)
1401 for (j = 0; j < lensub; j++)
1403 if (y->value.character.string[j] ==
1404 x->value.character.string[len - i])
1406 index = len - i + 1;
1414 for (i = 0; i < len; i++)
1416 for (j = 0; j < lensub; j++)
1418 if (y->value.character.string[j] ==
1419 x->value.character.string[len - i])
1422 if (start <= len - lensub)
1425 for (k = 0; k < lensub; k++)
1426 if (y->value.character.string[k] ==
1427 x->value.character.string[k + start])
1430 if (count == lensub)
1447 mpz_set_si (result->value.integer, index);
1448 return range_check (result, "INDEX");
1453 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1455 gfc_expr *rpart, *rtrunc, *result;
1458 kind = get_kind (BT_REAL, k, "INT", gfc_default_real_kind);
1460 return &gfc_bad_expr;
1462 if (e->expr_type != EXPR_CONSTANT)
1465 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1470 mpz_set (result->value.integer, e->value.integer);
1474 rtrunc = gfc_copy_expr (e);
1475 mpfr_trunc (rtrunc->value.real, e->value.real);
1476 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1477 gfc_free_expr (rtrunc);
1481 rpart = gfc_complex2real (e, kind);
1482 rtrunc = gfc_copy_expr (rpart);
1483 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1484 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1485 gfc_free_expr (rpart);
1486 gfc_free_expr (rtrunc);
1490 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1491 gfc_free_expr (result);
1492 return &gfc_bad_expr;
1495 return range_check (result, "INT");
1500 gfc_simplify_ifix (gfc_expr * e)
1502 gfc_expr *rtrunc, *result;
1504 if (e->expr_type != EXPR_CONSTANT)
1507 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1510 rtrunc = gfc_copy_expr (e);
1512 mpfr_trunc (rtrunc->value.real, e->value.real);
1513 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1515 gfc_free_expr (rtrunc);
1516 return range_check (result, "IFIX");
1521 gfc_simplify_idint (gfc_expr * e)
1523 gfc_expr *rtrunc, *result;
1525 if (e->expr_type != EXPR_CONSTANT)
1528 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1531 rtrunc = gfc_copy_expr (e);
1533 mpfr_trunc (rtrunc->value.real, e->value.real);
1534 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1536 gfc_free_expr (rtrunc);
1537 return range_check (result, "IDINT");
1542 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1546 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1549 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1551 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1552 return range_check (result, "IOR");
1557 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1560 int shift, ashift, isize, k;
1563 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1566 if (gfc_extract_int (s, &shift) != NULL)
1568 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1569 return &gfc_bad_expr;
1572 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1574 isize = gfc_integer_kinds[k].bit_size;
1584 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1586 return &gfc_bad_expr;
1589 e_int = mpz_get_si (e->value.integer);
1590 if (e_int > INT_MAX || e_int < INT_MIN)
1591 gfc_internal_error ("ISHFT: unable to extract integer");
1593 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1597 mpz_set (result->value.integer, e->value.integer);
1598 return range_check (result, "ISHFT");
1602 mpz_set_si (result->value.integer, e_int << shift);
1604 mpz_set_si (result->value.integer, e_int >> ashift);
1606 return range_check (result, "ISHFT");
1611 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1614 int shift, ashift, isize, delta, k;
1617 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1620 if (gfc_extract_int (s, &shift) != NULL)
1622 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1623 return &gfc_bad_expr;
1626 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1630 if (gfc_extract_int (sz, &isize) != NULL || isize < 0)
1632 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1633 return &gfc_bad_expr;
1637 isize = gfc_integer_kinds[k].bit_size;
1647 ("Magnitude of second argument of ISHFTC exceeds third argument "
1648 "at %L", &s->where);
1649 return &gfc_bad_expr;
1652 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1654 bits = gfc_getmem (isize * sizeof (int));
1656 for (i = 0; i < isize; i++)
1657 bits[i] = mpz_tstbit (e->value.integer, i);
1659 delta = isize - ashift;
1663 mpz_set (result->value.integer, e->value.integer);
1665 return range_check (result, "ISHFTC");
1670 for (i = 0; i < delta; i++)
1673 mpz_clrbit (result->value.integer, i + shift);
1675 mpz_setbit (result->value.integer, i + shift);
1678 for (i = delta; i < isize; i++)
1681 mpz_clrbit (result->value.integer, i - delta);
1683 mpz_setbit (result->value.integer, i - delta);
1687 return range_check (result, "ISHFTC");
1691 for (i = 0; i < ashift; i++)
1694 mpz_clrbit (result->value.integer, i + delta);
1696 mpz_setbit (result->value.integer, i + delta);
1699 for (i = ashift; i < isize; i++)
1702 mpz_clrbit (result->value.integer, i + shift);
1704 mpz_setbit (result->value.integer, i + shift);
1708 return range_check (result, "ISHFTC");
1714 gfc_simplify_kind (gfc_expr * e)
1717 if (e->ts.type == BT_DERIVED)
1719 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1720 return &gfc_bad_expr;
1723 return gfc_int_expr (e->ts.kind);
1728 gfc_simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1734 if (array->expr_type != EXPR_VARIABLE)
1740 if (dim->expr_type != EXPR_CONSTANT)
1743 /* Follow any component references. */
1744 as = array->symtree->n.sym->as;
1746 while (ref->next != NULL)
1748 if (ref->type == REF_COMPONENT)
1749 as = ref->u.c.sym->as;
1753 if (ref->type != REF_ARRAY || ref->u.ar.type != AR_FULL)
1756 i = mpz_get_si (dim->value.integer);
1758 return gfc_copy_expr (as->upper[i-1]);
1760 return gfc_copy_expr (as->lower[i-1]);
1765 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
1767 return gfc_simplify_bound (array, dim, 0);
1772 gfc_simplify_len (gfc_expr * e)
1776 if (e->expr_type != EXPR_CONSTANT)
1779 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1782 mpz_set_si (result->value.integer, e->value.character.length);
1783 return range_check (result, "LEN");
1788 gfc_simplify_len_trim (gfc_expr * e)
1791 int count, len, lentrim, i;
1793 if (e->expr_type != EXPR_CONSTANT)
1796 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1799 len = e->value.character.length;
1801 for (count = 0, i = 1; i <= len; i++)
1802 if (e->value.character.string[len - i] == ' ')
1807 lentrim = len - count;
1809 mpz_set_si (result->value.integer, lentrim);
1810 return range_check (result, "LEN_TRIM");
1815 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
1818 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1821 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
1827 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
1830 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1833 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
1839 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
1842 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1845 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
1851 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
1854 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1857 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
1863 gfc_simplify_log (gfc_expr * x)
1868 if (x->expr_type != EXPR_CONSTANT)
1871 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1873 gfc_set_model_kind (x->ts.kind);
1878 if (mpfr_sgn (x->value.real) <= 0)
1881 ("Argument of LOG at %L cannot be less than or equal to zero",
1883 gfc_free_expr (result);
1884 return &gfc_bad_expr;
1887 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
1891 if ((mpfr_sgn (x->value.complex.r) == 0)
1892 && (mpfr_sgn (x->value.complex.i) == 0))
1894 gfc_error ("Complex argument of LOG at %L cannot be zero",
1896 gfc_free_expr (result);
1897 return &gfc_bad_expr;
1903 arctangent2 (x->value.complex.i, x->value.complex.r,
1904 result->value.complex.i);
1906 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1907 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1908 mpfr_add (xr, xr, xi, GFC_RND_MODE);
1909 mpfr_sqrt (xr, xr, GFC_RND_MODE);
1910 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
1918 gfc_internal_error ("gfc_simplify_log: bad type");
1921 return range_check (result, "LOG");
1926 gfc_simplify_log10 (gfc_expr * x)
1930 if (x->expr_type != EXPR_CONSTANT)
1933 gfc_set_model_kind (x->ts.kind);
1935 if (mpfr_sgn (x->value.real) <= 0)
1938 ("Argument of LOG10 at %L cannot be less than or equal to zero",
1940 return &gfc_bad_expr;
1943 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1945 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
1947 return range_check (result, "LOG10");
1952 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
1957 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
1959 return &gfc_bad_expr;
1961 if (e->expr_type != EXPR_CONSTANT)
1964 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
1966 result->value.logical = e->value.logical;
1972 /* This function is special since MAX() can take any number of
1973 arguments. The simplified expression is a rewritten version of the
1974 argument list containing at most one constant element. Other
1975 constant elements are deleted. Because the argument list has
1976 already been checked, this function always succeeds. sign is 1 for
1977 MAX(), -1 for MIN(). */
1980 simplify_min_max (gfc_expr * expr, int sign)
1982 gfc_actual_arglist *arg, *last, *extremum;
1983 gfc_intrinsic_sym * specific;
1987 specific = expr->value.function.isym;
1989 arg = expr->value.function.actual;
1991 for (; arg; last = arg, arg = arg->next)
1993 if (arg->expr->expr_type != EXPR_CONSTANT)
1996 if (extremum == NULL)
2002 switch (arg->expr->ts.type)
2005 if (mpz_cmp (arg->expr->value.integer,
2006 extremum->expr->value.integer) * sign > 0)
2007 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2012 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2014 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2020 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2023 /* Delete the extra constant argument. */
2025 expr->value.function.actual = arg->next;
2027 last->next = arg->next;
2030 gfc_free_actual_arglist (arg);
2034 /* If there is one value left, replace the function call with the
2036 if (expr->value.function.actual->next != NULL)
2039 /* Convert to the correct type and kind. */
2040 if (expr->ts.type != BT_UNKNOWN)
2041 return gfc_convert_constant (expr->value.function.actual->expr,
2042 expr->ts.type, expr->ts.kind);
2044 if (specific->ts.type != BT_UNKNOWN)
2045 return gfc_convert_constant (expr->value.function.actual->expr,
2046 specific->ts.type, specific->ts.kind);
2048 return gfc_copy_expr (expr->value.function.actual->expr);
2053 gfc_simplify_min (gfc_expr * e)
2055 return simplify_min_max (e, -1);
2060 gfc_simplify_max (gfc_expr * e)
2062 return simplify_min_max (e, 1);
2067 gfc_simplify_maxexponent (gfc_expr * x)
2072 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2074 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2075 result->where = x->where;
2082 gfc_simplify_minexponent (gfc_expr * x)
2087 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2089 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2090 result->where = x->where;
2097 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2100 mpfr_t quot, iquot, term;
2102 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2105 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2110 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2112 /* Result is processor-dependent. */
2113 gfc_error ("Second argument MOD at %L is zero", &a->where);
2114 gfc_free_expr (result);
2115 return &gfc_bad_expr;
2117 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2121 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2123 /* Result is processor-dependent. */
2124 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2125 gfc_free_expr (result);
2126 return &gfc_bad_expr;
2129 gfc_set_model_kind (a->ts.kind);
2134 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2135 mpfr_trunc (iquot, quot);
2136 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2137 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2145 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2148 return range_check (result, "MOD");
2153 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2156 mpfr_t quot, iquot, term;
2158 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2161 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2166 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2168 /* Result is processor-dependent. This processor just opts
2169 to not handle it at all. */
2170 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2171 gfc_free_expr (result);
2172 return &gfc_bad_expr;
2174 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2179 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2181 /* Result is processor-dependent. */
2182 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2183 gfc_free_expr (result);
2184 return &gfc_bad_expr;
2187 gfc_set_model_kind (a->ts.kind);
2192 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2193 mpfr_floor (iquot, quot);
2194 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2200 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2204 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2207 return range_check (result, "MODULO");
2211 /* Exists for the sole purpose of consistency with other intrinsics. */
2213 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2214 gfc_expr * fp ATTRIBUTE_UNUSED,
2215 gfc_expr * l ATTRIBUTE_UNUSED,
2216 gfc_expr * to ATTRIBUTE_UNUSED,
2217 gfc_expr * tp ATTRIBUTE_UNUSED)
2224 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2229 int p, i, k, match_float;
2231 /* FIXME: This implementation is dopey and probably not quite right,
2232 but it's a start. */
2234 if (x->expr_type != EXPR_CONSTANT)
2237 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2239 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2241 val = mpfr_get_d (x->value.real, GFC_RND_MODE);
2242 p = gfc_real_kinds[k].digits;
2245 for (i = 1; i < p; ++i)
2250 /* TODO we should make sure that 'float' matches kind 4 */
2251 match_float = gfc_real_kinds[k].kind == 4;
2252 if (mpfr_cmp_ui (s->value.real, 0) > 0)
2258 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2263 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2266 else if (mpfr_cmp_ui (s->value.real, 0) < 0)
2272 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2277 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2282 gfc_error ("Invalid second argument of NEAREST at %L", &s->where);
2284 return &gfc_bad_expr;
2287 return range_check (result, "NEAREST");
2292 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2294 gfc_expr *rtrunc, *itrunc, *result;
2298 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2300 return &gfc_bad_expr;
2302 if (e->expr_type != EXPR_CONSTANT)
2305 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2307 rtrunc = gfc_copy_expr (e);
2308 itrunc = gfc_copy_expr (e);
2310 cmp = mpfr_cmp_ui (e->value.real, 0);
2312 gfc_set_model (e->value.real);
2314 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
2318 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2319 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2323 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2324 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2327 mpfr_set_ui (itrunc->value.real, 0, GFC_RND_MODE);
2329 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2331 gfc_free_expr (itrunc);
2332 gfc_free_expr (rtrunc);
2335 return range_check (result, name);
2340 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2342 return simplify_nint ("NINT", e, k);
2347 gfc_simplify_idnint (gfc_expr * e)
2349 return simplify_nint ("IDNINT", e, NULL);
2354 gfc_simplify_not (gfc_expr * e)
2359 if (e->expr_type != EXPR_CONSTANT)
2362 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2364 mpz_com (result->value.integer, e->value.integer);
2366 /* Because of how GMP handles numbers, the result must be ANDed with
2367 the max_int mask. For radices <> 2, this will require change. */
2369 i = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2371 mpz_and (result->value.integer, result->value.integer,
2372 gfc_integer_kinds[i].max_int);
2374 return range_check (result, "NOT");
2379 gfc_simplify_null (gfc_expr * mold)
2383 result = gfc_get_expr ();
2384 result->expr_type = EXPR_NULL;
2387 result->ts.type = BT_UNKNOWN;
2390 result->ts = mold->ts;
2391 result->where = mold->where;
2399 gfc_simplify_precision (gfc_expr * e)
2404 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2406 result = gfc_int_expr (gfc_real_kinds[i].precision);
2407 result->where = e->where;
2414 gfc_simplify_radix (gfc_expr * e)
2419 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2423 i = gfc_integer_kinds[i].radix;
2427 i = gfc_real_kinds[i].radix;
2434 result = gfc_int_expr (i);
2435 result->where = e->where;
2442 gfc_simplify_range (gfc_expr * e)
2448 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2453 j = gfc_integer_kinds[i].range;
2458 j = gfc_real_kinds[i].range;
2465 result = gfc_int_expr (j);
2466 result->where = e->where;
2473 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2478 if (e->ts.type == BT_COMPLEX)
2479 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2481 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2484 return &gfc_bad_expr;
2486 if (e->expr_type != EXPR_CONSTANT)
2492 result = gfc_int2real (e, kind);
2496 result = gfc_real2real (e, kind);
2500 result = gfc_complex2real (e, kind);
2504 gfc_internal_error ("bad type in REAL");
2508 return range_check (result, "REAL");
2512 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2515 int i, j, len, ncopies, nlen;
2517 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2520 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2522 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2523 return &gfc_bad_expr;
2526 len = e->value.character.length;
2527 nlen = ncopies * len;
2529 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2533 result->value.character.string = gfc_getmem (1);
2534 result->value.character.length = 0;
2535 result->value.character.string[0] = '\0';
2539 result->value.character.length = nlen;
2540 result->value.character.string = gfc_getmem (nlen + 1);
2542 for (i = 0; i < ncopies; i++)
2543 for (j = 0; j < len; j++)
2544 result->value.character.string[j + i * len] =
2545 e->value.character.string[j];
2547 result->value.character.string[nlen] = '\0'; /* For debugger */
2552 /* This one is a bear, but mainly has to do with shuffling elements. */
2555 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2556 gfc_expr * pad, gfc_expr * order_exp)
2559 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2560 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2561 gfc_constructor *head, *tail;
2567 /* Unpack the shape array. */
2568 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2571 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2575 && (pad->expr_type != EXPR_ARRAY
2576 || !gfc_is_constant_expr (pad)))
2579 if (order_exp != NULL
2580 && (order_exp->expr_type != EXPR_ARRAY
2581 || !gfc_is_constant_expr (order_exp)))
2590 e = gfc_get_array_element (shape_exp, rank);
2594 if (gfc_extract_int (e, &shape[rank]) != NULL)
2596 gfc_error ("Integer too large in shape specification at %L",
2604 if (rank >= GFC_MAX_DIMENSIONS)
2606 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2607 "at %L", &e->where);
2612 if (shape[rank] < 0)
2614 gfc_error ("Shape specification at %L cannot be negative",
2624 gfc_error ("Shape specification at %L cannot be the null array",
2629 /* Now unpack the order array if present. */
2630 if (order_exp == NULL)
2632 for (i = 0; i < rank; i++)
2639 for (i = 0; i < rank; i++)
2642 for (i = 0; i < rank; i++)
2644 e = gfc_get_array_element (order_exp, i);
2648 ("ORDER parameter of RESHAPE at %L is not the same size "
2649 "as SHAPE parameter", &order_exp->where);
2653 if (gfc_extract_int (e, &order[i]) != NULL)
2655 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2663 if (order[i] < 1 || order[i] > rank)
2665 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2674 gfc_error ("Invalid permutation in ORDER parameter at %L",
2683 /* Count the elements in the source and padding arrays. */
2688 gfc_array_size (pad, &size);
2689 npad = mpz_get_ui (size);
2693 gfc_array_size (source, &size);
2694 nsource = mpz_get_ui (size);
2697 /* If it weren't for that pesky permutation we could just loop
2698 through the source and round out any shortage with pad elements.
2699 But no, someone just had to have the compiler do something the
2700 user should be doing. */
2702 for (i = 0; i < rank; i++)
2707 /* Figure out which element to extract. */
2708 mpz_set_ui (index, 0);
2710 for (i = rank - 1; i >= 0; i--)
2712 mpz_add_ui (index, index, x[order[i]]);
2714 mpz_mul_ui (index, index, shape[order[i - 1]]);
2717 if (mpz_cmp_ui (index, INT_MAX) > 0)
2718 gfc_internal_error ("Reshaped array too large at %L", &e->where);
2720 j = mpz_get_ui (index);
2723 e = gfc_get_array_element (source, j);
2731 ("PAD parameter required for short SOURCE parameter at %L",
2737 e = gfc_get_array_element (pad, j);
2741 head = tail = gfc_get_constructor ();
2744 tail->next = gfc_get_constructor ();
2751 tail->where = e->where;
2754 /* Calculate the next element. */
2758 if (++x[i] < shape[i])
2769 e = gfc_get_expr ();
2770 e->where = source->where;
2771 e->expr_type = EXPR_ARRAY;
2772 e->value.constructor = head;
2773 e->shape = gfc_get_shape (rank);
2775 for (i = 0; i < rank; i++)
2776 mpz_init_set_ui (e->shape[i], shape[i]);
2778 e->ts = head->expr->ts;
2784 gfc_free_constructor (head);
2786 return &gfc_bad_expr;
2791 gfc_simplify_rrspacing (gfc_expr * x)
2794 mpfr_t absv, log2, exp, frac, pow2;
2797 if (x->expr_type != EXPR_CONSTANT)
2800 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2802 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2804 p = gfc_real_kinds[i].digits;
2806 gfc_set_model_kind (x->ts.kind);
2808 if (mpfr_sgn (x->value.real) == 0)
2810 mpfr_ui_div (result->value.real, 1, gfc_real_kinds[i].tiny, GFC_RND_MODE);
2819 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2820 mpfr_log2 (log2, absv, GFC_RND_MODE);
2822 mpfr_trunc (log2, log2);
2823 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
2825 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2826 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
2828 mpfr_mul_2exp (result->value.real, frac, (unsigned long)p, GFC_RND_MODE);
2835 return range_check (result, "RRSPACING");
2840 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
2842 int k, neg_flag, power, exp_range;
2843 mpfr_t scale, radix;
2846 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
2849 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2851 if (mpfr_sgn (x->value.real) == 0)
2853 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2857 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2859 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
2861 /* This check filters out values of i that would overflow an int. */
2862 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
2863 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
2865 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
2866 return &gfc_bad_expr;
2869 /* Compute scale = radix ** power. */
2870 power = mpz_get_si (i->value.integer);
2880 gfc_set_model_kind (x->ts.kind);
2883 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
2884 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
2887 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
2889 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
2894 return range_check (result, "SCALE");
2899 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
2904 size_t indx, len, lenc;
2906 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
2909 if (b != NULL && b->value.logical != 0)
2914 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2917 len = e->value.character.length;
2918 lenc = c->value.character.length;
2920 if (len == 0 || lenc == 0)
2929 strcspn (e->value.character.string, c->value.character.string) + 1;
2936 for (indx = len; indx > 0; indx--)
2938 for (i = 0; i < lenc; i++)
2940 if (c->value.character.string[i]
2941 == e->value.character.string[indx - 1])
2949 mpz_set_ui (result->value.integer, indx);
2950 return range_check (result, "SCAN");
2955 gfc_simplify_selected_int_kind (gfc_expr * e)
2960 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
2965 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
2966 if (gfc_integer_kinds[i].range >= range
2967 && gfc_integer_kinds[i].kind < kind)
2968 kind = gfc_integer_kinds[i].kind;
2970 if (kind == INT_MAX)
2973 result = gfc_int_expr (kind);
2974 result->where = e->where;
2981 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
2983 int range, precision, i, kind, found_precision, found_range;
2990 if (p->expr_type != EXPR_CONSTANT
2991 || gfc_extract_int (p, &precision) != NULL)
2999 if (q->expr_type != EXPR_CONSTANT
3000 || gfc_extract_int (q, &range) != NULL)
3005 found_precision = 0;
3008 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3010 if (gfc_real_kinds[i].precision >= precision)
3011 found_precision = 1;
3013 if (gfc_real_kinds[i].range >= range)
3016 if (gfc_real_kinds[i].precision >= precision
3017 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3018 kind = gfc_real_kinds[i].kind;
3021 if (kind == INT_MAX)
3025 if (!found_precision)
3031 result = gfc_int_expr (kind);
3032 result->where = (p != NULL) ? p->where : q->where;
3039 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3042 mpfr_t exp, absv, log2, pow2, frac;
3045 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3048 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3050 gfc_set_model_kind (x->ts.kind);
3052 if (mpfr_sgn (x->value.real) == 0)
3054 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3064 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3065 mpfr_log2 (log2, absv, GFC_RND_MODE);
3067 mpfr_trunc (log2, log2);
3068 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3070 /* Old exponent value, and fraction. */
3071 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3073 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3076 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3077 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3084 return range_check (result, "SET_EXPONENT");
3089 gfc_simplify_shape (gfc_expr * source)
3091 mpz_t shape[GFC_MAX_DIMENSIONS];
3092 gfc_expr *result, *e, *f;
3097 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3100 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3103 ar = gfc_find_array_ref (source);
3105 t = gfc_array_ref_shape (ar, shape);
3107 for (n = 0; n < source->rank; n++)
3109 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3114 mpz_set (e->value.integer, shape[n]);
3115 mpz_clear (shape[n]);
3119 mpz_set_ui (e->value.integer, n + 1);
3121 f = gfc_simplify_size (source, e);
3125 gfc_free_expr (result);
3134 gfc_append_constructor (result, e);
3142 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3150 if (gfc_array_size (array, &size) == FAILURE)
3155 if (dim->expr_type != EXPR_CONSTANT)
3158 d = mpz_get_ui (dim->value.integer) - 1;
3159 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3163 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3166 mpz_set (result->value.integer, size);
3173 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3177 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3180 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3185 mpz_abs (result->value.integer, x->value.integer);
3186 if (mpz_sgn (y->value.integer) < 0)
3187 mpz_neg (result->value.integer, result->value.integer);
3192 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3194 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3195 if (mpfr_sgn (y->value.real) < 0)
3196 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3201 gfc_internal_error ("Bad type in gfc_simplify_sign");
3209 gfc_simplify_sin (gfc_expr * x)
3214 if (x->expr_type != EXPR_CONSTANT)
3217 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3222 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3226 gfc_set_model (x->value.real);
3230 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3231 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3232 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3234 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3235 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3236 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3243 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3246 return range_check (result, "SIN");
3251 gfc_simplify_sinh (gfc_expr * x)
3255 if (x->expr_type != EXPR_CONSTANT)
3258 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3260 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3262 return range_check (result, "SINH");
3266 /* The argument is always a double precision real that is converted to
3267 single precision. TODO: Rounding! */
3270 gfc_simplify_sngl (gfc_expr * a)
3274 if (a->expr_type != EXPR_CONSTANT)
3277 result = gfc_real2real (a, gfc_default_real_kind);
3278 return range_check (result, "SNGL");
3283 gfc_simplify_spacing (gfc_expr * x)
3290 if (x->expr_type != EXPR_CONSTANT)
3293 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3295 p = gfc_real_kinds[i].digits;
3297 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3299 gfc_set_model_kind (x->ts.kind);
3301 if (mpfr_sgn (x->value.real) == 0)
3303 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3310 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3311 mpfr_log2 (log2, absv, GFC_RND_MODE);
3312 mpfr_trunc (log2, log2);
3314 mpfr_add_ui (log2, log2, 1, GFC_RND_MODE);
3316 /* FIXME: We should be using mpfr_get_si here, but this function is
3317 not available with the version of mpfr distributed with gmp (as of
3318 2004-09-17). Replace once mpfr has been imported into the gcc cvs
3320 diff = (long)mpfr_get_d (log2, GFC_RND_MODE) - (long)p;
3321 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3322 mpfr_mul_2si (result->value.real, result->value.real, diff, GFC_RND_MODE);
3327 if (mpfr_cmp (result->value.real, gfc_real_kinds[i].tiny) < 0)
3328 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3330 return range_check (result, "SPACING");
3335 gfc_simplify_sqrt (gfc_expr * e)
3338 mpfr_t ac, ad, s, t, w;
3340 if (e->expr_type != EXPR_CONSTANT)
3343 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3348 if (mpfr_cmp_si (e->value.real, 0) < 0)
3350 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3355 /* Formula taken from Numerical Recipes to avoid over- and
3358 gfc_set_model (e->value.real);
3365 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3366 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3369 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3370 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3374 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3375 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3377 if (mpfr_cmp (ac, ad) >= 0)
3379 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3380 mpfr_mul (t, t, t, GFC_RND_MODE);
3381 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3382 mpfr_sqrt (t, t, GFC_RND_MODE);
3383 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3384 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3385 mpfr_sqrt (t, t, GFC_RND_MODE);
3386 mpfr_sqrt (s, ac, GFC_RND_MODE);
3387 mpfr_mul (w, s, t, GFC_RND_MODE);
3391 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3392 mpfr_mul (t, s, s, GFC_RND_MODE);
3393 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3394 mpfr_sqrt (t, t, GFC_RND_MODE);
3395 mpfr_abs (s, s, GFC_RND_MODE);
3396 mpfr_add (t, t, s, GFC_RND_MODE);
3397 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3398 mpfr_sqrt (t, t, GFC_RND_MODE);
3399 mpfr_sqrt (s, ad, GFC_RND_MODE);
3400 mpfr_mul (w, s, t, GFC_RND_MODE);
3403 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3405 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3406 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3407 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3409 else if (mpfr_cmp_ui (w, 0) != 0
3410 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3411 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3413 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3414 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3415 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3417 else if (mpfr_cmp_ui (w, 0) != 0
3418 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3419 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3421 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3422 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3423 mpfr_neg (w, w, GFC_RND_MODE);
3424 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3427 gfc_internal_error ("invalid complex argument of SQRT at %L",
3439 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3442 return range_check (result, "SQRT");
3445 gfc_free_expr (result);
3446 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3447 return &gfc_bad_expr;
3452 gfc_simplify_tan (gfc_expr * x)
3457 if (x->expr_type != EXPR_CONSTANT)
3460 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3462 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3464 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3466 return range_check (result, "TAN");
3471 gfc_simplify_tanh (gfc_expr * x)
3475 if (x->expr_type != EXPR_CONSTANT)
3478 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3480 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3482 return range_check (result, "TANH");
3488 gfc_simplify_tiny (gfc_expr * e)
3493 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3495 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3496 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3503 gfc_simplify_trim (gfc_expr * e)
3506 int count, i, len, lentrim;
3508 if (e->expr_type != EXPR_CONSTANT)
3511 len = e->value.character.length;
3513 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3515 for (count = 0, i = 1; i <= len; ++i)
3517 if (e->value.character.string[len - i] == ' ')
3523 lentrim = len - count;
3525 result->value.character.length = lentrim;
3526 result->value.character.string = gfc_getmem (lentrim + 1);
3528 for (i = 0; i < lentrim; i++)
3529 result->value.character.string[i] = e->value.character.string[i];
3531 result->value.character.string[lentrim] = '\0'; /* For debugger */
3538 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3540 return gfc_simplify_bound (array, dim, 1);
3545 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3549 size_t index, len, lenset;
3552 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3555 if (b != NULL && b->value.logical != 0)
3560 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3563 len = s->value.character.length;
3564 lenset = set->value.character.length;
3568 mpz_set_ui (result->value.integer, 0);
3576 mpz_set_ui (result->value.integer, len);
3581 strspn (s->value.character.string, set->value.character.string) + 1;
3590 mpz_set_ui (result->value.integer, 1);
3593 for (index = len; index > 0; index --)
3595 for (i = 0; i < lenset; i++)
3597 if (s->value.character.string[index - 1]
3598 == set->value.character.string[i])
3606 mpz_set_ui (result->value.integer, index);
3610 /****************** Constant simplification *****************/
3612 /* Master function to convert one constant to another. While this is
3613 used as a simplification function, it requires the destination type
3614 and kind information which is supplied by a special case in
3618 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3620 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3621 gfc_constructor *head, *c, *tail = NULL;
3635 f = gfc_int2complex;
3652 f = gfc_real2complex;
3663 f = gfc_complex2int;
3666 f = gfc_complex2real;
3669 f = gfc_complex2complex;
3678 if (type != BT_LOGICAL)
3685 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3690 switch (e->expr_type)
3693 result = f (e, kind);
3695 return &gfc_bad_expr;
3699 if (!gfc_is_constant_expr (e))
3704 for (c = e->value.constructor; c; c = c->next)
3707 head = tail = gfc_get_constructor ();
3710 tail->next = gfc_get_constructor ();
3714 tail->where = c->where;
3716 if (c->iterator == NULL)
3717 tail->expr = f (c->expr, kind);
3720 g = gfc_convert_constant (c->expr, type, kind);
3721 if (g == &gfc_bad_expr)
3726 if (tail->expr == NULL)
3728 gfc_free_constructor (head);
3733 result = gfc_get_expr ();
3734 result->ts.type = type;
3735 result->ts.kind = kind;
3736 result->expr_type = EXPR_ARRAY;
3737 result->value.constructor = head;
3738 result->shape = gfc_copy_shape (e->shape, e->rank);
3739 result->where = e->where;
3740 result->rank = e->rank;
3751 /****************** Helper functions ***********************/
3753 /* Given a collating table, create the inverse table. */
3756 invert_table (const int *table, int *xtable)
3760 for (i = 0; i < 256; i++)
3763 for (i = 0; i < 256; i++)
3764 xtable[table[i]] = i;
3769 gfc_simplify_init_1 (void)
3772 invert_table (ascii_table, xascii_table);