1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #include "intrinsic.h"
33 gfc_expr gfc_bad_expr;
36 /* Note that 'simplification' is not just transforming expressions.
37 For functions that are not simplified at compile time, range
38 checking is done if possible.
40 The return convention is that each simplification function returns:
42 A new expression node corresponding to the simplified arguments.
43 The original arguments are destroyed by the caller, and must not
44 be a part of the new expression.
46 NULL pointer indicating that no simplification was possible and
47 the original expression should remain intact. If the
48 simplification function sets the type and/or the function name
49 via the pointer gfc_simple_expression, then this type is
52 An expression pointer to gfc_bad_expr (a static placeholder)
53 indicating that some error has prevented simplification. For
54 example, sqrt(-1.0). The error is generated within the function
55 and should be propagated upwards
57 By the time a simplification function gets control, it has been
58 decided that the function call is really supposed to be the
59 intrinsic. No type checking is strictly necessary, since only
60 valid types will be passed on. On the other hand, a simplification
61 subroutine may have to look at the type of an argument as part of
64 Array arguments are never passed to these subroutines.
66 The functions in this file don't have much comment with them, but
67 everything is reasonably straight-forward. The Standard, chapter 13
68 is the best comment you'll find for this file anyway. */
70 /* Static table for converting non-ascii character sets to ascii.
71 The xascii_table[] is the inverse table. */
73 static int ascii_table[256] = {
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
76 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
77 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
78 ' ', '!', '\'', '#', '$', '%', '&', '\'',
79 '(', ')', '*', '+', ',', '-', '.', '/',
80 '0', '1', '2', '3', '4', '5', '6', '7',
81 '8', '9', ':', ';', '<', '=', '>', '?',
82 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
83 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
84 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
85 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
86 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
87 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
88 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
89 'x', 'y', 'z', '{', '|', '}', '~', '\?'
92 static int xascii_table[256];
95 /* Range checks an expression node. If all goes well, returns the
96 node, otherwise returns &gfc_bad_expr and frees the node. */
99 range_check (gfc_expr * result, const char *name)
102 if (gfc_range_check (result) == ARITH_OK)
105 gfc_error ("Result of %s overflows its kind at %L", name, &result->where);
106 gfc_free_expr (result);
107 return &gfc_bad_expr;
111 /* A helper function that gets an optional and possibly missing
112 kind parameter. Returns the kind, -1 if something went wrong. */
115 get_kind (bt type, gfc_expr * k, const char *name, int default_kind)
122 if (k->expr_type != EXPR_CONSTANT)
124 gfc_error ("KIND parameter of %s at %L must be an initialization "
125 "expression", name, &k->where);
130 if (gfc_extract_int (k, &kind) != NULL
131 || gfc_validate_kind (type, kind) == -1)
134 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
142 /********************** Simplification functions *****************************/
145 gfc_simplify_abs (gfc_expr * e)
150 if (e->expr_type != EXPR_CONSTANT)
156 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
158 mpz_abs (result->value.integer, e->value.integer);
160 result = range_check (result, "IABS");
164 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
166 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
168 result = range_check (result, "ABS");
172 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
174 gfc_set_model_kind (e->ts.kind);
177 /* FIXME: Possible numerical problems. */
178 mpfr_mul (a, e->value.complex.r, e->value.complex.r, GFC_RND_MODE);
179 mpfr_mul (b, e->value.complex.i, e->value.complex.i, GFC_RND_MODE);
180 mpfr_add (a, a, b, GFC_RND_MODE);
181 mpfr_sqrt (result->value.real, a, GFC_RND_MODE);
186 result = range_check (result, "CABS");
190 gfc_internal_error ("gfc_simplify_abs(): Bad type");
198 gfc_simplify_achar (gfc_expr * e)
203 if (e->expr_type != EXPR_CONSTANT)
206 /* We cannot assume that the native character set is ASCII in this
208 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
210 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
211 "must be between 0 and 127", &e->where);
212 return &gfc_bad_expr;
215 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
218 result->value.character.string = gfc_getmem (2);
220 result->value.character.length = 1;
221 result->value.character.string[0] = ascii_table[index];
222 result->value.character.string[1] = '\0'; /* For debugger */
228 gfc_simplify_acos (gfc_expr * x)
232 if (x->expr_type != EXPR_CONSTANT)
235 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
237 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
239 return &gfc_bad_expr;
242 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
244 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
246 return range_check (result, "ACOS");
251 gfc_simplify_adjustl (gfc_expr * e)
257 if (e->expr_type != EXPR_CONSTANT)
260 len = e->value.character.length;
262 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
264 result->value.character.length = len;
265 result->value.character.string = gfc_getmem (len + 1);
267 for (count = 0, i = 0; i < len; ++i)
269 ch = e->value.character.string[i];
275 for (i = 0; i < len - count; ++i)
277 result->value.character.string[i] =
278 e->value.character.string[count + i];
281 for (i = len - count; i < len; ++i)
283 result->value.character.string[i] = ' ';
286 result->value.character.string[len] = '\0'; /* For debugger */
293 gfc_simplify_adjustr (gfc_expr * e)
299 if (e->expr_type != EXPR_CONSTANT)
302 len = e->value.character.length;
304 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
306 result->value.character.length = len;
307 result->value.character.string = gfc_getmem (len + 1);
309 for (count = 0, i = len - 1; i >= 0; --i)
311 ch = e->value.character.string[i];
317 for (i = 0; i < count; ++i)
319 result->value.character.string[i] = ' ';
322 for (i = count; i < len; ++i)
324 result->value.character.string[i] =
325 e->value.character.string[i - count];
328 result->value.character.string[len] = '\0'; /* For debugger */
335 gfc_simplify_aimag (gfc_expr * e)
339 if (e->expr_type != EXPR_CONSTANT)
342 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
343 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
345 return range_check (result, "AIMAG");
350 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
352 gfc_expr *rtrunc, *result;
355 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
357 return &gfc_bad_expr;
359 if (e->expr_type != EXPR_CONSTANT)
362 rtrunc = gfc_copy_expr (e);
364 mpfr_trunc (rtrunc->value.real, e->value.real);
366 result = gfc_real2real (rtrunc, kind);
367 gfc_free_expr (rtrunc);
369 return range_check (result, "AINT");
374 gfc_simplify_dint (gfc_expr * e)
376 gfc_expr *rtrunc, *result;
378 if (e->expr_type != EXPR_CONSTANT)
381 rtrunc = gfc_copy_expr (e);
383 mpfr_trunc (rtrunc->value.real, e->value.real);
385 result = gfc_real2real (rtrunc, gfc_default_double_kind ());
386 gfc_free_expr (rtrunc);
388 return range_check (result, "DINT");
394 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
396 gfc_expr *rtrunc, *result;
400 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
402 return &gfc_bad_expr;
404 if (e->expr_type != EXPR_CONSTANT)
407 result = gfc_constant_result (e->ts.type, kind, &e->where);
409 rtrunc = gfc_copy_expr (e);
411 cmp = mpfr_cmp_ui (e->value.real, 0);
413 gfc_set_model_kind (kind);
415 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
419 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
420 mpfr_trunc (result->value.real, rtrunc->value.real);
424 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
425 mpfr_trunc (result->value.real, rtrunc->value.real);
428 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
430 gfc_free_expr (rtrunc);
433 return range_check (result, "ANINT");
438 gfc_simplify_dnint (gfc_expr * e)
440 gfc_expr *rtrunc, *result;
444 if (e->expr_type != EXPR_CONSTANT)
448 gfc_constant_result (BT_REAL, gfc_default_double_kind (), &e->where);
450 rtrunc = gfc_copy_expr (e);
452 cmp = mpfr_cmp_ui (e->value.real, 0);
454 gfc_set_model_kind (gfc_default_double_kind ());
456 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
460 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
461 mpfr_trunc (result->value.real, rtrunc->value.real);
465 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
466 mpfr_trunc (result->value.real, rtrunc->value.real);
469 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
471 gfc_free_expr (rtrunc);
474 return range_check (result, "DNINT");
479 gfc_simplify_asin (gfc_expr * x)
483 if (x->expr_type != EXPR_CONSTANT)
486 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
488 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
490 return &gfc_bad_expr;
493 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
495 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
497 return range_check (result, "ASIN");
502 gfc_simplify_atan (gfc_expr * x)
506 if (x->expr_type != EXPR_CONSTANT)
509 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
511 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
513 return range_check (result, "ATAN");
519 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
523 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
526 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
528 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
531 ("If first argument of ATAN2 %L is zero, then the second argument "
532 "must not be zero", &x->where);
533 gfc_free_expr (result);
534 return &gfc_bad_expr;
537 arctangent2 (y->value.real, x->value.real, result->value.real);
539 return range_check (result, "ATAN2");
545 gfc_simplify_bit_size (gfc_expr * e)
550 i = gfc_validate_kind (e->ts.type, e->ts.kind);
552 gfc_internal_error ("In gfc_simplify_bit_size(): Bad kind");
554 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
555 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
562 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
566 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
569 if (gfc_extract_int (bit, &b) != NULL || b < 0)
570 return gfc_logical_expr (0, &e->where);
572 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
577 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
579 gfc_expr *ceil, *result;
582 kind = get_kind (BT_REAL, k, "CEILING", gfc_default_real_kind ());
584 return &gfc_bad_expr;
586 if (e->expr_type != EXPR_CONSTANT)
589 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
591 ceil = gfc_copy_expr (e);
593 mpfr_ceil (ceil->value.real, e->value.real);
594 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
596 gfc_free_expr (ceil);
598 return range_check (result, "CEILING");
603 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
608 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind ());
610 return &gfc_bad_expr;
612 if (e->expr_type != EXPR_CONSTANT)
615 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > 255)
617 gfc_error ("Bad character in CHAR function at %L", &e->where);
618 return &gfc_bad_expr;
621 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
623 result->value.character.length = 1;
624 result->value.character.string = gfc_getmem (2);
626 result->value.character.string[0] = c;
627 result->value.character.string[1] = '\0'; /* For debugger */
633 /* Common subroutine for simplifying CMPLX and DCMPLX. */
636 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
640 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
642 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
647 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
651 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
655 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
656 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
660 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
668 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
672 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
676 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
680 return range_check (result, name);
685 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
689 if (x->expr_type != EXPR_CONSTANT
690 || (y != NULL && y->expr_type != EXPR_CONSTANT))
693 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind ());
695 return &gfc_bad_expr;
697 return simplify_cmplx ("CMPLX", x, y, kind);
702 gfc_simplify_conjg (gfc_expr * e)
706 if (e->expr_type != EXPR_CONSTANT)
709 result = gfc_copy_expr (e);
710 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
712 return range_check (result, "CONJG");
717 gfc_simplify_cos (gfc_expr * x)
722 if (x->expr_type != EXPR_CONSTANT)
725 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
730 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
733 gfc_set_model_kind (x->ts.kind);
737 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
738 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
739 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
741 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
742 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
743 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
744 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
750 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
753 return range_check (result, "COS");
759 gfc_simplify_cosh (gfc_expr * x)
763 if (x->expr_type != EXPR_CONSTANT)
766 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
768 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
770 return range_check (result, "COSH");
775 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
778 if (x->expr_type != EXPR_CONSTANT
779 || (y != NULL && y->expr_type != EXPR_CONSTANT))
782 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind ());
787 gfc_simplify_dble (gfc_expr * e)
791 if (e->expr_type != EXPR_CONSTANT)
797 result = gfc_int2real (e, gfc_default_double_kind ());
801 result = gfc_real2real (e, gfc_default_double_kind ());
805 result = gfc_complex2real (e, gfc_default_double_kind ());
809 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
812 return range_check (result, "DBLE");
817 gfc_simplify_digits (gfc_expr * x)
821 i = gfc_validate_kind (x->ts.type, x->ts.kind);
828 digits = gfc_integer_kinds[i].digits;
833 digits = gfc_real_kinds[i].digits;
838 gfc_internal_error ("gfc_simplify_digits(): Bad type");
841 return gfc_int_expr (digits);
846 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
850 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
853 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
858 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
859 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
861 mpz_set_ui (result->value.integer, 0);
866 if (mpfr_cmp (x->value.real, y->value.real) > 0)
867 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
869 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
874 gfc_internal_error ("gfc_simplify_dim(): Bad type");
877 return range_check (result, "DIM");
882 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
884 gfc_expr *a1, *a2, *result;
886 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
890 gfc_constant_result (BT_REAL, gfc_default_double_kind (), &x->where);
892 a1 = gfc_real2real (x, gfc_default_double_kind ());
893 a2 = gfc_real2real (y, gfc_default_double_kind ());
895 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
900 return range_check (result, "DPROD");
905 gfc_simplify_epsilon (gfc_expr * e)
910 i = gfc_validate_kind (e->ts.type, e->ts.kind);
912 gfc_internal_error ("gfc_simplify_epsilon(): Bad kind");
914 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
916 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
918 return range_check (result, "EPSILON");
923 gfc_simplify_exp (gfc_expr * x)
928 if (x->expr_type != EXPR_CONSTANT)
931 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
936 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
940 gfc_set_model_kind (x->ts.kind);
943 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
944 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
945 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
946 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
947 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
953 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
956 return range_check (result, "EXP");
959 /* FIXME: MPFR should be able to do this better */
961 gfc_simplify_exponent (gfc_expr * x)
963 mpfr_t i2, absv, ln2, lnx, zero;
966 if (x->expr_type != EXPR_CONSTANT)
969 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
972 gfc_set_model (x->value.real);
974 mpfr_set_ui (zero, 0, GFC_RND_MODE);
976 if (mpfr_cmp (x->value.real, zero) == 0)
978 mpz_set_ui (result->value.integer, 0);
988 mpfr_set_ui (i2, 2, GFC_RND_MODE);
990 mpfr_log (ln2, i2, GFC_RND_MODE);
991 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
992 mpfr_log (lnx, absv, GFC_RND_MODE);
994 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
995 mpfr_trunc (lnx, lnx);
996 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
998 gfc_mpfr_to_mpz (result->value.integer, lnx);
1006 return range_check (result, "EXPONENT");
1011 gfc_simplify_float (gfc_expr * a)
1015 if (a->expr_type != EXPR_CONSTANT)
1018 result = gfc_int2real (a, gfc_default_real_kind ());
1019 return range_check (result, "FLOAT");
1024 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1030 kind = get_kind (BT_REAL, k, "FLOOR", gfc_default_real_kind ());
1032 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1034 if (e->expr_type != EXPR_CONSTANT)
1037 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1039 gfc_set_model_kind (kind);
1041 mpfr_floor (floor, e->value.real);
1043 gfc_mpfr_to_mpz (result->value.integer, floor);
1047 return range_check (result, "FLOOR");
1052 gfc_simplify_fraction (gfc_expr * x)
1055 mpfr_t i2, absv, ln2, lnx, pow2, zero;
1058 if (x->expr_type != EXPR_CONSTANT)
1061 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1063 gfc_set_model_kind (x->ts.kind);
1065 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1067 if (mpfr_cmp (x->value.real, zero) == 0)
1069 mpfr_set (result->value.real, zero, GFC_RND_MODE);
1080 mpfr_set_ui (i2, 2, GFC_RND_MODE);
1082 mpfr_log (ln2, i2, GFC_RND_MODE);
1083 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1084 mpfr_log (lnx, absv, GFC_RND_MODE);
1086 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
1087 mpfr_trunc (lnx, lnx);
1088 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
1090 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
1091 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
1093 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1102 return range_check (result, "FRACTION");
1107 gfc_simplify_huge (gfc_expr * e)
1112 i = gfc_validate_kind (e->ts.type, e->ts.kind);
1116 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1121 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1125 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1130 gfc_internal_error ("gfc_simplify_huge(): Bad type");
1138 gfc_simplify_iachar (gfc_expr * e)
1143 if (e->expr_type != EXPR_CONSTANT)
1146 if (e->value.character.length != 1)
1148 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1149 return &gfc_bad_expr;
1152 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1154 result = gfc_int_expr (index);
1155 result->where = e->where;
1157 return range_check (result, "IACHAR");
1162 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1166 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1169 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1171 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1173 return range_check (result, "IAND");
1178 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1183 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1186 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1188 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1189 return &gfc_bad_expr;
1192 k = gfc_validate_kind (x->ts.type, x->ts.kind);
1194 gfc_internal_error ("gfc_simplify_ibclr(): Bad kind");
1196 if (pos > gfc_integer_kinds[k].bit_size)
1198 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1200 return &gfc_bad_expr;
1203 result = gfc_copy_expr (x);
1205 mpz_clrbit (result->value.integer, pos);
1206 return range_check (result, "IBCLR");
1211 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1218 if (x->expr_type != EXPR_CONSTANT
1219 || y->expr_type != EXPR_CONSTANT
1220 || z->expr_type != EXPR_CONSTANT)
1223 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1225 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1226 return &gfc_bad_expr;
1229 if (gfc_extract_int (z, &len) != NULL || len < 0)
1231 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1232 return &gfc_bad_expr;
1235 k = gfc_validate_kind (BT_INTEGER, x->ts.kind);
1237 gfc_internal_error ("gfc_simplify_ibits(): Bad kind");
1239 bitsize = gfc_integer_kinds[k].bit_size;
1241 if (pos + len > bitsize)
1244 ("Sum of second and third arguments of IBITS exceeds bit size "
1245 "at %L", &y->where);
1246 return &gfc_bad_expr;
1249 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1251 bits = gfc_getmem (bitsize * sizeof (int));
1253 for (i = 0; i < bitsize; i++)
1256 for (i = 0; i < len; i++)
1257 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1259 for (i = 0; i < bitsize; i++)
1263 mpz_clrbit (result->value.integer, i);
1265 else if (bits[i] == 1)
1267 mpz_setbit (result->value.integer, i);
1271 gfc_internal_error ("IBITS: Bad bit");
1277 return range_check (result, "IBITS");
1282 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1287 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1290 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1292 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1293 return &gfc_bad_expr;
1296 k = gfc_validate_kind (x->ts.type, x->ts.kind);
1298 gfc_internal_error ("gfc_simplify_ibset(): Bad kind");
1300 if (pos > gfc_integer_kinds[k].bit_size)
1302 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1304 return &gfc_bad_expr;
1307 result = gfc_copy_expr (x);
1309 mpz_setbit (result->value.integer, pos);
1310 return range_check (result, "IBSET");
1315 gfc_simplify_ichar (gfc_expr * e)
1320 if (e->expr_type != EXPR_CONSTANT)
1323 if (e->value.character.length != 1)
1325 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1326 return &gfc_bad_expr;
1329 index = (int) e->value.character.string[0];
1331 if (index < CHAR_MIN || index > CHAR_MAX)
1333 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1335 return &gfc_bad_expr;
1338 result = gfc_int_expr (index);
1339 result->where = e->where;
1340 return range_check (result, "ICHAR");
1345 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1349 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1352 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1354 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1356 return range_check (result, "IEOR");
1361 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1364 int back, len, lensub;
1365 int i, j, k, count, index = 0, start;
1367 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1370 if (b != NULL && b->value.logical != 0)
1375 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1378 len = x->value.character.length;
1379 lensub = y->value.character.length;
1383 mpz_set_si (result->value.integer, 0);
1392 mpz_set_si (result->value.integer, 1);
1395 else if (lensub == 1)
1397 for (i = 0; i < len; i++)
1399 for (j = 0; j < lensub; j++)
1401 if (y->value.character.string[j] ==
1402 x->value.character.string[i])
1412 for (i = 0; i < len; i++)
1414 for (j = 0; j < lensub; j++)
1416 if (y->value.character.string[j] ==
1417 x->value.character.string[i])
1422 for (k = 0; k < lensub; k++)
1424 if (y->value.character.string[k] ==
1425 x->value.character.string[k + start])
1429 if (count == lensub)
1445 mpz_set_si (result->value.integer, len + 1);
1448 else if (lensub == 1)
1450 for (i = 0; i < len; i++)
1452 for (j = 0; j < lensub; j++)
1454 if (y->value.character.string[j] ==
1455 x->value.character.string[len - i])
1457 index = len - i + 1;
1465 for (i = 0; i < len; i++)
1467 for (j = 0; j < lensub; j++)
1469 if (y->value.character.string[j] ==
1470 x->value.character.string[len - i])
1473 if (start <= len - lensub)
1476 for (k = 0; k < lensub; k++)
1477 if (y->value.character.string[k] ==
1478 x->value.character.string[k + start])
1481 if (count == lensub)
1498 mpz_set_si (result->value.integer, index);
1499 return range_check (result, "INDEX");
1504 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1506 gfc_expr *rpart, *rtrunc, *result;
1509 kind = get_kind (BT_REAL, k, "INT", gfc_default_real_kind ());
1511 return &gfc_bad_expr;
1513 if (e->expr_type != EXPR_CONSTANT)
1516 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1521 mpz_set (result->value.integer, e->value.integer);
1525 rtrunc = gfc_copy_expr (e);
1526 mpfr_trunc (rtrunc->value.real, e->value.real);
1527 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1528 gfc_free_expr (rtrunc);
1532 rpart = gfc_complex2real (e, kind);
1533 rtrunc = gfc_copy_expr (rpart);
1534 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1535 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1536 gfc_free_expr (rpart);
1537 gfc_free_expr (rtrunc);
1541 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1542 gfc_free_expr (result);
1543 return &gfc_bad_expr;
1546 return range_check (result, "INT");
1551 gfc_simplify_ifix (gfc_expr * e)
1553 gfc_expr *rtrunc, *result;
1555 if (e->expr_type != EXPR_CONSTANT)
1558 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1561 rtrunc = gfc_copy_expr (e);
1563 mpfr_trunc (rtrunc->value.real, e->value.real);
1564 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1566 gfc_free_expr (rtrunc);
1567 return range_check (result, "IFIX");
1572 gfc_simplify_idint (gfc_expr * e)
1574 gfc_expr *rtrunc, *result;
1576 if (e->expr_type != EXPR_CONSTANT)
1579 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1582 rtrunc = gfc_copy_expr (e);
1584 mpfr_trunc (rtrunc->value.real, e->value.real);
1585 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1587 gfc_free_expr (rtrunc);
1588 return range_check (result, "IDINT");
1593 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1597 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1600 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1602 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1603 return range_check (result, "IOR");
1608 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1611 int shift, ashift, isize, k;
1614 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1617 if (gfc_extract_int (s, &shift) != NULL)
1619 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1620 return &gfc_bad_expr;
1623 k = gfc_validate_kind (BT_INTEGER, e->ts.kind);
1625 gfc_internal_error ("gfc_simplify_ishft(): Bad kind");
1627 isize = gfc_integer_kinds[k].bit_size;
1637 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1639 return &gfc_bad_expr;
1642 e_int = mpz_get_si (e->value.integer);
1643 if (e_int > INT_MAX || e_int < INT_MIN)
1644 gfc_internal_error ("ISHFT: unable to extract integer");
1646 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1650 mpz_set (result->value.integer, e->value.integer);
1651 return range_check (result, "ISHFT");
1655 mpz_set_si (result->value.integer, e_int << shift);
1657 mpz_set_si (result->value.integer, e_int >> ashift);
1659 return range_check (result, "ISHFT");
1664 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1667 int shift, ashift, isize, delta, k;
1670 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1673 if (gfc_extract_int (s, &shift) != NULL)
1675 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1676 return &gfc_bad_expr;
1679 k = gfc_validate_kind (e->ts.type, e->ts.kind);
1681 gfc_internal_error ("gfc_simplify_ishftc(): Bad kind");
1685 if (gfc_extract_int (sz, &isize) != NULL || isize < 0)
1687 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1688 return &gfc_bad_expr;
1692 isize = gfc_integer_kinds[k].bit_size;
1702 ("Magnitude of second argument of ISHFTC exceeds third argument "
1703 "at %L", &s->where);
1704 return &gfc_bad_expr;
1707 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1709 bits = gfc_getmem (isize * sizeof (int));
1711 for (i = 0; i < isize; i++)
1712 bits[i] = mpz_tstbit (e->value.integer, i);
1714 delta = isize - ashift;
1718 mpz_set (result->value.integer, e->value.integer);
1720 return range_check (result, "ISHFTC");
1725 for (i = 0; i < delta; i++)
1728 mpz_clrbit (result->value.integer, i + shift);
1730 mpz_setbit (result->value.integer, i + shift);
1733 for (i = delta; i < isize; i++)
1736 mpz_clrbit (result->value.integer, i - delta);
1738 mpz_setbit (result->value.integer, i - delta);
1742 return range_check (result, "ISHFTC");
1746 for (i = 0; i < ashift; i++)
1749 mpz_clrbit (result->value.integer, i + delta);
1751 mpz_setbit (result->value.integer, i + delta);
1754 for (i = ashift; i < isize; i++)
1757 mpz_clrbit (result->value.integer, i + shift);
1759 mpz_setbit (result->value.integer, i + shift);
1763 return range_check (result, "ISHFTC");
1769 gfc_simplify_kind (gfc_expr * e)
1772 if (e->ts.type == BT_DERIVED)
1774 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1775 return &gfc_bad_expr;
1778 return gfc_int_expr (e->ts.kind);
1783 gfc_simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1789 if (array->expr_type != EXPR_VARIABLE)
1795 if (dim->expr_type != EXPR_CONSTANT)
1798 /* Follow any component references. */
1799 as = array->symtree->n.sym->as;
1801 while (ref->next != NULL)
1803 if (ref->type == REF_COMPONENT)
1804 as = ref->u.c.sym->as;
1808 if (ref->type != REF_ARRAY || ref->u.ar.type != AR_FULL)
1811 i = mpz_get_si (dim->value.integer);
1813 return gfc_copy_expr (as->upper[i-1]);
1815 return gfc_copy_expr (as->lower[i-1]);
1820 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
1822 return gfc_simplify_bound (array, dim, 0);
1827 gfc_simplify_len (gfc_expr * e)
1831 if (e->expr_type != EXPR_CONSTANT)
1834 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1837 mpz_set_si (result->value.integer, e->value.character.length);
1838 return range_check (result, "LEN");
1843 gfc_simplify_len_trim (gfc_expr * e)
1846 int count, len, lentrim, i;
1848 if (e->expr_type != EXPR_CONSTANT)
1851 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
1854 len = e->value.character.length;
1856 for (count = 0, i = 1; i <= len; i++)
1857 if (e->value.character.string[len - i] == ' ')
1862 lentrim = len - count;
1864 mpz_set_si (result->value.integer, lentrim);
1865 return range_check (result, "LEN_TRIM");
1870 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
1873 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1876 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
1882 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
1885 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1888 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
1894 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
1897 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1900 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
1906 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
1909 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
1912 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
1918 gfc_simplify_log (gfc_expr * x)
1921 mpfr_t xr, xi, zero;
1923 if (x->expr_type != EXPR_CONSTANT)
1926 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1928 gfc_set_model_kind (x->ts.kind);
1930 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1935 if (mpfr_cmp (x->value.real, zero) <= 0)
1938 ("Argument of LOG at %L cannot be less than or equal to zero",
1940 gfc_free_expr (result);
1942 return &gfc_bad_expr;
1945 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
1950 if ((mpfr_cmp (x->value.complex.r, zero) == 0)
1951 && (mpfr_cmp (x->value.complex.i, zero) == 0))
1953 gfc_error ("Complex argument of LOG at %L cannot be zero",
1955 gfc_free_expr (result);
1957 return &gfc_bad_expr;
1963 arctangent2 (x->value.complex.i, x->value.complex.r,
1964 result->value.complex.i);
1966 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1967 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1968 mpfr_add (xr, xr, xi, GFC_RND_MODE);
1969 mpfr_sqrt (xr, xr, GFC_RND_MODE);
1970 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
1979 gfc_internal_error ("gfc_simplify_log: bad type");
1982 return range_check (result, "LOG");
1987 gfc_simplify_log10 (gfc_expr * x)
1992 if (x->expr_type != EXPR_CONSTANT)
1995 gfc_set_model_kind (x->ts.kind);
1997 mpfr_set_ui (zero, 0, GFC_RND_MODE);
1999 if (mpfr_cmp (x->value.real, zero) <= 0)
2002 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2005 return &gfc_bad_expr;
2008 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2010 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2013 return range_check (result, "LOG10");
2018 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
2023 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind ());
2025 return &gfc_bad_expr;
2027 if (e->expr_type != EXPR_CONSTANT)
2030 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2032 result->value.logical = e->value.logical;
2038 /* This function is special since MAX() can take any number of
2039 arguments. The simplified expression is a rewritten version of the
2040 argument list containing at most one constant element. Other
2041 constant elements are deleted. Because the argument list has
2042 already been checked, this function always succeeds. sign is 1 for
2043 MAX(), -1 for MIN(). */
2046 simplify_min_max (gfc_expr * expr, int sign)
2048 gfc_actual_arglist *arg, *last, *extremum;
2049 gfc_intrinsic_sym * specific;
2053 specific = expr->value.function.isym;
2055 arg = expr->value.function.actual;
2057 for (; arg; last = arg, arg = arg->next)
2059 if (arg->expr->expr_type != EXPR_CONSTANT)
2062 if (extremum == NULL)
2068 switch (arg->expr->ts.type)
2071 if (mpz_cmp (arg->expr->value.integer,
2072 extremum->expr->value.integer) * sign > 0)
2073 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2078 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2080 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2086 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2089 /* Delete the extra constant argument. */
2091 expr->value.function.actual = arg->next;
2093 last->next = arg->next;
2096 gfc_free_actual_arglist (arg);
2100 /* If there is one value left, replace the function call with the
2102 if (expr->value.function.actual->next != NULL)
2105 /* Convert to the correct type and kind. */
2106 if (expr->ts.type != BT_UNKNOWN)
2107 return gfc_convert_constant (expr->value.function.actual->expr,
2108 expr->ts.type, expr->ts.kind);
2110 if (specific->ts.type != BT_UNKNOWN)
2111 return gfc_convert_constant (expr->value.function.actual->expr,
2112 specific->ts.type, specific->ts.kind);
2114 return gfc_copy_expr (expr->value.function.actual->expr);
2119 gfc_simplify_min (gfc_expr * e)
2122 return simplify_min_max (e, -1);
2127 gfc_simplify_max (gfc_expr * e)
2130 return simplify_min_max (e, 1);
2135 gfc_simplify_maxexponent (gfc_expr * x)
2140 i = gfc_validate_kind (BT_REAL, x->ts.kind);
2142 gfc_internal_error ("gfc_simplify_maxexponent(): Bad kind");
2144 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2145 result->where = x->where;
2152 gfc_simplify_minexponent (gfc_expr * x)
2157 i = gfc_validate_kind (BT_REAL, x->ts.kind);
2159 gfc_internal_error ("gfc_simplify_minexponent(): Bad kind");
2161 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2162 result->where = x->where;
2169 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2172 mpfr_t quot, iquot, term;
2174 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2177 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2182 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2184 /* Result is processor-dependent. */
2185 gfc_error ("Second argument MOD at %L is zero", &a->where);
2186 gfc_free_expr (result);
2187 return &gfc_bad_expr;
2189 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2193 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2195 /* Result is processor-dependent. */
2196 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2197 gfc_free_expr (result);
2198 return &gfc_bad_expr;
2201 gfc_set_model_kind (a->ts.kind);
2206 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2207 mpfr_trunc (iquot, quot);
2208 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2209 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2217 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2220 return range_check (result, "MOD");
2225 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2228 mpfr_t quot, iquot, term;
2230 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2233 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
2238 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2240 /* Result is processor-dependent. This processor just opts
2241 to not handle it at all. */
2242 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2243 gfc_free_expr (result);
2244 return &gfc_bad_expr;
2246 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2251 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2253 /* Result is processor-dependent. */
2254 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2255 gfc_free_expr (result);
2256 return &gfc_bad_expr;
2259 gfc_set_model_kind (a->ts.kind);
2264 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2265 mpfr_floor (iquot, quot);
2266 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2272 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2276 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2279 return range_check (result, "MODULO");
2283 /* Exists for the sole purpose of consistency with other intrinsics. */
2285 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2286 gfc_expr * fp ATTRIBUTE_UNUSED,
2287 gfc_expr * l ATTRIBUTE_UNUSED,
2288 gfc_expr * to ATTRIBUTE_UNUSED,
2289 gfc_expr * tp ATTRIBUTE_UNUSED)
2296 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2301 int p, i, k, match_float;
2303 /* FIXME: This implementation is dopey and probably not quite right,
2304 but it's a start. */
2306 if (x->expr_type != EXPR_CONSTANT)
2309 k = gfc_validate_kind (x->ts.type, x->ts.kind);
2311 gfc_internal_error ("gfc_simplify_precision(): Bad kind");
2313 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2315 val = mpfr_get_d (x->value.real, GFC_RND_MODE);
2316 p = gfc_real_kinds[k].digits;
2319 for (i = 1; i < p; ++i)
2324 /* TODO we should make sure that 'float' matches kind 4 */
2325 match_float = gfc_real_kinds[k].kind == 4;
2326 if (mpfr_cmp_ui (s->value.real, 0) > 0)
2332 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2337 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2340 else if (mpfr_cmp_ui (s->value.real, 0) < 0)
2346 mpfr_set_d (result->value.real, rval, GFC_RND_MODE);
2351 mpfr_set_d (result->value.real, val, GFC_RND_MODE);
2356 gfc_error ("Invalid second argument of NEAREST at %L", &s->where);
2358 return &gfc_bad_expr;
2361 return range_check (result, "NEAREST");
2367 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2369 gfc_expr *rtrunc, *itrunc, *result;
2373 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind ());
2375 return &gfc_bad_expr;
2377 if (e->expr_type != EXPR_CONSTANT)
2380 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2382 rtrunc = gfc_copy_expr (e);
2383 itrunc = gfc_copy_expr (e);
2385 cmp = mpfr_cmp_ui (e->value.real, 0);
2387 gfc_set_model (e->value.real);
2389 mpfr_set_str (half, "0.5", 10, GFC_RND_MODE);
2393 mpfr_add (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2394 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2398 mpfr_sub (rtrunc->value.real, e->value.real, half, GFC_RND_MODE);
2399 mpfr_trunc (itrunc->value.real, rtrunc->value.real);
2402 mpfr_set_ui (itrunc->value.real, 0, GFC_RND_MODE);
2404 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2406 gfc_free_expr (itrunc);
2407 gfc_free_expr (rtrunc);
2410 return range_check (result, name);
2415 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2418 return simplify_nint ("NINT", e, k);
2423 gfc_simplify_idnint (gfc_expr * e)
2426 return simplify_nint ("IDNINT", e, NULL);
2431 gfc_simplify_not (gfc_expr * e)
2436 if (e->expr_type != EXPR_CONSTANT)
2439 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2441 mpz_com (result->value.integer, e->value.integer);
2443 /* Because of how GMP handles numbers, the result must be ANDed with
2444 the max_int mask. For radices <> 2, this will require change. */
2446 i = gfc_validate_kind (BT_INTEGER, e->ts.kind);
2448 gfc_internal_error ("gfc_simplify_not(): Bad kind");
2450 mpz_and (result->value.integer, result->value.integer,
2451 gfc_integer_kinds[i].max_int);
2453 return range_check (result, "NOT");
2458 gfc_simplify_null (gfc_expr * mold)
2462 result = gfc_get_expr ();
2463 result->expr_type = EXPR_NULL;
2466 result->ts.type = BT_UNKNOWN;
2469 result->ts = mold->ts;
2470 result->where = mold->where;
2478 gfc_simplify_precision (gfc_expr * e)
2483 i = gfc_validate_kind (e->ts.type, e->ts.kind);
2485 gfc_internal_error ("gfc_simplify_precision(): Bad kind");
2487 result = gfc_int_expr (gfc_real_kinds[i].precision);
2488 result->where = e->where;
2495 gfc_simplify_radix (gfc_expr * e)
2500 i = gfc_validate_kind (e->ts.type, e->ts.kind);
2507 i = gfc_integer_kinds[i].radix;
2511 i = gfc_real_kinds[i].radix;
2516 gfc_internal_error ("gfc_simplify_radix(): Bad type");
2519 result = gfc_int_expr (i);
2520 result->where = e->where;
2527 gfc_simplify_range (gfc_expr * e)
2533 i = gfc_validate_kind (e->ts.type, e->ts.kind);
2540 j = gfc_integer_kinds[i].range;
2545 j = gfc_real_kinds[i].range;
2550 gfc_internal_error ("gfc_simplify_range(): Bad kind");
2553 result = gfc_int_expr (j);
2554 result->where = e->where;
2561 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2566 if (e->ts.type == BT_COMPLEX)
2567 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2569 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind ());
2572 return &gfc_bad_expr;
2574 if (e->expr_type != EXPR_CONSTANT)
2580 result = gfc_int2real (e, kind);
2584 result = gfc_real2real (e, kind);
2588 result = gfc_complex2real (e, kind);
2592 gfc_internal_error ("bad type in REAL");
2596 return range_check (result, "REAL");
2600 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2603 int i, j, len, ncopies, nlen;
2605 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2608 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2610 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2611 return &gfc_bad_expr;
2614 len = e->value.character.length;
2615 nlen = ncopies * len;
2617 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2621 result->value.character.string = gfc_getmem (1);
2622 result->value.character.length = 0;
2623 result->value.character.string[0] = '\0';
2627 result->value.character.length = nlen;
2628 result->value.character.string = gfc_getmem (nlen + 1);
2630 for (i = 0; i < ncopies; i++)
2631 for (j = 0; j < len; j++)
2632 result->value.character.string[j + i * len] =
2633 e->value.character.string[j];
2635 result->value.character.string[nlen] = '\0'; /* For debugger */
2640 /* This one is a bear, but mainly has to do with shuffling elements. */
2643 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2644 gfc_expr * pad, gfc_expr * order_exp)
2647 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2648 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2649 gfc_constructor *head, *tail;
2655 /* Unpack the shape array. */
2656 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2659 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2663 && (pad->expr_type != EXPR_ARRAY
2664 || !gfc_is_constant_expr (pad)))
2667 if (order_exp != NULL
2668 && (order_exp->expr_type != EXPR_ARRAY
2669 || !gfc_is_constant_expr (order_exp)))
2678 e = gfc_get_array_element (shape_exp, rank);
2682 if (gfc_extract_int (e, &shape[rank]) != NULL)
2684 gfc_error ("Integer too large in shape specification at %L",
2692 if (rank >= GFC_MAX_DIMENSIONS)
2694 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2695 "at %L", &e->where);
2700 if (shape[rank] < 0)
2702 gfc_error ("Shape specification at %L cannot be negative",
2712 gfc_error ("Shape specification at %L cannot be the null array",
2717 /* Now unpack the order array if present. */
2718 if (order_exp == NULL)
2720 for (i = 0; i < rank; i++)
2727 for (i = 0; i < rank; i++)
2730 for (i = 0; i < rank; i++)
2732 e = gfc_get_array_element (order_exp, i);
2736 ("ORDER parameter of RESHAPE at %L is not the same size "
2737 "as SHAPE parameter", &order_exp->where);
2741 if (gfc_extract_int (e, &order[i]) != NULL)
2743 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2751 if (order[i] < 1 || order[i] > rank)
2753 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2762 gfc_error ("Invalid permutation in ORDER parameter at %L",
2771 /* Count the elements in the source and padding arrays. */
2776 gfc_array_size (pad, &size);
2777 npad = mpz_get_ui (size);
2781 gfc_array_size (source, &size);
2782 nsource = mpz_get_ui (size);
2785 /* If it weren't for that pesky permutation we could just loop
2786 through the source and round out any shortage with pad elements.
2787 But no, someone just had to have the compiler do something the
2788 user should be doing. */
2790 for (i = 0; i < rank; i++)
2795 /* Figure out which element to extract. */
2796 mpz_set_ui (index, 0);
2798 for (i = rank - 1; i >= 0; i--)
2800 mpz_add_ui (index, index, x[order[i]]);
2802 mpz_mul_ui (index, index, shape[order[i - 1]]);
2805 if (mpz_cmp_ui (index, INT_MAX) > 0)
2806 gfc_internal_error ("Reshaped array too large at %L", &e->where);
2808 j = mpz_get_ui (index);
2811 e = gfc_get_array_element (source, j);
2819 ("PAD parameter required for short SOURCE parameter at %L",
2825 e = gfc_get_array_element (pad, j);
2829 head = tail = gfc_get_constructor ();
2832 tail->next = gfc_get_constructor ();
2839 tail->where = e->where;
2842 /* Calculate the next element. */
2846 if (++x[i] < shape[i])
2857 e = gfc_get_expr ();
2858 e->where = source->where;
2859 e->expr_type = EXPR_ARRAY;
2860 e->value.constructor = head;
2861 e->shape = gfc_get_shape (rank);
2863 for (i = 0; i < rank; i++)
2864 mpz_init_set_ui (e->shape[i], shape[order[i]]);
2866 e->ts = head->expr->ts;
2872 gfc_free_constructor (head);
2874 return &gfc_bad_expr;
2879 gfc_simplify_rrspacing (gfc_expr * x)
2882 mpfr_t i2, absv, ln2, lnx, frac, pow2, zero;
2886 if (x->expr_type != EXPR_CONSTANT)
2889 i = gfc_validate_kind (x->ts.type, x->ts.kind);
2891 gfc_internal_error ("gfc_simplify_rrspacing(): Bad kind");
2893 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2895 p = gfc_real_kinds[i].digits;
2897 gfc_set_model_kind (x->ts.kind);
2899 mpfr_set_ui (zero, 0, GFC_RND_MODE);
2901 if (mpfr_cmp (x->value.real, zero) == 0)
2903 mpfr_ui_div (result->value.real, 1, gfc_real_kinds[i].tiny, GFC_RND_MODE);
2915 mpfr_set_ui (i2, 2, GFC_RND_MODE);
2917 mpfr_log (ln2, i2, GFC_RND_MODE);
2918 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2919 mpfr_log (lnx, absv, GFC_RND_MODE);
2921 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
2922 mpfr_trunc (lnx, lnx);
2923 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
2925 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
2926 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
2927 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
2929 exp2 = (unsigned long) p;
2930 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
2940 return range_check (result, "RRSPACING");
2945 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
2947 int k, neg_flag, power, exp_range;
2948 mpfr_t scale, radix;
2951 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
2954 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2956 if (mpfr_sgn (x->value.real) == 0)
2958 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2962 k = gfc_validate_kind (BT_REAL, x->ts.kind);
2964 gfc_internal_error ("gfc_simplify_scale(): Bad kind");
2966 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
2968 /* This check filters out values of i that would overflow an int. */
2969 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
2970 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
2972 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
2973 return &gfc_bad_expr;
2976 /* Compute scale = radix ** power. */
2977 power = mpz_get_si (i->value.integer);
2987 gfc_set_model_kind (x->ts.kind);
2990 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
2991 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
2994 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
2996 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3001 return range_check (result, "SCALE");
3006 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
3011 size_t indx, len, lenc;
3013 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3016 if (b != NULL && b->value.logical != 0)
3021 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3024 len = e->value.character.length;
3025 lenc = c->value.character.length;
3027 if (len == 0 || lenc == 0)
3036 strcspn (e->value.character.string, c->value.character.string) + 1;
3043 for (indx = len; indx > 0; indx--)
3045 for (i = 0; i < lenc; i++)
3047 if (c->value.character.string[i]
3048 == e->value.character.string[indx - 1])
3056 mpz_set_ui (result->value.integer, indx);
3057 return range_check (result, "SCAN");
3062 gfc_simplify_selected_int_kind (gfc_expr * e)
3067 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3072 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3073 if (gfc_integer_kinds[i].range >= range
3074 && gfc_integer_kinds[i].kind < kind)
3075 kind = gfc_integer_kinds[i].kind;
3077 if (kind == INT_MAX)
3080 result = gfc_int_expr (kind);
3081 result->where = e->where;
3088 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
3090 int range, precision, i, kind, found_precision, found_range;
3097 if (p->expr_type != EXPR_CONSTANT
3098 || gfc_extract_int (p, &precision) != NULL)
3106 if (q->expr_type != EXPR_CONSTANT
3107 || gfc_extract_int (q, &range) != NULL)
3112 found_precision = 0;
3115 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3117 if (gfc_real_kinds[i].precision >= precision)
3118 found_precision = 1;
3120 if (gfc_real_kinds[i].range >= range)
3123 if (gfc_real_kinds[i].precision >= precision
3124 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3125 kind = gfc_real_kinds[i].kind;
3128 if (kind == INT_MAX)
3132 if (!found_precision)
3138 result = gfc_int_expr (kind);
3139 result->where = (p != NULL) ? p->where : q->where;
3146 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3149 mpfr_t i2, ln2, absv, lnx, pow2, frac, zero;
3152 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3155 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3157 gfc_set_model_kind (x->ts.kind);
3159 mpfr_set_ui (zero, 0, GFC_RND_MODE);
3161 if (mpfr_cmp (x->value.real, zero) == 0)
3163 mpfr_set (result->value.real, zero, GFC_RND_MODE);
3175 mpfr_set_ui (i2, 2, GFC_RND_MODE);
3176 mpfr_log (ln2, i2, GFC_RND_MODE);
3178 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3179 mpfr_log (lnx, absv, GFC_RND_MODE);
3181 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
3182 mpfr_trunc (lnx, lnx);
3183 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
3185 /* Old exponent value, and fraction. */
3186 exp2 = (unsigned long) mpfr_get_d (lnx, GFC_RND_MODE);
3187 mpfr_pow_ui (pow2, i2, exp2, GFC_RND_MODE);
3189 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3192 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3193 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3203 return range_check (result, "SET_EXPONENT");
3208 gfc_simplify_shape (gfc_expr * source)
3210 mpz_t shape[GFC_MAX_DIMENSIONS];
3211 gfc_expr *result, *e, *f;
3216 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind (),
3219 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3222 ar = gfc_find_array_ref (source);
3224 t = gfc_array_ref_shape (ar, shape);
3226 for (n = 0; n < source->rank; n++)
3228 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3233 mpz_set (e->value.integer, shape[n]);
3234 mpz_clear (shape[n]);
3238 mpz_set_ui (e->value.integer, n + 1);
3240 f = gfc_simplify_size (source, e);
3244 gfc_free_expr (result);
3253 gfc_append_constructor (result, e);
3261 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3269 if (gfc_array_size (array, &size) == FAILURE)
3274 if (dim->expr_type != EXPR_CONSTANT)
3277 d = mpz_get_ui (dim->value.integer) - 1;
3278 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3282 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3285 mpz_set (result->value.integer, size);
3292 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3296 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3299 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3304 mpz_abs (result->value.integer, x->value.integer);
3305 if (mpz_sgn (y->value.integer) < 0)
3306 mpz_neg (result->value.integer, result->value.integer);
3311 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3313 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3314 if (mpfr_sgn (y->value.real) < 0)
3315 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3320 gfc_internal_error ("Bad type in gfc_simplify_sign");
3328 gfc_simplify_sin (gfc_expr * x)
3333 if (x->expr_type != EXPR_CONSTANT)
3336 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3341 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3345 gfc_set_model (x->value.real);
3349 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3350 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3351 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3353 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3354 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3355 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3362 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3365 return range_check (result, "SIN");
3370 gfc_simplify_sinh (gfc_expr * x)
3374 if (x->expr_type != EXPR_CONSTANT)
3377 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3379 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3381 return range_check (result, "SINH");
3385 /* The argument is always a double precision real that is converted to
3386 single precision. TODO: Rounding! */
3389 gfc_simplify_sngl (gfc_expr * a)
3393 if (a->expr_type != EXPR_CONSTANT)
3396 result = gfc_real2real (a, gfc_default_real_kind ());
3397 return range_check (result, "SNGL");
3402 gfc_simplify_spacing (gfc_expr * x)
3405 mpfr_t i1, i2, ln2, absv, lnx, zero;
3410 if (x->expr_type != EXPR_CONSTANT)
3413 i = gfc_validate_kind (x->ts.type, x->ts.kind);
3415 gfc_internal_error ("gfc_simplify_spacing(): Bad kind");
3417 p = gfc_real_kinds[i].digits;
3419 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3421 gfc_set_model_kind (x->ts.kind);
3423 mpfr_set_ui (zero, 0, GFC_RND_MODE);
3425 if (mpfr_cmp (x->value.real, zero) == 0)
3427 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3438 mpfr_set_ui (i1, 1, GFC_RND_MODE);
3439 mpfr_set_ui (i2, 2, GFC_RND_MODE);
3441 mpfr_log (ln2, i2, GFC_RND_MODE);
3442 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3443 mpfr_log (lnx, absv, GFC_RND_MODE);
3445 mpfr_div (lnx, lnx, ln2, GFC_RND_MODE);
3446 mpfr_trunc (lnx, lnx);
3447 mpfr_add_ui (lnx, lnx, 1, GFC_RND_MODE);
3449 diff = (long) mpfr_get_d (lnx, GFC_RND_MODE) - (long) p;
3452 exp2 = (unsigned) diff;
3453 mpfr_mul_2exp (result->value.real, i1, exp2, GFC_RND_MODE);
3458 exp2 = (unsigned) diff;
3459 mpfr_div_2exp (result->value.real, i1, exp2, GFC_RND_MODE);
3469 if (mpfr_cmp (result->value.real, gfc_real_kinds[i].tiny) < 0)
3470 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3472 return range_check (result, "SPACING");
3477 gfc_simplify_sqrt (gfc_expr * e)
3480 mpfr_t ac, ad, s, t, w;
3482 if (e->expr_type != EXPR_CONSTANT)
3485 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3490 if (mpfr_cmp_si (e->value.real, 0) < 0)
3492 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3497 /* Formula taken from Numerical Recipes to avoid over- and
3500 gfc_set_model (e->value.real);
3507 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3508 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3511 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3512 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3516 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3517 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3519 if (mpfr_cmp (ac, ad) >= 0)
3521 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3522 mpfr_mul (t, t, t, GFC_RND_MODE);
3523 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3524 mpfr_sqrt (t, t, GFC_RND_MODE);
3525 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3526 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3527 mpfr_sqrt (t, t, GFC_RND_MODE);
3528 mpfr_sqrt (s, ac, GFC_RND_MODE);
3529 mpfr_mul (w, s, t, GFC_RND_MODE);
3533 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3534 mpfr_mul (t, s, s, GFC_RND_MODE);
3535 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3536 mpfr_sqrt (t, t, GFC_RND_MODE);
3537 mpfr_abs (s, s, GFC_RND_MODE);
3538 mpfr_add (t, t, s, GFC_RND_MODE);
3539 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3540 mpfr_sqrt (t, t, GFC_RND_MODE);
3541 mpfr_sqrt (s, ad, GFC_RND_MODE);
3542 mpfr_mul (w, s, t, GFC_RND_MODE);
3545 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3547 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3548 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3549 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3551 else if (mpfr_cmp_ui (w, 0) != 0
3552 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3553 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3555 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3556 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3557 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3559 else if (mpfr_cmp_ui (w, 0) != 0
3560 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3561 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3563 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3564 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3565 mpfr_neg (w, w, GFC_RND_MODE);
3566 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3569 gfc_internal_error ("invalid complex argument of SQRT at %L",
3581 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3584 return range_check (result, "SQRT");
3587 gfc_free_expr (result);
3588 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3589 return &gfc_bad_expr;
3594 gfc_simplify_tan (gfc_expr * x)
3599 if (x->expr_type != EXPR_CONSTANT)
3602 i = gfc_validate_kind (BT_REAL, x->ts.kind);
3604 gfc_internal_error ("gfc_simplify_tan(): Bad kind");
3606 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3608 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3610 return range_check (result, "TAN");
3615 gfc_simplify_tanh (gfc_expr * x)
3619 if (x->expr_type != EXPR_CONSTANT)
3622 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3624 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3626 return range_check (result, "TANH");
3632 gfc_simplify_tiny (gfc_expr * e)
3637 i = gfc_validate_kind (BT_REAL, e->ts.kind);
3639 gfc_internal_error ("gfc_simplify_error(): Bad kind");
3641 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3642 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3649 gfc_simplify_trim (gfc_expr * e)
3652 int count, i, len, lentrim;
3654 if (e->expr_type != EXPR_CONSTANT)
3657 len = e->value.character.length;
3659 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3661 for (count = 0, i = 1; i <= len; ++i)
3663 if (e->value.character.string[len - i] == ' ')
3669 lentrim = len - count;
3671 result->value.character.length = lentrim;
3672 result->value.character.string = gfc_getmem (lentrim + 1);
3674 for (i = 0; i < lentrim; i++)
3675 result->value.character.string[i] = e->value.character.string[i];
3677 result->value.character.string[lentrim] = '\0'; /* For debugger */
3684 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3686 return gfc_simplify_bound (array, dim, 1);
3691 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3695 size_t index, len, lenset;
3698 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3701 if (b != NULL && b->value.logical != 0)
3706 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind (),
3709 len = s->value.character.length;
3710 lenset = set->value.character.length;
3714 mpz_set_ui (result->value.integer, 0);
3722 mpz_set_ui (result->value.integer, len);
3727 strspn (s->value.character.string, set->value.character.string) + 1;
3736 mpz_set_ui (result->value.integer, 1);
3739 for (index = len; index > 0; index --)
3741 for (i = 0; i < lenset; i++)
3743 if (s->value.character.string[index - 1]
3744 == set->value.character.string[i])
3752 mpz_set_ui (result->value.integer, index);
3756 /****************** Constant simplification *****************/
3758 /* Master function to convert one constant to another. While this is
3759 used as a simplification function, it requires the destination type
3760 and kind information which is supplied by a special case in
3764 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3766 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3767 gfc_constructor *head, *c, *tail = NULL;
3781 f = gfc_int2complex;
3798 f = gfc_real2complex;
3809 f = gfc_complex2int;
3812 f = gfc_complex2real;
3815 f = gfc_complex2complex;
3824 if (type != BT_LOGICAL)
3831 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3836 switch (e->expr_type)
3839 result = f (e, kind);
3841 return &gfc_bad_expr;
3845 if (!gfc_is_constant_expr (e))
3850 for (c = e->value.constructor; c; c = c->next)
3853 head = tail = gfc_get_constructor ();
3856 tail->next = gfc_get_constructor ();
3860 tail->where = c->where;
3862 if (c->iterator == NULL)
3863 tail->expr = f (c->expr, kind);
3866 g = gfc_convert_constant (c->expr, type, kind);
3867 if (g == &gfc_bad_expr)
3872 if (tail->expr == NULL)
3874 gfc_free_constructor (head);
3879 result = gfc_get_expr ();
3880 result->ts.type = type;
3881 result->ts.kind = kind;
3882 result->expr_type = EXPR_ARRAY;
3883 result->value.constructor = head;
3884 result->shape = gfc_copy_shape (e->shape, e->rank);
3885 result->where = e->where;
3886 result->rank = e->rank;
3897 /****************** Helper functions ***********************/
3899 /* Given a collating table, create the inverse table. */
3902 invert_table (const int *table, int *xtable)
3906 for (i = 0; i < 256; i++)
3909 for (i = 0; i < 256; i++)
3910 xtable[table[i]] = i;
3915 gfc_simplify_init_1 (void)
3918 invert_table (ascii_table, xascii_table);