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 /* Helper function to convert to/from mpfr_t & mpc_t and call the
218 supplied mpc function on the respective values. */
222 call_mpc_func (mpfr_ptr result_re, mpfr_ptr result_im,
223 mpfr_srcptr input_re, mpfr_srcptr input_im,
224 int (*func)(mpc_ptr, mpc_srcptr, mpc_rnd_t))
227 mpc_init2 (c, mpfr_get_default_prec());
228 mpc_set_fr_fr (c, input_re, input_im, GFC_MPC_RND_MODE);
229 func (c, c, GFC_MPC_RND_MODE);
230 mpfr_set (result_re, mpc_realref (c), GFC_RND_MODE);
231 mpfr_set (result_im, mpc_imagref (c), GFC_RND_MODE);
237 /* Test that the expression is an constant array. */
240 is_constant_array_expr (gfc_expr *e)
247 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
250 for (c = e->value.constructor; c; c = c->next)
251 if (c->expr->expr_type != EXPR_CONSTANT)
258 /* Initialize a transformational result expression with a given value. */
261 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
263 if (e && e->expr_type == EXPR_ARRAY)
265 gfc_constructor *ctor = e->value.constructor;
268 init_result_expr (ctor->expr, init, array);
272 else if (e && e->expr_type == EXPR_CONSTANT)
274 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
281 e->value.logical = (init ? 1 : 0);
286 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
287 else if (init == INT_MAX)
288 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
290 mpz_set_si (e->value.integer, init);
296 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
297 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
299 else if (init == INT_MAX)
300 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
302 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
306 mpfr_set_si (e->value.complex.r, init, GFC_RND_MODE);
307 mpfr_set_si (e->value.complex.i, 0, GFC_RND_MODE);
313 gfc_expr *len = gfc_simplify_len (array, NULL);
314 gfc_extract_int (len, &length);
315 string = gfc_get_wide_string (length + 1);
316 gfc_wide_memset (string, 0, length);
318 else if (init == INT_MAX)
320 gfc_expr *len = gfc_simplify_len (array, NULL);
321 gfc_extract_int (len, &length);
322 string = gfc_get_wide_string (length + 1);
323 gfc_wide_memset (string, 255, length);
328 string = gfc_get_wide_string (1);
331 string[length] = '\0';
332 e->value.character.length = length;
333 e->value.character.string = string;
345 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
348 compute_dot_product (gfc_constructor *ctor_a, int stride_a,
349 gfc_constructor *ctor_b, int stride_b)
352 gfc_expr *a = ctor_a->expr, *b = ctor_b->expr;
354 gcc_assert (gfc_compare_types (&a->ts, &b->ts));
356 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
357 init_result_expr (result, 0, NULL);
359 while (ctor_a && ctor_b)
361 /* Copying of expressions is required as operands are free'd
362 by the gfc_arith routines. */
363 switch (result->ts.type)
366 result = gfc_or (result,
367 gfc_and (gfc_copy_expr (ctor_a->expr),
368 gfc_copy_expr (ctor_b->expr)));
374 result = gfc_add (result,
375 gfc_multiply (gfc_copy_expr (ctor_a->expr),
376 gfc_copy_expr (ctor_b->expr)));
383 ADVANCE (ctor_a, stride_a);
384 ADVANCE (ctor_b, stride_b);
391 /* Build a result expression for transformational intrinsics,
395 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
396 int kind, locus* where)
401 if (!dim || array->rank == 1)
402 return gfc_constant_result (type, kind, where);
404 result = gfc_start_constructor (type, kind, where);
405 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
406 result->rank = array->rank - 1;
408 /* gfc_array_size() would count the number of elements in the constructor,
409 we have not built those yet. */
411 for (i = 0; i < result->rank; ++i)
412 nelem *= mpz_get_ui (result->shape[i]);
414 for (i = 0; i < nelem; ++i)
416 gfc_expr *e = gfc_constant_result (type, kind, where);
417 gfc_append_constructor (result, e);
424 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
426 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
427 of COUNT intrinsic is .TRUE..
429 Interface and implimentation mimics arith functions as
430 gfc_add, gfc_multiply, etc. */
432 static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
436 gcc_assert (op1->ts.type == BT_INTEGER);
437 gcc_assert (op2->ts.type == BT_LOGICAL);
438 gcc_assert (op2->value.logical);
440 result = gfc_copy_expr (op1);
441 mpz_add_ui (result->value.integer, result->value.integer, 1);
449 /* Transforms an ARRAY with operation OP, according to MASK, to a
450 scalar RESULT. E.g. called if
452 REAL, PARAMETER :: array(n, m) = ...
453 REAL, PARAMETER :: s = SUM(array)
455 where OP == gfc_add(). */
458 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
459 transformational_op op)
462 gfc_constructor *array_ctor, *mask_ctor;
464 /* Shortcut for constant .FALSE. MASK. */
466 && mask->expr_type == EXPR_CONSTANT
467 && !mask->value.logical)
470 array_ctor = array->value.constructor;
472 if (mask && mask->expr_type == EXPR_ARRAY)
473 mask_ctor = mask->value.constructor;
477 a = array_ctor->expr;
478 array_ctor = array_ctor->next;
480 /* A constant MASK equals .TRUE. here and can be ignored. */
484 mask_ctor = mask_ctor->next;
485 if (!m->value.logical)
489 result = op (result, gfc_copy_expr (a));
495 /* Transforms an ARRAY with operation OP, according to MASK, to an
496 array RESULT. E.g. called if
498 REAL, PARAMETER :: array(n, m) = ...
499 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
501 where OP == gfc_multiply(). */
504 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
505 gfc_expr *mask, transformational_op op)
508 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
509 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
510 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
512 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
513 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
514 tmpstride[GFC_MAX_DIMENSIONS];
516 /* Shortcut for constant .FALSE. MASK. */
518 && mask->expr_type == EXPR_CONSTANT
519 && !mask->value.logical)
522 /* Build an indexed table for array element expressions to minimize
523 linked-list traversal. Masked elements are set to NULL. */
524 gfc_array_size (array, &size);
525 arraysize = mpz_get_ui (size);
527 arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);
529 array_ctor = array->value.constructor;
531 if (mask && mask->expr_type == EXPR_ARRAY)
532 mask_ctor = mask->value.constructor;
534 for (i = 0; i < arraysize; ++i)
536 arrayvec[i] = array_ctor->expr;
537 array_ctor = array_ctor->next;
541 if (!mask_ctor->expr->value.logical)
544 mask_ctor = mask_ctor->next;
548 /* Same for the result expression. */
549 gfc_array_size (result, &size);
550 resultsize = mpz_get_ui (size);
553 resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
554 result_ctor = result->value.constructor;
555 for (i = 0; i < resultsize; ++i)
557 resultvec[i] = result_ctor->expr;
558 result_ctor = result_ctor->next;
561 gfc_extract_int (dim, &dim_index);
562 dim_index -= 1; /* zero-base index */
566 for (i = 0, n = 0; i < array->rank; ++i)
569 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
572 dim_extent = mpz_get_si (array->shape[i]);
573 dim_stride = tmpstride[i];
577 extent[n] = mpz_get_si (array->shape[i]);
578 sstride[n] = tmpstride[i];
579 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
588 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
590 *dest = op (*dest, gfc_copy_expr (*src));
597 while (!done && count[n] == extent[n])
600 base -= sstride[n] * extent[n];
601 dest -= dstride[n] * extent[n];
604 if (n < result->rank)
615 /* Place updated expression in result constructor. */
616 result_ctor = result->value.constructor;
617 for (i = 0; i < resultsize; ++i)
619 result_ctor->expr = resultvec[i];
620 result_ctor = result_ctor->next;
624 gfc_free (resultvec);
630 /********************** Simplification functions *****************************/
633 gfc_simplify_abs (gfc_expr *e)
637 if (e->expr_type != EXPR_CONSTANT)
643 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
645 mpz_abs (result->value.integer, e->value.integer);
647 result = range_check (result, "IABS");
651 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
653 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
655 result = range_check (result, "ABS");
659 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
661 gfc_set_model_kind (e->ts.kind);
663 mpfr_hypot (result->value.real, e->value.complex.r,
664 e->value.complex.i, GFC_RND_MODE);
665 result = range_check (result, "CABS");
669 gfc_internal_error ("gfc_simplify_abs(): Bad type");
677 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
681 bool too_large = false;
683 if (e->expr_type != EXPR_CONSTANT)
686 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
688 return &gfc_bad_expr;
690 if (mpz_cmp_si (e->value.integer, 0) < 0)
692 gfc_error ("Argument of %s function at %L is negative", name,
694 return &gfc_bad_expr;
697 if (ascii && gfc_option.warn_surprising
698 && mpz_cmp_si (e->value.integer, 127) > 0)
699 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
702 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
707 mpz_init_set_ui (t, 2);
708 mpz_pow_ui (t, t, 32);
709 mpz_sub_ui (t, t, 1);
710 if (mpz_cmp (e->value.integer, t) > 0)
717 gfc_error ("Argument of %s function at %L is too large for the "
718 "collating sequence of kind %d", name, &e->where, kind);
719 return &gfc_bad_expr;
722 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
723 result->value.character.string = gfc_get_wide_string (2);
724 result->value.character.length = 1;
725 result->value.character.string[0] = mpz_get_ui (e->value.integer);
726 result->value.character.string[1] = '\0'; /* For debugger */
732 /* We use the processor's collating sequence, because all
733 systems that gfortran currently works on are ASCII. */
736 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
738 return simplify_achar_char (e, k, "ACHAR", true);
743 gfc_simplify_acos (gfc_expr *x)
747 if (x->expr_type != EXPR_CONSTANT)
750 if (mpfr_cmp_si (x->value.real, 1) > 0
751 || mpfr_cmp_si (x->value.real, -1) < 0)
753 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
755 return &gfc_bad_expr;
758 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
760 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
762 return range_check (result, "ACOS");
766 gfc_simplify_acosh (gfc_expr *x)
770 if (x->expr_type != EXPR_CONSTANT)
773 if (mpfr_cmp_si (x->value.real, 1) < 0)
775 gfc_error ("Argument of ACOSH at %L must not be less than 1",
777 return &gfc_bad_expr;
780 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
782 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
784 return range_check (result, "ACOSH");
788 gfc_simplify_adjustl (gfc_expr *e)
794 if (e->expr_type != EXPR_CONSTANT)
797 len = e->value.character.length;
799 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
801 result->value.character.length = len;
802 result->value.character.string = gfc_get_wide_string (len + 1);
804 for (count = 0, i = 0; i < len; ++i)
806 ch = e->value.character.string[i];
812 for (i = 0; i < len - count; ++i)
813 result->value.character.string[i] = e->value.character.string[count + i];
815 for (i = len - count; i < len; ++i)
816 result->value.character.string[i] = ' ';
818 result->value.character.string[len] = '\0'; /* For debugger */
825 gfc_simplify_adjustr (gfc_expr *e)
831 if (e->expr_type != EXPR_CONSTANT)
834 len = e->value.character.length;
836 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
838 result->value.character.length = len;
839 result->value.character.string = gfc_get_wide_string (len + 1);
841 for (count = 0, i = len - 1; i >= 0; --i)
843 ch = e->value.character.string[i];
849 for (i = 0; i < count; ++i)
850 result->value.character.string[i] = ' ';
852 for (i = count; i < len; ++i)
853 result->value.character.string[i] = e->value.character.string[i - count];
855 result->value.character.string[len] = '\0'; /* For debugger */
862 gfc_simplify_aimag (gfc_expr *e)
866 if (e->expr_type != EXPR_CONSTANT)
869 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
870 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
872 return range_check (result, "AIMAG");
877 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
879 gfc_expr *rtrunc, *result;
882 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
884 return &gfc_bad_expr;
886 if (e->expr_type != EXPR_CONSTANT)
889 rtrunc = gfc_copy_expr (e);
891 mpfr_trunc (rtrunc->value.real, e->value.real);
893 result = gfc_real2real (rtrunc, kind);
894 gfc_free_expr (rtrunc);
896 return range_check (result, "AINT");
901 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
905 if (!is_constant_array_expr (mask)
906 || !gfc_is_constant_expr (dim))
909 result = transformational_result (mask, dim, mask->ts.type,
910 mask->ts.kind, &mask->where);
911 init_result_expr (result, true, NULL);
913 return !dim || mask->rank == 1 ?
914 simplify_transformation_to_scalar (result, mask, NULL, gfc_and) :
915 simplify_transformation_to_array (result, mask, dim, NULL, gfc_and);
920 gfc_simplify_dint (gfc_expr *e)
922 gfc_expr *rtrunc, *result;
924 if (e->expr_type != EXPR_CONSTANT)
927 rtrunc = gfc_copy_expr (e);
929 mpfr_trunc (rtrunc->value.real, e->value.real);
931 result = gfc_real2real (rtrunc, gfc_default_double_kind);
932 gfc_free_expr (rtrunc);
934 return range_check (result, "DINT");
939 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
944 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
946 return &gfc_bad_expr;
948 if (e->expr_type != EXPR_CONSTANT)
951 result = gfc_constant_result (e->ts.type, kind, &e->where);
953 mpfr_round (result->value.real, e->value.real);
955 return range_check (result, "ANINT");
960 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
965 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
968 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
969 if (x->ts.type == BT_INTEGER)
971 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
972 mpz_and (result->value.integer, x->value.integer, y->value.integer);
973 return range_check (result, "AND");
975 else /* BT_LOGICAL */
977 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
978 result->value.logical = x->value.logical && y->value.logical;
985 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
989 if (!is_constant_array_expr (mask)
990 || !gfc_is_constant_expr (dim))
993 result = transformational_result (mask, dim, mask->ts.type,
994 mask->ts.kind, &mask->where);
995 init_result_expr (result, false, NULL);
997 return !dim || mask->rank == 1 ?
998 simplify_transformation_to_scalar (result, mask, NULL, gfc_or) :
999 simplify_transformation_to_array (result, mask, dim, NULL, gfc_or);
1004 gfc_simplify_dnint (gfc_expr *e)
1008 if (e->expr_type != EXPR_CONSTANT)
1011 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
1013 mpfr_round (result->value.real, e->value.real);
1015 return range_check (result, "DNINT");
1020 gfc_simplify_asin (gfc_expr *x)
1024 if (x->expr_type != EXPR_CONSTANT)
1027 if (mpfr_cmp_si (x->value.real, 1) > 0
1028 || mpfr_cmp_si (x->value.real, -1) < 0)
1030 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1032 return &gfc_bad_expr;
1035 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1037 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1039 return range_check (result, "ASIN");
1044 gfc_simplify_asinh (gfc_expr *x)
1048 if (x->expr_type != EXPR_CONSTANT)
1051 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1053 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1055 return range_check (result, "ASINH");
1060 gfc_simplify_atan (gfc_expr *x)
1064 if (x->expr_type != EXPR_CONSTANT)
1067 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1069 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1071 return range_check (result, "ATAN");
1076 gfc_simplify_atanh (gfc_expr *x)
1080 if (x->expr_type != EXPR_CONSTANT)
1083 if (mpfr_cmp_si (x->value.real, 1) >= 0
1084 || mpfr_cmp_si (x->value.real, -1) <= 0)
1086 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
1088 return &gfc_bad_expr;
1091 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1093 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1095 return range_check (result, "ATANH");
1100 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1104 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1107 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1109 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1110 "second argument must not be zero", &x->where);
1111 return &gfc_bad_expr;
1114 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1116 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1118 return range_check (result, "ATAN2");
1123 gfc_simplify_bessel_j0 (gfc_expr *x ATTRIBUTE_UNUSED)
1127 if (x->expr_type != EXPR_CONSTANT)
1130 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1131 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1133 return range_check (result, "BESSEL_J0");
1138 gfc_simplify_bessel_j1 (gfc_expr *x ATTRIBUTE_UNUSED)
1142 if (x->expr_type != EXPR_CONSTANT)
1145 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1146 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1148 return range_check (result, "BESSEL_J1");
1153 gfc_simplify_bessel_jn (gfc_expr *order ATTRIBUTE_UNUSED,
1154 gfc_expr *x ATTRIBUTE_UNUSED)
1159 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1162 n = mpz_get_si (order->value.integer);
1163 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1164 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1166 return range_check (result, "BESSEL_JN");
1171 gfc_simplify_bessel_y0 (gfc_expr *x ATTRIBUTE_UNUSED)
1175 if (x->expr_type != EXPR_CONSTANT)
1178 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1179 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1181 return range_check (result, "BESSEL_Y0");
1186 gfc_simplify_bessel_y1 (gfc_expr *x ATTRIBUTE_UNUSED)
1190 if (x->expr_type != EXPR_CONSTANT)
1193 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1194 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1196 return range_check (result, "BESSEL_Y1");
1201 gfc_simplify_bessel_yn (gfc_expr *order ATTRIBUTE_UNUSED,
1202 gfc_expr *x ATTRIBUTE_UNUSED)
1207 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1210 n = mpz_get_si (order->value.integer);
1211 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1212 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1214 return range_check (result, "BESSEL_YN");
1219 gfc_simplify_bit_size (gfc_expr *e)
1224 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1225 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
1226 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
1233 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1237 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1240 if (gfc_extract_int (bit, &b) != NULL || b < 0)
1241 return gfc_logical_expr (0, &e->where);
1243 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
1248 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1250 gfc_expr *ceil, *result;
1253 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1255 return &gfc_bad_expr;
1257 if (e->expr_type != EXPR_CONSTANT)
1260 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1262 ceil = gfc_copy_expr (e);
1264 mpfr_ceil (ceil->value.real, e->value.real);
1265 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1267 gfc_free_expr (ceil);
1269 return range_check (result, "CEILING");
1274 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1276 return simplify_achar_char (e, k, "CHAR", false);
1280 /* Common subroutine for simplifying CMPLX and DCMPLX. */
1283 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1287 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
1289 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1295 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
1299 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
1303 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1304 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1308 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1317 mpfr_set_z (result->value.complex.i, y->value.integer,
1322 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
1326 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1335 ts.kind = result->ts.kind;
1337 if (!gfc_convert_boz (x, &ts))
1338 return &gfc_bad_expr;
1339 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
1346 ts.kind = result->ts.kind;
1348 if (!gfc_convert_boz (y, &ts))
1349 return &gfc_bad_expr;
1350 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
1353 return range_check (result, name);
1357 /* Function called when we won't simplify an expression like CMPLX (or
1358 COMPLEX or DCMPLX) but still want to convert BOZ arguments. */
1361 only_convert_cmplx_boz (gfc_expr *x, gfc_expr *y, int kind)
1368 if (x->is_boz && !gfc_convert_boz (x, &ts))
1369 return &gfc_bad_expr;
1371 if (y && y->is_boz && !gfc_convert_boz (y, &ts))
1372 return &gfc_bad_expr;
1379 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1383 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
1385 return &gfc_bad_expr;
1387 if (x->expr_type != EXPR_CONSTANT
1388 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1389 return only_convert_cmplx_boz (x, y, kind);
1391 return simplify_cmplx ("CMPLX", x, y, kind);
1396 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1400 if (x->ts.type == BT_INTEGER)
1402 if (y->ts.type == BT_INTEGER)
1403 kind = gfc_default_real_kind;
1409 if (y->ts.type == BT_REAL)
1410 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1415 if (x->expr_type != EXPR_CONSTANT
1416 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1417 return only_convert_cmplx_boz (x, y, kind);
1419 return simplify_cmplx ("COMPLEX", x, y, kind);
1424 gfc_simplify_conjg (gfc_expr *e)
1428 if (e->expr_type != EXPR_CONSTANT)
1431 result = gfc_copy_expr (e);
1432 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
1434 return range_check (result, "CONJG");
1439 gfc_simplify_cos (gfc_expr *x)
1443 if (x->expr_type != EXPR_CONSTANT)
1446 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1451 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1454 gfc_set_model_kind (x->ts.kind);
1456 call_mpc_func (result->value.complex.r, result->value.complex.i,
1457 x->value.complex.r, x->value.complex.i, mpc_cos);
1464 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
1465 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
1466 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
1468 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
1469 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
1470 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
1471 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
1473 mpfr_clears (xp, xq, NULL);
1478 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1481 return range_check (result, "COS");
1487 gfc_simplify_cosh (gfc_expr *x)
1491 if (x->expr_type != EXPR_CONSTANT)
1494 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1496 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
1498 return range_check (result, "COSH");
1503 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1507 if (!is_constant_array_expr (mask)
1508 || !gfc_is_constant_expr (dim)
1509 || !gfc_is_constant_expr (kind))
1512 result = transformational_result (mask, dim,
1514 get_kind (BT_INTEGER, kind, "COUNT",
1515 gfc_default_integer_kind),
1518 init_result_expr (result, 0, NULL);
1520 /* Passing MASK twice, once as data array, once as mask.
1521 Whenever gfc_count is called, '1' is added to the result. */
1522 return !dim || mask->rank == 1 ?
1523 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1524 simplify_transformation_to_array (result, mask, dim, mask, gfc_count);
1529 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1532 if (x->expr_type != EXPR_CONSTANT
1533 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1534 return only_convert_cmplx_boz (x, y, gfc_default_double_kind);
1536 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1541 gfc_simplify_dble (gfc_expr *e)
1543 gfc_expr *result = NULL;
1545 if (e->expr_type != EXPR_CONSTANT)
1552 result = gfc_int2real (e, gfc_default_double_kind);
1556 result = gfc_real2real (e, gfc_default_double_kind);
1560 result = gfc_complex2real (e, gfc_default_double_kind);
1564 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
1567 if (e->ts.type == BT_INTEGER && e->is_boz)
1572 ts.kind = gfc_default_double_kind;
1573 result = gfc_copy_expr (e);
1574 if (!gfc_convert_boz (result, &ts))
1576 gfc_free_expr (result);
1577 return &gfc_bad_expr;
1581 return range_check (result, "DBLE");
1586 gfc_simplify_digits (gfc_expr *x)
1590 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1594 digits = gfc_integer_kinds[i].digits;
1599 digits = gfc_real_kinds[i].digits;
1606 return gfc_int_expr (digits);
1611 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1616 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1619 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1620 result = gfc_constant_result (x->ts.type, kind, &x->where);
1625 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
1626 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
1628 mpz_set_ui (result->value.integer, 0);
1633 if (mpfr_cmp (x->value.real, y->value.real) > 0)
1634 mpfr_sub (result->value.real, x->value.real, y->value.real,
1637 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1642 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1645 return range_check (result, "DIM");
1650 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1654 if (!is_constant_array_expr (vector_a)
1655 || !is_constant_array_expr (vector_b))
1658 gcc_assert (vector_a->rank == 1);
1659 gcc_assert (vector_b->rank == 1);
1660 gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));
1662 if (vector_a->value.constructor && vector_b->value.constructor)
1663 return compute_dot_product (vector_a->value.constructor, 1,
1664 vector_b->value.constructor, 1);
1666 /* Zero sized array ... */
1667 result = gfc_constant_result (vector_a->ts.type,
1670 init_result_expr (result, 0, NULL);
1676 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1678 gfc_expr *a1, *a2, *result;
1680 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1683 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
1685 a1 = gfc_real2real (x, gfc_default_double_kind);
1686 a2 = gfc_real2real (y, gfc_default_double_kind);
1688 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1693 return range_check (result, "DPROD");
1698 gfc_simplify_erf (gfc_expr *x)
1702 if (x->expr_type != EXPR_CONSTANT)
1705 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1707 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
1709 return range_check (result, "ERF");
1714 gfc_simplify_erfc (gfc_expr *x)
1718 if (x->expr_type != EXPR_CONSTANT)
1721 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1723 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
1725 return range_check (result, "ERFC");
1729 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1731 #define MAX_ITER 200
1732 #define ARG_LIMIT 12
1734 /* Calculate ERFC_SCALED directly by its definition:
1736 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1738 using a large precision for intermediate results. This is used for all
1739 but large values of the argument. */
1741 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
1746 prec = mpfr_get_default_prec ();
1747 mpfr_set_default_prec (10 * prec);
1752 mpfr_set (a, arg, GFC_RND_MODE);
1753 mpfr_sqr (b, a, GFC_RND_MODE);
1754 mpfr_exp (b, b, GFC_RND_MODE);
1755 mpfr_erfc (a, a, GFC_RND_MODE);
1756 mpfr_mul (a, a, b, GFC_RND_MODE);
1758 mpfr_set (res, a, GFC_RND_MODE);
1759 mpfr_set_default_prec (prec);
1765 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
1767 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
1768 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
1771 This is used for large values of the argument. Intermediate calculations
1772 are performed with twice the precision. We don't do a fixed number of
1773 iterations of the sum, but stop when it has converged to the required
1776 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
1778 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
1783 prec = mpfr_get_default_prec ();
1784 mpfr_set_default_prec (2 * prec);
1794 mpfr_init (sumtrunc);
1795 mpfr_set_prec (oldsum, prec);
1796 mpfr_set_prec (sumtrunc, prec);
1798 mpfr_set (x, arg, GFC_RND_MODE);
1799 mpfr_set_ui (sum, 1, GFC_RND_MODE);
1800 mpz_set_ui (num, 1);
1802 mpfr_set (u, x, GFC_RND_MODE);
1803 mpfr_sqr (u, u, GFC_RND_MODE);
1804 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
1805 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
1807 for (i = 1; i < MAX_ITER; i++)
1809 mpfr_set (oldsum, sum, GFC_RND_MODE);
1811 mpz_mul_ui (num, num, 2 * i - 1);
1814 mpfr_set (w, u, GFC_RND_MODE);
1815 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
1817 mpfr_set_z (v, num, GFC_RND_MODE);
1818 mpfr_mul (v, v, w, GFC_RND_MODE);
1820 mpfr_add (sum, sum, v, GFC_RND_MODE);
1822 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
1823 if (mpfr_cmp (sumtrunc, oldsum) == 0)
1827 /* We should have converged by now; otherwise, ARG_LIMIT is probably
1829 gcc_assert (i < MAX_ITER);
1831 /* Divide by x * sqrt(Pi). */
1832 mpfr_const_pi (u, GFC_RND_MODE);
1833 mpfr_sqrt (u, u, GFC_RND_MODE);
1834 mpfr_mul (u, u, x, GFC_RND_MODE);
1835 mpfr_div (sum, sum, u, GFC_RND_MODE);
1837 mpfr_set (res, sum, GFC_RND_MODE);
1838 mpfr_set_default_prec (prec);
1840 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
1846 gfc_simplify_erfc_scaled (gfc_expr *x)
1850 if (x->expr_type != EXPR_CONSTANT)
1853 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1854 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
1855 asympt_erfc_scaled (result->value.real, x->value.real);
1857 fullprec_erfc_scaled (result->value.real, x->value.real);
1859 return range_check (result, "ERFC_SCALED");
1867 gfc_simplify_epsilon (gfc_expr *e)
1872 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1874 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1876 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1878 return range_check (result, "EPSILON");
1883 gfc_simplify_exp (gfc_expr *x)
1887 if (x->expr_type != EXPR_CONSTANT)
1890 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1895 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
1899 gfc_set_model_kind (x->ts.kind);
1901 call_mpc_func (result->value.complex.r, result->value.complex.i,
1902 x->value.complex.r, x->value.complex.i, mpc_exp);
1908 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1909 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1910 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1911 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1912 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1913 mpfr_clears (xp, xq, NULL);
1919 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1922 return range_check (result, "EXP");
1926 gfc_simplify_exponent (gfc_expr *x)
1931 if (x->expr_type != EXPR_CONSTANT)
1934 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1937 gfc_set_model (x->value.real);
1939 if (mpfr_sgn (x->value.real) == 0)
1941 mpz_set_ui (result->value.integer, 0);
1945 i = (int) mpfr_get_exp (x->value.real);
1946 mpz_set_si (result->value.integer, i);
1948 return range_check (result, "EXPONENT");
1953 gfc_simplify_float (gfc_expr *a)
1957 if (a->expr_type != EXPR_CONSTANT)
1966 ts.kind = gfc_default_real_kind;
1968 result = gfc_copy_expr (a);
1969 if (!gfc_convert_boz (result, &ts))
1971 gfc_free_expr (result);
1972 return &gfc_bad_expr;
1976 result = gfc_int2real (a, gfc_default_real_kind);
1977 return range_check (result, "FLOAT");
1982 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
1988 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1990 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1992 if (e->expr_type != EXPR_CONSTANT)
1995 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1997 gfc_set_model_kind (kind);
1999 mpfr_floor (floor, e->value.real);
2001 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2005 return range_check (result, "FLOOR");
2010 gfc_simplify_fraction (gfc_expr *x)
2013 mpfr_t absv, exp, pow2;
2015 if (x->expr_type != EXPR_CONSTANT)
2018 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2020 if (mpfr_sgn (x->value.real) == 0)
2022 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2026 gfc_set_model_kind (x->ts.kind);
2031 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2032 mpfr_log2 (exp, absv, GFC_RND_MODE);
2034 mpfr_trunc (exp, exp);
2035 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2037 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2039 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2041 mpfr_clears (exp, absv, pow2, NULL);
2043 return range_check (result, "FRACTION");
2048 gfc_simplify_gamma (gfc_expr *x)
2052 if (x->expr_type != EXPR_CONSTANT)
2055 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2057 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
2059 return range_check (result, "GAMMA");
2064 gfc_simplify_huge (gfc_expr *e)
2069 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2071 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2076 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2080 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
2092 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
2096 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2099 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2100 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
2101 return range_check (result, "HYPOT");
2105 /* We use the processor's collating sequence, because all
2106 systems that gfortran currently works on are ASCII. */
2109 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2114 if (e->expr_type != EXPR_CONSTANT)
2117 if (e->value.character.length != 1)
2119 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
2120 return &gfc_bad_expr;
2123 index = e->value.character.string[0];
2125 if (gfc_option.warn_surprising && index > 127)
2126 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2129 if ((result = int_expr_with_kind (index, kind, "IACHAR")) == NULL)
2130 return &gfc_bad_expr;
2132 result->where = e->where;
2134 return range_check (result, "IACHAR");
2139 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2143 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2146 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2148 mpz_and (result->value.integer, x->value.integer, y->value.integer);
2150 return range_check (result, "IAND");
2155 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2160 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2163 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2165 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
2166 return &gfc_bad_expr;
2169 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2171 if (pos >= gfc_integer_kinds[k].bit_size)
2173 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
2175 return &gfc_bad_expr;
2178 result = gfc_copy_expr (x);
2180 convert_mpz_to_unsigned (result->value.integer,
2181 gfc_integer_kinds[k].bit_size);
2183 mpz_clrbit (result->value.integer, pos);
2185 convert_mpz_to_signed (result->value.integer,
2186 gfc_integer_kinds[k].bit_size);
2193 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2200 if (x->expr_type != EXPR_CONSTANT
2201 || y->expr_type != EXPR_CONSTANT
2202 || z->expr_type != EXPR_CONSTANT)
2205 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2207 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
2208 return &gfc_bad_expr;
2211 if (gfc_extract_int (z, &len) != NULL || len < 0)
2213 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
2214 return &gfc_bad_expr;
2217 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2219 bitsize = gfc_integer_kinds[k].bit_size;
2221 if (pos + len > bitsize)
2223 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2224 "bit size at %L", &y->where);
2225 return &gfc_bad_expr;
2228 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2229 convert_mpz_to_unsigned (result->value.integer,
2230 gfc_integer_kinds[k].bit_size);
2232 bits = XCNEWVEC (int, bitsize);
2234 for (i = 0; i < bitsize; i++)
2237 for (i = 0; i < len; i++)
2238 bits[i] = mpz_tstbit (x->value.integer, i + pos);
2240 for (i = 0; i < bitsize; i++)
2243 mpz_clrbit (result->value.integer, i);
2244 else if (bits[i] == 1)
2245 mpz_setbit (result->value.integer, i);
2247 gfc_internal_error ("IBITS: Bad bit");
2252 convert_mpz_to_signed (result->value.integer,
2253 gfc_integer_kinds[k].bit_size);
2260 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2265 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2268 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2270 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
2271 return &gfc_bad_expr;
2274 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2276 if (pos >= gfc_integer_kinds[k].bit_size)
2278 gfc_error ("Second argument of IBSET exceeds bit size at %L",
2280 return &gfc_bad_expr;
2283 result = gfc_copy_expr (x);
2285 convert_mpz_to_unsigned (result->value.integer,
2286 gfc_integer_kinds[k].bit_size);
2288 mpz_setbit (result->value.integer, pos);
2290 convert_mpz_to_signed (result->value.integer,
2291 gfc_integer_kinds[k].bit_size);
2298 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2303 if (e->expr_type != EXPR_CONSTANT)
2306 if (e->value.character.length != 1)
2308 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
2309 return &gfc_bad_expr;
2312 index = e->value.character.string[0];
2314 if ((result = int_expr_with_kind (index, kind, "ICHAR")) == NULL)
2315 return &gfc_bad_expr;
2317 result->where = e->where;
2318 return range_check (result, "ICHAR");
2323 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2327 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2330 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2332 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
2334 return range_check (result, "IEOR");
2339 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2342 int back, len, lensub;
2343 int i, j, k, count, index = 0, start;
2345 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
2346 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
2349 if (b != NULL && b->value.logical != 0)
2354 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
2356 return &gfc_bad_expr;
2358 result = gfc_constant_result (BT_INTEGER, k, &x->where);
2360 len = x->value.character.length;
2361 lensub = y->value.character.length;
2365 mpz_set_si (result->value.integer, 0);
2373 mpz_set_si (result->value.integer, 1);
2376 else if (lensub == 1)
2378 for (i = 0; i < len; i++)
2380 for (j = 0; j < lensub; j++)
2382 if (y->value.character.string[j]
2383 == x->value.character.string[i])
2393 for (i = 0; i < len; i++)
2395 for (j = 0; j < lensub; j++)
2397 if (y->value.character.string[j]
2398 == x->value.character.string[i])
2403 for (k = 0; k < lensub; k++)
2405 if (y->value.character.string[k]
2406 == x->value.character.string[k + start])
2410 if (count == lensub)
2425 mpz_set_si (result->value.integer, len + 1);
2428 else if (lensub == 1)
2430 for (i = 0; i < len; i++)
2432 for (j = 0; j < lensub; j++)
2434 if (y->value.character.string[j]
2435 == x->value.character.string[len - i])
2437 index = len - i + 1;
2445 for (i = 0; i < len; i++)
2447 for (j = 0; j < lensub; j++)
2449 if (y->value.character.string[j]
2450 == x->value.character.string[len - i])
2453 if (start <= len - lensub)
2456 for (k = 0; k < lensub; k++)
2457 if (y->value.character.string[k]
2458 == x->value.character.string[k + start])
2461 if (count == lensub)
2478 mpz_set_si (result->value.integer, index);
2479 return range_check (result, "INDEX");
2484 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2486 gfc_expr *result = NULL;
2489 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
2491 return &gfc_bad_expr;
2493 if (e->expr_type != EXPR_CONSTANT)
2499 result = gfc_int2int (e, kind);
2503 result = gfc_real2int (e, kind);
2507 result = gfc_complex2int (e, kind);
2511 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
2512 return &gfc_bad_expr;
2515 return range_check (result, "INT");
2520 simplify_intconv (gfc_expr *e, int kind, const char *name)
2522 gfc_expr *result = NULL;
2524 if (e->expr_type != EXPR_CONSTANT)
2530 result = gfc_int2int (e, kind);
2534 result = gfc_real2int (e, kind);
2538 result = gfc_complex2int (e, kind);
2542 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
2543 return &gfc_bad_expr;
2546 return range_check (result, name);
2551 gfc_simplify_int2 (gfc_expr *e)
2553 return simplify_intconv (e, 2, "INT2");
2558 gfc_simplify_int8 (gfc_expr *e)
2560 return simplify_intconv (e, 8, "INT8");
2565 gfc_simplify_long (gfc_expr *e)
2567 return simplify_intconv (e, 4, "LONG");
2572 gfc_simplify_ifix (gfc_expr *e)
2574 gfc_expr *rtrunc, *result;
2576 if (e->expr_type != EXPR_CONSTANT)
2579 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2582 rtrunc = gfc_copy_expr (e);
2584 mpfr_trunc (rtrunc->value.real, e->value.real);
2585 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2587 gfc_free_expr (rtrunc);
2588 return range_check (result, "IFIX");
2593 gfc_simplify_idint (gfc_expr *e)
2595 gfc_expr *rtrunc, *result;
2597 if (e->expr_type != EXPR_CONSTANT)
2600 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2603 rtrunc = gfc_copy_expr (e);
2605 mpfr_trunc (rtrunc->value.real, e->value.real);
2606 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2608 gfc_free_expr (rtrunc);
2609 return range_check (result, "IDINT");
2614 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2618 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2621 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2623 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2624 return range_check (result, "IOR");
2629 gfc_simplify_is_iostat_end (gfc_expr *x)
2633 if (x->expr_type != EXPR_CONSTANT)
2636 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2638 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_END) == 0);
2645 gfc_simplify_is_iostat_eor (gfc_expr *x)
2649 if (x->expr_type != EXPR_CONSTANT)
2652 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2654 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_EOR) == 0);
2661 gfc_simplify_isnan (gfc_expr *x)
2665 if (x->expr_type != EXPR_CONSTANT)
2668 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2670 result->value.logical = mpfr_nan_p (x->value.real);
2677 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
2680 int shift, ashift, isize, k, *bits, i;
2682 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2685 if (gfc_extract_int (s, &shift) != NULL)
2687 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
2688 return &gfc_bad_expr;
2691 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2693 isize = gfc_integer_kinds[k].bit_size;
2702 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
2703 "at %L", &s->where);
2704 return &gfc_bad_expr;
2707 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2711 mpz_set (result->value.integer, e->value.integer);
2712 return range_check (result, "ISHFT");
2715 bits = XCNEWVEC (int, isize);
2717 for (i = 0; i < isize; i++)
2718 bits[i] = mpz_tstbit (e->value.integer, i);
2722 for (i = 0; i < shift; i++)
2723 mpz_clrbit (result->value.integer, i);
2725 for (i = 0; i < isize - shift; i++)
2728 mpz_clrbit (result->value.integer, i + shift);
2730 mpz_setbit (result->value.integer, i + shift);
2735 for (i = isize - 1; i >= isize - ashift; i--)
2736 mpz_clrbit (result->value.integer, i);
2738 for (i = isize - 1; i >= ashift; i--)
2741 mpz_clrbit (result->value.integer, i - ashift);
2743 mpz_setbit (result->value.integer, i - ashift);
2747 convert_mpz_to_signed (result->value.integer, isize);
2755 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
2758 int shift, ashift, isize, ssize, delta, k;
2761 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2764 if (gfc_extract_int (s, &shift) != NULL)
2766 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
2767 return &gfc_bad_expr;
2770 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2771 isize = gfc_integer_kinds[k].bit_size;
2775 if (sz->expr_type != EXPR_CONSTANT)
2778 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
2780 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
2781 return &gfc_bad_expr;
2786 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
2787 "BIT_SIZE of first argument at %L", &s->where);
2788 return &gfc_bad_expr;
2802 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2803 "third argument at %L", &s->where);
2805 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2806 "BIT_SIZE of first argument at %L", &s->where);
2807 return &gfc_bad_expr;
2810 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2812 mpz_set (result->value.integer, e->value.integer);
2817 convert_mpz_to_unsigned (result->value.integer, isize);
2819 bits = XCNEWVEC (int, ssize);
2821 for (i = 0; i < ssize; i++)
2822 bits[i] = mpz_tstbit (e->value.integer, i);
2824 delta = ssize - ashift;
2828 for (i = 0; i < delta; i++)
2831 mpz_clrbit (result->value.integer, i + shift);
2833 mpz_setbit (result->value.integer, i + shift);
2836 for (i = delta; i < ssize; i++)
2839 mpz_clrbit (result->value.integer, i - delta);
2841 mpz_setbit (result->value.integer, i - delta);
2846 for (i = 0; i < ashift; i++)
2849 mpz_clrbit (result->value.integer, i + delta);
2851 mpz_setbit (result->value.integer, i + delta);
2854 for (i = ashift; i < ssize; i++)
2857 mpz_clrbit (result->value.integer, i + shift);
2859 mpz_setbit (result->value.integer, i + shift);
2863 convert_mpz_to_signed (result->value.integer, isize);
2871 gfc_simplify_kind (gfc_expr *e)
2874 if (e->ts.type == BT_DERIVED)
2876 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
2877 return &gfc_bad_expr;
2880 return gfc_int_expr (e->ts.kind);
2885 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
2886 gfc_array_spec *as, gfc_ref *ref)
2888 gfc_expr *l, *u, *result;
2891 /* The last dimension of an assumed-size array is special. */
2892 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2894 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2895 return gfc_copy_expr (as->lower[d-1]);
2900 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
2901 gfc_default_integer_kind);
2903 return &gfc_bad_expr;
2905 result = gfc_constant_result (BT_INTEGER, k, &array->where);
2908 /* Then, we need to know the extent of the given dimension. */
2909 if (ref->u.ar.type == AR_FULL)
2914 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
2917 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
2921 mpz_set_si (result->value.integer, 0);
2923 mpz_set_si (result->value.integer, 1);
2927 /* Nonzero extent. */
2929 mpz_set (result->value.integer, u->value.integer);
2931 mpz_set (result->value.integer, l->value.integer);
2938 if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer)
2943 mpz_set_si (result->value.integer, (long int) 1);
2946 return range_check (result, upper ? "UBOUND" : "LBOUND");
2951 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
2957 if (array->expr_type != EXPR_VARIABLE)
2960 /* Follow any component references. */
2961 as = array->symtree->n.sym->as;
2962 for (ref = array->ref; ref; ref = ref->next)
2967 switch (ref->u.ar.type)
2974 /* We're done because 'as' has already been set in the
2975 previous iteration. */
2992 as = ref->u.c.component->as;
3004 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
3009 /* Multi-dimensional bounds. */
3010 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3012 gfc_constructor *head, *tail;
3015 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3016 if (upper && as->type == AS_ASSUMED_SIZE)
3018 /* An error message will be emitted in
3019 check_assumed_size_reference (resolve.c). */
3020 return &gfc_bad_expr;
3023 /* Simplify the bounds for each dimension. */
3024 for (d = 0; d < array->rank; d++)
3026 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref);
3027 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3031 for (j = 0; j < d; j++)
3032 gfc_free_expr (bounds[j]);
3037 /* Allocate the result expression. */
3038 e = gfc_get_expr ();
3039 e->where = array->where;
3040 e->expr_type = EXPR_ARRAY;
3041 e->ts.type = BT_INTEGER;
3042 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3043 gfc_default_integer_kind);
3047 return &gfc_bad_expr;
3051 /* The result is a rank 1 array; its size is the rank of the first
3052 argument to {L,U}BOUND. */
3054 e->shape = gfc_get_shape (1);
3055 mpz_init_set_ui (e->shape[0], array->rank);
3057 /* Create the constructor for this array. */
3059 for (d = 0; d < array->rank; d++)
3061 /* Get a new constructor element. */
3063 head = tail = gfc_get_constructor ();
3066 tail->next = gfc_get_constructor ();
3070 tail->where = e->where;
3071 tail->expr = bounds[d];
3073 e->value.constructor = head;
3079 /* A DIM argument is specified. */
3080 if (dim->expr_type != EXPR_CONSTANT)
3083 d = mpz_get_si (dim->value.integer);
3085 if (d < 1 || d > as->rank
3086 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
3088 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3089 return &gfc_bad_expr;
3092 return simplify_bound_dim (array, kind, d, upper, as, ref);
3098 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3100 return simplify_bound (array, dim, kind, 0);
3105 gfc_simplify_leadz (gfc_expr *e)
3108 unsigned long lz, bs;
3111 if (e->expr_type != EXPR_CONSTANT)
3114 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3115 bs = gfc_integer_kinds[i].bit_size;
3116 if (mpz_cmp_si (e->value.integer, 0) == 0)
3118 else if (mpz_cmp_si (e->value.integer, 0) < 0)
3121 lz = bs - mpz_sizeinbase (e->value.integer, 2);
3123 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3125 mpz_set_ui (result->value.integer, lz);
3132 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3135 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
3138 return &gfc_bad_expr;
3140 if (e->expr_type == EXPR_CONSTANT)
3142 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3143 mpz_set_si (result->value.integer, e->value.character.length);
3144 if (gfc_range_check (result) == ARITH_OK)
3148 gfc_free_expr (result);
3153 if (e->ts.cl != NULL && e->ts.cl->length != NULL
3154 && e->ts.cl->length->expr_type == EXPR_CONSTANT
3155 && e->ts.cl->length->ts.type == BT_INTEGER)
3157 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3158 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
3159 if (gfc_range_check (result) == ARITH_OK)
3163 gfc_free_expr (result);
3173 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3176 int count, len, lentrim, i;
3177 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
3180 return &gfc_bad_expr;
3182 if (e->expr_type != EXPR_CONSTANT)
3185 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3186 len = e->value.character.length;
3188 for (count = 0, i = 1; i <= len; i++)
3189 if (e->value.character.string[len - i] == ' ')
3194 lentrim = len - count;
3196 mpz_set_si (result->value.integer, lentrim);
3197 return range_check (result, "LEN_TRIM");
3201 gfc_simplify_lgamma (gfc_expr *x ATTRIBUTE_UNUSED)
3206 if (x->expr_type != EXPR_CONSTANT)
3209 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3211 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3213 return range_check (result, "LGAMMA");
3218 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3220 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3223 return gfc_logical_expr (gfc_compare_string (a, b) >= 0, &a->where);
3228 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3230 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3233 return gfc_logical_expr (gfc_compare_string (a, b) > 0,
3239 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3241 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3244 return gfc_logical_expr (gfc_compare_string (a, b) <= 0, &a->where);
3249 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3251 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3254 return gfc_logical_expr (gfc_compare_string (a, b) < 0, &a->where);
3259 gfc_simplify_log (gfc_expr *x)
3263 if (x->expr_type != EXPR_CONSTANT)
3266 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3272 if (mpfr_sgn (x->value.real) <= 0)
3274 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3275 "to zero", &x->where);
3276 gfc_free_expr (result);
3277 return &gfc_bad_expr;
3280 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3284 if ((mpfr_sgn (x->value.complex.r) == 0)
3285 && (mpfr_sgn (x->value.complex.i) == 0))
3287 gfc_error ("Complex argument of LOG at %L cannot be zero",
3289 gfc_free_expr (result);
3290 return &gfc_bad_expr;
3293 gfc_set_model_kind (x->ts.kind);
3295 call_mpc_func (result->value.complex.r, result->value.complex.i,
3296 x->value.complex.r, x->value.complex.i, mpc_log);
3303 mpfr_atan2 (result->value.complex.i, x->value.complex.i,
3304 x->value.complex.r, GFC_RND_MODE);
3306 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
3307 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
3308 mpfr_add (xr, xr, xi, GFC_RND_MODE);
3309 mpfr_sqrt (xr, xr, GFC_RND_MODE);
3310 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
3312 mpfr_clears (xr, xi, NULL);
3318 gfc_internal_error ("gfc_simplify_log: bad type");
3321 return range_check (result, "LOG");
3326 gfc_simplify_log10 (gfc_expr *x)
3330 if (x->expr_type != EXPR_CONSTANT)
3333 if (mpfr_sgn (x->value.real) <= 0)
3335 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3336 "to zero", &x->where);
3337 return &gfc_bad_expr;
3340 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3342 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3344 return range_check (result, "LOG10");
3349 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3354 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3356 return &gfc_bad_expr;
3358 if (e->expr_type != EXPR_CONSTANT)
3361 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
3363 result->value.logical = e->value.logical;
3370 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3373 gfc_constructor *ma_ctor, *mb_ctor;
3374 int row, result_rows, col, result_columns, stride_a, stride_b;
3376 if (!is_constant_array_expr (matrix_a)
3377 || !is_constant_array_expr (matrix_b))
3380 gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
3381 result = gfc_start_constructor (matrix_a->ts.type,
3385 if (matrix_a->rank == 1 && matrix_b->rank == 2)
3388 result_columns = mpz_get_si (matrix_b->shape[0]);
3390 stride_b = mpz_get_si (matrix_b->shape[0]);
3393 result->shape = gfc_get_shape (result->rank);
3394 mpz_init_set_si (result->shape[0], result_columns);
3396 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
3398 result_rows = mpz_get_si (matrix_b->shape[0]);
3400 stride_a = mpz_get_si (matrix_a->shape[0]);
3404 result->shape = gfc_get_shape (result->rank);
3405 mpz_init_set_si (result->shape[0], result_rows);
3407 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
3409 result_rows = mpz_get_si (matrix_a->shape[0]);
3410 result_columns = mpz_get_si (matrix_b->shape[1]);
3411 stride_a = mpz_get_si (matrix_a->shape[1]);
3412 stride_b = mpz_get_si (matrix_b->shape[0]);
3415 result->shape = gfc_get_shape (result->rank);
3416 mpz_init_set_si (result->shape[0], result_rows);
3417 mpz_init_set_si (result->shape[1], result_columns);
3422 ma_ctor = matrix_a->value.constructor;
3423 mb_ctor = matrix_b->value.constructor;
3425 for (col = 0; col < result_columns; ++col)
3427 ma_ctor = matrix_a->value.constructor;
3429 for (row = 0; row < result_rows; ++row)
3432 e = compute_dot_product (ma_ctor, stride_a,
3435 gfc_append_constructor (result, e);
3437 ADVANCE (ma_ctor, 1);
3440 ADVANCE (mb_ctor, stride_b);
3448 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3450 if (tsource->expr_type != EXPR_CONSTANT
3451 || fsource->expr_type != EXPR_CONSTANT
3452 || mask->expr_type != EXPR_CONSTANT)
3455 return gfc_copy_expr (mask->value.logical ? tsource : fsource);
3459 /* Selects bewteen current value and extremum for simplify_min_max
3460 and simplify_minval_maxval. */
3462 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
3464 switch (arg->ts.type)
3467 if (mpz_cmp (arg->value.integer,
3468 extremum->value.integer) * sign > 0)
3469 mpz_set (extremum->value.integer, arg->value.integer);
3473 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
3475 mpfr_max (extremum->value.real, extremum->value.real,
3476 arg->value.real, GFC_RND_MODE);
3478 mpfr_min (extremum->value.real, extremum->value.real,
3479 arg->value.real, GFC_RND_MODE);
3483 #define LENGTH(x) ((x)->value.character.length)
3484 #define STRING(x) ((x)->value.character.string)
3485 if (LENGTH(extremum) < LENGTH(arg))
3487 gfc_char_t *tmp = STRING(extremum);
3489 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
3490 memcpy (STRING(extremum), tmp,
3491 LENGTH(extremum) * sizeof (gfc_char_t));
3492 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
3493 LENGTH(arg) - LENGTH(extremum));
3494 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
3495 LENGTH(extremum) = LENGTH(arg);
3499 if (gfc_compare_string (arg, extremum) * sign > 0)
3501 gfc_free (STRING(extremum));
3502 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
3503 memcpy (STRING(extremum), STRING(arg),
3504 LENGTH(arg) * sizeof (gfc_char_t));
3505 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
3506 LENGTH(extremum) - LENGTH(arg));
3507 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
3514 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
3519 /* This function is special since MAX() can take any number of
3520 arguments. The simplified expression is a rewritten version of the
3521 argument list containing at most one constant element. Other
3522 constant elements are deleted. Because the argument list has
3523 already been checked, this function always succeeds. sign is 1 for
3524 MAX(), -1 for MIN(). */
3527 simplify_min_max (gfc_expr *expr, int sign)
3529 gfc_actual_arglist *arg, *last, *extremum;
3530 gfc_intrinsic_sym * specific;
3534 specific = expr->value.function.isym;
3536 arg = expr->value.function.actual;
3538 for (; arg; last = arg, arg = arg->next)
3540 if (arg->expr->expr_type != EXPR_CONSTANT)
3543 if (extremum == NULL)
3549 min_max_choose (arg->expr, extremum->expr, sign);
3551 /* Delete the extra constant argument. */
3553 expr->value.function.actual = arg->next;
3555 last->next = arg->next;
3558 gfc_free_actual_arglist (arg);
3562 /* If there is one value left, replace the function call with the
3564 if (expr->value.function.actual->next != NULL)
3567 /* Convert to the correct type and kind. */
3568 if (expr->ts.type != BT_UNKNOWN)
3569 return gfc_convert_constant (expr->value.function.actual->expr,
3570 expr->ts.type, expr->ts.kind);
3572 if (specific->ts.type != BT_UNKNOWN)
3573 return gfc_convert_constant (expr->value.function.actual->expr,
3574 specific->ts.type, specific->ts.kind);
3576 return gfc_copy_expr (expr->value.function.actual->expr);
3581 gfc_simplify_min (gfc_expr *e)
3583 return simplify_min_max (e, -1);
3588 gfc_simplify_max (gfc_expr *e)
3590 return simplify_min_max (e, 1);
3594 /* This is a simplified version of simplify_min_max to provide
3595 simplification of minval and maxval for a vector. */
3598 simplify_minval_maxval (gfc_expr *expr, int sign)
3600 gfc_constructor *ctr, *extremum;
3601 gfc_intrinsic_sym * specific;
3604 specific = expr->value.function.isym;
3606 ctr = expr->value.constructor;
3608 for (; ctr; ctr = ctr->next)
3610 if (ctr->expr->expr_type != EXPR_CONSTANT)
3613 if (extremum == NULL)
3619 min_max_choose (ctr->expr, extremum->expr, sign);
3622 if (extremum == NULL)
3625 /* Convert to the correct type and kind. */
3626 if (expr->ts.type != BT_UNKNOWN)
3627 return gfc_convert_constant (extremum->expr,
3628 expr->ts.type, expr->ts.kind);
3630 if (specific->ts.type != BT_UNKNOWN)
3631 return gfc_convert_constant (extremum->expr,
3632 specific->ts.type, specific->ts.kind);
3634 return gfc_copy_expr (extremum->expr);
3639 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3641 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3644 return simplify_minval_maxval (array, -1);
3649 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3651 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3653 return simplify_minval_maxval (array, 1);
3658 gfc_simplify_maxexponent (gfc_expr *x)
3663 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3665 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
3666 result->where = x->where;
3673 gfc_simplify_minexponent (gfc_expr *x)
3678 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3680 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
3681 result->where = x->where;
3688 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
3694 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3697 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3698 result = gfc_constant_result (a->ts.type, kind, &a->where);
3703 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3705 /* Result is processor-dependent. */
3706 gfc_error ("Second argument MOD at %L is zero", &a->where);
3707 gfc_free_expr (result);
3708 return &gfc_bad_expr;
3710 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
3714 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3716 /* Result is processor-dependent. */
3717 gfc_error ("Second argument of MOD at %L is zero", &p->where);
3718 gfc_free_expr (result);
3719 return &gfc_bad_expr;
3722 gfc_set_model_kind (kind);
3724 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3725 mpfr_trunc (tmp, tmp);
3726 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3727 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3732 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
3735 return range_check (result, "MOD");
3740 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
3746 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3749 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3750 result = gfc_constant_result (a->ts.type, kind, &a->where);
3755 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3757 /* Result is processor-dependent. This processor just opts
3758 to not handle it at all. */
3759 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
3760 gfc_free_expr (result);
3761 return &gfc_bad_expr;
3763 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
3768 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3770 /* Result is processor-dependent. */
3771 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
3772 gfc_free_expr (result);
3773 return &gfc_bad_expr;
3776 gfc_set_model_kind (kind);
3778 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3779 mpfr_floor (tmp, tmp);
3780 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3781 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3786 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
3789 return range_check (result, "MODULO");
3793 /* Exists for the sole purpose of consistency with other intrinsics. */
3795 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
3796 gfc_expr *fp ATTRIBUTE_UNUSED,
3797 gfc_expr *l ATTRIBUTE_UNUSED,
3798 gfc_expr *to ATTRIBUTE_UNUSED,
3799 gfc_expr *tp ATTRIBUTE_UNUSED)
3806 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
3809 mp_exp_t emin, emax;
3812 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3815 if (mpfr_sgn (s->value.real) == 0)
3817 gfc_error ("Second argument of NEAREST at %L shall not be zero",
3819 return &gfc_bad_expr;
3822 result = gfc_copy_expr (x);
3824 /* Save current values of emin and emax. */
3825 emin = mpfr_get_emin ();
3826 emax = mpfr_get_emax ();
3828 /* Set emin and emax for the current model number. */
3829 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
3830 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[kind].min_exponent -
3831 mpfr_get_prec(result->value.real) + 1);
3832 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[kind].max_exponent - 1);
3833 mpfr_check_range (result->value.real, 0, GMP_RNDU);
3835 if (mpfr_sgn (s->value.real) > 0)
3837 mpfr_nextabove (result->value.real);
3838 mpfr_subnormalize (result->value.real, 0, GMP_RNDU);
3842 mpfr_nextbelow (result->value.real);
3843 mpfr_subnormalize (result->value.real, 0, GMP_RNDD);
3846 mpfr_set_emin (emin);
3847 mpfr_set_emax (emax);
3849 /* Only NaN can occur. Do not use range check as it gives an
3850 error for denormal numbers. */
3851 if (mpfr_nan_p (result->value.real) && gfc_option.flag_range_check)
3853 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
3854 gfc_free_expr (result);
3855 return &gfc_bad_expr;
3863 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
3865 gfc_expr *itrunc, *result;
3868 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
3870 return &gfc_bad_expr;
3872 if (e->expr_type != EXPR_CONSTANT)
3875 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
3877 itrunc = gfc_copy_expr (e);
3879 mpfr_round (itrunc->value.real, e->value.real);
3881 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
3883 gfc_free_expr (itrunc);
3885 return range_check (result, name);
3890 gfc_simplify_new_line (gfc_expr *e)
3894 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3895 result->value.character.string = gfc_get_wide_string (2);
3896 result->value.character.length = 1;
3897 result->value.character.string[0] = '\n';
3898 result->value.character.string[1] = '\0'; /* For debugger */
3904 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
3906 return simplify_nint ("NINT", e, k);
3911 gfc_simplify_idnint (gfc_expr *e)
3913 return simplify_nint ("IDNINT", e, NULL);
3918 gfc_simplify_not (gfc_expr *e)
3922 if (e->expr_type != EXPR_CONSTANT)
3925 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3927 mpz_com (result->value.integer, e->value.integer);
3929 return range_check (result, "NOT");
3934 gfc_simplify_null (gfc_expr *mold)
3940 result = gfc_get_expr ();
3941 result->ts.type = BT_UNKNOWN;
3944 result = gfc_copy_expr (mold);
3945 result->expr_type = EXPR_NULL;
3952 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
3957 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3960 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3961 if (x->ts.type == BT_INTEGER)
3963 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3964 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
3965 return range_check (result, "OR");
3967 else /* BT_LOGICAL */
3969 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3970 result->value.logical = x->value.logical || y->value.logical;
3977 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3980 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
3982 if (!is_constant_array_expr(array)
3983 || !is_constant_array_expr(vector)
3984 || (!gfc_is_constant_expr (mask)
3985 && !is_constant_array_expr(mask)))
3988 result = gfc_start_constructor (array->ts.type,
3992 array_ctor = array->value.constructor;
3993 vector_ctor = vector ? vector->value.constructor : NULL;
3995 if (mask->expr_type == EXPR_CONSTANT
3996 && mask->value.logical)
3998 /* Copy all elements of ARRAY to RESULT. */
4001 gfc_append_constructor (result,
4002 gfc_copy_expr (array_ctor->expr));
4004 ADVANCE (array_ctor, 1);
4005 ADVANCE (vector_ctor, 1);
4008 else if (mask->expr_type == EXPR_ARRAY)
4010 /* Copy only those elements of ARRAY to RESULT whose
4011 MASK equals .TRUE.. */
4012 mask_ctor = mask->value.constructor;
4015 if (mask_ctor->expr->value.logical)
4017 gfc_append_constructor (result,
4018 gfc_copy_expr (array_ctor->expr));
4019 ADVANCE (vector_ctor, 1);
4022 ADVANCE (array_ctor, 1);
4023 ADVANCE (mask_ctor, 1);
4027 /* Append any left-over elements from VECTOR to RESULT. */
4030 gfc_append_constructor (result,
4031 gfc_copy_expr (vector_ctor->expr));
4032 ADVANCE (vector_ctor, 1);
4035 result->shape = gfc_get_shape (1);
4036 gfc_array_size (result, &result->shape[0]);
4038 if (array->ts.type == BT_CHARACTER)
4039 result->ts.cl = array->ts.cl;
4046 gfc_simplify_precision (gfc_expr *e)
4051 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4053 result = gfc_int_expr (gfc_real_kinds[i].precision);
4054 result->where = e->where;
4061 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
4065 if (!is_constant_array_expr (array)
4066 || !gfc_is_constant_expr (dim))
4070 && !is_constant_array_expr (mask)
4071 && mask->expr_type != EXPR_CONSTANT)
4074 result = transformational_result (array, dim, array->ts.type,
4075 array->ts.kind, &array->where);
4076 init_result_expr (result, 1, NULL);
4078 return !dim || array->rank == 1 ?
4079 simplify_transformation_to_scalar (result, array, mask, gfc_multiply) :
4080 simplify_transformation_to_array (result, array, dim, mask, gfc_multiply);
4085 gfc_simplify_radix (gfc_expr *e)
4090 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4094 i = gfc_integer_kinds[i].radix;
4098 i = gfc_real_kinds[i].radix;
4105 result = gfc_int_expr (i);
4106 result->where = e->where;
4113 gfc_simplify_range (gfc_expr *e)
4119 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4124 j = gfc_integer_kinds[i].range;
4129 j = gfc_real_kinds[i].range;
4136 result = gfc_int_expr (j);
4137 result->where = e->where;
4144 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
4146 gfc_expr *result = NULL;
4149 if (e->ts.type == BT_COMPLEX)
4150 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
4152 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
4155 return &gfc_bad_expr;
4157 if (e->expr_type != EXPR_CONSTANT)
4164 result = gfc_int2real (e, kind);
4168 result = gfc_real2real (e, kind);
4172 result = gfc_complex2real (e, kind);
4176 gfc_internal_error ("bad type in REAL");
4180 if (e->ts.type == BT_INTEGER && e->is_boz)
4186 result = gfc_copy_expr (e);
4187 if (!gfc_convert_boz (result, &ts))
4189 gfc_free_expr (result);
4190 return &gfc_bad_expr;
4194 return range_check (result, "REAL");
4199 gfc_simplify_realpart (gfc_expr *e)
4203 if (e->expr_type != EXPR_CONSTANT)
4206 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
4207 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
4209 return range_check (result, "REALPART");
4213 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
4216 int i, j, len, ncop, nlen;
4218 bool have_length = false;
4220 /* If NCOPIES isn't a constant, there's nothing we can do. */
4221 if (n->expr_type != EXPR_CONSTANT)
4224 /* If NCOPIES is negative, it's an error. */
4225 if (mpz_sgn (n->value.integer) < 0)
4227 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4229 return &gfc_bad_expr;
4232 /* If we don't know the character length, we can do no more. */
4233 if (e->ts.cl && e->ts.cl->length
4234 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
4236 len = mpz_get_si (e->ts.cl->length->value.integer);
4239 else if (e->expr_type == EXPR_CONSTANT
4240 && (e->ts.cl == NULL || e->ts.cl->length == NULL))
4242 len = e->value.character.length;
4247 /* If the source length is 0, any value of NCOPIES is valid
4248 and everything behaves as if NCOPIES == 0. */
4251 mpz_set_ui (ncopies, 0);
4253 mpz_set (ncopies, n->value.integer);
4255 /* Check that NCOPIES isn't too large. */
4261 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4263 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4267 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
4268 e->ts.cl->length->value.integer);
4272 mpz_init_set_si (mlen, len);
4273 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
4277 /* The check itself. */
4278 if (mpz_cmp (ncopies, max) > 0)
4281 mpz_clear (ncopies);
4282 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4284 return &gfc_bad_expr;
4289 mpz_clear (ncopies);
4291 /* For further simplification, we need the character string to be
4293 if (e->expr_type != EXPR_CONSTANT)
4297 (e->ts.cl->length &&
4298 mpz_sgn (e->ts.cl->length->value.integer)) != 0)
4300 const char *res = gfc_extract_int (n, &ncop);
4301 gcc_assert (res == NULL);
4306 len = e->value.character.length;
4309 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
4313 result->value.character.string = gfc_get_wide_string (1);
4314 result->value.character.length = 0;
4315 result->value.character.string[0] = '\0';
4319 result->value.character.length = nlen;
4320 result->value.character.string = gfc_get_wide_string (nlen + 1);
4322 for (i = 0; i < ncop; i++)
4323 for (j = 0; j < len; j++)
4324 result->value.character.string[j+i*len]= e->value.character.string[j];
4326 result->value.character.string[nlen] = '\0'; /* For debugger */
4331 /* This one is a bear, but mainly has to do with shuffling elements. */
4334 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
4335 gfc_expr *pad, gfc_expr *order_exp)
4337 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
4338 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
4339 gfc_constructor *head, *tail;
4345 /* Check that argument expression types are OK. */
4346 if (!is_constant_array_expr (source)
4347 || !is_constant_array_expr (shape_exp)
4348 || !is_constant_array_expr (pad)
4349 || !is_constant_array_expr (order_exp))
4352 /* Proceed with simplification, unpacking the array. */
4360 e = gfc_get_array_element (shape_exp, rank);
4364 gfc_extract_int (e, &shape[rank]);
4366 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
4367 gcc_assert (shape[rank] >= 0);
4373 gcc_assert (rank > 0);
4375 /* Now unpack the order array if present. */
4376 if (order_exp == NULL)
4378 for (i = 0; i < rank; i++)
4383 for (i = 0; i < rank; i++)
4386 for (i = 0; i < rank; i++)
4388 e = gfc_get_array_element (order_exp, i);
4391 gfc_extract_int (e, &order[i]);
4394 gcc_assert (order[i] >= 1 && order[i] <= rank);
4396 gcc_assert (x[order[i]] == 0);
4401 /* Count the elements in the source and padding arrays. */
4406 gfc_array_size (pad, &size);
4407 npad = mpz_get_ui (size);
4411 gfc_array_size (source, &size);
4412 nsource = mpz_get_ui (size);
4415 /* If it weren't for that pesky permutation we could just loop
4416 through the source and round out any shortage with pad elements.
4417 But no, someone just had to have the compiler do something the
4418 user should be doing. */
4420 for (i = 0; i < rank; i++)
4423 while (nsource > 0 || npad > 0)
4425 /* Figure out which element to extract. */
4426 mpz_set_ui (index, 0);
4428 for (i = rank - 1; i >= 0; i--)
4430 mpz_add_ui (index, index, x[order[i]]);
4432 mpz_mul_ui (index, index, shape[order[i - 1]]);
4435 if (mpz_cmp_ui (index, INT_MAX) > 0)
4436 gfc_internal_error ("Reshaped array too large at %C");
4438 j = mpz_get_ui (index);
4441 e = gfc_get_array_element (source, j);
4444 gcc_assert (npad > 0);
4448 e = gfc_get_array_element (pad, j);
4453 head = tail = gfc_get_constructor ();
4456 tail->next = gfc_get_constructor ();
4460 tail->where = e->where;
4463 /* Calculate the next element. */
4467 if (++x[i] < shape[i])
4478 e = gfc_get_expr ();
4479 e->where = source->where;
4480 e->expr_type = EXPR_ARRAY;
4481 e->value.constructor = head;
4482 e->shape = gfc_get_shape (rank);
4484 for (i = 0; i < rank; i++)
4485 mpz_init_set_ui (e->shape[i], shape[i]);
4495 gfc_simplify_rrspacing (gfc_expr *x)
4501 if (x->expr_type != EXPR_CONSTANT)
4504 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
4506 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4508 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
4510 /* Special case x = -0 and 0. */
4511 if (mpfr_sgn (result->value.real) == 0)
4513 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4517 /* | x * 2**(-e) | * 2**p. */
4518 e = - (long int) mpfr_get_exp (x->value.real);
4519 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
4521 p = (long int) gfc_real_kinds[i].digits;
4522 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
4524 return range_check (result, "RRSPACING");
4529 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
4531 int k, neg_flag, power, exp_range;
4532 mpfr_t scale, radix;
4535 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4538 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4540 if (mpfr_sgn (x->value.real) == 0)
4542 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4546 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
4548 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
4550 /* This check filters out values of i that would overflow an int. */
4551 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
4552 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
4554 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
4555 gfc_free_expr (result);
4556 return &gfc_bad_expr;
4559 /* Compute scale = radix ** power. */
4560 power = mpz_get_si (i->value.integer);
4570 gfc_set_model_kind (x->ts.kind);
4573 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
4574 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
4577 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
4579 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
4581 mpfr_clears (scale, radix, NULL);
4583 return range_check (result, "SCALE");
4587 /* Variants of strspn and strcspn that operate on wide characters. */
4590 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
4593 const gfc_char_t *c;
4597 for (c = s2; *c; c++)
4611 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
4614 const gfc_char_t *c;
4618 for (c = s2; *c; c++)
4633 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
4638 size_t indx, len, lenc;
4639 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
4642 return &gfc_bad_expr;
4644 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
4647 if (b != NULL && b->value.logical != 0)
4652 result = gfc_constant_result (BT_INTEGER, k, &e->where);
4654 len = e->value.character.length;
4655 lenc = c->value.character.length;
4657 if (len == 0 || lenc == 0)
4665 indx = wide_strcspn (e->value.character.string,
4666 c->value.character.string) + 1;
4673 for (indx = len; indx > 0; indx--)
4675 for (i = 0; i < lenc; i++)
4677 if (c->value.character.string[i]
4678 == e->value.character.string[indx - 1])
4686 mpz_set_ui (result->value.integer, indx);
4687 return range_check (result, "SCAN");
4692 gfc_simplify_selected_char_kind (gfc_expr *e)
4697 if (e->expr_type != EXPR_CONSTANT)
4700 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
4701 || gfc_compare_with_Cstring (e, "default", false) == 0)
4703 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
4708 result = gfc_int_expr (kind);
4709 result->where = e->where;
4716 gfc_simplify_selected_int_kind (gfc_expr *e)
4721 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
4726 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
4727 if (gfc_integer_kinds[i].range >= range
4728 && gfc_integer_kinds[i].kind < kind)
4729 kind = gfc_integer_kinds[i].kind;
4731 if (kind == INT_MAX)
4734 result = gfc_int_expr (kind);
4735 result->where = e->where;
4742 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q)
4744 int range, precision, i, kind, found_precision, found_range;
4751 if (p->expr_type != EXPR_CONSTANT
4752 || gfc_extract_int (p, &precision) != NULL)
4760 if (q->expr_type != EXPR_CONSTANT
4761 || gfc_extract_int (q, &range) != NULL)
4766 found_precision = 0;
4769 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
4771 if (gfc_real_kinds[i].precision >= precision)
4772 found_precision = 1;
4774 if (gfc_real_kinds[i].range >= range)
4777 if (gfc_real_kinds[i].precision >= precision
4778 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
4779 kind = gfc_real_kinds[i].kind;
4782 if (kind == INT_MAX)
4786 if (!found_precision)
4792 result = gfc_int_expr (kind);
4793 result->where = (p != NULL) ? p->where : q->where;
4800 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
4803 mpfr_t exp, absv, log2, pow2, frac;
4806 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4809 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4811 if (mpfr_sgn (x->value.real) == 0)
4813 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4817 gfc_set_model_kind (x->ts.kind);
4824 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
4825 mpfr_log2 (log2, absv, GFC_RND_MODE);
4827 mpfr_trunc (log2, log2);
4828 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
4830 /* Old exponent value, and fraction. */
4831 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
4833 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
4836 exp2 = (unsigned long) mpz_get_d (i->value.integer);
4837 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
4839 mpfr_clears (absv, log2, pow2, frac, NULL);
4841 return range_check (result, "SET_EXPONENT");
4846 gfc_simplify_shape (gfc_expr *source)
4848 mpz_t shape[GFC_MAX_DIMENSIONS];
4849 gfc_expr *result, *e, *f;
4854 if (source->rank == 0)
4855 return gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4858 if (source->expr_type != EXPR_VARIABLE)
4861 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4864 ar = gfc_find_array_ref (source);
4866 t = gfc_array_ref_shape (ar, shape);
4868 for (n = 0; n < source->rank; n++)
4870 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
4875 mpz_set (e->value.integer, shape[n]);
4876 mpz_clear (shape[n]);
4880 mpz_set_ui (e->value.integer, n + 1);
4882 f = gfc_simplify_size (source, e, NULL);
4886 gfc_free_expr (result);
4895 gfc_append_constructor (result, e);
4903 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4908 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
4911 return &gfc_bad_expr;
4915 if (gfc_array_size (array, &size) == FAILURE)
4920 if (dim->expr_type != EXPR_CONSTANT)
4923 d = mpz_get_ui (dim->value.integer) - 1;
4924 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
4928 result = gfc_constant_result (BT_INTEGER, k, &array->where);
4929 mpz_set (result->value.integer, size);
4935 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
4939 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4942 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
4947 mpz_abs (result->value.integer, x->value.integer);
4948 if (mpz_sgn (y->value.integer) < 0)
4949 mpz_neg (result->value.integer, result->value.integer);
4954 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
4956 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
4957 if (mpfr_sgn (y->value.real) < 0)
4958 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
4963 gfc_internal_error ("Bad type in gfc_simplify_sign");
4971 gfc_simplify_sin (gfc_expr *x)
4975 if (x->expr_type != EXPR_CONSTANT)
4978 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
4983 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
4987 gfc_set_model (x->value.real);
4989 call_mpc_func (result->value.complex.r, result->value.complex.i,
4990 x->value.complex.r, x->value.complex.i, mpc_sin);
4997 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
4998 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
4999 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
5001 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
5002 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
5003 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
5005 mpfr_clears (xp, xq, NULL);
5011 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5014 return range_check (result, "SIN");
5019 gfc_simplify_sinh (gfc_expr *x)
5023 if (x->expr_type != EXPR_CONSTANT)
5026 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5028 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
5030 return range_check (result, "SINH");
5034 /* The argument is always a double precision real that is converted to
5035 single precision. TODO: Rounding! */
5038 gfc_simplify_sngl (gfc_expr *a)
5042 if (a->expr_type != EXPR_CONSTANT)
5045 result = gfc_real2real (a, gfc_default_real_kind);
5046 return range_check (result, "SNGL");
5051 gfc_simplify_spacing (gfc_expr *x)
5057 if (x->expr_type != EXPR_CONSTANT)
5060 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
5062 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
5064 /* Special case x = 0 and -0. */
5065 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
5066 if (mpfr_sgn (result->value.real) == 0)
5068 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5072 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5073 are the radix, exponent of x, and precision. This excludes the
5074 possibility of subnormal numbers. Fortran 2003 states the result is
5075 b**max(e - p, emin - 1). */
5077 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
5078 en = (long int) gfc_real_kinds[i].min_exponent - 1;
5079 en = en > ep ? en : ep;
5081 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
5082 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
5084 return range_check (result, "SPACING");
5089 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
5091 gfc_expr *result = 0L;
5092 int i, j, dim, ncopies;
5094 if ((!gfc_is_constant_expr (source)
5095 && !is_constant_array_expr (source))
5096 || !gfc_is_constant_expr (dim_expr)
5097 || !gfc_is_constant_expr (ncopies_expr))
5100 gcc_assert (dim_expr->ts.type == BT_INTEGER);
5101 gfc_extract_int (dim_expr, &dim);
5102 dim -= 1; /* zero-base DIM */
5104 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
5105 gfc_extract_int (ncopies_expr, &ncopies);
5106 ncopies = MAX (ncopies, 0);
5108 if (source->expr_type == EXPR_CONSTANT)
5110 gcc_assert (dim == 0);
5112 result = gfc_start_constructor (source->ts.type,
5116 result->shape = gfc_get_shape (result->rank);
5117 mpz_init_set_si (result->shape[0], ncopies);
5119 for (i = 0; i < ncopies; ++i)
5120 gfc_append_constructor (result, gfc_copy_expr (source));
5122 else if (source->expr_type == EXPR_ARRAY)
5124 int result_size, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
5125 gfc_constructor *ctor, *source_ctor, *result_ctor;
5127 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
5128 gcc_assert (dim >= 0 && dim <= source->rank);
5130 result = gfc_start_constructor (source->ts.type,
5133 result->rank = source->rank + 1;
5134 result->shape = gfc_get_shape (result->rank);
5137 for (i = 0, j = 0; i < result->rank; ++i)
5140 mpz_init_set (result->shape[i], source->shape[j++]);
5142 mpz_init_set_si (result->shape[i], ncopies);
5144 extent[i] = mpz_get_si (result->shape[i]);
5145 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
5146 result_size *= extent[i];
5149 for (i = 0; i < result_size; ++i)
5150 gfc_append_constructor (result, NULL);
5152 source_ctor = source->value.constructor;
5153 result_ctor = result->value.constructor;
5158 for (i = 0; i < ncopies; ++i)
5160 ctor->expr = gfc_copy_expr (source_ctor->expr);
5161 ADVANCE (ctor, rstride[dim]);
5164 ADVANCE (result_ctor, (dim == 0 ? ncopies : 1));
5165 ADVANCE (source_ctor, 1);
5169 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5170 Replace NULL with gcc_unreachable() after implementing
5171 gfc_simplify_cshift(). */
5174 if (source->ts.type == BT_CHARACTER)
5175 result->ts.cl = source->ts.cl;
5182 gfc_simplify_sqrt (gfc_expr *e)
5186 if (e->expr_type != EXPR_CONSTANT)
5189 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
5194 if (mpfr_cmp_si (e->value.real, 0) < 0)
5196 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
5201 gfc_set_model (e->value.real);
5203 call_mpc_func (result->value.complex.r, result->value.complex.i,
5204 e->value.complex.r, e->value.complex.i, mpc_sqrt);
5207 /* Formula taken from Numerical Recipes to avoid over- and
5210 mpfr_t ac, ad, s, t, w;
5217 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
5218 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
5220 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
5221 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
5225 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
5226 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
5228 if (mpfr_cmp (ac, ad) >= 0)
5230 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
5231 mpfr_mul (t, t, t, GFC_RND_MODE);
5232 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5233 mpfr_sqrt (t, t, GFC_RND_MODE);
5234 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5235 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5236 mpfr_sqrt (t, t, GFC_RND_MODE);
5237 mpfr_sqrt (s, ac, GFC_RND_MODE);
5238 mpfr_mul (w, s, t, GFC_RND_MODE);
5242 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
5243 mpfr_mul (t, s, s, GFC_RND_MODE);
5244 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5245 mpfr_sqrt (t, t, GFC_RND_MODE);
5246 mpfr_abs (s, s, GFC_RND_MODE);
5247 mpfr_add (t, t, s, GFC_RND_MODE);
5248 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5249 mpfr_sqrt (t, t, GFC_RND_MODE);
5250 mpfr_sqrt (s, ad, GFC_RND_MODE);
5251 mpfr_mul (w, s, t, GFC_RND_MODE);
5254 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
5256 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5257 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
5258 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
5260 else if (mpfr_cmp_ui (w, 0) != 0
5261 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5262 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
5264 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5265 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
5266 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5268 else if (mpfr_cmp_ui (w, 0) != 0
5269 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5270 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
5272 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5273 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
5274 mpfr_neg (w, w, GFC_RND_MODE);
5275 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5278 gfc_internal_error ("invalid complex argument of SQRT at %L",
5281 mpfr_clears (s, t, ac, ad, w, NULL);
5287 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
5290 return range_check (result, "SQRT");
5293 gfc_free_expr (result);
5294 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
5295 return &gfc_bad_expr;
5300 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
5304 if (!is_constant_array_expr (array)
5305 || !gfc_is_constant_expr (dim))
5309 && !is_constant_array_expr (mask)
5310 && mask->expr_type != EXPR_CONSTANT)
5313 result = transformational_result (array, dim, array->ts.type,
5314 array->ts.kind, &array->where);
5315 init_result_expr (result, 0, NULL);
5317 return !dim || array->rank == 1 ?
5318 simplify_transformation_to_scalar (result, array, mask, gfc_add) :
5319 simplify_transformation_to_array (result, array, dim, mask, gfc_add);
5324 gfc_simplify_tan (gfc_expr *x)
5329 if (x->expr_type != EXPR_CONSTANT)
5332 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5334 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5336 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
5338 return range_check (result, "TAN");
5343 gfc_simplify_tanh (gfc_expr *x)
5347 if (x->expr_type != EXPR_CONSTANT)
5350 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5352 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
5354 return range_check (result, "TANH");
5360 gfc_simplify_tiny (gfc_expr *e)
5365 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
5367 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
5368 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5375 gfc_simplify_trailz (gfc_expr *e)
5378 unsigned long tz, bs;
5381 if (e->expr_type != EXPR_CONSTANT)
5384 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
5385 bs = gfc_integer_kinds[i].bit_size;
5386 tz = mpz_scan1 (e->value.integer, 0);
5388 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind, &e->where);
5389 mpz_set_ui (result->value.integer, MIN (tz, bs));
5396 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5399 gfc_expr *mold_element;
5402 size_t result_elt_size;
5405 unsigned char *buffer;
5407 if (!gfc_is_constant_expr (source)
5408 || (gfc_init_expr && !gfc_is_constant_expr (mold))
5409 || !gfc_is_constant_expr (size))
5412 if (source->expr_type == EXPR_FUNCTION)
5415 /* Calculate the size of the source. */
5416 if (source->expr_type == EXPR_ARRAY
5417 && gfc_array_size (source, &tmp) == FAILURE)
5418 gfc_internal_error ("Failure getting length of a constant array.");
5420 source_size = gfc_target_expr_size (source);
5422 /* Create an empty new expression with the appropriate characteristics. */
5423 result = gfc_constant_result (mold->ts.type, mold->ts.kind,
5425 result->ts = mold->ts;
5427 mold_element = mold->expr_type == EXPR_ARRAY
5428 ? mold->value.constructor->expr
5431 /* Set result character length, if needed. Note that this needs to be
5432 set even for array expressions, in order to pass this information into
5433 gfc_target_interpret_expr. */
5434 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
5435 result->value.character.length = mold_element->value.character.length;
5437 /* Set the number of elements in the result, and determine its size. */
5438 result_elt_size = gfc_target_expr_size (mold_element);
5439 if (result_elt_size == 0)
5441 gfc_free_expr (result);
5445 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5449 result->expr_type = EXPR_ARRAY;
5453 result_length = (size_t)mpz_get_ui (size->value.integer);
5456 result_length = source_size / result_elt_size;
5457 if (result_length * result_elt_size < source_size)
5461 result->shape = gfc_get_shape (1);
5462 mpz_init_set_ui (result->shape[0], result_length);
5464 result_size = result_length * result_elt_size;
5469 result_size = result_elt_size;
5472 if (gfc_option.warn_surprising && source_size < result_size)
5473 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
5474 "source size %ld < result size %ld", &source->where,
5475 (long) source_size, (long) result_size);
5477 /* Allocate the buffer to store the binary version of the source. */
5478 buffer_size = MAX (source_size, result_size);
5479 buffer = (unsigned char*)alloca (buffer_size);
5480 memset (buffer, 0, buffer_size);
5482 /* Now write source to the buffer. */
5483 gfc_target_encode_expr (source, buffer, buffer_size);
5485 /* And read the buffer back into the new expression. */
5486 gfc_target_interpret_expr (buffer, buffer_size, result);
5493 gfc_simplify_transpose (gfc_expr *matrix)
5497 gfc_constructor *matrix_ctor;
5499 if (!is_constant_array_expr (matrix))
5502 gcc_assert (matrix->rank == 2);
5504 result = gfc_start_constructor (matrix->ts.type, matrix->ts.kind, &matrix->where);
5506 result->shape = gfc_get_shape (result->rank);
5507 mpz_set (result->shape[0], matrix->shape[1]);
5508 mpz_set (result->shape[1], matrix->shape[0]);
5510 if (matrix->ts.type == BT_CHARACTER)
5511 result->ts.cl = matrix->ts.cl;
5513 matrix_rows = mpz_get_si (matrix->shape[0]);
5514 matrix_ctor = matrix->value.constructor;
5515 for (i = 0; i < matrix_rows; ++i)
5517 gfc_constructor *column_ctor = matrix_ctor;
5520 gfc_append_constructor (result,
5521 gfc_copy_expr (column_ctor->expr));
5523 ADVANCE (column_ctor, matrix_rows);
5526 ADVANCE (matrix_ctor, 1);
5534 gfc_simplify_trim (gfc_expr *e)
5537 int count, i, len, lentrim;
5539 if (e->expr_type != EXPR_CONSTANT)
5542 len = e->value.character.length;
5544 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
5546 for (count = 0, i = 1; i <= len; ++i)
5548 if (e->value.character.string[len - i] == ' ')
5554 lentrim = len - count;
5556 result->value.character.length = lentrim;
5557 result->value.character.string = gfc_get_wide_string (lentrim + 1);
5559 for (i = 0; i < lentrim; i++)
5560 result->value.character.string[i] = e->value.character.string[i];
5562 result->value.character.string[lentrim] = '\0'; /* For debugger */
5569 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5571 return simplify_bound (array, dim, kind, 1);
5576 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5578 gfc_expr *result, *e;
5579 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
5581 if (!is_constant_array_expr (vector)
5582 || !is_constant_array_expr (mask)
5583 || (!gfc_is_constant_expr (field)
5584 && !is_constant_array_expr(field)))
5587 result = gfc_start_constructor (vector->ts.type,
5590 result->rank = mask->rank;
5591 result->shape = gfc_copy_shape (mask->shape, mask->rank);
5593 if (vector->ts.type == BT_CHARACTER)
5594 result->ts.cl = vector->ts.cl;
5596 vector_ctor = vector->value.constructor;
5597 mask_ctor = mask->value.constructor;
5598 field_ctor = field->expr_type == EXPR_ARRAY ? field->value.constructor : NULL;
5602 if (mask_ctor->expr->value.logical)
5604 gcc_assert (vector_ctor);
5605 e = gfc_copy_expr (vector_ctor->expr);
5606 ADVANCE (vector_ctor, 1);
5608 else if (field->expr_type == EXPR_ARRAY)
5609 e = gfc_copy_expr (field_ctor->expr);
5611 e = gfc_copy_expr (field);
5613 gfc_append_constructor (result, e);
5615 ADVANCE (mask_ctor, 1);
5616 ADVANCE (field_ctor, 1);
5624 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
5628 size_t index, len, lenset;
5630 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
5633 return &gfc_bad_expr;
5635 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
5638 if (b != NULL && b->value.logical != 0)
5643 result = gfc_constant_result (BT_INTEGER, k, &s->where);
5645 len = s->value.character.length;
5646 lenset = set->value.character.length;
5650 mpz_set_ui (result->value.integer, 0);
5658 mpz_set_ui (result->value.integer, 1);
5662 index = wide_strspn (s->value.character.string,
5663 set->value.character.string) + 1;
5672 mpz_set_ui (result->value.integer, len);
5675 for (index = len; index > 0; index --)
5677 for (i = 0; i < lenset; i++)
5679 if (s->value.character.string[index - 1]
5680 == set->value.character.string[i])
5688 mpz_set_ui (result->value.integer, index);
5694 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
5699 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
5702 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
5703 if (x->ts.type == BT_INTEGER)
5705 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
5706 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
5707 return range_check (result, "XOR");
5709 else /* BT_LOGICAL */
5711 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
5712 result->value.logical = (x->value.logical && !y->value.logical)
5713 || (!x->value.logical && y->value.logical);
5720 /****************** Constant simplification *****************/
5722 /* Master function to convert one constant to another. While this is
5723 used as a simplification function, it requires the destination type
5724 and kind information which is supplied by a special case in
5728 gfc_convert_constant (gfc_expr *e, bt type, int kind)
5730 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
5731 gfc_constructor *head, *c, *tail = NULL;
5745 f = gfc_int2complex;
5765 f = gfc_real2complex;
5776 f = gfc_complex2int;
5779 f = gfc_complex2real;
5782 f = gfc_complex2complex;
5808 f = gfc_hollerith2int;
5812 f = gfc_hollerith2real;
5816 f = gfc_hollerith2complex;
5820 f = gfc_hollerith2character;
5824 f = gfc_hollerith2logical;
5834 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
5839 switch (e->expr_type)
5842 result = f (e, kind);
5844 return &gfc_bad_expr;
5848 if (!gfc_is_constant_expr (e))
5853 for (c = e->value.constructor; c; c = c->next)
5856 head = tail = gfc_get_constructor ();
5859 tail->next = gfc_get_constructor ();
5863 tail->where = c->where;
5865 if (c->iterator == NULL)
5866 tail->expr = f (c->expr, kind);
5869 g = gfc_convert_constant (c->expr, type, kind);
5870 if (g == &gfc_bad_expr)
5875 if (tail->expr == NULL)
5877 gfc_free_constructor (head);
5882 result = gfc_get_expr ();
5883 result->ts.type = type;
5884 result->ts.kind = kind;
5885 result->expr_type = EXPR_ARRAY;
5886 result->value.constructor = head;
5887 result->shape = gfc_copy_shape (e->shape, e->rank);
5888 result->where = e->where;
5889 result->rank = e->rank;
5900 /* Function for converting character constants. */
5902 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
5907 if (!gfc_is_constant_expr (e))
5910 if (e->expr_type == EXPR_CONSTANT)
5912 /* Simple case of a scalar. */
5913 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
5915 return &gfc_bad_expr;
5917 result->value.character.length = e->value.character.length;
5918 result->value.character.string
5919 = gfc_get_wide_string (e->value.character.length + 1);
5920 memcpy (result->value.character.string, e->value.character.string,
5921 (e->value.character.length + 1) * sizeof (gfc_char_t));
5923 /* Check we only have values representable in the destination kind. */
5924 for (i = 0; i < result->value.character.length; i++)
5925 if (!gfc_check_character_range (result->value.character.string[i],
5928 gfc_error ("Character '%s' in string at %L cannot be converted "
5929 "into character kind %d",
5930 gfc_print_wide_char (result->value.character.string[i]),
5932 return &gfc_bad_expr;
5937 else if (e->expr_type == EXPR_ARRAY)
5939 /* For an array constructor, we convert each constructor element. */
5940 gfc_constructor *head = NULL, *tail = NULL, *c;
5942 for (c = e->value.constructor; c; c = c->next)
5945 head = tail = gfc_get_constructor ();
5948 tail->next = gfc_get_constructor ();
5952 tail->where = c->where;
5953 tail->expr = gfc_convert_char_constant (c->expr, type, kind);
5954 if (tail->expr == &gfc_bad_expr)
5957 return &gfc_bad_expr;
5960 if (tail->expr == NULL)
5962 gfc_free_constructor (head);
5967 result = gfc_get_expr ();
5968 result->ts.type = type;
5969 result->ts.kind = kind;
5970 result->expr_type = EXPR_ARRAY;
5971 result->value.constructor = head;
5972 result->shape = gfc_copy_shape (e->shape, e->rank);
5973 result->where = e->where;
5974 result->rank = e->rank;
5975 result->ts.cl = e->ts.cl;