1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
27 #include "intrinsic.h"
28 #include "target-memory.h"
30 /* Savely advance an array constructor by 'n' elements.
31 Mainly used by simplifiers of transformational intrinsics. */
32 #define ADVANCE(ctor, n) do { int i; for (i = 0; i < n && ctor; ++i) ctor = ctor->next; } while (0)
34 gfc_expr gfc_bad_expr;
37 /* Note that 'simplification' is not just transforming expressions.
38 For functions that are not simplified at compile time, range
39 checking is done if possible.
41 The return convention is that each simplification function returns:
43 A new expression node corresponding to the simplified arguments.
44 The original arguments are destroyed by the caller, and must not
45 be a part of the new expression.
47 NULL pointer indicating that no simplification was possible and
48 the original expression should remain intact. If the
49 simplification function sets the type and/or the function name
50 via the pointer gfc_simple_expression, then this type is
53 An expression pointer to gfc_bad_expr (a static placeholder)
54 indicating that some error has prevented simplification. For
55 example, sqrt(-1.0). The error is generated within the function
56 and should be propagated upwards
58 By the time a simplification function gets control, it has been
59 decided that the function call is really supposed to be the
60 intrinsic. No type checking is strictly necessary, since only
61 valid types will be passed on. On the other hand, a simplification
62 subroutine may have to look at the type of an argument as part of
65 Array arguments are never passed to these subroutines.
67 The functions in this file don't have much comment with them, but
68 everything is reasonably straight-forward. The Standard, chapter 13
69 is the best comment you'll find for this file anyway. */
71 /* Range checks an expression node. If all goes well, returns the
72 node, otherwise returns &gfc_bad_expr and frees the node. */
75 range_check (gfc_expr *result, const char *name)
80 switch (gfc_range_check (result))
86 gfc_error ("Result of %s overflows its kind at %L", name,
91 gfc_error ("Result of %s underflows its kind at %L", name,
96 gfc_error ("Result of %s is NaN at %L", name, &result->where);
100 gfc_error ("Result of %s gives range error for its kind at %L", name,
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);
128 if (gfc_extract_int (k, &kind) != NULL
129 || gfc_validate_kind (type, kind, true) < 0)
131 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
139 /* Helper function to get an integer constant with a kind number given
140 by an integer constant expression. */
142 int_expr_with_kind (int i, gfc_expr *kind, const char *name)
144 gfc_expr *res = gfc_int_expr (i);
145 res->ts.kind = get_kind (BT_INTEGER, kind, name, gfc_default_integer_kind);
146 if (res->ts.kind == -1)
153 /* Converts an mpz_t signed variable into an unsigned one, assuming
154 two's complement representations and a binary width of bitsize.
155 The conversion is a no-op unless x is negative; otherwise, it can
156 be accomplished by masking out the high bits. */
159 convert_mpz_to_unsigned (mpz_t x, int bitsize)
165 /* Confirm that no bits above the signed range are unset. */
166 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
168 mpz_init_set_ui (mask, 1);
169 mpz_mul_2exp (mask, mask, bitsize);
170 mpz_sub_ui (mask, mask, 1);
172 mpz_and (x, x, mask);
178 /* Confirm that no bits above the signed range are set. */
179 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
184 /* Converts an mpz_t unsigned variable into a signed one, assuming
185 two's complement representations and a binary width of bitsize.
186 If the bitsize-1 bit is set, this is taken as a sign bit and
187 the number is converted to the corresponding negative number. */
190 convert_mpz_to_signed (mpz_t x, int bitsize)
194 /* Confirm that no bits above the unsigned range are set. */
195 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
197 if (mpz_tstbit (x, bitsize - 1) == 1)
199 mpz_init_set_ui (mask, 1);
200 mpz_mul_2exp (mask, mask, bitsize);
201 mpz_sub_ui (mask, mask, 1);
203 /* We negate the number by hand, zeroing the high bits, that is
204 make it the corresponding positive number, and then have it
205 negated by GMP, giving the correct representation of the
208 mpz_add_ui (x, x, 1);
209 mpz_and (x, x, mask);
217 /* Test that the expression is an constant array. */
220 is_constant_array_expr (gfc_expr *e)
227 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
230 for (c = e->value.constructor; c; c = c->next)
231 if (c->expr->expr_type != EXPR_CONSTANT)
238 /* Initialize a transformational result expression with a given value. */
241 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
243 if (e && e->expr_type == EXPR_ARRAY)
245 gfc_constructor *ctor = e->value.constructor;
248 init_result_expr (ctor->expr, init, array);
252 else if (e && e->expr_type == EXPR_CONSTANT)
254 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
261 e->value.logical = (init ? 1 : 0);
266 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
267 else if (init == INT_MAX)
268 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
270 mpz_set_si (e->value.integer, init);
276 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
277 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
279 else if (init == INT_MAX)
280 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
282 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
287 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
289 mpfr_set_si (e->value.complex.r, init, GFC_RND_MODE);
290 mpfr_set_si (e->value.complex.i, 0, GFC_RND_MODE);
297 gfc_expr *len = gfc_simplify_len (array, NULL);
298 gfc_extract_int (len, &length);
299 string = gfc_get_wide_string (length + 1);
300 gfc_wide_memset (string, 0, length);
302 else if (init == INT_MAX)
304 gfc_expr *len = gfc_simplify_len (array, NULL);
305 gfc_extract_int (len, &length);
306 string = gfc_get_wide_string (length + 1);
307 gfc_wide_memset (string, 255, length);
312 string = gfc_get_wide_string (1);
315 string[length] = '\0';
316 e->value.character.length = length;
317 e->value.character.string = string;
329 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
332 compute_dot_product (gfc_constructor *ctor_a, int stride_a,
333 gfc_constructor *ctor_b, int stride_b)
336 gfc_expr *a = ctor_a->expr, *b = ctor_b->expr;
338 gcc_assert (gfc_compare_types (&a->ts, &b->ts));
340 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
341 init_result_expr (result, 0, NULL);
343 while (ctor_a && ctor_b)
345 /* Copying of expressions is required as operands are free'd
346 by the gfc_arith routines. */
347 switch (result->ts.type)
350 result = gfc_or (result,
351 gfc_and (gfc_copy_expr (ctor_a->expr),
352 gfc_copy_expr (ctor_b->expr)));
358 result = gfc_add (result,
359 gfc_multiply (gfc_copy_expr (ctor_a->expr),
360 gfc_copy_expr (ctor_b->expr)));
367 ADVANCE (ctor_a, stride_a);
368 ADVANCE (ctor_b, stride_b);
375 /* Build a result expression for transformational intrinsics,
379 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
380 int kind, locus* where)
385 if (!dim || array->rank == 1)
386 return gfc_constant_result (type, kind, where);
388 result = gfc_start_constructor (type, kind, where);
389 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
390 result->rank = array->rank - 1;
392 /* gfc_array_size() would count the number of elements in the constructor,
393 we have not built those yet. */
395 for (i = 0; i < result->rank; ++i)
396 nelem *= mpz_get_ui (result->shape[i]);
398 for (i = 0; i < nelem; ++i)
400 gfc_expr *e = gfc_constant_result (type, kind, where);
401 gfc_append_constructor (result, e);
408 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
410 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
411 of COUNT intrinsic is .TRUE..
413 Interface and implimentation mimics arith functions as
414 gfc_add, gfc_multiply, etc. */
416 static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
420 gcc_assert (op1->ts.type == BT_INTEGER);
421 gcc_assert (op2->ts.type == BT_LOGICAL);
422 gcc_assert (op2->value.logical);
424 result = gfc_copy_expr (op1);
425 mpz_add_ui (result->value.integer, result->value.integer, 1);
433 /* Transforms an ARRAY with operation OP, according to MASK, to a
434 scalar RESULT. E.g. called if
436 REAL, PARAMETER :: array(n, m) = ...
437 REAL, PARAMETER :: s = SUM(array)
439 where OP == gfc_add(). */
442 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
443 transformational_op op)
446 gfc_constructor *array_ctor, *mask_ctor;
448 /* Shortcut for constant .FALSE. MASK. */
450 && mask->expr_type == EXPR_CONSTANT
451 && !mask->value.logical)
454 array_ctor = array->value.constructor;
456 if (mask && mask->expr_type == EXPR_ARRAY)
457 mask_ctor = mask->value.constructor;
461 a = array_ctor->expr;
462 array_ctor = array_ctor->next;
464 /* A constant MASK equals .TRUE. here and can be ignored. */
468 mask_ctor = mask_ctor->next;
469 if (!m->value.logical)
473 result = op (result, gfc_copy_expr (a));
479 /* Transforms an ARRAY with operation OP, according to MASK, to an
480 array RESULT. E.g. called if
482 REAL, PARAMETER :: array(n, m) = ...
483 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
485 where OP == gfc_multiply(). */
488 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
489 gfc_expr *mask, transformational_op op)
492 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
493 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
494 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
496 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
497 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
498 tmpstride[GFC_MAX_DIMENSIONS];
500 /* Shortcut for constant .FALSE. MASK. */
502 && mask->expr_type == EXPR_CONSTANT
503 && !mask->value.logical)
506 /* Build an indexed table for array element expressions to minimize
507 linked-list traversal. Masked elements are set to NULL. */
508 gfc_array_size (array, &size);
509 arraysize = mpz_get_ui (size);
511 arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);
513 array_ctor = array->value.constructor;
515 if (mask && mask->expr_type == EXPR_ARRAY)
516 mask_ctor = mask->value.constructor;
518 for (i = 0; i < arraysize; ++i)
520 arrayvec[i] = array_ctor->expr;
521 array_ctor = array_ctor->next;
525 if (!mask_ctor->expr->value.logical)
528 mask_ctor = mask_ctor->next;
532 /* Same for the result expression. */
533 gfc_array_size (result, &size);
534 resultsize = mpz_get_ui (size);
537 resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
538 result_ctor = result->value.constructor;
539 for (i = 0; i < resultsize; ++i)
541 resultvec[i] = result_ctor->expr;
542 result_ctor = result_ctor->next;
545 gfc_extract_int (dim, &dim_index);
546 dim_index -= 1; /* zero-base index */
550 for (i = 0, n = 0; i < array->rank; ++i)
553 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
556 dim_extent = mpz_get_si (array->shape[i]);
557 dim_stride = tmpstride[i];
561 extent[n] = mpz_get_si (array->shape[i]);
562 sstride[n] = tmpstride[i];
563 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
572 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
574 *dest = op (*dest, gfc_copy_expr (*src));
581 while (!done && count[n] == extent[n])
584 base -= sstride[n] * extent[n];
585 dest -= dstride[n] * extent[n];
588 if (n < result->rank)
599 /* Place updated expression in result constructor. */
600 result_ctor = result->value.constructor;
601 for (i = 0; i < resultsize; ++i)
603 result_ctor->expr = resultvec[i];
604 result_ctor = result_ctor->next;
608 gfc_free (resultvec);
614 /********************** Simplification functions *****************************/
617 gfc_simplify_abs (gfc_expr *e)
621 if (e->expr_type != EXPR_CONSTANT)
627 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
629 mpz_abs (result->value.integer, e->value.integer);
631 result = range_check (result, "IABS");
635 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
637 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
639 result = range_check (result, "ABS");
643 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
645 gfc_set_model_kind (e->ts.kind);
648 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
650 mpfr_hypot (result->value.real, e->value.complex.r,
651 e->value.complex.i, GFC_RND_MODE);
653 result = range_check (result, "CABS");
657 gfc_internal_error ("gfc_simplify_abs(): Bad type");
665 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
669 bool too_large = false;
671 if (e->expr_type != EXPR_CONSTANT)
674 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
676 return &gfc_bad_expr;
678 if (mpz_cmp_si (e->value.integer, 0) < 0)
680 gfc_error ("Argument of %s function at %L is negative", name,
682 return &gfc_bad_expr;
685 if (ascii && gfc_option.warn_surprising
686 && mpz_cmp_si (e->value.integer, 127) > 0)
687 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
690 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
695 mpz_init_set_ui (t, 2);
696 mpz_pow_ui (t, t, 32);
697 mpz_sub_ui (t, t, 1);
698 if (mpz_cmp (e->value.integer, t) > 0)
705 gfc_error ("Argument of %s function at %L is too large for the "
706 "collating sequence of kind %d", name, &e->where, kind);
707 return &gfc_bad_expr;
710 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
711 result->value.character.string = gfc_get_wide_string (2);
712 result->value.character.length = 1;
713 result->value.character.string[0] = mpz_get_ui (e->value.integer);
714 result->value.character.string[1] = '\0'; /* For debugger */
720 /* We use the processor's collating sequence, because all
721 systems that gfortran currently works on are ASCII. */
724 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
726 return simplify_achar_char (e, k, "ACHAR", true);
731 gfc_simplify_acos (gfc_expr *x)
735 if (x->expr_type != EXPR_CONSTANT)
741 if (mpfr_cmp_si (x->value.real, 1) > 0
742 || mpfr_cmp_si (x->value.real, -1) < 0)
744 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
746 return &gfc_bad_expr;
748 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
749 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
753 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
754 mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
760 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
764 return range_check (result, "ACOS");
768 gfc_simplify_acosh (gfc_expr *x)
772 if (x->expr_type != EXPR_CONSTANT)
778 if (mpfr_cmp_si (x->value.real, 1) < 0)
780 gfc_error ("Argument of ACOSH at %L must not be less than 1",
782 return &gfc_bad_expr;
785 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
786 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
790 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
791 mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
797 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
800 return range_check (result, "ACOSH");
804 gfc_simplify_adjustl (gfc_expr *e)
810 if (e->expr_type != EXPR_CONSTANT)
813 len = e->value.character.length;
815 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
817 result->value.character.length = len;
818 result->value.character.string = gfc_get_wide_string (len + 1);
820 for (count = 0, i = 0; i < len; ++i)
822 ch = e->value.character.string[i];
828 for (i = 0; i < len - count; ++i)
829 result->value.character.string[i] = e->value.character.string[count + i];
831 for (i = len - count; i < len; ++i)
832 result->value.character.string[i] = ' ';
834 result->value.character.string[len] = '\0'; /* For debugger */
841 gfc_simplify_adjustr (gfc_expr *e)
847 if (e->expr_type != EXPR_CONSTANT)
850 len = e->value.character.length;
852 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
854 result->value.character.length = len;
855 result->value.character.string = gfc_get_wide_string (len + 1);
857 for (count = 0, i = len - 1; i >= 0; --i)
859 ch = e->value.character.string[i];
865 for (i = 0; i < count; ++i)
866 result->value.character.string[i] = ' ';
868 for (i = count; i < len; ++i)
869 result->value.character.string[i] = e->value.character.string[i - count];
871 result->value.character.string[len] = '\0'; /* For debugger */
878 gfc_simplify_aimag (gfc_expr *e)
882 if (e->expr_type != EXPR_CONSTANT)
885 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
886 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
888 return range_check (result, "AIMAG");
893 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
895 gfc_expr *rtrunc, *result;
898 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
900 return &gfc_bad_expr;
902 if (e->expr_type != EXPR_CONSTANT)
905 rtrunc = gfc_copy_expr (e);
907 mpfr_trunc (rtrunc->value.real, e->value.real);
909 result = gfc_real2real (rtrunc, kind);
910 gfc_free_expr (rtrunc);
912 return range_check (result, "AINT");
917 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
921 if (!is_constant_array_expr (mask)
922 || !gfc_is_constant_expr (dim))
925 result = transformational_result (mask, dim, mask->ts.type,
926 mask->ts.kind, &mask->where);
927 init_result_expr (result, true, NULL);
929 return !dim || mask->rank == 1 ?
930 simplify_transformation_to_scalar (result, mask, NULL, gfc_and) :
931 simplify_transformation_to_array (result, mask, dim, NULL, gfc_and);
936 gfc_simplify_dint (gfc_expr *e)
938 gfc_expr *rtrunc, *result;
940 if (e->expr_type != EXPR_CONSTANT)
943 rtrunc = gfc_copy_expr (e);
945 mpfr_trunc (rtrunc->value.real, e->value.real);
947 result = gfc_real2real (rtrunc, gfc_default_double_kind);
948 gfc_free_expr (rtrunc);
950 return range_check (result, "DINT");
955 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
960 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
962 return &gfc_bad_expr;
964 if (e->expr_type != EXPR_CONSTANT)
967 result = gfc_constant_result (e->ts.type, kind, &e->where);
969 mpfr_round (result->value.real, e->value.real);
971 return range_check (result, "ANINT");
976 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
981 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
984 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
985 if (x->ts.type == BT_INTEGER)
987 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
988 mpz_and (result->value.integer, x->value.integer, y->value.integer);
989 return range_check (result, "AND");
991 else /* BT_LOGICAL */
993 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
994 result->value.logical = x->value.logical && y->value.logical;
1001 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
1005 if (!is_constant_array_expr (mask)
1006 || !gfc_is_constant_expr (dim))
1009 result = transformational_result (mask, dim, mask->ts.type,
1010 mask->ts.kind, &mask->where);
1011 init_result_expr (result, false, NULL);
1013 return !dim || mask->rank == 1 ?
1014 simplify_transformation_to_scalar (result, mask, NULL, gfc_or) :
1015 simplify_transformation_to_array (result, mask, dim, NULL, gfc_or);
1020 gfc_simplify_dnint (gfc_expr *e)
1024 if (e->expr_type != EXPR_CONSTANT)
1027 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
1029 mpfr_round (result->value.real, e->value.real);
1031 return range_check (result, "DNINT");
1036 gfc_simplify_asin (gfc_expr *x)
1040 if (x->expr_type != EXPR_CONSTANT)
1046 if (mpfr_cmp_si (x->value.real, 1) > 0
1047 || mpfr_cmp_si (x->value.real, -1) < 0)
1049 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1051 return &gfc_bad_expr;
1053 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1054 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1058 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1059 mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1065 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1068 return range_check (result, "ASIN");
1073 gfc_simplify_asinh (gfc_expr *x)
1077 if (x->expr_type != EXPR_CONSTANT)
1083 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1084 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1088 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1089 mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1095 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1098 return range_check (result, "ASINH");
1103 gfc_simplify_atan (gfc_expr *x)
1107 if (x->expr_type != EXPR_CONSTANT)
1113 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1114 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1118 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1119 mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1125 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1128 return range_check (result, "ATAN");
1133 gfc_simplify_atanh (gfc_expr *x)
1137 if (x->expr_type != EXPR_CONSTANT)
1143 if (mpfr_cmp_si (x->value.real, 1) >= 0
1144 || mpfr_cmp_si (x->value.real, -1) <= 0)
1146 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1148 return &gfc_bad_expr;
1151 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1152 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1156 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1157 mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1163 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1166 return range_check (result, "ATANH");
1171 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1175 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1178 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1180 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1181 "second argument must not be zero", &x->where);
1182 return &gfc_bad_expr;
1185 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1187 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1189 return range_check (result, "ATAN2");
1194 gfc_simplify_bessel_j0 (gfc_expr *x ATTRIBUTE_UNUSED)
1198 if (x->expr_type != EXPR_CONSTANT)
1201 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1202 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1204 return range_check (result, "BESSEL_J0");
1209 gfc_simplify_bessel_j1 (gfc_expr *x ATTRIBUTE_UNUSED)
1213 if (x->expr_type != EXPR_CONSTANT)
1216 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1217 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1219 return range_check (result, "BESSEL_J1");
1224 gfc_simplify_bessel_jn (gfc_expr *order ATTRIBUTE_UNUSED,
1225 gfc_expr *x ATTRIBUTE_UNUSED)
1230 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1233 n = mpz_get_si (order->value.integer);
1234 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1235 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1237 return range_check (result, "BESSEL_JN");
1242 gfc_simplify_bessel_y0 (gfc_expr *x ATTRIBUTE_UNUSED)
1246 if (x->expr_type != EXPR_CONSTANT)
1249 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1250 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1252 return range_check (result, "BESSEL_Y0");
1257 gfc_simplify_bessel_y1 (gfc_expr *x ATTRIBUTE_UNUSED)
1261 if (x->expr_type != EXPR_CONSTANT)
1264 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1265 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1267 return range_check (result, "BESSEL_Y1");
1272 gfc_simplify_bessel_yn (gfc_expr *order ATTRIBUTE_UNUSED,
1273 gfc_expr *x ATTRIBUTE_UNUSED)
1278 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1281 n = mpz_get_si (order->value.integer);
1282 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1283 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1285 return range_check (result, "BESSEL_YN");
1290 gfc_simplify_bit_size (gfc_expr *e)
1295 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1296 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
1297 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
1304 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1308 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1311 if (gfc_extract_int (bit, &b) != NULL || b < 0)
1312 return gfc_logical_expr (0, &e->where);
1314 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
1319 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1321 gfc_expr *ceil, *result;
1324 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1326 return &gfc_bad_expr;
1328 if (e->expr_type != EXPR_CONSTANT)
1331 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1333 ceil = gfc_copy_expr (e);
1335 mpfr_ceil (ceil->value.real, e->value.real);
1336 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1338 gfc_free_expr (ceil);
1340 return range_check (result, "CEILING");
1345 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1347 return simplify_achar_char (e, k, "CHAR", false);
1351 /* Common subroutine for simplifying CMPLX and DCMPLX. */
1354 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1358 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
1361 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1369 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1371 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
1377 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1379 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
1385 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1387 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1388 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1393 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1402 mpfr_set_z (mpc_imagref (result->value.complex),
1403 y->value.integer, GFC_RND_MODE);
1407 mpfr_set (mpc_imagref (result->value.complex),
1408 y->value.real, GFC_RND_MODE);
1412 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1421 ts.kind = result->ts.kind;
1423 if (!gfc_convert_boz (x, &ts))
1424 return &gfc_bad_expr;
1425 mpfr_set (mpc_realref (result->value.complex),
1426 x->value.real, GFC_RND_MODE);
1433 ts.kind = result->ts.kind;
1435 if (!gfc_convert_boz (y, &ts))
1436 return &gfc_bad_expr;
1437 mpfr_set (mpc_imagref (result->value.complex),
1438 y->value.real, GFC_RND_MODE);
1441 return range_check (result, name);
1445 /* Function called when we won't simplify an expression like CMPLX (or
1446 COMPLEX or DCMPLX) but still want to convert BOZ arguments. */
1449 only_convert_cmplx_boz (gfc_expr *x, gfc_expr *y, int kind)
1456 if (x->is_boz && !gfc_convert_boz (x, &ts))
1457 return &gfc_bad_expr;
1459 if (y && y->is_boz && !gfc_convert_boz (y, &ts))
1460 return &gfc_bad_expr;
1467 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1471 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
1473 return &gfc_bad_expr;
1475 if (x->expr_type != EXPR_CONSTANT
1476 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1477 return only_convert_cmplx_boz (x, y, kind);
1479 return simplify_cmplx ("CMPLX", x, y, kind);
1484 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1488 if (x->ts.type == BT_INTEGER)
1490 if (y->ts.type == BT_INTEGER)
1491 kind = gfc_default_real_kind;
1497 if (y->ts.type == BT_REAL)
1498 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1503 if (x->expr_type != EXPR_CONSTANT
1504 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1505 return only_convert_cmplx_boz (x, y, kind);
1507 return simplify_cmplx ("COMPLEX", x, y, kind);
1512 gfc_simplify_conjg (gfc_expr *e)
1516 if (e->expr_type != EXPR_CONSTANT)
1519 result = gfc_copy_expr (e);
1521 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1523 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
1526 return range_check (result, "CONJG");
1531 gfc_simplify_cos (gfc_expr *x)
1535 if (x->expr_type != EXPR_CONSTANT)
1538 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1543 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1546 gfc_set_model_kind (x->ts.kind);
1548 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1555 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
1556 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
1557 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
1559 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
1560 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
1561 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
1562 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
1564 mpfr_clears (xp, xq, NULL);
1569 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1572 return range_check (result, "COS");
1578 gfc_simplify_cosh (gfc_expr *x)
1582 if (x->expr_type != EXPR_CONSTANT)
1585 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1587 if (x->ts.type == BT_REAL)
1588 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
1589 else if (x->ts.type == BT_COMPLEX)
1592 mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1594 gfc_free_expr (result);
1601 return range_check (result, "COSH");
1606 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1610 if (!is_constant_array_expr (mask)
1611 || !gfc_is_constant_expr (dim)
1612 || !gfc_is_constant_expr (kind))
1615 result = transformational_result (mask, dim,
1617 get_kind (BT_INTEGER, kind, "COUNT",
1618 gfc_default_integer_kind),
1621 init_result_expr (result, 0, NULL);
1623 /* Passing MASK twice, once as data array, once as mask.
1624 Whenever gfc_count is called, '1' is added to the result. */
1625 return !dim || mask->rank == 1 ?
1626 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1627 simplify_transformation_to_array (result, mask, dim, mask, gfc_count);
1632 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1635 if (x->expr_type != EXPR_CONSTANT
1636 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1637 return only_convert_cmplx_boz (x, y, gfc_default_double_kind);
1639 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1644 gfc_simplify_dble (gfc_expr *e)
1646 gfc_expr *result = NULL;
1648 if (e->expr_type != EXPR_CONSTANT)
1655 result = gfc_int2real (e, gfc_default_double_kind);
1659 result = gfc_real2real (e, gfc_default_double_kind);
1663 result = gfc_complex2real (e, gfc_default_double_kind);
1667 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
1670 if (e->ts.type == BT_INTEGER && e->is_boz)
1675 ts.kind = gfc_default_double_kind;
1676 result = gfc_copy_expr (e);
1677 if (!gfc_convert_boz (result, &ts))
1679 gfc_free_expr (result);
1680 return &gfc_bad_expr;
1684 return range_check (result, "DBLE");
1689 gfc_simplify_digits (gfc_expr *x)
1693 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1697 digits = gfc_integer_kinds[i].digits;
1702 digits = gfc_real_kinds[i].digits;
1709 return gfc_int_expr (digits);
1714 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1719 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1722 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1723 result = gfc_constant_result (x->ts.type, kind, &x->where);
1728 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
1729 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
1731 mpz_set_ui (result->value.integer, 0);
1736 if (mpfr_cmp (x->value.real, y->value.real) > 0)
1737 mpfr_sub (result->value.real, x->value.real, y->value.real,
1740 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1745 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1748 return range_check (result, "DIM");
1753 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1757 if (!is_constant_array_expr (vector_a)
1758 || !is_constant_array_expr (vector_b))
1761 gcc_assert (vector_a->rank == 1);
1762 gcc_assert (vector_b->rank == 1);
1763 gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));
1765 if (vector_a->value.constructor && vector_b->value.constructor)
1766 return compute_dot_product (vector_a->value.constructor, 1,
1767 vector_b->value.constructor, 1);
1769 /* Zero sized array ... */
1770 result = gfc_constant_result (vector_a->ts.type,
1773 init_result_expr (result, 0, NULL);
1779 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1781 gfc_expr *a1, *a2, *result;
1783 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1786 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
1788 a1 = gfc_real2real (x, gfc_default_double_kind);
1789 a2 = gfc_real2real (y, gfc_default_double_kind);
1791 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1796 return range_check (result, "DPROD");
1801 gfc_simplify_erf (gfc_expr *x)
1805 if (x->expr_type != EXPR_CONSTANT)
1808 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1810 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
1812 return range_check (result, "ERF");
1817 gfc_simplify_erfc (gfc_expr *x)
1821 if (x->expr_type != EXPR_CONSTANT)
1824 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1826 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
1828 return range_check (result, "ERFC");
1832 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1834 #define MAX_ITER 200
1835 #define ARG_LIMIT 12
1837 /* Calculate ERFC_SCALED directly by its definition:
1839 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1841 using a large precision for intermediate results. This is used for all
1842 but large values of the argument. */
1844 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
1849 prec = mpfr_get_default_prec ();
1850 mpfr_set_default_prec (10 * prec);
1855 mpfr_set (a, arg, GFC_RND_MODE);
1856 mpfr_sqr (b, a, GFC_RND_MODE);
1857 mpfr_exp (b, b, GFC_RND_MODE);
1858 mpfr_erfc (a, a, GFC_RND_MODE);
1859 mpfr_mul (a, a, b, GFC_RND_MODE);
1861 mpfr_set (res, a, GFC_RND_MODE);
1862 mpfr_set_default_prec (prec);
1868 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
1870 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
1871 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
1874 This is used for large values of the argument. Intermediate calculations
1875 are performed with twice the precision. We don't do a fixed number of
1876 iterations of the sum, but stop when it has converged to the required
1879 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
1881 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
1886 prec = mpfr_get_default_prec ();
1887 mpfr_set_default_prec (2 * prec);
1897 mpfr_init (sumtrunc);
1898 mpfr_set_prec (oldsum, prec);
1899 mpfr_set_prec (sumtrunc, prec);
1901 mpfr_set (x, arg, GFC_RND_MODE);
1902 mpfr_set_ui (sum, 1, GFC_RND_MODE);
1903 mpz_set_ui (num, 1);
1905 mpfr_set (u, x, GFC_RND_MODE);
1906 mpfr_sqr (u, u, GFC_RND_MODE);
1907 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
1908 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
1910 for (i = 1; i < MAX_ITER; i++)
1912 mpfr_set (oldsum, sum, GFC_RND_MODE);
1914 mpz_mul_ui (num, num, 2 * i - 1);
1917 mpfr_set (w, u, GFC_RND_MODE);
1918 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
1920 mpfr_set_z (v, num, GFC_RND_MODE);
1921 mpfr_mul (v, v, w, GFC_RND_MODE);
1923 mpfr_add (sum, sum, v, GFC_RND_MODE);
1925 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
1926 if (mpfr_cmp (sumtrunc, oldsum) == 0)
1930 /* We should have converged by now; otherwise, ARG_LIMIT is probably
1932 gcc_assert (i < MAX_ITER);
1934 /* Divide by x * sqrt(Pi). */
1935 mpfr_const_pi (u, GFC_RND_MODE);
1936 mpfr_sqrt (u, u, GFC_RND_MODE);
1937 mpfr_mul (u, u, x, GFC_RND_MODE);
1938 mpfr_div (sum, sum, u, GFC_RND_MODE);
1940 mpfr_set (res, sum, GFC_RND_MODE);
1941 mpfr_set_default_prec (prec);
1943 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
1949 gfc_simplify_erfc_scaled (gfc_expr *x)
1953 if (x->expr_type != EXPR_CONSTANT)
1956 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1957 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
1958 asympt_erfc_scaled (result->value.real, x->value.real);
1960 fullprec_erfc_scaled (result->value.real, x->value.real);
1962 return range_check (result, "ERFC_SCALED");
1970 gfc_simplify_epsilon (gfc_expr *e)
1975 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1977 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1979 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1981 return range_check (result, "EPSILON");
1986 gfc_simplify_exp (gfc_expr *x)
1990 if (x->expr_type != EXPR_CONSTANT)
1993 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1998 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
2002 gfc_set_model_kind (x->ts.kind);
2004 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2010 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
2011 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
2012 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
2013 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
2014 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
2015 mpfr_clears (xp, xq, NULL);
2021 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2024 return range_check (result, "EXP");
2028 gfc_simplify_exponent (gfc_expr *x)
2033 if (x->expr_type != EXPR_CONSTANT)
2036 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2039 gfc_set_model (x->value.real);
2041 if (mpfr_sgn (x->value.real) == 0)
2043 mpz_set_ui (result->value.integer, 0);
2047 i = (int) mpfr_get_exp (x->value.real);
2048 mpz_set_si (result->value.integer, i);
2050 return range_check (result, "EXPONENT");
2055 gfc_simplify_float (gfc_expr *a)
2059 if (a->expr_type != EXPR_CONSTANT)
2068 ts.kind = gfc_default_real_kind;
2070 result = gfc_copy_expr (a);
2071 if (!gfc_convert_boz (result, &ts))
2073 gfc_free_expr (result);
2074 return &gfc_bad_expr;
2078 result = gfc_int2real (a, gfc_default_real_kind);
2079 return range_check (result, "FLOAT");
2084 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
2090 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
2092 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
2094 if (e->expr_type != EXPR_CONSTANT)
2097 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2099 gfc_set_model_kind (kind);
2101 mpfr_floor (floor, e->value.real);
2103 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2107 return range_check (result, "FLOOR");
2112 gfc_simplify_fraction (gfc_expr *x)
2115 mpfr_t absv, exp, pow2;
2117 if (x->expr_type != EXPR_CONSTANT)
2120 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2122 if (mpfr_sgn (x->value.real) == 0)
2124 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2128 gfc_set_model_kind (x->ts.kind);
2133 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2134 mpfr_log2 (exp, absv, GFC_RND_MODE);
2136 mpfr_trunc (exp, exp);
2137 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2139 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2141 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2143 mpfr_clears (exp, absv, pow2, NULL);
2145 return range_check (result, "FRACTION");
2150 gfc_simplify_gamma (gfc_expr *x)
2154 if (x->expr_type != EXPR_CONSTANT)
2157 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2159 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
2161 return range_check (result, "GAMMA");
2166 gfc_simplify_huge (gfc_expr *e)
2171 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2173 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2178 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2182 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
2194 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
2198 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2201 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2202 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
2203 return range_check (result, "HYPOT");
2207 /* We use the processor's collating sequence, because all
2208 systems that gfortran currently works on are ASCII. */
2211 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2216 if (e->expr_type != EXPR_CONSTANT)
2219 if (e->value.character.length != 1)
2221 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
2222 return &gfc_bad_expr;
2225 index = e->value.character.string[0];
2227 if (gfc_option.warn_surprising && index > 127)
2228 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2231 if ((result = int_expr_with_kind (index, kind, "IACHAR")) == NULL)
2232 return &gfc_bad_expr;
2234 result->where = e->where;
2236 return range_check (result, "IACHAR");
2241 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2245 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2248 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2250 mpz_and (result->value.integer, x->value.integer, y->value.integer);
2252 return range_check (result, "IAND");
2257 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2262 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2265 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2267 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
2268 return &gfc_bad_expr;
2271 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2273 if (pos >= gfc_integer_kinds[k].bit_size)
2275 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
2277 return &gfc_bad_expr;
2280 result = gfc_copy_expr (x);
2282 convert_mpz_to_unsigned (result->value.integer,
2283 gfc_integer_kinds[k].bit_size);
2285 mpz_clrbit (result->value.integer, pos);
2287 convert_mpz_to_signed (result->value.integer,
2288 gfc_integer_kinds[k].bit_size);
2295 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2302 if (x->expr_type != EXPR_CONSTANT
2303 || y->expr_type != EXPR_CONSTANT
2304 || z->expr_type != EXPR_CONSTANT)
2307 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2309 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
2310 return &gfc_bad_expr;
2313 if (gfc_extract_int (z, &len) != NULL || len < 0)
2315 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
2316 return &gfc_bad_expr;
2319 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2321 bitsize = gfc_integer_kinds[k].bit_size;
2323 if (pos + len > bitsize)
2325 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2326 "bit size at %L", &y->where);
2327 return &gfc_bad_expr;
2330 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2331 convert_mpz_to_unsigned (result->value.integer,
2332 gfc_integer_kinds[k].bit_size);
2334 bits = XCNEWVEC (int, bitsize);
2336 for (i = 0; i < bitsize; i++)
2339 for (i = 0; i < len; i++)
2340 bits[i] = mpz_tstbit (x->value.integer, i + pos);
2342 for (i = 0; i < bitsize; i++)
2345 mpz_clrbit (result->value.integer, i);
2346 else if (bits[i] == 1)
2347 mpz_setbit (result->value.integer, i);
2349 gfc_internal_error ("IBITS: Bad bit");
2354 convert_mpz_to_signed (result->value.integer,
2355 gfc_integer_kinds[k].bit_size);
2362 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2367 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2370 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2372 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
2373 return &gfc_bad_expr;
2376 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2378 if (pos >= gfc_integer_kinds[k].bit_size)
2380 gfc_error ("Second argument of IBSET exceeds bit size at %L",
2382 return &gfc_bad_expr;
2385 result = gfc_copy_expr (x);
2387 convert_mpz_to_unsigned (result->value.integer,
2388 gfc_integer_kinds[k].bit_size);
2390 mpz_setbit (result->value.integer, pos);
2392 convert_mpz_to_signed (result->value.integer,
2393 gfc_integer_kinds[k].bit_size);
2400 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2405 if (e->expr_type != EXPR_CONSTANT)
2408 if (e->value.character.length != 1)
2410 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
2411 return &gfc_bad_expr;
2414 index = e->value.character.string[0];
2416 if ((result = int_expr_with_kind (index, kind, "ICHAR")) == NULL)
2417 return &gfc_bad_expr;
2419 result->where = e->where;
2420 return range_check (result, "ICHAR");
2425 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2429 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2432 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2434 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
2436 return range_check (result, "IEOR");
2441 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2444 int back, len, lensub;
2445 int i, j, k, count, index = 0, start;
2447 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
2448 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
2451 if (b != NULL && b->value.logical != 0)
2456 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
2458 return &gfc_bad_expr;
2460 result = gfc_constant_result (BT_INTEGER, k, &x->where);
2462 len = x->value.character.length;
2463 lensub = y->value.character.length;
2467 mpz_set_si (result->value.integer, 0);
2475 mpz_set_si (result->value.integer, 1);
2478 else if (lensub == 1)
2480 for (i = 0; i < len; i++)
2482 for (j = 0; j < lensub; j++)
2484 if (y->value.character.string[j]
2485 == x->value.character.string[i])
2495 for (i = 0; i < len; i++)
2497 for (j = 0; j < lensub; j++)
2499 if (y->value.character.string[j]
2500 == x->value.character.string[i])
2505 for (k = 0; k < lensub; k++)
2507 if (y->value.character.string[k]
2508 == x->value.character.string[k + start])
2512 if (count == lensub)
2527 mpz_set_si (result->value.integer, len + 1);
2530 else if (lensub == 1)
2532 for (i = 0; i < len; i++)
2534 for (j = 0; j < lensub; j++)
2536 if (y->value.character.string[j]
2537 == x->value.character.string[len - i])
2539 index = len - i + 1;
2547 for (i = 0; i < len; i++)
2549 for (j = 0; j < lensub; j++)
2551 if (y->value.character.string[j]
2552 == x->value.character.string[len - i])
2555 if (start <= len - lensub)
2558 for (k = 0; k < lensub; k++)
2559 if (y->value.character.string[k]
2560 == x->value.character.string[k + start])
2563 if (count == lensub)
2580 mpz_set_si (result->value.integer, index);
2581 return range_check (result, "INDEX");
2586 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2588 gfc_expr *result = NULL;
2591 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
2593 return &gfc_bad_expr;
2595 if (e->expr_type != EXPR_CONSTANT)
2601 result = gfc_int2int (e, kind);
2605 result = gfc_real2int (e, kind);
2609 result = gfc_complex2int (e, kind);
2613 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
2614 return &gfc_bad_expr;
2617 return range_check (result, "INT");
2622 simplify_intconv (gfc_expr *e, int kind, const char *name)
2624 gfc_expr *result = NULL;
2626 if (e->expr_type != EXPR_CONSTANT)
2632 result = gfc_int2int (e, kind);
2636 result = gfc_real2int (e, kind);
2640 result = gfc_complex2int (e, kind);
2644 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
2645 return &gfc_bad_expr;
2648 return range_check (result, name);
2653 gfc_simplify_int2 (gfc_expr *e)
2655 return simplify_intconv (e, 2, "INT2");
2660 gfc_simplify_int8 (gfc_expr *e)
2662 return simplify_intconv (e, 8, "INT8");
2667 gfc_simplify_long (gfc_expr *e)
2669 return simplify_intconv (e, 4, "LONG");
2674 gfc_simplify_ifix (gfc_expr *e)
2676 gfc_expr *rtrunc, *result;
2678 if (e->expr_type != EXPR_CONSTANT)
2681 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2684 rtrunc = gfc_copy_expr (e);
2686 mpfr_trunc (rtrunc->value.real, e->value.real);
2687 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2689 gfc_free_expr (rtrunc);
2690 return range_check (result, "IFIX");
2695 gfc_simplify_idint (gfc_expr *e)
2697 gfc_expr *rtrunc, *result;
2699 if (e->expr_type != EXPR_CONSTANT)
2702 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2705 rtrunc = gfc_copy_expr (e);
2707 mpfr_trunc (rtrunc->value.real, e->value.real);
2708 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2710 gfc_free_expr (rtrunc);
2711 return range_check (result, "IDINT");
2716 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2720 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2723 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2725 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2726 return range_check (result, "IOR");
2731 gfc_simplify_is_iostat_end (gfc_expr *x)
2735 if (x->expr_type != EXPR_CONSTANT)
2738 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2740 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_END) == 0);
2747 gfc_simplify_is_iostat_eor (gfc_expr *x)
2751 if (x->expr_type != EXPR_CONSTANT)
2754 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2756 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_EOR) == 0);
2763 gfc_simplify_isnan (gfc_expr *x)
2767 if (x->expr_type != EXPR_CONSTANT)
2770 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2772 result->value.logical = mpfr_nan_p (x->value.real);
2779 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
2782 int shift, ashift, isize, k, *bits, i;
2784 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2787 if (gfc_extract_int (s, &shift) != NULL)
2789 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
2790 return &gfc_bad_expr;
2793 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2795 isize = gfc_integer_kinds[k].bit_size;
2804 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
2805 "at %L", &s->where);
2806 return &gfc_bad_expr;
2809 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2813 mpz_set (result->value.integer, e->value.integer);
2814 return range_check (result, "ISHFT");
2817 bits = XCNEWVEC (int, isize);
2819 for (i = 0; i < isize; i++)
2820 bits[i] = mpz_tstbit (e->value.integer, i);
2824 for (i = 0; i < shift; i++)
2825 mpz_clrbit (result->value.integer, i);
2827 for (i = 0; i < isize - shift; i++)
2830 mpz_clrbit (result->value.integer, i + shift);
2832 mpz_setbit (result->value.integer, i + shift);
2837 for (i = isize - 1; i >= isize - ashift; i--)
2838 mpz_clrbit (result->value.integer, i);
2840 for (i = isize - 1; i >= ashift; i--)
2843 mpz_clrbit (result->value.integer, i - ashift);
2845 mpz_setbit (result->value.integer, i - ashift);
2849 convert_mpz_to_signed (result->value.integer, isize);
2857 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
2860 int shift, ashift, isize, ssize, delta, k;
2863 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2866 if (gfc_extract_int (s, &shift) != NULL)
2868 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
2869 return &gfc_bad_expr;
2872 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2873 isize = gfc_integer_kinds[k].bit_size;
2877 if (sz->expr_type != EXPR_CONSTANT)
2880 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
2882 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
2883 return &gfc_bad_expr;
2888 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
2889 "BIT_SIZE of first argument at %L", &s->where);
2890 return &gfc_bad_expr;
2904 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2905 "third argument at %L", &s->where);
2907 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2908 "BIT_SIZE of first argument at %L", &s->where);
2909 return &gfc_bad_expr;
2912 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2914 mpz_set (result->value.integer, e->value.integer);
2919 convert_mpz_to_unsigned (result->value.integer, isize);
2921 bits = XCNEWVEC (int, ssize);
2923 for (i = 0; i < ssize; i++)
2924 bits[i] = mpz_tstbit (e->value.integer, i);
2926 delta = ssize - ashift;
2930 for (i = 0; i < delta; i++)
2933 mpz_clrbit (result->value.integer, i + shift);
2935 mpz_setbit (result->value.integer, i + shift);
2938 for (i = delta; i < ssize; i++)
2941 mpz_clrbit (result->value.integer, i - delta);
2943 mpz_setbit (result->value.integer, i - delta);
2948 for (i = 0; i < ashift; i++)
2951 mpz_clrbit (result->value.integer, i + delta);
2953 mpz_setbit (result->value.integer, i + delta);
2956 for (i = ashift; i < ssize; i++)
2959 mpz_clrbit (result->value.integer, i + shift);
2961 mpz_setbit (result->value.integer, i + shift);
2965 convert_mpz_to_signed (result->value.integer, isize);
2973 gfc_simplify_kind (gfc_expr *e)
2976 if (e->ts.type == BT_DERIVED)
2978 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
2979 return &gfc_bad_expr;
2982 return gfc_int_expr (e->ts.kind);
2987 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
2988 gfc_array_spec *as, gfc_ref *ref)
2990 gfc_expr *l, *u, *result;
2993 /* The last dimension of an assumed-size array is special. */
2994 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2996 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2997 return gfc_copy_expr (as->lower[d-1]);
3002 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3003 gfc_default_integer_kind);
3005 return &gfc_bad_expr;
3007 result = gfc_constant_result (BT_INTEGER, k, &array->where);
3010 /* Then, we need to know the extent of the given dimension. */
3011 if (ref->u.ar.type == AR_FULL)
3016 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
3019 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
3023 mpz_set_si (result->value.integer, 0);
3025 mpz_set_si (result->value.integer, 1);
3029 /* Nonzero extent. */
3031 mpz_set (result->value.integer, u->value.integer);
3033 mpz_set (result->value.integer, l->value.integer);
3040 if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer)
3045 mpz_set_si (result->value.integer, (long int) 1);
3048 return range_check (result, upper ? "UBOUND" : "LBOUND");
3053 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3059 if (array->expr_type != EXPR_VARIABLE)
3062 /* Follow any component references. */
3063 as = array->symtree->n.sym->as;
3064 for (ref = array->ref; ref; ref = ref->next)
3069 switch (ref->u.ar.type)
3076 /* We're done because 'as' has already been set in the
3077 previous iteration. */
3094 as = ref->u.c.component->as;
3106 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
3111 /* Multi-dimensional bounds. */
3112 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3114 gfc_constructor *head, *tail;
3117 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3118 if (upper && as->type == AS_ASSUMED_SIZE)
3120 /* An error message will be emitted in
3121 check_assumed_size_reference (resolve.c). */
3122 return &gfc_bad_expr;
3125 /* Simplify the bounds for each dimension. */
3126 for (d = 0; d < array->rank; d++)
3128 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref);
3129 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3133 for (j = 0; j < d; j++)
3134 gfc_free_expr (bounds[j]);
3139 /* Allocate the result expression. */
3140 e = gfc_get_expr ();
3141 e->where = array->where;
3142 e->expr_type = EXPR_ARRAY;
3143 e->ts.type = BT_INTEGER;
3144 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3145 gfc_default_integer_kind);
3149 return &gfc_bad_expr;
3153 /* The result is a rank 1 array; its size is the rank of the first
3154 argument to {L,U}BOUND. */
3156 e->shape = gfc_get_shape (1);
3157 mpz_init_set_ui (e->shape[0], array->rank);
3159 /* Create the constructor for this array. */
3161 for (d = 0; d < array->rank; d++)
3163 /* Get a new constructor element. */
3165 head = tail = gfc_get_constructor ();
3168 tail->next = gfc_get_constructor ();
3172 tail->where = e->where;
3173 tail->expr = bounds[d];
3175 e->value.constructor = head;
3181 /* A DIM argument is specified. */
3182 if (dim->expr_type != EXPR_CONSTANT)
3185 d = mpz_get_si (dim->value.integer);
3187 if (d < 1 || d > as->rank
3188 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
3190 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3191 return &gfc_bad_expr;
3194 return simplify_bound_dim (array, kind, d, upper, as, ref);
3200 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3202 return simplify_bound (array, dim, kind, 0);
3207 gfc_simplify_leadz (gfc_expr *e)
3210 unsigned long lz, bs;
3213 if (e->expr_type != EXPR_CONSTANT)
3216 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3217 bs = gfc_integer_kinds[i].bit_size;
3218 if (mpz_cmp_si (e->value.integer, 0) == 0)
3220 else if (mpz_cmp_si (e->value.integer, 0) < 0)
3223 lz = bs - mpz_sizeinbase (e->value.integer, 2);
3225 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3227 mpz_set_ui (result->value.integer, lz);
3234 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3237 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
3240 return &gfc_bad_expr;
3242 if (e->expr_type == EXPR_CONSTANT)
3244 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3245 mpz_set_si (result->value.integer, e->value.character.length);
3246 if (gfc_range_check (result) == ARITH_OK)
3250 gfc_free_expr (result);
3255 if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
3256 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
3257 && e->ts.u.cl->length->ts.type == BT_INTEGER)
3259 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3260 mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
3261 if (gfc_range_check (result) == ARITH_OK)
3265 gfc_free_expr (result);
3275 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3278 int count, len, lentrim, i;
3279 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
3282 return &gfc_bad_expr;
3284 if (e->expr_type != EXPR_CONSTANT)
3287 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3288 len = e->value.character.length;
3290 for (count = 0, i = 1; i <= len; i++)
3291 if (e->value.character.string[len - i] == ' ')
3296 lentrim = len - count;
3298 mpz_set_si (result->value.integer, lentrim);
3299 return range_check (result, "LEN_TRIM");
3303 gfc_simplify_lgamma (gfc_expr *x ATTRIBUTE_UNUSED)
3308 if (x->expr_type != EXPR_CONSTANT)
3311 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3313 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3315 return range_check (result, "LGAMMA");
3320 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3322 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3325 return gfc_logical_expr (gfc_compare_string (a, b) >= 0, &a->where);
3330 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3332 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3335 return gfc_logical_expr (gfc_compare_string (a, b) > 0,
3341 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3343 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3346 return gfc_logical_expr (gfc_compare_string (a, b) <= 0, &a->where);
3351 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3353 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3356 return gfc_logical_expr (gfc_compare_string (a, b) < 0, &a->where);
3361 gfc_simplify_log (gfc_expr *x)
3365 if (x->expr_type != EXPR_CONSTANT)
3368 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3374 if (mpfr_sgn (x->value.real) <= 0)
3376 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3377 "to zero", &x->where);
3378 gfc_free_expr (result);
3379 return &gfc_bad_expr;
3382 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3386 if ((mpfr_sgn (mpc_realref (x->value.complex)) == 0)
3387 && (mpfr_sgn (mpc_imagref (x->value.complex)) == 0))
3389 gfc_error ("Complex argument of LOG at %L cannot be zero",
3391 gfc_free_expr (result);
3392 return &gfc_bad_expr;
3395 gfc_set_model_kind (x->ts.kind);
3397 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3404 mpfr_atan2 (result->value.complex.i, x->value.complex.i,
3405 x->value.complex.r, GFC_RND_MODE);
3407 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
3408 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
3409 mpfr_add (xr, xr, xi, GFC_RND_MODE);
3410 mpfr_sqrt (xr, xr, GFC_RND_MODE);
3411 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
3413 mpfr_clears (xr, xi, NULL);
3419 gfc_internal_error ("gfc_simplify_log: bad type");
3422 return range_check (result, "LOG");
3427 gfc_simplify_log10 (gfc_expr *x)
3431 if (x->expr_type != EXPR_CONSTANT)
3434 if (mpfr_sgn (x->value.real) <= 0)
3436 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3437 "to zero", &x->where);
3438 return &gfc_bad_expr;
3441 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3443 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3445 return range_check (result, "LOG10");
3450 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3455 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3457 return &gfc_bad_expr;
3459 if (e->expr_type != EXPR_CONSTANT)
3462 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
3464 result->value.logical = e->value.logical;
3471 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3474 gfc_constructor *ma_ctor, *mb_ctor;
3475 int row, result_rows, col, result_columns, stride_a, stride_b;
3477 if (!is_constant_array_expr (matrix_a)
3478 || !is_constant_array_expr (matrix_b))
3481 gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
3482 result = gfc_start_constructor (matrix_a->ts.type,
3486 if (matrix_a->rank == 1 && matrix_b->rank == 2)
3489 result_columns = mpz_get_si (matrix_b->shape[0]);
3491 stride_b = mpz_get_si (matrix_b->shape[0]);
3494 result->shape = gfc_get_shape (result->rank);
3495 mpz_init_set_si (result->shape[0], result_columns);
3497 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
3499 result_rows = mpz_get_si (matrix_b->shape[0]);
3501 stride_a = mpz_get_si (matrix_a->shape[0]);
3505 result->shape = gfc_get_shape (result->rank);
3506 mpz_init_set_si (result->shape[0], result_rows);
3508 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
3510 result_rows = mpz_get_si (matrix_a->shape[0]);
3511 result_columns = mpz_get_si (matrix_b->shape[1]);
3512 stride_a = mpz_get_si (matrix_a->shape[1]);
3513 stride_b = mpz_get_si (matrix_b->shape[0]);
3516 result->shape = gfc_get_shape (result->rank);
3517 mpz_init_set_si (result->shape[0], result_rows);
3518 mpz_init_set_si (result->shape[1], result_columns);
3523 ma_ctor = matrix_a->value.constructor;
3524 mb_ctor = matrix_b->value.constructor;
3526 for (col = 0; col < result_columns; ++col)
3528 ma_ctor = matrix_a->value.constructor;
3530 for (row = 0; row < result_rows; ++row)
3533 e = compute_dot_product (ma_ctor, stride_a,
3536 gfc_append_constructor (result, e);
3538 ADVANCE (ma_ctor, 1);
3541 ADVANCE (mb_ctor, stride_b);
3549 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3551 if (tsource->expr_type != EXPR_CONSTANT
3552 || fsource->expr_type != EXPR_CONSTANT
3553 || mask->expr_type != EXPR_CONSTANT)
3556 return gfc_copy_expr (mask->value.logical ? tsource : fsource);
3560 /* Selects bewteen current value and extremum for simplify_min_max
3561 and simplify_minval_maxval. */
3563 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
3565 switch (arg->ts.type)
3568 if (mpz_cmp (arg->value.integer,
3569 extremum->value.integer) * sign > 0)
3570 mpz_set (extremum->value.integer, arg->value.integer);
3574 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
3576 mpfr_max (extremum->value.real, extremum->value.real,
3577 arg->value.real, GFC_RND_MODE);
3579 mpfr_min (extremum->value.real, extremum->value.real,
3580 arg->value.real, GFC_RND_MODE);
3584 #define LENGTH(x) ((x)->value.character.length)
3585 #define STRING(x) ((x)->value.character.string)
3586 if (LENGTH(extremum) < LENGTH(arg))
3588 gfc_char_t *tmp = STRING(extremum);
3590 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
3591 memcpy (STRING(extremum), tmp,
3592 LENGTH(extremum) * sizeof (gfc_char_t));
3593 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
3594 LENGTH(arg) - LENGTH(extremum));
3595 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
3596 LENGTH(extremum) = LENGTH(arg);
3600 if (gfc_compare_string (arg, extremum) * sign > 0)
3602 gfc_free (STRING(extremum));
3603 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
3604 memcpy (STRING(extremum), STRING(arg),
3605 LENGTH(arg) * sizeof (gfc_char_t));
3606 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
3607 LENGTH(extremum) - LENGTH(arg));
3608 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
3615 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
3620 /* This function is special since MAX() can take any number of
3621 arguments. The simplified expression is a rewritten version of the
3622 argument list containing at most one constant element. Other
3623 constant elements are deleted. Because the argument list has
3624 already been checked, this function always succeeds. sign is 1 for
3625 MAX(), -1 for MIN(). */
3628 simplify_min_max (gfc_expr *expr, int sign)
3630 gfc_actual_arglist *arg, *last, *extremum;
3631 gfc_intrinsic_sym * specific;
3635 specific = expr->value.function.isym;
3637 arg = expr->value.function.actual;
3639 for (; arg; last = arg, arg = arg->next)
3641 if (arg->expr->expr_type != EXPR_CONSTANT)
3644 if (extremum == NULL)
3650 min_max_choose (arg->expr, extremum->expr, sign);
3652 /* Delete the extra constant argument. */
3654 expr->value.function.actual = arg->next;
3656 last->next = arg->next;
3659 gfc_free_actual_arglist (arg);
3663 /* If there is one value left, replace the function call with the
3665 if (expr->value.function.actual->next != NULL)
3668 /* Convert to the correct type and kind. */
3669 if (expr->ts.type != BT_UNKNOWN)
3670 return gfc_convert_constant (expr->value.function.actual->expr,
3671 expr->ts.type, expr->ts.kind);
3673 if (specific->ts.type != BT_UNKNOWN)
3674 return gfc_convert_constant (expr->value.function.actual->expr,
3675 specific->ts.type, specific->ts.kind);
3677 return gfc_copy_expr (expr->value.function.actual->expr);
3682 gfc_simplify_min (gfc_expr *e)
3684 return simplify_min_max (e, -1);
3689 gfc_simplify_max (gfc_expr *e)
3691 return simplify_min_max (e, 1);
3695 /* This is a simplified version of simplify_min_max to provide
3696 simplification of minval and maxval for a vector. */
3699 simplify_minval_maxval (gfc_expr *expr, int sign)
3701 gfc_constructor *ctr, *extremum;
3702 gfc_intrinsic_sym * specific;
3705 specific = expr->value.function.isym;
3707 ctr = expr->value.constructor;
3709 for (; ctr; ctr = ctr->next)
3711 if (ctr->expr->expr_type != EXPR_CONSTANT)
3714 if (extremum == NULL)
3720 min_max_choose (ctr->expr, extremum->expr, sign);
3723 if (extremum == NULL)
3726 /* Convert to the correct type and kind. */
3727 if (expr->ts.type != BT_UNKNOWN)
3728 return gfc_convert_constant (extremum->expr,
3729 expr->ts.type, expr->ts.kind);
3731 if (specific->ts.type != BT_UNKNOWN)
3732 return gfc_convert_constant (extremum->expr,
3733 specific->ts.type, specific->ts.kind);
3735 return gfc_copy_expr (extremum->expr);
3740 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3742 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3745 return simplify_minval_maxval (array, -1);
3750 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3752 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3754 return simplify_minval_maxval (array, 1);
3759 gfc_simplify_maxexponent (gfc_expr *x)
3764 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3766 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
3767 result->where = x->where;
3774 gfc_simplify_minexponent (gfc_expr *x)
3779 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3781 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
3782 result->where = x->where;
3789 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
3795 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3798 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3799 result = gfc_constant_result (a->ts.type, kind, &a->where);
3804 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3806 /* Result is processor-dependent. */
3807 gfc_error ("Second argument MOD at %L is zero", &a->where);
3808 gfc_free_expr (result);
3809 return &gfc_bad_expr;
3811 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
3815 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3817 /* Result is processor-dependent. */
3818 gfc_error ("Second argument of MOD at %L is zero", &p->where);
3819 gfc_free_expr (result);
3820 return &gfc_bad_expr;
3823 gfc_set_model_kind (kind);
3825 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3826 mpfr_trunc (tmp, tmp);
3827 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3828 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3833 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
3836 return range_check (result, "MOD");
3841 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
3847 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3850 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3851 result = gfc_constant_result (a->ts.type, kind, &a->where);
3856 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3858 /* Result is processor-dependent. This processor just opts
3859 to not handle it at all. */
3860 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
3861 gfc_free_expr (result);
3862 return &gfc_bad_expr;
3864 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
3869 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3871 /* Result is processor-dependent. */
3872 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
3873 gfc_free_expr (result);
3874 return &gfc_bad_expr;
3877 gfc_set_model_kind (kind);
3879 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3880 mpfr_floor (tmp, tmp);
3881 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3882 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3887 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
3890 return range_check (result, "MODULO");
3894 /* Exists for the sole purpose of consistency with other intrinsics. */
3896 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
3897 gfc_expr *fp ATTRIBUTE_UNUSED,
3898 gfc_expr *l ATTRIBUTE_UNUSED,
3899 gfc_expr *to ATTRIBUTE_UNUSED,
3900 gfc_expr *tp ATTRIBUTE_UNUSED)
3907 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
3910 mp_exp_t emin, emax;
3913 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3916 if (mpfr_sgn (s->value.real) == 0)
3918 gfc_error ("Second argument of NEAREST at %L shall not be zero",
3920 return &gfc_bad_expr;
3923 result = gfc_copy_expr (x);
3925 /* Save current values of emin and emax. */
3926 emin = mpfr_get_emin ();
3927 emax = mpfr_get_emax ();
3929 /* Set emin and emax for the current model number. */
3930 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
3931 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[kind].min_exponent -
3932 mpfr_get_prec(result->value.real) + 1);
3933 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[kind].max_exponent - 1);
3934 mpfr_check_range (result->value.real, 0, GMP_RNDU);
3936 if (mpfr_sgn (s->value.real) > 0)
3938 mpfr_nextabove (result->value.real);
3939 mpfr_subnormalize (result->value.real, 0, GMP_RNDU);
3943 mpfr_nextbelow (result->value.real);
3944 mpfr_subnormalize (result->value.real, 0, GMP_RNDD);
3947 mpfr_set_emin (emin);
3948 mpfr_set_emax (emax);
3950 /* Only NaN can occur. Do not use range check as it gives an
3951 error for denormal numbers. */
3952 if (mpfr_nan_p (result->value.real) && gfc_option.flag_range_check)
3954 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
3955 gfc_free_expr (result);
3956 return &gfc_bad_expr;
3964 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
3966 gfc_expr *itrunc, *result;
3969 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
3971 return &gfc_bad_expr;
3973 if (e->expr_type != EXPR_CONSTANT)
3976 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
3978 itrunc = gfc_copy_expr (e);
3980 mpfr_round (itrunc->value.real, e->value.real);
3982 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
3984 gfc_free_expr (itrunc);
3986 return range_check (result, name);
3991 gfc_simplify_new_line (gfc_expr *e)
3995 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3996 result->value.character.string = gfc_get_wide_string (2);
3997 result->value.character.length = 1;
3998 result->value.character.string[0] = '\n';
3999 result->value.character.string[1] = '\0'; /* For debugger */
4005 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
4007 return simplify_nint ("NINT", e, k);
4012 gfc_simplify_idnint (gfc_expr *e)
4014 return simplify_nint ("IDNINT", e, NULL);
4019 gfc_simplify_not (gfc_expr *e)
4023 if (e->expr_type != EXPR_CONSTANT)
4026 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
4028 mpz_com (result->value.integer, e->value.integer);
4030 return range_check (result, "NOT");
4035 gfc_simplify_null (gfc_expr *mold)
4041 result = gfc_get_expr ();
4042 result->ts.type = BT_UNKNOWN;
4045 result = gfc_copy_expr (mold);
4046 result->expr_type = EXPR_NULL;
4053 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
4058 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4061 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
4062 if (x->ts.type == BT_INTEGER)
4064 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
4065 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
4066 return range_check (result, "OR");
4068 else /* BT_LOGICAL */
4070 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
4071 result->value.logical = x->value.logical || y->value.logical;
4078 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
4081 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
4083 if (!is_constant_array_expr(array)
4084 || !is_constant_array_expr(vector)
4085 || (!gfc_is_constant_expr (mask)
4086 && !is_constant_array_expr(mask)))
4089 result = gfc_start_constructor (array->ts.type,
4093 array_ctor = array->value.constructor;
4094 vector_ctor = vector ? vector->value.constructor : NULL;
4096 if (mask->expr_type == EXPR_CONSTANT
4097 && mask->value.logical)
4099 /* Copy all elements of ARRAY to RESULT. */
4102 gfc_append_constructor (result,
4103 gfc_copy_expr (array_ctor->expr));
4105 ADVANCE (array_ctor, 1);
4106 ADVANCE (vector_ctor, 1);
4109 else if (mask->expr_type == EXPR_ARRAY)
4111 /* Copy only those elements of ARRAY to RESULT whose
4112 MASK equals .TRUE.. */
4113 mask_ctor = mask->value.constructor;
4116 if (mask_ctor->expr->value.logical)
4118 gfc_append_constructor (result,
4119 gfc_copy_expr (array_ctor->expr));
4120 ADVANCE (vector_ctor, 1);
4123 ADVANCE (array_ctor, 1);
4124 ADVANCE (mask_ctor, 1);
4128 /* Append any left-over elements from VECTOR to RESULT. */
4131 gfc_append_constructor (result,
4132 gfc_copy_expr (vector_ctor->expr));
4133 ADVANCE (vector_ctor, 1);
4136 result->shape = gfc_get_shape (1);
4137 gfc_array_size (result, &result->shape[0]);
4139 if (array->ts.type == BT_CHARACTER)
4140 result->ts.u.cl = array->ts.u.cl;
4147 gfc_simplify_precision (gfc_expr *e)
4152 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4154 result = gfc_int_expr (gfc_real_kinds[i].precision);
4155 result->where = e->where;
4162 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
4166 if (!is_constant_array_expr (array)
4167 || !gfc_is_constant_expr (dim))
4171 && !is_constant_array_expr (mask)
4172 && mask->expr_type != EXPR_CONSTANT)
4175 result = transformational_result (array, dim, array->ts.type,
4176 array->ts.kind, &array->where);
4177 init_result_expr (result, 1, NULL);
4179 return !dim || array->rank == 1 ?
4180 simplify_transformation_to_scalar (result, array, mask, gfc_multiply) :
4181 simplify_transformation_to_array (result, array, dim, mask, gfc_multiply);
4186 gfc_simplify_radix (gfc_expr *e)
4191 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4195 i = gfc_integer_kinds[i].radix;
4199 i = gfc_real_kinds[i].radix;
4206 result = gfc_int_expr (i);
4207 result->where = e->where;
4214 gfc_simplify_range (gfc_expr *e)
4220 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4225 j = gfc_integer_kinds[i].range;
4230 j = gfc_real_kinds[i].range;
4237 result = gfc_int_expr (j);
4238 result->where = e->where;
4245 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
4247 gfc_expr *result = NULL;
4250 if (e->ts.type == BT_COMPLEX)
4251 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
4253 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
4256 return &gfc_bad_expr;
4258 if (e->expr_type != EXPR_CONSTANT)
4265 result = gfc_int2real (e, kind);
4269 result = gfc_real2real (e, kind);
4273 result = gfc_complex2real (e, kind);
4277 gfc_internal_error ("bad type in REAL");
4281 if (e->ts.type == BT_INTEGER && e->is_boz)
4287 result = gfc_copy_expr (e);
4288 if (!gfc_convert_boz (result, &ts))
4290 gfc_free_expr (result);
4291 return &gfc_bad_expr;
4295 return range_check (result, "REAL");
4300 gfc_simplify_realpart (gfc_expr *e)
4304 if (e->expr_type != EXPR_CONSTANT)
4307 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
4309 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
4311 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
4314 return range_check (result, "REALPART");
4318 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
4321 int i, j, len, ncop, nlen;
4323 bool have_length = false;
4325 /* If NCOPIES isn't a constant, there's nothing we can do. */
4326 if (n->expr_type != EXPR_CONSTANT)
4329 /* If NCOPIES is negative, it's an error. */
4330 if (mpz_sgn (n->value.integer) < 0)
4332 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4334 return &gfc_bad_expr;
4337 /* If we don't know the character length, we can do no more. */
4338 if (e->ts.u.cl && e->ts.u.cl->length
4339 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
4341 len = mpz_get_si (e->ts.u.cl->length->value.integer);
4344 else if (e->expr_type == EXPR_CONSTANT
4345 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
4347 len = e->value.character.length;
4352 /* If the source length is 0, any value of NCOPIES is valid
4353 and everything behaves as if NCOPIES == 0. */
4356 mpz_set_ui (ncopies, 0);
4358 mpz_set (ncopies, n->value.integer);
4360 /* Check that NCOPIES isn't too large. */
4366 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4368 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4372 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
4373 e->ts.u.cl->length->value.integer);
4377 mpz_init_set_si (mlen, len);
4378 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
4382 /* The check itself. */
4383 if (mpz_cmp (ncopies, max) > 0)
4386 mpz_clear (ncopies);
4387 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4389 return &gfc_bad_expr;
4394 mpz_clear (ncopies);
4396 /* For further simplification, we need the character string to be
4398 if (e->expr_type != EXPR_CONSTANT)
4402 (e->ts.u.cl->length &&
4403 mpz_sgn (e->ts.u.cl->length->value.integer)) != 0)
4405 const char *res = gfc_extract_int (n, &ncop);
4406 gcc_assert (res == NULL);
4411 len = e->value.character.length;
4414 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
4418 result->value.character.string = gfc_get_wide_string (1);
4419 result->value.character.length = 0;
4420 result->value.character.string[0] = '\0';
4424 result->value.character.length = nlen;
4425 result->value.character.string = gfc_get_wide_string (nlen + 1);
4427 for (i = 0; i < ncop; i++)
4428 for (j = 0; j < len; j++)
4429 result->value.character.string[j+i*len]= e->value.character.string[j];
4431 result->value.character.string[nlen] = '\0'; /* For debugger */
4436 /* This one is a bear, but mainly has to do with shuffling elements. */
4439 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
4440 gfc_expr *pad, gfc_expr *order_exp)
4442 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
4443 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
4444 gfc_constructor *head, *tail;
4450 /* Check that argument expression types are OK. */
4451 if (!is_constant_array_expr (source)
4452 || !is_constant_array_expr (shape_exp)
4453 || !is_constant_array_expr (pad)
4454 || !is_constant_array_expr (order_exp))
4457 /* Proceed with simplification, unpacking the array. */
4465 e = gfc_get_array_element (shape_exp, rank);
4469 gfc_extract_int (e, &shape[rank]);
4471 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
4472 gcc_assert (shape[rank] >= 0);
4478 gcc_assert (rank > 0);
4480 /* Now unpack the order array if present. */
4481 if (order_exp == NULL)
4483 for (i = 0; i < rank; i++)
4488 for (i = 0; i < rank; i++)
4491 for (i = 0; i < rank; i++)
4493 e = gfc_get_array_element (order_exp, i);
4496 gfc_extract_int (e, &order[i]);
4499 gcc_assert (order[i] >= 1 && order[i] <= rank);
4501 gcc_assert (x[order[i]] == 0);
4506 /* Count the elements in the source and padding arrays. */
4511 gfc_array_size (pad, &size);
4512 npad = mpz_get_ui (size);
4516 gfc_array_size (source, &size);
4517 nsource = mpz_get_ui (size);
4520 /* If it weren't for that pesky permutation we could just loop
4521 through the source and round out any shortage with pad elements.
4522 But no, someone just had to have the compiler do something the
4523 user should be doing. */
4525 for (i = 0; i < rank; i++)
4528 while (nsource > 0 || npad > 0)
4530 /* Figure out which element to extract. */
4531 mpz_set_ui (index, 0);
4533 for (i = rank - 1; i >= 0; i--)
4535 mpz_add_ui (index, index, x[order[i]]);
4537 mpz_mul_ui (index, index, shape[order[i - 1]]);
4540 if (mpz_cmp_ui (index, INT_MAX) > 0)
4541 gfc_internal_error ("Reshaped array too large at %C");
4543 j = mpz_get_ui (index);
4546 e = gfc_get_array_element (source, j);
4549 gcc_assert (npad > 0);
4553 e = gfc_get_array_element (pad, j);
4558 head = tail = gfc_get_constructor ();
4561 tail->next = gfc_get_constructor ();
4565 tail->where = e->where;
4568 /* Calculate the next element. */
4572 if (++x[i] < shape[i])
4583 e = gfc_get_expr ();
4584 e->where = source->where;
4585 e->expr_type = EXPR_ARRAY;
4586 e->value.constructor = head;
4587 e->shape = gfc_get_shape (rank);
4589 for (i = 0; i < rank; i++)
4590 mpz_init_set_ui (e->shape[i], shape[i]);
4600 gfc_simplify_rrspacing (gfc_expr *x)
4606 if (x->expr_type != EXPR_CONSTANT)
4609 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
4611 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4613 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
4615 /* Special case x = -0 and 0. */
4616 if (mpfr_sgn (result->value.real) == 0)
4618 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4622 /* | x * 2**(-e) | * 2**p. */
4623 e = - (long int) mpfr_get_exp (x->value.real);
4624 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
4626 p = (long int) gfc_real_kinds[i].digits;
4627 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
4629 return range_check (result, "RRSPACING");
4634 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
4636 int k, neg_flag, power, exp_range;
4637 mpfr_t scale, radix;
4640 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4643 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4645 if (mpfr_sgn (x->value.real) == 0)
4647 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4651 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
4653 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
4655 /* This check filters out values of i that would overflow an int. */
4656 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
4657 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
4659 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
4660 gfc_free_expr (result);
4661 return &gfc_bad_expr;
4664 /* Compute scale = radix ** power. */
4665 power = mpz_get_si (i->value.integer);
4675 gfc_set_model_kind (x->ts.kind);
4678 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
4679 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
4682 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
4684 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
4686 mpfr_clears (scale, radix, NULL);
4688 return range_check (result, "SCALE");
4692 /* Variants of strspn and strcspn that operate on wide characters. */
4695 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
4698 const gfc_char_t *c;
4702 for (c = s2; *c; c++)
4716 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
4719 const gfc_char_t *c;
4723 for (c = s2; *c; c++)
4738 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
4743 size_t indx, len, lenc;
4744 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
4747 return &gfc_bad_expr;
4749 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
4752 if (b != NULL && b->value.logical != 0)
4757 result = gfc_constant_result (BT_INTEGER, k, &e->where);
4759 len = e->value.character.length;
4760 lenc = c->value.character.length;
4762 if (len == 0 || lenc == 0)
4770 indx = wide_strcspn (e->value.character.string,
4771 c->value.character.string) + 1;
4778 for (indx = len; indx > 0; indx--)
4780 for (i = 0; i < lenc; i++)
4782 if (c->value.character.string[i]
4783 == e->value.character.string[indx - 1])
4791 mpz_set_ui (result->value.integer, indx);
4792 return range_check (result, "SCAN");
4797 gfc_simplify_selected_char_kind (gfc_expr *e)
4802 if (e->expr_type != EXPR_CONSTANT)
4805 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
4806 || gfc_compare_with_Cstring (e, "default", false) == 0)
4808 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
4813 result = gfc_int_expr (kind);
4814 result->where = e->where;
4821 gfc_simplify_selected_int_kind (gfc_expr *e)
4826 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
4831 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
4832 if (gfc_integer_kinds[i].range >= range
4833 && gfc_integer_kinds[i].kind < kind)
4834 kind = gfc_integer_kinds[i].kind;
4836 if (kind == INT_MAX)
4839 result = gfc_int_expr (kind);
4840 result->where = e->where;
4847 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q)
4849 int range, precision, i, kind, found_precision, found_range;
4856 if (p->expr_type != EXPR_CONSTANT
4857 || gfc_extract_int (p, &precision) != NULL)
4865 if (q->expr_type != EXPR_CONSTANT
4866 || gfc_extract_int (q, &range) != NULL)
4871 found_precision = 0;
4874 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
4876 if (gfc_real_kinds[i].precision >= precision)
4877 found_precision = 1;
4879 if (gfc_real_kinds[i].range >= range)
4882 if (gfc_real_kinds[i].precision >= precision
4883 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
4884 kind = gfc_real_kinds[i].kind;
4887 if (kind == INT_MAX)
4891 if (!found_precision)
4897 result = gfc_int_expr (kind);
4898 result->where = (p != NULL) ? p->where : q->where;
4905 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
4908 mpfr_t exp, absv, log2, pow2, frac;
4911 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4914 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4916 if (mpfr_sgn (x->value.real) == 0)
4918 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4922 gfc_set_model_kind (x->ts.kind);
4929 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
4930 mpfr_log2 (log2, absv, GFC_RND_MODE);
4932 mpfr_trunc (log2, log2);
4933 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
4935 /* Old exponent value, and fraction. */
4936 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
4938 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
4941 exp2 = (unsigned long) mpz_get_d (i->value.integer);
4942 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
4944 mpfr_clears (absv, log2, pow2, frac, NULL);
4946 return range_check (result, "SET_EXPONENT");
4951 gfc_simplify_shape (gfc_expr *source)
4953 mpz_t shape[GFC_MAX_DIMENSIONS];
4954 gfc_expr *result, *e, *f;
4959 if (source->rank == 0)
4960 return gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4963 if (source->expr_type != EXPR_VARIABLE)
4966 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4969 ar = gfc_find_array_ref (source);
4971 t = gfc_array_ref_shape (ar, shape);
4973 for (n = 0; n < source->rank; n++)
4975 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
4980 mpz_set (e->value.integer, shape[n]);
4981 mpz_clear (shape[n]);
4985 mpz_set_ui (e->value.integer, n + 1);
4987 f = gfc_simplify_size (source, e, NULL);
4991 gfc_free_expr (result);
5000 gfc_append_constructor (result, e);
5008 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5013 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
5016 return &gfc_bad_expr;
5020 if (gfc_array_size (array, &size) == FAILURE)
5025 if (dim->expr_type != EXPR_CONSTANT)
5028 d = mpz_get_ui (dim->value.integer) - 1;
5029 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
5033 result = gfc_constant_result (BT_INTEGER, k, &array->where);
5034 mpz_set (result->value.integer, size);
5040 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
5044 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
5047 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5052 mpz_abs (result->value.integer, x->value.integer);
5053 if (mpz_sgn (y->value.integer) < 0)
5054 mpz_neg (result->value.integer, result->value.integer);
5058 if (gfc_option.flag_sign_zero)
5059 mpfr_copysign (result->value.real, x->value.real, y->value.real,
5062 mpfr_setsign (result->value.real, x->value.real,
5063 mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
5067 gfc_internal_error ("Bad type in gfc_simplify_sign");
5075 gfc_simplify_sin (gfc_expr *x)
5079 if (x->expr_type != EXPR_CONSTANT)
5082 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5087 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
5091 gfc_set_model (x->value.real);
5093 mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5100 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
5101 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
5102 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
5104 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
5105 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
5106 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
5108 mpfr_clears (xp, xq, NULL);
5114 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5117 return range_check (result, "SIN");
5122 gfc_simplify_sinh (gfc_expr *x)
5126 if (x->expr_type != EXPR_CONSTANT)
5129 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5131 if (x->ts.type == BT_REAL)
5132 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
5133 else if (x->ts.type == BT_COMPLEX)
5136 mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5138 gfc_free_expr (result);
5146 return range_check (result, "SINH");
5150 /* The argument is always a double precision real that is converted to
5151 single precision. TODO: Rounding! */
5154 gfc_simplify_sngl (gfc_expr *a)
5158 if (a->expr_type != EXPR_CONSTANT)
5161 result = gfc_real2real (a, gfc_default_real_kind);
5162 return range_check (result, "SNGL");
5167 gfc_simplify_spacing (gfc_expr *x)
5173 if (x->expr_type != EXPR_CONSTANT)
5176 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
5178 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
5180 /* Special case x = 0 and -0. */
5181 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
5182 if (mpfr_sgn (result->value.real) == 0)
5184 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5188 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5189 are the radix, exponent of x, and precision. This excludes the
5190 possibility of subnormal numbers. Fortran 2003 states the result is
5191 b**max(e - p, emin - 1). */
5193 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
5194 en = (long int) gfc_real_kinds[i].min_exponent - 1;
5195 en = en > ep ? en : ep;
5197 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
5198 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
5200 return range_check (result, "SPACING");
5205 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
5207 gfc_expr *result = 0L;
5208 int i, j, dim, ncopies;
5211 if ((!gfc_is_constant_expr (source)
5212 && !is_constant_array_expr (source))
5213 || !gfc_is_constant_expr (dim_expr)
5214 || !gfc_is_constant_expr (ncopies_expr))
5217 gcc_assert (dim_expr->ts.type == BT_INTEGER);
5218 gfc_extract_int (dim_expr, &dim);
5219 dim -= 1; /* zero-base DIM */
5221 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
5222 gfc_extract_int (ncopies_expr, &ncopies);
5223 ncopies = MAX (ncopies, 0);
5225 /* Do not allow the array size to exceed the limit for an array
5227 if (source->expr_type == EXPR_ARRAY)
5229 if (gfc_array_size (source, &size) == FAILURE)
5230 gfc_internal_error ("Failure getting length of a constant array.");
5233 mpz_init_set_ui (size, 1);
5235 if (mpz_get_si (size)*ncopies > gfc_option.flag_max_array_constructor)
5238 if (source->expr_type == EXPR_CONSTANT)
5240 gcc_assert (dim == 0);
5242 result = gfc_start_constructor (source->ts.type,
5246 result->shape = gfc_get_shape (result->rank);
5247 mpz_init_set_si (result->shape[0], ncopies);
5249 for (i = 0; i < ncopies; ++i)
5250 gfc_append_constructor (result, gfc_copy_expr (source));
5252 else if (source->expr_type == EXPR_ARRAY)
5254 int result_size, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
5255 gfc_constructor *ctor, *source_ctor, *result_ctor;
5257 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
5258 gcc_assert (dim >= 0 && dim <= source->rank);
5260 result = gfc_start_constructor (source->ts.type,
5263 result->rank = source->rank + 1;
5264 result->shape = gfc_get_shape (result->rank);
5267 for (i = 0, j = 0; i < result->rank; ++i)
5270 mpz_init_set (result->shape[i], source->shape[j++]);
5272 mpz_init_set_si (result->shape[i], ncopies);
5274 extent[i] = mpz_get_si (result->shape[i]);
5275 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
5276 result_size *= extent[i];
5279 for (i = 0; i < result_size; ++i)
5280 gfc_append_constructor (result, NULL);
5282 source_ctor = source->value.constructor;
5283 result_ctor = result->value.constructor;
5288 for (i = 0; i < ncopies; ++i)
5290 ctor->expr = gfc_copy_expr (source_ctor->expr);
5291 ADVANCE (ctor, rstride[dim]);
5294 ADVANCE (result_ctor, (dim == 0 ? ncopies : 1));
5295 ADVANCE (source_ctor, 1);
5299 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5300 Replace NULL with gcc_unreachable() after implementing
5301 gfc_simplify_cshift(). */
5304 if (source->ts.type == BT_CHARACTER)
5305 result->ts.u.cl = source->ts.u.cl;
5312 gfc_simplify_sqrt (gfc_expr *e)
5316 if (e->expr_type != EXPR_CONSTANT)
5319 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
5324 if (mpfr_cmp_si (e->value.real, 0) < 0)
5326 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
5331 gfc_set_model (e->value.real);
5333 mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
5336 /* Formula taken from Numerical Recipes to avoid over- and
5339 mpfr_t ac, ad, s, t, w;
5346 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
5347 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
5349 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
5350 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
5354 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
5355 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
5357 if (mpfr_cmp (ac, ad) >= 0)
5359 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
5360 mpfr_mul (t, t, t, GFC_RND_MODE);
5361 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5362 mpfr_sqrt (t, t, GFC_RND_MODE);
5363 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5364 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5365 mpfr_sqrt (t, t, GFC_RND_MODE);
5366 mpfr_sqrt (s, ac, GFC_RND_MODE);
5367 mpfr_mul (w, s, t, GFC_RND_MODE);
5371 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
5372 mpfr_mul (t, s, s, GFC_RND_MODE);
5373 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5374 mpfr_sqrt (t, t, GFC_RND_MODE);
5375 mpfr_abs (s, s, GFC_RND_MODE);
5376 mpfr_add (t, t, s, GFC_RND_MODE);
5377 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5378 mpfr_sqrt (t, t, GFC_RND_MODE);
5379 mpfr_sqrt (s, ad, GFC_RND_MODE);
5380 mpfr_mul (w, s, t, GFC_RND_MODE);
5383 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
5385 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5386 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
5387 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
5389 else if (mpfr_cmp_ui (w, 0) != 0
5390 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5391 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
5393 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5394 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
5395 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5397 else if (mpfr_cmp_ui (w, 0) != 0
5398 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5399 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
5401 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5402 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
5403 mpfr_neg (w, w, GFC_RND_MODE);
5404 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5407 gfc_internal_error ("invalid complex argument of SQRT at %L",
5410 mpfr_clears (s, t, ac, ad, w, NULL);
5416 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
5419 return range_check (result, "SQRT");
5422 gfc_free_expr (result);
5423 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
5424 return &gfc_bad_expr;
5429 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
5433 if (!is_constant_array_expr (array)
5434 || !gfc_is_constant_expr (dim))
5438 && !is_constant_array_expr (mask)
5439 && mask->expr_type != EXPR_CONSTANT)
5442 result = transformational_result (array, dim, array->ts.type,
5443 array->ts.kind, &array->where);
5444 init_result_expr (result, 0, NULL);
5446 return !dim || array->rank == 1 ?
5447 simplify_transformation_to_scalar (result, array, mask, gfc_add) :
5448 simplify_transformation_to_array (result, array, dim, mask, gfc_add);
5453 gfc_simplify_tan (gfc_expr *x)
5457 if (x->expr_type != EXPR_CONSTANT)
5460 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5462 if (x->ts.type == BT_REAL)
5463 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
5464 else if (x->ts.type == BT_COMPLEX)
5467 mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5469 gfc_free_expr (result);
5476 return range_check (result, "TAN");
5481 gfc_simplify_tanh (gfc_expr *x)
5485 if (x->expr_type != EXPR_CONSTANT)
5488 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5490 if (x->ts.type == BT_REAL)
5491 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
5492 else if (x->ts.type == BT_COMPLEX)
5495 mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5497 gfc_free_expr (result);
5504 return range_check (result, "TANH");
5510 gfc_simplify_tiny (gfc_expr *e)
5515 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
5517 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
5518 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5525 gfc_simplify_trailz (gfc_expr *e)
5528 unsigned long tz, bs;
5531 if (e->expr_type != EXPR_CONSTANT)
5534 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
5535 bs = gfc_integer_kinds[i].bit_size;
5536 tz = mpz_scan1 (e->value.integer, 0);
5538 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind, &e->where);
5539 mpz_set_ui (result->value.integer, MIN (tz, bs));
5546 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5549 gfc_expr *mold_element;
5552 size_t result_elt_size;
5555 unsigned char *buffer;
5557 if (!gfc_is_constant_expr (source)
5558 || (gfc_init_expr && !gfc_is_constant_expr (mold))
5559 || !gfc_is_constant_expr (size))
5562 if (source->expr_type == EXPR_FUNCTION)
5565 /* Calculate the size of the source. */
5566 if (source->expr_type == EXPR_ARRAY
5567 && gfc_array_size (source, &tmp) == FAILURE)
5568 gfc_internal_error ("Failure getting length of a constant array.");
5570 source_size = gfc_target_expr_size (source);
5572 /* Create an empty new expression with the appropriate characteristics. */
5573 result = gfc_constant_result (mold->ts.type, mold->ts.kind,
5575 result->ts = mold->ts;
5577 mold_element = mold->expr_type == EXPR_ARRAY
5578 ? mold->value.constructor->expr
5581 /* Set result character length, if needed. Note that this needs to be
5582 set even for array expressions, in order to pass this information into
5583 gfc_target_interpret_expr. */
5584 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
5585 result->value.character.length = mold_element->value.character.length;
5587 /* Set the number of elements in the result, and determine its size. */
5588 result_elt_size = gfc_target_expr_size (mold_element);
5589 if (result_elt_size == 0)
5591 gfc_free_expr (result);
5595 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5599 result->expr_type = EXPR_ARRAY;
5603 result_length = (size_t)mpz_get_ui (size->value.integer);
5606 result_length = source_size / result_elt_size;
5607 if (result_length * result_elt_size < source_size)
5611 result->shape = gfc_get_shape (1);
5612 mpz_init_set_ui (result->shape[0], result_length);
5614 result_size = result_length * result_elt_size;
5619 result_size = result_elt_size;
5622 if (gfc_option.warn_surprising && source_size < result_size)
5623 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
5624 "source size %ld < result size %ld", &source->where,
5625 (long) source_size, (long) result_size);
5627 /* Allocate the buffer to store the binary version of the source. */
5628 buffer_size = MAX (source_size, result_size);
5629 buffer = (unsigned char*)alloca (buffer_size);
5630 memset (buffer, 0, buffer_size);
5632 /* Now write source to the buffer. */
5633 gfc_target_encode_expr (source, buffer, buffer_size);
5635 /* And read the buffer back into the new expression. */
5636 gfc_target_interpret_expr (buffer, buffer_size, result);
5643 gfc_simplify_transpose (gfc_expr *matrix)
5647 gfc_constructor *matrix_ctor;
5649 if (!is_constant_array_expr (matrix))
5652 gcc_assert (matrix->rank == 2);
5654 result = gfc_start_constructor (matrix->ts.type, matrix->ts.kind, &matrix->where);
5656 result->shape = gfc_get_shape (result->rank);
5657 mpz_set (result->shape[0], matrix->shape[1]);
5658 mpz_set (result->shape[1], matrix->shape[0]);
5660 if (matrix->ts.type == BT_CHARACTER)
5661 result->ts.u.cl = matrix->ts.u.cl;
5663 matrix_rows = mpz_get_si (matrix->shape[0]);
5664 matrix_ctor = matrix->value.constructor;
5665 for (i = 0; i < matrix_rows; ++i)
5667 gfc_constructor *column_ctor = matrix_ctor;
5670 gfc_append_constructor (result,
5671 gfc_copy_expr (column_ctor->expr));
5673 ADVANCE (column_ctor, matrix_rows);
5676 ADVANCE (matrix_ctor, 1);
5684 gfc_simplify_trim (gfc_expr *e)
5687 int count, i, len, lentrim;
5689 if (e->expr_type != EXPR_CONSTANT)
5692 len = e->value.character.length;
5694 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
5696 for (count = 0, i = 1; i <= len; ++i)
5698 if (e->value.character.string[len - i] == ' ')
5704 lentrim = len - count;
5706 result->value.character.length = lentrim;
5707 result->value.character.string = gfc_get_wide_string (lentrim + 1);
5709 for (i = 0; i < lentrim; i++)
5710 result->value.character.string[i] = e->value.character.string[i];
5712 result->value.character.string[lentrim] = '\0'; /* For debugger */
5719 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5721 return simplify_bound (array, dim, kind, 1);
5726 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5728 gfc_expr *result, *e;
5729 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
5731 if (!is_constant_array_expr (vector)
5732 || !is_constant_array_expr (mask)
5733 || (!gfc_is_constant_expr (field)
5734 && !is_constant_array_expr(field)))
5737 result = gfc_start_constructor (vector->ts.type,
5740 result->rank = mask->rank;
5741 result->shape = gfc_copy_shape (mask->shape, mask->rank);
5743 if (vector->ts.type == BT_CHARACTER)
5744 result->ts.u.cl = vector->ts.u.cl;
5746 vector_ctor = vector->value.constructor;
5747 mask_ctor = mask->value.constructor;
5748 field_ctor = field->expr_type == EXPR_ARRAY ? field->value.constructor : NULL;
5752 if (mask_ctor->expr->value.logical)
5754 gcc_assert (vector_ctor);
5755 e = gfc_copy_expr (vector_ctor->expr);
5756 ADVANCE (vector_ctor, 1);
5758 else if (field->expr_type == EXPR_ARRAY)
5759 e = gfc_copy_expr (field_ctor->expr);
5761 e = gfc_copy_expr (field);
5763 gfc_append_constructor (result, e);
5765 ADVANCE (mask_ctor, 1);
5766 ADVANCE (field_ctor, 1);
5774 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
5778 size_t index, len, lenset;
5780 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
5783 return &gfc_bad_expr;
5785 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
5788 if (b != NULL && b->value.logical != 0)
5793 result = gfc_constant_result (BT_INTEGER, k, &s->where);
5795 len = s->value.character.length;
5796 lenset = set->value.character.length;
5800 mpz_set_ui (result->value.integer, 0);
5808 mpz_set_ui (result->value.integer, 1);
5812 index = wide_strspn (s->value.character.string,
5813 set->value.character.string) + 1;
5822 mpz_set_ui (result->value.integer, len);
5825 for (index = len; index > 0; index --)
5827 for (i = 0; i < lenset; i++)
5829 if (s->value.character.string[index - 1]
5830 == set->value.character.string[i])
5838 mpz_set_ui (result->value.integer, index);
5844 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
5849 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
5852 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
5853 if (x->ts.type == BT_INTEGER)
5855 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
5856 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
5857 return range_check (result, "XOR");
5859 else /* BT_LOGICAL */
5861 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
5862 result->value.logical = (x->value.logical && !y->value.logical)
5863 || (!x->value.logical && y->value.logical);
5870 /****************** Constant simplification *****************/
5872 /* Master function to convert one constant to another. While this is
5873 used as a simplification function, it requires the destination type
5874 and kind information which is supplied by a special case in
5878 gfc_convert_constant (gfc_expr *e, bt type, int kind)
5880 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
5881 gfc_constructor *head, *c, *tail = NULL;
5895 f = gfc_int2complex;
5915 f = gfc_real2complex;
5926 f = gfc_complex2int;
5929 f = gfc_complex2real;
5932 f = gfc_complex2complex;
5958 f = gfc_hollerith2int;
5962 f = gfc_hollerith2real;
5966 f = gfc_hollerith2complex;
5970 f = gfc_hollerith2character;
5974 f = gfc_hollerith2logical;
5984 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
5989 switch (e->expr_type)
5992 result = f (e, kind);
5994 return &gfc_bad_expr;
5998 if (!gfc_is_constant_expr (e))
6003 for (c = e->value.constructor; c; c = c->next)
6006 head = tail = gfc_get_constructor ();
6009 tail->next = gfc_get_constructor ();
6013 tail->where = c->where;
6015 if (c->iterator == NULL)
6016 tail->expr = f (c->expr, kind);
6019 g = gfc_convert_constant (c->expr, type, kind);
6020 if (g == &gfc_bad_expr)
6025 if (tail->expr == NULL)
6027 gfc_free_constructor (head);
6032 result = gfc_get_expr ();
6033 result->ts.type = type;
6034 result->ts.kind = kind;
6035 result->expr_type = EXPR_ARRAY;
6036 result->value.constructor = head;
6037 result->shape = gfc_copy_shape (e->shape, e->rank);
6038 result->where = e->where;
6039 result->rank = e->rank;
6050 /* Function for converting character constants. */
6052 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
6057 if (!gfc_is_constant_expr (e))
6060 if (e->expr_type == EXPR_CONSTANT)
6062 /* Simple case of a scalar. */
6063 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
6065 return &gfc_bad_expr;
6067 result->value.character.length = e->value.character.length;
6068 result->value.character.string
6069 = gfc_get_wide_string (e->value.character.length + 1);
6070 memcpy (result->value.character.string, e->value.character.string,
6071 (e->value.character.length + 1) * sizeof (gfc_char_t));
6073 /* Check we only have values representable in the destination kind. */
6074 for (i = 0; i < result->value.character.length; i++)
6075 if (!gfc_check_character_range (result->value.character.string[i],
6078 gfc_error ("Character '%s' in string at %L cannot be converted "
6079 "into character kind %d",
6080 gfc_print_wide_char (result->value.character.string[i]),
6082 return &gfc_bad_expr;
6087 else if (e->expr_type == EXPR_ARRAY)
6089 /* For an array constructor, we convert each constructor element. */
6090 gfc_constructor *head = NULL, *tail = NULL, *c;
6092 for (c = e->value.constructor; c; c = c->next)
6095 head = tail = gfc_get_constructor ();
6098 tail->next = gfc_get_constructor ();
6102 tail->where = c->where;
6103 tail->expr = gfc_convert_char_constant (c->expr, type, kind);
6104 if (tail->expr == &gfc_bad_expr)
6107 return &gfc_bad_expr;
6110 if (tail->expr == NULL)
6112 gfc_free_constructor (head);
6117 result = gfc_get_expr ();
6118 result->ts.type = type;
6119 result->ts.kind = kind;
6120 result->expr_type = EXPR_ARRAY;
6121 result->value.constructor = head;
6122 result->shape = gfc_copy_shape (e->shape, e->rank);
6123 result->where = e->where;
6124 result->rank = e->rank;
6125 result->ts.u.cl = e->ts.u.cl;