OSDN Git Service

2004-06-20 Steven G. Kargl <kargls@comcast.net>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / arith.c
1 /* Compiler arithmetic
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
3    Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
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
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /* Since target arithmetic must be done on the host, there has to
24    be some way of evaluating arithmetic expressions as the host
25    would evaluate them.  We use the GNU MP library to do arithmetic,
26    and this file provides the interface.  */
27
28 #include "config.h"
29
30 #include <string.h>
31
32 #include "gfortran.h"
33 #include "arith.h"
34
35 mpf_t pi, half_pi, two_pi, e;
36
37 /* The gfc_(integer|real)_kinds[] structures have everything the front
38    end needs to know about integers and real numbers on the target.
39    Other entries of the structure are calculated from these values.
40    The first entry is the default kind, the second entry of the real
41    structure is the default double kind.  */
42
43 #define MPZ_NULL {{0,0,0}}
44 #define MPF_NULL {{0,0,0,0}}
45
46 #define DEF_GFC_INTEGER_KIND(KIND,RADIX,DIGITS,BIT_SIZE)                \
47         {KIND, RADIX, DIGITS, BIT_SIZE, 0, MPZ_NULL, MPZ_NULL, MPZ_NULL}
48
49 #define DEF_GFC_LOGICAL_KIND(KIND,BIT_SIZE)                             \
50         {KIND, BIT_SIZE}
51
52 #define DEF_GFC_REAL_KIND(KIND,RADIX,DIGITS,MIN_EXP, MAX_EXP)           \
53         {KIND, RADIX, DIGITS, MIN_EXP, MAX_EXP,                         \
54          0, 0, MPF_NULL, MPF_NULL, MPF_NULL}
55
56 gfc_integer_info gfc_integer_kinds[] = {
57   DEF_GFC_INTEGER_KIND (4, 2, 31, 32),
58   DEF_GFC_INTEGER_KIND (8, 2, 63, 64),
59   DEF_GFC_INTEGER_KIND (2, 2, 15, 16),
60   DEF_GFC_INTEGER_KIND (1, 2,  7,  8),
61   DEF_GFC_INTEGER_KIND (0, 0,  0,  0)
62 };
63
64 gfc_logical_info gfc_logical_kinds[] = {
65   DEF_GFC_LOGICAL_KIND (4, 32),
66   DEF_GFC_LOGICAL_KIND (8, 64),
67   DEF_GFC_LOGICAL_KIND (2, 16),
68   DEF_GFC_LOGICAL_KIND (1,  8),
69   DEF_GFC_LOGICAL_KIND (0,  0)
70 };
71
72 gfc_real_info gfc_real_kinds[] = {
73   DEF_GFC_REAL_KIND (4, 2, 24,  -125,  128),
74   DEF_GFC_REAL_KIND (8, 2, 53, -1021, 1024),
75   DEF_GFC_REAL_KIND (0, 0,  0,     0,    0)
76 };
77
78
79 /* The integer kind to use for array indices.  This will be set to the
80    proper value based on target information from the backend.  */
81
82 int gfc_index_integer_kind;
83
84
85 /* Compute the natural log of arg.
86
87    We first get the argument into the range 0.5 to 1.5 by successive
88    multiplications or divisions by e.  Then we use the series:
89
90      ln(x) = (x-1) - (x-1)^2/2 + (x-1)^3/3 - (x-1)^4/4 + ...
91
92    Because we are expanding in powers of (x-1), and 0.5 < x < 1.5, we
93    have -0.5 < (x-1) < 0.5.  Ignoring the harmonic term, this means
94    that each term is at most 1/(2^i), meaning one bit is gained per
95    iteration.
96
97    Not very efficient, but it doesn't have to be.  */
98
99 void
100 natural_logarithm (mpf_t * arg, mpf_t * result)
101 {
102   mpf_t x, xp, t, log;
103   int i, p;
104
105   mpf_init_set (x, *arg);
106   mpf_init (t);
107
108   p = 0;
109
110   mpf_set_str (t, "0.5", 10);
111   while (mpf_cmp (x, t) < 0)
112     {
113       mpf_mul (x, x, e);
114       p--;
115     }
116
117   mpf_set_str (t, "1.5", 10);
118   while (mpf_cmp (x, t) > 0)
119     {
120       mpf_div (x, x, e);
121       p++;
122     }
123
124   mpf_sub_ui (x, x, 1);
125   mpf_init_set_ui (log, 0);
126   mpf_init_set_ui (xp, 1);
127
128   for (i = 1; i < GFC_REAL_BITS; i++)
129     {
130       mpf_mul (xp, xp, x);
131       mpf_div_ui (t, xp, i);
132
133       if (i % 2 == 0)
134         mpf_sub (log, log, t);
135       else
136         mpf_add (log, log, t);
137     }
138
139   /* Add in the log (e^p) = p */
140
141   if (p < 0)
142     mpf_sub_ui (log, log, -p);
143   else
144     mpf_add_ui (log, log, p);
145
146   mpf_clear (x);
147   mpf_clear (xp);
148   mpf_clear (t);
149
150   mpf_set (*result, log);
151   mpf_clear (log);
152 }
153
154
155 /* Calculate the common logarithm of arg.  We use the natural
156    logarithm of arg and of 10:
157
158    log10(arg) = log(arg)/log(10)  */
159
160 void
161 common_logarithm (mpf_t * arg, mpf_t * result)
162 {
163   mpf_t i10, log10;
164
165   natural_logarithm (arg, result);
166
167   mpf_init_set_ui (i10, 10);
168   mpf_init (log10);
169   natural_logarithm (&i10, &log10);
170
171   mpf_div (*result, *result, log10);
172   mpf_clear (i10);
173   mpf_clear (log10);
174 }
175
176 /* Calculate exp(arg).
177
178    We use a reduction of the form
179
180      x = Nln2 + r
181
182    Then we obtain exp(r) from the Maclaurin series.
183    exp(x) is then recovered from the identity
184
185      exp(x) = 2^N*exp(r).  */
186
187 void
188 exponential (mpf_t * arg, mpf_t * result)
189 {
190   mpf_t two, ln2, power, q, r, num, denom, term, x, xp;
191   int i;
192   long n;
193   unsigned long p, mp;
194
195
196   mpf_init_set (x, *arg);
197
198   if (mpf_cmp_ui (x, 0) == 0)
199     {
200       mpf_set_ui (*result, 1);
201     }
202   else if (mpf_cmp_ui (x, 1) == 0)
203     {
204       mpf_set (*result, e);
205     }
206   else
207     {
208       mpf_init_set_ui (two, 2);
209       mpf_init (ln2);
210       mpf_init (q);
211       mpf_init (r);
212       mpf_init (power);
213       mpf_init (term);
214
215       natural_logarithm (&two, &ln2);
216
217       mpf_div (q, x, ln2);
218       mpf_floor (power, q);
219       mpf_mul (q, power, ln2);
220       mpf_sub (r, x, q);
221
222       mpf_init_set_ui (xp, 1);
223       mpf_init_set_ui (num, 1);
224       mpf_init_set_ui (denom, 1);
225
226       for (i = 1; i <= GFC_REAL_BITS + 10; i++)
227         {
228           mpf_mul (num, num, r);
229           mpf_mul_ui (denom, denom, i);
230           mpf_div (term, num, denom);
231           mpf_add (xp, xp, term);
232         }
233
234       /* Reconstruction step */
235       n = (long) mpf_get_d (power);
236
237       if (n > 0)
238         {
239           p = (unsigned int) n;
240           mpf_mul_2exp (*result, xp, p);
241         }
242       else
243         {
244           mp = (unsigned int) (-n);
245           mpf_div_2exp (*result, xp, mp);
246         }
247
248       mpf_clear (two);
249       mpf_clear (ln2);
250       mpf_clear (q);
251       mpf_clear (r);
252       mpf_clear (power);
253       mpf_clear (num);
254       mpf_clear (denom);
255       mpf_clear (term);
256       mpf_clear (xp);
257     }
258
259   mpf_clear (x);
260 }
261
262
263 /* Calculate sin(arg).
264
265    We use a reduction of the form
266
267      x= N*2pi + r
268
269    Then we obtain sin(r) from the Maclaurin series.  */
270
271 void
272 sine (mpf_t * arg, mpf_t * result)
273 {
274   mpf_t factor, q, r, num, denom, term, x, xp;
275   int i, sign;
276
277   mpf_init_set (x, *arg);
278
279   /* Special case (we do not treat multiples of pi due to roundoff issues) */
280   if (mpf_cmp_ui (x, 0) == 0)
281     {
282       mpf_set_ui (*result, 0);
283     }
284   else
285     {
286       mpf_init (q);
287       mpf_init (r);
288       mpf_init (factor);
289       mpf_init (term);
290
291       mpf_div (q, x, two_pi);
292       mpf_floor (factor, q);
293       mpf_mul (q, factor, two_pi);
294       mpf_sub (r, x, q);
295
296       mpf_init_set_ui (xp, 0);
297       mpf_init_set_ui (num, 1);
298       mpf_init_set_ui (denom, 1);
299
300       sign = -1;
301       for (i = 1; i < GFC_REAL_BITS + 10; i++)
302         {
303           mpf_mul (num, num, r);
304           mpf_mul_ui (denom, denom, i);
305           if (i % 2 == 0)
306             continue;
307
308           sign = -sign;
309           mpf_div (term, num, denom);
310           if (sign > 0)
311             mpf_add (xp, xp, term);
312           else
313             mpf_sub (xp, xp, term);
314         }
315
316       mpf_set (*result, xp);
317
318       mpf_clear (q);
319       mpf_clear (r);
320       mpf_clear (factor);
321       mpf_clear (num);
322       mpf_clear (denom);
323       mpf_clear (term);
324       mpf_clear (xp);
325     }
326
327   mpf_clear (x);
328 }
329
330
331 /* Calculate cos(arg).
332
333    Similar to sine.  */
334
335 void
336 cosine (mpf_t * arg, mpf_t * result)
337 {
338   mpf_t factor, q, r, num, denom, term, x, xp;
339   int i, sign;
340
341   mpf_init_set (x, *arg);
342
343   /* Special case (we do not treat multiples of pi due to roundoff issues) */
344   if (mpf_cmp_ui (x, 0) == 0)
345     {
346       mpf_set_ui (*result, 1);
347     }
348   else
349     {
350       mpf_init (q);
351       mpf_init (r);
352       mpf_init (factor);
353       mpf_init (term);
354
355       mpf_div (q, x, two_pi);
356       mpf_floor (factor, q);
357       mpf_mul (q, factor, two_pi);
358       mpf_sub (r, x, q);
359
360       mpf_init_set_ui (xp, 1);
361       mpf_init_set_ui (num, 1);
362       mpf_init_set_ui (denom, 1);
363
364       sign = 1;
365       for (i = 1; i < GFC_REAL_BITS + 10; i++)
366         {
367           mpf_mul (num, num, r);
368           mpf_mul_ui (denom, denom, i);
369           if (i % 2 != 0)
370             continue;
371
372           sign = -sign;
373           mpf_div (term, num, denom);
374           if (sign > 0)
375             mpf_add (xp, xp, term);
376           else
377             mpf_sub (xp, xp, term);
378         }
379       mpf_set (*result, xp);
380
381       mpf_clear (q);
382       mpf_clear (r);
383       mpf_clear (factor);
384       mpf_clear (num);
385       mpf_clear (denom);
386       mpf_clear (term);
387       mpf_clear (xp);
388     }
389
390   mpf_clear (x);
391 }
392
393
394 /* Calculate atan(arg).
395
396    Similar to sine but requires special handling for x near 1.  */
397
398 void
399 arctangent (mpf_t * arg, mpf_t * result)
400 {
401   mpf_t absval, convgu, convgl, num, term, x, xp;
402   int i, sign;
403
404   mpf_init_set (x, *arg);
405
406   /* Special cases */
407   if (mpf_cmp_ui (x, 0) == 0)
408     {
409       mpf_set_ui (*result, 0);
410     }
411   else if (mpf_cmp_ui (x, 1) == 0)
412     {
413       mpf_init (num);
414       mpf_div_ui (num, half_pi, 2);
415       mpf_set (*result, num);
416       mpf_clear (num);
417     }
418   else if (mpf_cmp_si (x, -1) == 0)
419     {
420       mpf_init (num);
421       mpf_div_ui (num, half_pi, 2);
422       mpf_neg (*result, num);
423       mpf_clear (num);
424     }
425   else
426     {                           /* General cases */
427
428       mpf_init (absval);
429       mpf_abs (absval, x);
430
431       mpf_init_set_d (convgu, 1.5);
432       mpf_init_set_d (convgl, 0.5);
433       mpf_init_set_ui (num, 1);
434       mpf_init (term);
435
436       if (mpf_cmp (absval, convgl) < 0)
437         {
438           mpf_init_set_ui (xp, 0);
439           sign = -1;
440           for (i = 1; i < GFC_REAL_BITS + 10; i++)
441             {
442               mpf_mul (num, num, absval);
443               if (i % 2 == 0)
444                 continue;
445
446               sign = -sign;
447               mpf_div_ui (term, num, i);
448               if (sign > 0)
449                 mpf_add (xp, xp, term);
450               else
451                 mpf_sub (xp, xp, term);
452             }
453         }
454       else if (mpf_cmp (absval, convgu) >= 0)
455         {
456           mpf_init_set (xp, half_pi);
457           sign = 1;
458           for (i = 1; i < GFC_REAL_BITS + 10; i++)
459             {
460               mpf_div (num, num, absval);
461               if (i % 2 == 0)
462                 continue;
463
464               sign = -sign;
465               mpf_div_ui (term, num, i);
466               if (sign > 0)
467                 mpf_add (xp, xp, term);
468               else
469                 mpf_sub (xp, xp, term);
470             }
471         }
472       else
473         {
474           mpf_init_set_ui (xp, 0);
475
476           mpf_sub_ui (num, absval, 1);
477           mpf_add_ui (term, absval, 1);
478           mpf_div (absval, num, term);
479
480           mpf_set_ui (num, 1);
481
482           sign = -1;
483           for (i = 1; i < GFC_REAL_BITS + 10; i++)
484             {
485               mpf_mul (num, num, absval);
486               if (i % 2 == 0)
487                 continue;
488               sign = -sign;
489               mpf_div_ui (term, num, i);
490               if (sign > 0)
491                 mpf_add (xp, xp, term);
492               else
493                 mpf_sub (xp, xp, term);
494             }
495
496           mpf_div_ui (term, half_pi, 2);
497           mpf_add (xp, term, xp);
498         }
499
500       /* This makes sure to preserve the identity arctan(-x) = -arctan(x)
501          and improves accuracy to boot.  */
502
503       if (mpf_cmp_ui (x, 0) > 0)
504         mpf_set (*result, xp);
505       else
506         mpf_neg (*result, xp);
507
508       mpf_clear (absval);
509       mpf_clear (convgl);
510       mpf_clear (convgu);
511       mpf_clear (num);
512       mpf_clear (term);
513       mpf_clear (xp);
514     }
515   mpf_clear (x);
516 }
517
518
519 /* Calculate atan2 (y, x)
520
521 atan2(y, x) = atan(y/x)                         if x > 0,
522               sign(y)*(pi - atan(|y/x|))        if x < 0,
523               0                                 if x = 0 && y == 0,
524               sign(y)*pi/2                      if x = 0 && y != 0.
525 */
526
527 void
528 arctangent2 (mpf_t * y, mpf_t * x, mpf_t * result)
529 {
530   mpf_t t;
531
532   mpf_init (t);
533
534   switch (mpf_sgn (*x))
535     {
536     case 1:
537       mpf_div (t, *y, *x);
538       arctangent (&t, result);
539       break;
540     case -1:
541       mpf_div (t, *y, *x);
542       mpf_abs (t, t);
543       arctangent (&t, &t);
544       mpf_sub (*result, pi, t);
545       if (mpf_sgn (*y) == -1)
546         mpf_neg (*result, *result);
547       break;
548     case 0:
549       if (mpf_sgn (*y) == 0)
550         mpf_set_ui (*result, 0);
551       else
552         {
553           mpf_set (*result, half_pi);
554           if (mpf_sgn (*y) == -1)
555             mpf_neg (*result, *result);
556         }
557        break;
558     }
559   mpf_clear (t);
560 }
561
562 /* Calculate cosh(arg). */
563
564 void
565 hypercos (mpf_t * arg, mpf_t * result)
566 {
567   mpf_t neg, term1, term2, x, xp;
568
569   mpf_init_set (x, *arg);
570
571   mpf_init (neg);
572   mpf_init (term1);
573   mpf_init (term2);
574   mpf_init (xp);
575
576   mpf_neg (neg, x);
577
578   exponential (&x, &term1);
579   exponential (&neg, &term2);
580
581   mpf_add (xp, term1, term2);
582   mpf_div_ui (*result, xp, 2);
583
584   mpf_clear (neg);
585   mpf_clear (term1);
586   mpf_clear (term2);
587   mpf_clear (x);
588   mpf_clear (xp);
589 }
590
591
592 /* Calculate sinh(arg). */
593
594 void
595 hypersine (mpf_t * arg, mpf_t * result)
596 {
597   mpf_t neg, term1, term2, x, xp;
598
599   mpf_init_set (x, *arg);
600
601   mpf_init (neg);
602   mpf_init (term1);
603   mpf_init (term2);
604   mpf_init (xp);
605
606   mpf_neg (neg, x);
607
608   exponential (&x, &term1);
609   exponential (&neg, &term2);
610
611   mpf_sub (xp, term1, term2);
612   mpf_div_ui (*result, xp, 2);
613
614   mpf_clear (neg);
615   mpf_clear (term1);
616   mpf_clear (term2);
617   mpf_clear (x);
618   mpf_clear (xp);
619 }
620
621
622 /* Given an arithmetic error code, return a pointer to a string that
623    explains the error.  */
624
625 static const char *
626 gfc_arith_error (arith code)
627 {
628   const char *p;
629
630   switch (code)
631     {
632     case ARITH_OK:
633       p = "Arithmetic OK";
634       break;
635     case ARITH_OVERFLOW:
636       p = "Arithmetic overflow";
637       break;
638     case ARITH_UNDERFLOW:
639       p = "Arithmetic underflow";
640       break;
641     case ARITH_DIV0:
642       p = "Division by zero";
643       break;
644     case ARITH_0TO0:
645       p = "Indeterminate form 0 ** 0";
646       break;
647     case ARITH_INCOMMENSURATE:
648       p = "Array operands are incommensurate";
649       break;
650     default:
651       gfc_internal_error ("gfc_arith_error(): Bad error code");
652     }
653
654   return p;
655 }
656
657
658 /* Get things ready to do math.  */
659
660 void
661 gfc_arith_init_1 (void)
662 {
663   gfc_integer_info *int_info;
664   gfc_real_info *real_info;
665   mpf_t a, b;
666   mpz_t r;
667   int i, n, limit;
668
669   /* Set the default precision for GMP computations.  */
670   mpf_set_default_prec (GFC_REAL_BITS + 30);
671
672   /* Calculate e, needed by the natural_logarithm() subroutine.  */
673   mpf_init (b);
674   mpf_init_set_ui (e, 0);
675   mpf_init_set_ui (a, 1);
676
677   for (i = 1; i < 100; i++)
678     {
679       mpf_add (e, e, a);
680       mpf_div_ui (a, a, i);     /* 1/(i!) */
681     }
682
683   /* Calculate pi, 2pi, pi/2, and -pi/2, needed for trigonometric
684      functions.
685
686      We use the Bailey, Borwein and Plouffe formula:
687
688        pi = \sum{n=0}^\infty (1/16)^n [4/(8n+1) - 2/(8n+4) - 1/(8n+5) - 1/(8n+6)]
689
690      which gives about four bits per iteration.  */
691
692   mpf_init_set_ui (pi, 0);
693
694   mpf_init (two_pi);
695   mpf_init (half_pi);
696
697   limit = (GFC_REAL_BITS / 4) + 10;     /* (1/16)^n gives 4 bits per iteration */
698
699   for (n = 0; n < limit; n++)
700     {
701       mpf_set_ui (b, 4);
702       mpf_div_ui (b, b, 8 * n + 1);     /* 4/(8n+1) */
703
704       mpf_set_ui (a, 2);
705       mpf_div_ui (a, a, 8 * n + 4);     /* 2/(8n+4) */
706       mpf_sub (b, b, a);
707
708       mpf_set_ui (a, 1);
709       mpf_div_ui (a, a, 8 * n + 5);     /* 1/(8n+5) */
710       mpf_sub (b, b, a);
711
712       mpf_set_ui (a, 1);
713       mpf_div_ui (a, a, 8 * n + 6);     /* 1/(8n+6) */
714       mpf_sub (b, b, a);
715
716       mpf_set_ui (a, 16);
717       mpf_pow_ui (a, a, n);     /* 16^n */
718
719       mpf_div (b, b, a);
720
721       mpf_add (pi, pi, b);
722     }
723
724   mpf_mul_ui (two_pi, pi, 2);
725   mpf_div_ui (half_pi, pi, 2);
726
727   /* Convert the minimum/maximum values for each kind into their
728      GNU MP representation.  */
729   mpz_init (r);
730
731   for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
732     {
733       /* Huge */
734       mpz_set_ui (r, int_info->radix);
735       mpz_pow_ui (r, r, int_info->digits);
736
737       mpz_init (int_info->huge);
738       mpz_sub_ui (int_info->huge, r, 1);
739
740       /* These are the numbers that are actually representable by the
741          target.  For bases other than two, this needs to be changed.  */
742       if (int_info->radix != 2)
743         gfc_internal_error ("Fix min_int, max_int calculation");
744
745       mpz_init (int_info->min_int);
746       mpz_neg (int_info->min_int, int_info->huge);
747       /* No -1 here, because the representation is symmetric.  */
748
749       mpz_init (int_info->max_int);
750       mpz_add (int_info->max_int, int_info->huge, int_info->huge);
751       mpz_add_ui (int_info->max_int, int_info->max_int, 1);
752
753       /* Range */
754       mpf_set_z (a, int_info->huge);
755       common_logarithm (&a, &a);
756       mpf_trunc (a, a);
757       mpz_set_f (r, a);
758       int_info->range = mpz_get_si (r);
759     }
760
761   /*  mpf_set_default_prec(GFC_REAL_BITS); */
762   for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
763     {
764       /* Huge */
765       mpf_set_ui (a, real_info->radix);
766       mpf_set_ui (b, real_info->radix);
767
768       mpf_pow_ui (a, a, real_info->max_exponent);
769       mpf_pow_ui (b, b, real_info->max_exponent - real_info->digits);
770
771       mpf_init (real_info->huge);
772       mpf_sub (real_info->huge, a, b);
773
774       /* Tiny */
775       mpf_set_ui (b, real_info->radix);
776       mpf_pow_ui (b, b, 1 - real_info->min_exponent);
777
778       mpf_init (real_info->tiny);
779       mpf_ui_div (real_info->tiny, 1, b);
780
781       /* Epsilon */
782       mpf_set_ui (b, real_info->radix);
783       mpf_pow_ui (b, b, real_info->digits - 1);
784
785       mpf_init (real_info->epsilon);
786       mpf_ui_div (real_info->epsilon, 1, b);
787
788       /* Range */
789       common_logarithm (&real_info->huge, &a);
790       common_logarithm (&real_info->tiny, &b);
791       mpf_neg (b, b);
792
793       if (mpf_cmp (a, b) > 0)
794         mpf_set (a, b);         /* a = min(a, b) */
795
796       mpf_trunc (a, a);
797       mpz_set_f (r, a);
798       real_info->range = mpz_get_si (r);
799
800       /* Precision */
801       mpf_set_ui (a, real_info->radix);
802       common_logarithm (&a, &a);
803
804       mpf_mul_ui (a, a, real_info->digits - 1);
805       mpf_trunc (a, a);
806       mpz_set_f (r, a);
807       real_info->precision = mpz_get_si (r);
808
809       /* If the radix is an integral power of 10, add one to the
810          precision.  */
811       for (i = 10; i <= real_info->radix; i *= 10)
812         if (i == real_info->radix)
813           real_info->precision++;
814     }
815
816   mpz_clear (r);
817   mpf_clear (a);
818   mpf_clear (b);
819 }
820
821
822 /* Clean up, get rid of numeric constants.  */
823
824 void
825 gfc_arith_done_1 (void)
826 {
827   gfc_integer_info *ip;
828   gfc_real_info *rp;
829
830   mpf_clear (e);
831
832   mpf_clear (pi);
833   mpf_clear (half_pi);
834   mpf_clear (two_pi);
835
836   for (ip = gfc_integer_kinds; ip->kind; ip++)
837     {
838       mpz_clear (ip->min_int);
839       mpz_clear (ip->max_int);
840       mpz_clear (ip->huge);
841     }
842
843   for (rp = gfc_real_kinds; rp->kind; rp++)
844     {
845       mpf_clear (rp->epsilon);
846       mpf_clear (rp->huge);
847       mpf_clear (rp->tiny);
848     }
849 }
850
851
852 /* Return default kinds.  */
853
854 int
855 gfc_default_integer_kind (void)
856 {
857   return gfc_integer_kinds[gfc_option.i8 ? 1 : 0].kind;
858 }
859
860 int
861 gfc_default_real_kind (void)
862 {
863   return gfc_real_kinds[gfc_option.r8 ? 1 : 0].kind;
864 }
865
866 int
867 gfc_default_double_kind (void)
868 {
869   return gfc_real_kinds[1].kind;
870 }
871
872 int
873 gfc_default_character_kind (void)
874 {
875   return 1;
876 }
877
878 int
879 gfc_default_logical_kind (void)
880 {
881   return gfc_logical_kinds[gfc_option.i8 ? 1 : 0].kind;
882 }
883
884 int
885 gfc_default_complex_kind (void)
886 {
887   return gfc_default_real_kind ();
888 }
889
890
891 /* Make sure that a valid kind is present.  Returns an index into the
892    gfc_integer_kinds array, -1 if the kind is not present.  */
893
894 static int
895 validate_integer (int kind)
896 {
897   int i;
898
899   for (i = 0;; i++)
900     {
901       if (gfc_integer_kinds[i].kind == 0)
902         {
903           i = -1;
904           break;
905         }
906       if (gfc_integer_kinds[i].kind == kind)
907         break;
908     }
909
910   return i;
911 }
912
913
914 static int
915 validate_real (int kind)
916 {
917   int i;
918
919   for (i = 0;; i++)
920     {
921       if (gfc_real_kinds[i].kind == 0)
922         {
923           i = -1;
924           break;
925         }
926       if (gfc_real_kinds[i].kind == kind)
927         break;
928     }
929
930   return i;
931 }
932
933
934 static int
935 validate_logical (int kind)
936 {
937   int i;
938
939   for (i = 0;; i++)
940     {
941       if (gfc_logical_kinds[i].kind == 0)
942         {
943           i = -1;
944           break;
945         }
946       if (gfc_logical_kinds[i].kind == kind)
947         break;
948     }
949
950   return i;
951 }
952
953
954 static int
955 validate_character (int kind)
956 {
957
958   if (kind == gfc_default_character_kind ())
959     return 0;
960   return -1;
961 }
962
963
964 /* Validate a kind given a basic type.  The return value is the same
965    for the child functions, with -1 indicating nonexistence of the
966    type.  */
967
968 int
969 gfc_validate_kind (bt type, int kind)
970 {
971   int rc;
972
973   switch (type)
974     {
975     case BT_REAL:               /* Fall through */
976     case BT_COMPLEX:
977       rc = validate_real (kind);
978       break;
979     case BT_INTEGER:
980       rc = validate_integer (kind);
981       break;
982     case BT_LOGICAL:
983       rc = validate_logical (kind);
984       break;
985     case BT_CHARACTER:
986       rc = validate_character (kind);
987       break;
988
989     default:
990       gfc_internal_error ("gfc_validate_kind(): Got bad type");
991     }
992
993   return rc;
994 }
995
996
997 /* Given an integer and a kind, make sure that the integer lies within
998    the range of the kind.  Returns ARITH_OK or ARITH_OVERFLOW.  */
999
1000 static arith
1001 gfc_check_integer_range (mpz_t p, int kind)
1002 {
1003   arith result;
1004   int i;
1005
1006   i = validate_integer (kind);
1007   if (i == -1)
1008     gfc_internal_error ("gfc_check_integer_range(): Bad kind");
1009
1010   result = ARITH_OK;
1011
1012   if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
1013       || mpz_cmp (p, gfc_integer_kinds[i].max_int) > 0)
1014     result = ARITH_OVERFLOW;
1015
1016   return result;
1017 }
1018
1019
1020 /* Given a real and a kind, make sure that the real lies within the
1021    range of the kind.  Returns ARITH_OK, ARITH_OVERFLOW or
1022    ARITH_UNDERFLOW.  */
1023
1024 static arith
1025 gfc_check_real_range (mpf_t p, int kind)
1026 {
1027   arith retval;
1028   mpf_t q;
1029   int i;
1030
1031   mpf_init (q);
1032   mpf_abs (q, p);
1033
1034   i = validate_real (kind);
1035   if (i == -1)
1036     gfc_internal_error ("gfc_check_real_range(): Bad kind");
1037
1038   retval = ARITH_OK;
1039   if (mpf_sgn (q) == 0)
1040     goto done;
1041
1042   if (mpf_cmp (q, gfc_real_kinds[i].huge) == 1)
1043     {
1044       retval = ARITH_OVERFLOW;
1045       goto done;
1046     }
1047
1048   if (mpf_cmp (q, gfc_real_kinds[i].tiny) == -1)
1049     retval = ARITH_UNDERFLOW;
1050
1051 done:
1052   mpf_clear (q);
1053
1054   return retval;
1055 }
1056
1057
1058 /* Function to return a constant expression node of a given type and
1059    kind.  */
1060
1061 gfc_expr *
1062 gfc_constant_result (bt type, int kind, locus * where)
1063 {
1064   gfc_expr *result;
1065
1066   if (!where)
1067     gfc_internal_error
1068       ("gfc_constant_result(): locus 'where' cannot be NULL");
1069
1070   result = gfc_get_expr ();
1071
1072   result->expr_type = EXPR_CONSTANT;
1073   result->ts.type = type;
1074   result->ts.kind = kind;
1075   result->where = *where;
1076
1077   switch (type)
1078     {
1079     case BT_INTEGER:
1080       mpz_init (result->value.integer);
1081       break;
1082
1083     case BT_REAL:
1084       mpf_init (result->value.real);
1085       break;
1086
1087     case BT_COMPLEX:
1088       mpf_init (result->value.complex.r);
1089       mpf_init (result->value.complex.i);
1090       break;
1091
1092     default:
1093       break;
1094     }
1095
1096   return result;
1097 }
1098
1099
1100 /* Low-level arithmetic functions.  All of these subroutines assume
1101    that all operands are of the same type and return an operand of the
1102    same type.  The other thing about these subroutines is that they
1103    can fail in various ways -- overflow, underflow, division by zero,
1104    zero raised to the zero, etc.  */
1105
1106 static arith
1107 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
1108 {
1109   gfc_expr *result;
1110
1111   result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
1112   result->value.logical = !op1->value.logical;
1113   *resultp = result;
1114
1115   return ARITH_OK;
1116 }
1117
1118
1119 static arith
1120 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1121 {
1122   gfc_expr *result;
1123
1124   result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1125                                 &op1->where);
1126   result->value.logical = op1->value.logical && op2->value.logical;
1127   *resultp = result;
1128
1129   return ARITH_OK;
1130 }
1131
1132
1133 static arith
1134 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1135 {
1136   gfc_expr *result;
1137
1138   result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1139                                 &op1->where);
1140   result->value.logical = op1->value.logical || op2->value.logical;
1141   *resultp = result;
1142
1143   return ARITH_OK;
1144 }
1145
1146
1147 static arith
1148 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1149 {
1150   gfc_expr *result;
1151
1152   result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1153                                 &op1->where);
1154   result->value.logical = op1->value.logical == op2->value.logical;
1155   *resultp = result;
1156
1157   return ARITH_OK;
1158 }
1159
1160
1161 static arith
1162 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1163 {
1164   gfc_expr *result;
1165
1166   result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
1167                                 &op1->where);
1168   result->value.logical = op1->value.logical != op2->value.logical;
1169   *resultp = result;
1170
1171   return ARITH_OK;
1172 }
1173
1174
1175 /* Make sure a constant numeric expression is within the range for
1176    its type and kind.  GMP is doing 130 bit arithmetic, so an UNDERFLOW
1177    is numerically zero for REAL(4) and REAL(8) types.  Reset the value(s)
1178    to exactly 0 for UNDERFLOW.  Note that there's also a gfc_check_range(),
1179    but that one deals with the intrinsic RANGE function.  */
1180
1181 arith
1182 gfc_range_check (gfc_expr * e)
1183 {
1184   arith rc;
1185
1186   switch (e->ts.type)
1187     {
1188     case BT_INTEGER:
1189       rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
1190       break;
1191
1192     case BT_REAL:
1193       rc = gfc_check_real_range (e->value.real, e->ts.kind);
1194       if (rc == ARITH_UNDERFLOW)
1195         mpf_set_ui (e->value.real, 0);
1196       break;
1197
1198     case BT_COMPLEX:
1199       rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
1200       if (rc == ARITH_UNDERFLOW)
1201         mpf_set_ui (e->value.complex.r, 0);
1202       if (rc == ARITH_OK || rc == ARITH_UNDERFLOW)
1203         {
1204           rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
1205           if (rc == ARITH_UNDERFLOW)
1206             mpf_set_ui (e->value.complex.i, 0);
1207         }
1208
1209       break;
1210
1211     default:
1212       gfc_internal_error ("gfc_range_check(): Bad type");
1213     }
1214
1215   return rc;
1216 }
1217
1218
1219 /* It may seem silly to have a subroutine that actually computes the
1220    unary plus of a constant, but it prevents us from making exceptions
1221    in the code elsewhere.  */
1222
1223 static arith
1224 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
1225 {
1226
1227   *resultp = gfc_copy_expr (op1);
1228   return ARITH_OK;
1229 }
1230
1231
1232 static arith
1233 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
1234 {
1235   gfc_expr *result;
1236   arith rc;
1237
1238   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1239
1240   switch (op1->ts.type)
1241     {
1242     case BT_INTEGER:
1243       mpz_neg (result->value.integer, op1->value.integer);
1244       break;
1245
1246     case BT_REAL:
1247       mpf_neg (result->value.real, op1->value.real);
1248       break;
1249
1250     case BT_COMPLEX:
1251       mpf_neg (result->value.complex.r, op1->value.complex.r);
1252       mpf_neg (result->value.complex.i, op1->value.complex.i);
1253       break;
1254
1255     default:
1256       gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
1257     }
1258
1259   rc = gfc_range_check (result);
1260
1261   if (rc == ARITH_UNDERFLOW)
1262     {
1263       if (gfc_option.warn_underflow)
1264         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1265       rc = ARITH_OK;
1266       *resultp = result;
1267     }
1268   else if (rc != ARITH_OK)
1269     gfc_free_expr (result);
1270   else
1271     *resultp = result;
1272
1273   return rc;
1274 }
1275
1276
1277 static arith
1278 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1279 {
1280   gfc_expr *result;
1281   arith rc;
1282
1283   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1284
1285   switch (op1->ts.type)
1286     {
1287     case BT_INTEGER:
1288       mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
1289       break;
1290
1291     case BT_REAL:
1292       mpf_add (result->value.real, op1->value.real, op2->value.real);
1293       break;
1294
1295     case BT_COMPLEX:
1296       mpf_add (result->value.complex.r, op1->value.complex.r,
1297                op2->value.complex.r);
1298
1299       mpf_add (result->value.complex.i, op1->value.complex.i,
1300                op2->value.complex.i);
1301       break;
1302
1303     default:
1304       gfc_internal_error ("gfc_arith_plus(): Bad basic type");
1305     }
1306
1307   rc = gfc_range_check (result);
1308
1309   if (rc == ARITH_UNDERFLOW)
1310     {
1311       if (gfc_option.warn_underflow)
1312         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1313       rc = ARITH_OK;
1314       *resultp = result;
1315     }
1316   else if (rc != ARITH_OK)
1317     gfc_free_expr (result);
1318   else
1319     *resultp = result;
1320
1321   return rc;
1322 }
1323
1324
1325 static arith
1326 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1327 {
1328   gfc_expr *result;
1329   arith rc;
1330
1331   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1332
1333   switch (op1->ts.type)
1334     {
1335     case BT_INTEGER:
1336       mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
1337       break;
1338
1339     case BT_REAL:
1340       mpf_sub (result->value.real, op1->value.real, op2->value.real);
1341       break;
1342
1343     case BT_COMPLEX:
1344       mpf_sub (result->value.complex.r, op1->value.complex.r,
1345                op2->value.complex.r);
1346
1347       mpf_sub (result->value.complex.i, op1->value.complex.i,
1348                op2->value.complex.i);
1349
1350       break;
1351
1352     default:
1353       gfc_internal_error ("gfc_arith_minus(): Bad basic type");
1354     }
1355
1356   rc = gfc_range_check (result);
1357
1358   if (rc == ARITH_UNDERFLOW)
1359     {
1360       if (gfc_option.warn_underflow)
1361         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1362       rc = ARITH_OK;
1363       *resultp = result;
1364     }
1365   else if (rc != ARITH_OK)
1366     gfc_free_expr (result);
1367   else
1368     *resultp = result;
1369
1370   return rc;
1371 }
1372
1373
1374 static arith
1375 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1376 {
1377   gfc_expr *result;
1378   mpf_t x, y;
1379   arith rc;
1380
1381   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1382
1383   switch (op1->ts.type)
1384     {
1385     case BT_INTEGER:
1386       mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
1387       break;
1388
1389     case BT_REAL:
1390       mpf_mul (result->value.real, op1->value.real, op2->value.real);
1391       break;
1392
1393     case BT_COMPLEX:
1394       mpf_init (x);
1395       mpf_init (y);
1396
1397       mpf_mul (x, op1->value.complex.r, op2->value.complex.r);
1398       mpf_mul (y, op1->value.complex.i, op2->value.complex.i);
1399       mpf_sub (result->value.complex.r, x, y);
1400
1401       mpf_mul (x, op1->value.complex.r, op2->value.complex.i);
1402       mpf_mul (y, op1->value.complex.i, op2->value.complex.r);
1403       mpf_add (result->value.complex.i, x, y);
1404
1405       mpf_clear (x);
1406       mpf_clear (y);
1407
1408       break;
1409
1410     default:
1411       gfc_internal_error ("gfc_arith_times(): Bad basic type");
1412     }
1413
1414   rc = gfc_range_check (result);
1415
1416   if (rc == ARITH_UNDERFLOW)
1417     {
1418       if (gfc_option.warn_underflow)
1419         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1420       rc = ARITH_OK;
1421       *resultp = result;
1422     }
1423   else if (rc != ARITH_OK)
1424     gfc_free_expr (result);
1425   else
1426     *resultp = result;
1427
1428   return rc;
1429 }
1430
1431
1432 static arith
1433 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1434 {
1435   gfc_expr *result;
1436   mpf_t x, y, div;
1437   arith rc;
1438
1439   rc = ARITH_OK;
1440
1441   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1442
1443   switch (op1->ts.type)
1444     {
1445     case BT_INTEGER:
1446       if (mpz_sgn (op2->value.integer) == 0)
1447         {
1448           rc = ARITH_DIV0;
1449           break;
1450         }
1451
1452       mpz_tdiv_q (result->value.integer, op1->value.integer,
1453                   op2->value.integer);
1454       break;
1455
1456     case BT_REAL:
1457       if (mpf_sgn (op2->value.real) == 0)
1458         {
1459           rc = ARITH_DIV0;
1460           break;
1461         }
1462
1463       mpf_div (result->value.real, op1->value.real, op2->value.real);
1464       break;
1465
1466     case BT_COMPLEX:
1467       if (mpf_sgn (op2->value.complex.r) == 0
1468           && mpf_sgn (op2->value.complex.i) == 0)
1469         {
1470           rc = ARITH_DIV0;
1471           break;
1472         }
1473
1474       mpf_init (x);
1475       mpf_init (y);
1476       mpf_init (div);
1477
1478       mpf_mul (x, op2->value.complex.r, op2->value.complex.r);
1479       mpf_mul (y, op2->value.complex.i, op2->value.complex.i);
1480       mpf_add (div, x, y);
1481
1482       mpf_mul (x, op1->value.complex.r, op2->value.complex.r);
1483       mpf_mul (y, op1->value.complex.i, op2->value.complex.i);
1484       mpf_add (result->value.complex.r, x, y);
1485       mpf_div (result->value.complex.r, result->value.complex.r, div);
1486
1487       mpf_mul (x, op1->value.complex.i, op2->value.complex.r);
1488       mpf_mul (y, op1->value.complex.r, op2->value.complex.i);
1489       mpf_sub (result->value.complex.i, x, y);
1490       mpf_div (result->value.complex.i, result->value.complex.i, div);
1491
1492       mpf_clear (x);
1493       mpf_clear (y);
1494       mpf_clear (div);
1495
1496       break;
1497
1498     default:
1499       gfc_internal_error ("gfc_arith_divide(): Bad basic type");
1500     }
1501
1502   if (rc == ARITH_OK)
1503     rc = gfc_range_check (result);
1504
1505   if (rc == ARITH_UNDERFLOW)
1506     {
1507       if (gfc_option.warn_underflow)
1508         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1509       rc = ARITH_OK;
1510       *resultp = result;
1511     }
1512   else if (rc != ARITH_OK)
1513     gfc_free_expr (result);
1514   else
1515     *resultp = result;
1516
1517   return rc;
1518 }
1519
1520
1521 /* Compute the reciprocal of a complex number (guaranteed nonzero).  */
1522
1523 static void
1524 complex_reciprocal (gfc_expr * op)
1525 {
1526   mpf_t mod, a, result_r, result_i;
1527
1528   mpf_init (mod);
1529   mpf_init (a);
1530
1531   mpf_mul (mod, op->value.complex.r, op->value.complex.r);
1532   mpf_mul (a, op->value.complex.i, op->value.complex.i);
1533   mpf_add (mod, mod, a);
1534
1535   mpf_init (result_r);
1536   mpf_div (result_r, op->value.complex.r, mod);
1537
1538   mpf_init (result_i);
1539   mpf_neg (result_i, op->value.complex.i);
1540   mpf_div (result_i, result_i, mod);
1541
1542   mpf_set (op->value.complex.r, result_r);
1543   mpf_set (op->value.complex.i, result_i);
1544
1545   mpf_clear (result_r);
1546   mpf_clear (result_i);
1547
1548   mpf_clear (mod);
1549   mpf_clear (a);
1550 }
1551
1552
1553 /* Raise a complex number to positive power.  */
1554
1555 static void
1556 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
1557 {
1558   mpf_t temp_r, temp_i, a;
1559
1560   mpf_set_ui (result->value.complex.r, 1);
1561   mpf_set_ui (result->value.complex.i, 0);
1562
1563   mpf_init (temp_r);
1564   mpf_init (temp_i);
1565   mpf_init (a);
1566
1567   for (; power > 0; power--)
1568     {
1569       mpf_mul (temp_r, base->value.complex.r, result->value.complex.r);
1570       mpf_mul (a, base->value.complex.i, result->value.complex.i);
1571       mpf_sub (temp_r, temp_r, a);
1572
1573       mpf_mul (temp_i, base->value.complex.r, result->value.complex.i);
1574       mpf_mul (a, base->value.complex.i, result->value.complex.r);
1575       mpf_add (temp_i, temp_i, a);
1576
1577       mpf_set (result->value.complex.r, temp_r);
1578       mpf_set (result->value.complex.i, temp_i);
1579     }
1580
1581   mpf_clear (temp_r);
1582   mpf_clear (temp_i);
1583   mpf_clear (a);
1584 }
1585
1586
1587 /* Raise a number to an integer power.  */
1588
1589 static arith
1590 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1591 {
1592   int power, apower;
1593   gfc_expr *result;
1594   mpz_t unity_z;
1595   mpf_t unity_f;
1596   arith rc;
1597
1598   rc = ARITH_OK;
1599
1600   if (gfc_extract_int (op2, &power) != NULL)
1601     gfc_internal_error ("gfc_arith_power(): Bad exponent");
1602
1603   result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1604
1605   if (power == 0)
1606     {                           /* Handle something to the zeroth power */
1607       switch (op1->ts.type)
1608         {
1609         case BT_INTEGER:
1610           if (mpz_sgn (op1->value.integer) == 0)
1611             rc = ARITH_0TO0;
1612           else
1613             mpz_set_ui (result->value.integer, 1);
1614
1615           break;
1616
1617         case BT_REAL:
1618           if (mpf_sgn (op1->value.real) == 0)
1619             rc = ARITH_0TO0;
1620           else
1621             mpf_set_ui (result->value.real, 1);
1622
1623           break;
1624
1625         case BT_COMPLEX:
1626           if (mpf_sgn (op1->value.complex.r) == 0
1627               && mpf_sgn (op1->value.complex.i) == 0)
1628             rc = ARITH_0TO0;
1629           else
1630             {
1631               mpf_set_ui (result->value.complex.r, 1);
1632               mpf_set_ui (result->value.complex.i, 0);
1633             }
1634
1635           break;
1636
1637         default:
1638           gfc_internal_error ("gfc_arith_power(): Bad base");
1639         }
1640     }
1641
1642   if (power != 0)
1643     {
1644       apower = power;
1645       if (power < 0)
1646         apower = -power;
1647
1648       switch (op1->ts.type)
1649         {
1650         case BT_INTEGER:
1651           mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1652
1653           if (power < 0)
1654             {
1655               mpz_init_set_ui (unity_z, 1);
1656               mpz_tdiv_q (result->value.integer, unity_z,
1657                           result->value.integer);
1658               mpz_clear (unity_z);
1659             }
1660
1661           break;
1662
1663         case BT_REAL:
1664           mpf_pow_ui (result->value.real, op1->value.real, apower);
1665
1666           if (power < 0)
1667             {
1668               mpf_init_set_ui (unity_f, 1);
1669               mpf_div (result->value.real, unity_f, result->value.real);
1670               mpf_clear (unity_f);
1671             }
1672
1673           break;
1674
1675         case BT_COMPLEX:
1676           complex_pow_ui (op1, apower, result);
1677           if (power < 0)
1678             complex_reciprocal (result);
1679
1680           break;
1681
1682         default:
1683           break;
1684         }
1685     }
1686
1687   if (rc == ARITH_OK)
1688     rc = gfc_range_check (result);
1689
1690   if (rc == ARITH_UNDERFLOW)
1691     {
1692       if (gfc_option.warn_underflow)
1693         gfc_warning ("%s at %L", gfc_arith_error (rc), &op1->where);
1694       rc = ARITH_OK;
1695       *resultp = result;
1696     }
1697   else if (rc != ARITH_OK)
1698     gfc_free_expr (result);
1699   else
1700     *resultp = result;
1701
1702   return rc;
1703 }
1704
1705
1706 /* Concatenate two string constants.  */
1707
1708 static arith
1709 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1710 {
1711   gfc_expr *result;
1712   int len;
1713
1714   result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind (),
1715                                 &op1->where);
1716
1717   len = op1->value.character.length + op2->value.character.length;
1718
1719   result->value.character.string = gfc_getmem (len + 1);
1720   result->value.character.length = len;
1721
1722   memcpy (result->value.character.string, op1->value.character.string,
1723           op1->value.character.length);
1724
1725   memcpy (result->value.character.string + op1->value.character.length,
1726           op2->value.character.string, op2->value.character.length);
1727
1728   result->value.character.string[len] = '\0';
1729
1730   *resultp = result;
1731
1732   return ARITH_OK;
1733 }
1734
1735
1736 /* Comparison operators.  Assumes that the two expression nodes
1737    contain two constants of the same type.  */
1738
1739 int
1740 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1741 {
1742   int rc;
1743
1744   switch (op1->ts.type)
1745     {
1746     case BT_INTEGER:
1747       rc = mpz_cmp (op1->value.integer, op2->value.integer);
1748       break;
1749
1750     case BT_REAL:
1751       rc = mpf_cmp (op1->value.real, op2->value.real);
1752       break;
1753
1754     case BT_CHARACTER:
1755       rc = gfc_compare_string (op1, op2, NULL);
1756       break;
1757
1758     case BT_LOGICAL:
1759       rc = ((!op1->value.logical && op2->value.logical)
1760             || (op1->value.logical && !op2->value.logical));
1761       break;
1762
1763     default:
1764       gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1765     }
1766
1767   return rc;
1768 }
1769
1770
1771 /* Compare a pair of complex numbers.  Naturally, this is only for
1772    equality/nonequality.  */
1773
1774 static int
1775 compare_complex (gfc_expr * op1, gfc_expr * op2)
1776 {
1777
1778   return (mpf_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1779           && mpf_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1780 }
1781
1782
1783 /* Given two constant strings and the inverse collating sequence,
1784    compare the strings.  We return -1 for a<b, 0 for a==b and 1 for
1785    a>b.  If the xcoll_table is NULL, we use the processor's default
1786    collating sequence.  */
1787
1788 int
1789 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
1790 {
1791   int len, alen, blen, i, ac, bc;
1792
1793   alen = a->value.character.length;
1794   blen = b->value.character.length;
1795
1796   len = (alen > blen) ? alen : blen;
1797
1798   for (i = 0; i < len; i++)
1799     {
1800       ac = (i < alen) ? a->value.character.string[i] : ' ';
1801       bc = (i < blen) ? b->value.character.string[i] : ' ';
1802
1803       if (xcoll_table != NULL)
1804         {
1805           ac = xcoll_table[ac];
1806           bc = xcoll_table[bc];
1807         }
1808
1809       if (ac < bc)
1810         return -1;
1811       if (ac > bc)
1812         return 1;
1813     }
1814
1815   /* Strings are equal */
1816
1817   return 0;
1818 }
1819
1820
1821 /* Specific comparison subroutines.  */
1822
1823 static arith
1824 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1825 {
1826   gfc_expr *result;
1827
1828   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1829                                 &op1->where);
1830   result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1831     compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1832
1833   *resultp = result;
1834   return ARITH_OK;
1835 }
1836
1837
1838 static arith
1839 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1840 {
1841   gfc_expr *result;
1842
1843   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1844                                 &op1->where);
1845   result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1846     !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1847
1848   *resultp = result;
1849   return ARITH_OK;
1850 }
1851
1852
1853 static arith
1854 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1855 {
1856   gfc_expr *result;
1857
1858   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1859                                 &op1->where);
1860   result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1861   *resultp = result;
1862
1863   return ARITH_OK;
1864 }
1865
1866
1867 static arith
1868 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1869 {
1870   gfc_expr *result;
1871
1872   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1873                                 &op1->where);
1874   result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1875   *resultp = result;
1876
1877   return ARITH_OK;
1878 }
1879
1880
1881 static arith
1882 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1883 {
1884   gfc_expr *result;
1885
1886   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1887                                 &op1->where);
1888   result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1889   *resultp = result;
1890
1891   return ARITH_OK;
1892 }
1893
1894
1895 static arith
1896 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1897 {
1898   gfc_expr *result;
1899
1900   result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind (),
1901                                 &op1->where);
1902   result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1903   *resultp = result;
1904
1905   return ARITH_OK;
1906 }
1907
1908
1909 static arith
1910 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1911               gfc_expr ** result)
1912 {
1913   gfc_constructor *c, *head;
1914   gfc_expr *r;
1915   arith rc;
1916
1917   if (op->expr_type == EXPR_CONSTANT)
1918     return eval (op, result);
1919
1920   rc = ARITH_OK;
1921   head = gfc_copy_constructor (op->value.constructor);
1922
1923   for (c = head; c; c = c->next)
1924     {
1925       rc = eval (c->expr, &r);
1926       if (rc != ARITH_OK)
1927         break;
1928
1929       gfc_replace_expr (c->expr, r);
1930     }
1931
1932   if (rc != ARITH_OK)
1933     gfc_free_constructor (head);
1934   else
1935     {
1936       r = gfc_get_expr ();
1937       r->expr_type = EXPR_ARRAY;
1938       r->value.constructor = head;
1939       r->shape = gfc_copy_shape (op->shape, op->rank);
1940
1941       r->ts = head->expr->ts;
1942       r->where = op->where;
1943       r->rank = op->rank;
1944
1945       *result = r;
1946     }
1947
1948   return rc;
1949 }
1950
1951
1952 static arith
1953 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1954                   gfc_expr * op1, gfc_expr * op2,
1955                   gfc_expr ** result)
1956 {
1957   gfc_constructor *c, *head;
1958   gfc_expr *r;
1959   arith rc;
1960
1961   head = gfc_copy_constructor (op1->value.constructor);
1962   rc = ARITH_OK;
1963
1964   for (c = head; c; c = c->next)
1965     {
1966       rc = eval (c->expr, op2, &r);
1967       if (rc != ARITH_OK)
1968         break;
1969
1970       gfc_replace_expr (c->expr, r);
1971     }
1972
1973   if (rc != ARITH_OK)
1974     gfc_free_constructor (head);
1975   else
1976     {
1977       r = gfc_get_expr ();
1978       r->expr_type = EXPR_ARRAY;
1979       r->value.constructor = head;
1980       r->shape = gfc_copy_shape (op1->shape, op1->rank);
1981
1982       r->ts = head->expr->ts;
1983       r->where = op1->where;
1984       r->rank = op1->rank;
1985
1986       *result = r;
1987     }
1988
1989   return rc;
1990 }
1991
1992
1993 static arith
1994 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1995                   gfc_expr * op1, gfc_expr * op2,
1996                   gfc_expr ** result)
1997 {
1998   gfc_constructor *c, *head;
1999   gfc_expr *r;
2000   arith rc;
2001
2002   head = gfc_copy_constructor (op2->value.constructor);
2003   rc = ARITH_OK;
2004
2005   for (c = head; c; c = c->next)
2006     {
2007       rc = eval (op1, c->expr, &r);
2008       if (rc != ARITH_OK)
2009         break;
2010
2011       gfc_replace_expr (c->expr, r);
2012     }
2013
2014   if (rc != ARITH_OK)
2015     gfc_free_constructor (head);
2016   else
2017     {
2018       r = gfc_get_expr ();
2019       r->expr_type = EXPR_ARRAY;
2020       r->value.constructor = head;
2021       r->shape = gfc_copy_shape (op2->shape, op2->rank);
2022
2023       r->ts = head->expr->ts;
2024       r->where = op2->where;
2025       r->rank = op2->rank;
2026
2027       *result = r;
2028     }
2029
2030   return rc;
2031 }
2032
2033
2034 static arith
2035 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2036                   gfc_expr * op1, gfc_expr * op2,
2037                   gfc_expr ** result)
2038 {
2039   gfc_constructor *c, *d, *head;
2040   gfc_expr *r;
2041   arith rc;
2042
2043   head = gfc_copy_constructor (op1->value.constructor);
2044
2045   rc = ARITH_OK;
2046   d = op2->value.constructor;
2047
2048   if (gfc_check_conformance ("Elemental binary operation", op1, op2)
2049       != SUCCESS)
2050     rc = ARITH_INCOMMENSURATE;
2051   else
2052     {
2053
2054       for (c = head; c; c = c->next, d = d->next)
2055         {
2056           if (d == NULL)
2057             {
2058               rc = ARITH_INCOMMENSURATE;
2059               break;
2060             }
2061
2062           rc = eval (c->expr, d->expr, &r);
2063           if (rc != ARITH_OK)
2064             break;
2065
2066           gfc_replace_expr (c->expr, r);
2067         }
2068
2069       if (d != NULL)
2070         rc = ARITH_INCOMMENSURATE;
2071     }
2072
2073   if (rc != ARITH_OK)
2074     gfc_free_constructor (head);
2075   else
2076     {
2077       r = gfc_get_expr ();
2078       r->expr_type = EXPR_ARRAY;
2079       r->value.constructor = head;
2080       r->shape = gfc_copy_shape (op1->shape, op1->rank);
2081
2082       r->ts = head->expr->ts;
2083       r->where = op1->where;
2084       r->rank = op1->rank;
2085
2086       *result = r;
2087     }
2088
2089   return rc;
2090 }
2091
2092
2093 static arith
2094 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2095                gfc_expr * op1, gfc_expr * op2,
2096                gfc_expr ** result)
2097 {
2098
2099   if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
2100     return eval (op1, op2, result);
2101
2102   if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
2103     return reduce_binary_ca (eval, op1, op2, result);
2104
2105   if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
2106     return reduce_binary_ac (eval, op1, op2, result);
2107
2108   return reduce_binary_aa (eval, op1, op2, result);
2109 }
2110
2111
2112 typedef union
2113 {
2114   arith (*f2)(gfc_expr *, gfc_expr **);
2115   arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
2116 }
2117 eval_f;
2118
2119 /* High level arithmetic subroutines.  These subroutines go into
2120    eval_intrinsic(), which can do one of several things to its
2121    operands.  If the operands are incompatible with the intrinsic
2122    operation, we return a node pointing to the operands and hope that
2123    an operator interface is found during resolution.
2124
2125    If the operands are compatible and are constants, then we try doing
2126    the arithmetic.  We also handle the cases where either or both
2127    operands are array constructors.  */
2128
2129 static gfc_expr *
2130 eval_intrinsic (gfc_intrinsic_op operator,
2131                 eval_f eval, gfc_expr * op1, gfc_expr * op2)
2132 {
2133   gfc_expr temp, *result;
2134   int unary;
2135   arith rc;
2136
2137   gfc_clear_ts (&temp.ts);
2138
2139   switch (operator)
2140     {
2141     case INTRINSIC_NOT: /* Logical unary */
2142       if (op1->ts.type != BT_LOGICAL)
2143         goto runtime;
2144
2145       temp.ts.type = BT_LOGICAL;
2146       temp.ts.kind = gfc_default_logical_kind ();
2147
2148       unary = 1;
2149       break;
2150
2151       /* Logical binary operators */
2152     case INTRINSIC_OR:
2153     case INTRINSIC_AND:
2154     case INTRINSIC_NEQV:
2155     case INTRINSIC_EQV:
2156       if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
2157         goto runtime;
2158
2159       temp.ts.type = BT_LOGICAL;
2160       temp.ts.kind = gfc_default_logical_kind ();
2161
2162       unary = 0;
2163       break;
2164
2165     case INTRINSIC_UPLUS:
2166     case INTRINSIC_UMINUS:      /* Numeric unary */
2167       if (!gfc_numeric_ts (&op1->ts))
2168         goto runtime;
2169
2170       temp.ts = op1->ts;
2171
2172       unary = 1;
2173       break;
2174
2175     case INTRINSIC_GE:
2176     case INTRINSIC_LT:          /* Additional restrictions  */
2177     case INTRINSIC_LE:          /* for ordering relations.  */
2178     case INTRINSIC_GT:
2179       if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
2180         {
2181           temp.ts.type = BT_LOGICAL;
2182           temp.ts.kind = gfc_default_logical_kind();
2183           goto runtime;
2184         }
2185
2186       /* else fall through */
2187
2188     case INTRINSIC_EQ:
2189     case INTRINSIC_NE:
2190       if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
2191         {
2192           unary = 0;
2193           temp.ts.type = BT_LOGICAL;
2194           temp.ts.kind = gfc_default_logical_kind();
2195           break;
2196         }
2197
2198       /* else fall through */
2199
2200     case INTRINSIC_PLUS:
2201     case INTRINSIC_MINUS:
2202     case INTRINSIC_TIMES:
2203     case INTRINSIC_DIVIDE:
2204     case INTRINSIC_POWER:       /* Numeric binary */
2205       if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
2206         goto runtime;
2207
2208       /* Insert any necessary type conversions to make the operands compatible.  */
2209
2210       temp.expr_type = EXPR_OP;
2211       gfc_clear_ts (&temp.ts);
2212       temp.operator = operator;
2213
2214       temp.op1 = op1;
2215       temp.op2 = op2;
2216
2217       gfc_type_convert_binary (&temp);
2218
2219       if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
2220           || operator == INTRINSIC_GE || operator == INTRINSIC_GT
2221           || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
2222         {
2223           temp.ts.type = BT_LOGICAL;
2224           temp.ts.kind = gfc_default_logical_kind ();
2225         }
2226
2227       unary = 0;
2228       break;
2229
2230     case INTRINSIC_CONCAT:      /* Character binary */
2231       if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
2232         goto runtime;
2233
2234       temp.ts.type = BT_CHARACTER;
2235       temp.ts.kind = gfc_default_character_kind ();
2236
2237       unary = 0;
2238       break;
2239
2240     case INTRINSIC_USER:
2241       goto runtime;
2242
2243     default:
2244       gfc_internal_error ("eval_intrinsic(): Bad operator");
2245     }
2246
2247   /* Try to combine the operators.  */
2248   if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
2249     goto runtime;
2250
2251   if (op1->expr_type != EXPR_CONSTANT
2252       && (op1->expr_type != EXPR_ARRAY
2253           || !gfc_is_constant_expr (op1)
2254           || !gfc_expanded_ac (op1)))
2255     goto runtime;
2256
2257   if (op2 != NULL
2258       && op2->expr_type != EXPR_CONSTANT
2259       && (op2->expr_type != EXPR_ARRAY
2260           || !gfc_is_constant_expr (op2)
2261           || !gfc_expanded_ac (op2)))
2262     goto runtime;
2263
2264   if (unary)
2265     rc = reduce_unary (eval.f2, op1, &result);
2266   else
2267     rc = reduce_binary (eval.f3, op1, op2, &result);
2268
2269   if (rc != ARITH_OK)
2270     {                           /* Something went wrong */
2271       gfc_error ("%s at %L", gfc_arith_error (rc), &op1->where);
2272       return NULL;
2273     }
2274
2275   gfc_free_expr (op1);
2276   gfc_free_expr (op2);
2277   return result;
2278
2279 runtime:
2280   /* Create a run-time expression */
2281   result = gfc_get_expr ();
2282   result->ts = temp.ts;
2283
2284   result->expr_type = EXPR_OP;
2285   result->operator = operator;
2286
2287   result->op1 = op1;
2288   result->op2 = op2;
2289
2290   result->where = op1->where;
2291
2292   return result;
2293 }
2294
2295
2296 /* Modify type of expression for zero size array.  */
2297 static gfc_expr *
2298 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
2299 {
2300   if (op == NULL)
2301     gfc_internal_error("eval_type_intrinsic0(): op NULL");
2302
2303   switch(operator)
2304     {
2305     case INTRINSIC_GE:
2306     case INTRINSIC_LT:
2307     case INTRINSIC_LE:
2308     case INTRINSIC_GT:
2309     case INTRINSIC_EQ:
2310     case INTRINSIC_NE:
2311       op->ts.type = BT_LOGICAL;
2312       op->ts.kind = gfc_default_logical_kind();
2313       break;
2314
2315     default:
2316       break;
2317     }
2318
2319   return op;
2320 }
2321
2322
2323 /* Return nonzero if the expression is a zero size array.  */
2324
2325 static int
2326 gfc_zero_size_array (gfc_expr * e)
2327 {
2328
2329   if (e->expr_type != EXPR_ARRAY)
2330     return 0;
2331
2332   return e->value.constructor == NULL;
2333 }
2334
2335
2336 /* Reduce a binary expression where at least one of the operands
2337    involves a zero-length array.  Returns NULL if neither of the
2338    operands is a zero-length array.  */
2339
2340 static gfc_expr *
2341 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
2342 {
2343
2344   if (gfc_zero_size_array (op1))
2345     {
2346       gfc_free_expr (op2);
2347       return op1;
2348     }
2349
2350   if (gfc_zero_size_array (op2))
2351     {
2352       gfc_free_expr (op1);
2353       return op2;
2354     }
2355
2356   return NULL;
2357 }
2358
2359
2360 static gfc_expr *
2361 eval_intrinsic_f2 (gfc_intrinsic_op operator,
2362                    arith (*eval) (gfc_expr *, gfc_expr **),
2363                    gfc_expr * op1, gfc_expr * op2)
2364 {
2365   gfc_expr *result;
2366   eval_f f;
2367
2368   if (op2 == NULL)
2369     {
2370       if (gfc_zero_size_array (op1))
2371         return eval_type_intrinsic0(operator, op1);
2372     }
2373   else
2374     {
2375       result = reduce_binary0 (op1, op2);
2376       if (result != NULL)
2377         return eval_type_intrinsic0(operator, result);
2378     }
2379
2380   f.f2 = eval;
2381   return eval_intrinsic (operator, f, op1, op2);
2382 }
2383
2384
2385 static gfc_expr *
2386 eval_intrinsic_f3 (gfc_intrinsic_op operator,
2387                    arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
2388                    gfc_expr * op1, gfc_expr * op2)
2389 {
2390   gfc_expr *result;
2391   eval_f f;
2392
2393   result = reduce_binary0 (op1, op2);
2394   if (result != NULL)
2395     return eval_type_intrinsic0(operator, result);
2396
2397   f.f3 = eval;
2398   return eval_intrinsic (operator, f, op1, op2);
2399 }
2400
2401
2402
2403 gfc_expr *
2404 gfc_uplus (gfc_expr * op)
2405 {
2406   return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
2407 }
2408
2409 gfc_expr *
2410 gfc_uminus (gfc_expr * op)
2411 {
2412   return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
2413 }
2414
2415 gfc_expr *
2416 gfc_add (gfc_expr * op1, gfc_expr * op2)
2417 {
2418   return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
2419 }
2420
2421 gfc_expr *
2422 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
2423 {
2424   return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
2425 }
2426
2427 gfc_expr *
2428 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
2429 {
2430   return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
2431 }
2432
2433 gfc_expr *
2434 gfc_divide (gfc_expr * op1, gfc_expr * op2)
2435 {
2436   return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
2437 }
2438
2439 gfc_expr *
2440 gfc_power (gfc_expr * op1, gfc_expr * op2)
2441 {
2442   return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
2443 }
2444
2445 gfc_expr *
2446 gfc_concat (gfc_expr * op1, gfc_expr * op2)
2447 {
2448   return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
2449 }
2450
2451 gfc_expr *
2452 gfc_and (gfc_expr * op1, gfc_expr * op2)
2453 {
2454   return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
2455 }
2456
2457 gfc_expr *
2458 gfc_or (gfc_expr * op1, gfc_expr * op2)
2459 {
2460   return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
2461 }
2462
2463 gfc_expr *
2464 gfc_not (gfc_expr * op1)
2465 {
2466   return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
2467 }
2468
2469 gfc_expr *
2470 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
2471 {
2472   return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
2473 }
2474
2475 gfc_expr *
2476 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
2477 {
2478   return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
2479 }
2480
2481 gfc_expr *
2482 gfc_eq (gfc_expr * op1, gfc_expr * op2)
2483 {
2484   return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
2485 }
2486
2487 gfc_expr *
2488 gfc_ne (gfc_expr * op1, gfc_expr * op2)
2489 {
2490   return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
2491 }
2492
2493 gfc_expr *
2494 gfc_gt (gfc_expr * op1, gfc_expr * op2)
2495 {
2496   return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
2497 }
2498
2499 gfc_expr *
2500 gfc_ge (gfc_expr * op1, gfc_expr * op2)
2501 {
2502   return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
2503 }
2504
2505 gfc_expr *
2506 gfc_lt (gfc_expr * op1, gfc_expr * op2)
2507 {
2508   return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
2509 }
2510
2511 gfc_expr *
2512 gfc_le (gfc_expr * op1, gfc_expr * op2)
2513 {
2514   return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
2515 }
2516
2517
2518 /* Convert an integer string to an expression node.  */
2519
2520 gfc_expr *
2521 gfc_convert_integer (const char *buffer, int kind, int radix, locus * where)
2522 {
2523   gfc_expr *e;
2524   const char *t;
2525
2526   e = gfc_constant_result (BT_INTEGER, kind, where);
2527   /* a leading plus is allowed, but not by mpz_set_str */
2528   if (buffer[0] == '+')
2529     t = buffer + 1;
2530   else
2531     t = buffer;
2532   mpz_set_str (e->value.integer, t, radix);
2533
2534   return e;
2535 }
2536
2537
2538 /* Convert a real string to an expression node.  */
2539
2540 gfc_expr *
2541 gfc_convert_real (const char *buffer, int kind, locus * where)
2542 {
2543   gfc_expr *e;
2544   const char *t;
2545
2546   e = gfc_constant_result (BT_REAL, kind, where);
2547   /* a leading plus is allowed, but not by mpf_set_str */
2548   if (buffer[0] == '+')
2549     t = buffer + 1;
2550   else
2551     t = buffer;
2552   mpf_set_str (e->value.real, t, 10);
2553
2554   return e;
2555 }
2556
2557
2558 /* Convert a pair of real, constant expression nodes to a single
2559    complex expression node.  */
2560
2561 gfc_expr *
2562 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
2563 {
2564   gfc_expr *e;
2565
2566   e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
2567   mpf_set (e->value.complex.r, real->value.real);
2568   mpf_set (e->value.complex.i, imag->value.real);
2569
2570   return e;
2571 }
2572
2573
2574 /******* Simplification of intrinsic functions with constant arguments *****/
2575
2576
2577 /* Deal with an arithmetic error.  */
2578
2579 static void
2580 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
2581 {
2582
2583   gfc_error ("%s converting %s to %s at %L", gfc_arith_error (rc),
2584              gfc_typename (from), gfc_typename (to), where);
2585
2586   /* TODO: Do something about the error, ie, throw exception, return
2587      NaN, etc.  */
2588 }
2589
2590 /* Convert integers to integers.  */
2591
2592 gfc_expr *
2593 gfc_int2int (gfc_expr * src, int kind)
2594 {
2595   gfc_expr *result;
2596   arith rc;
2597
2598   result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2599
2600   mpz_set (result->value.integer, src->value.integer);
2601
2602   if ((rc = gfc_check_integer_range (result->value.integer, kind))
2603       != ARITH_OK)
2604     {
2605       arith_error (rc, &src->ts, &result->ts, &src->where);
2606       gfc_free_expr (result);
2607       return NULL;
2608     }
2609
2610   return result;
2611 }
2612
2613
2614 /* Convert integers to reals.  */
2615
2616 gfc_expr *
2617 gfc_int2real (gfc_expr * src, int kind)
2618 {
2619   gfc_expr *result;
2620   arith rc;
2621
2622   result = gfc_constant_result (BT_REAL, kind, &src->where);
2623
2624   mpf_set_z (result->value.real, src->value.integer);
2625
2626   if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2627     {
2628       arith_error (rc, &src->ts, &result->ts, &src->where);
2629       gfc_free_expr (result);
2630       return NULL;
2631     }
2632
2633   return result;
2634 }
2635
2636
2637 /* Convert default integer to default complex.  */
2638
2639 gfc_expr *
2640 gfc_int2complex (gfc_expr * src, int kind)
2641 {
2642   gfc_expr *result;
2643   arith rc;
2644
2645   result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2646
2647   mpf_set_z (result->value.complex.r, src->value.integer);
2648   mpf_set_ui (result->value.complex.i, 0);
2649
2650   if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2651     {
2652       arith_error (rc, &src->ts, &result->ts, &src->where);
2653       gfc_free_expr (result);
2654       return NULL;
2655     }
2656
2657   return result;
2658 }
2659
2660
2661 /* Convert default real to default integer.  */
2662
2663 gfc_expr *
2664 gfc_real2int (gfc_expr * src, int kind)
2665 {
2666   gfc_expr *result;
2667   arith rc;
2668
2669   result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2670
2671   mpz_set_f (result->value.integer, src->value.real);
2672
2673   if ((rc = gfc_check_integer_range (result->value.integer, kind))
2674       != ARITH_OK)
2675     {
2676       arith_error (rc, &src->ts, &result->ts, &src->where);
2677       gfc_free_expr (result);
2678       return NULL;
2679     }
2680
2681   return result;
2682 }
2683
2684
2685 /* Convert real to real.  */
2686
2687 gfc_expr *
2688 gfc_real2real (gfc_expr * src, int kind)
2689 {
2690   gfc_expr *result;
2691   arith rc;
2692
2693   result = gfc_constant_result (BT_REAL, kind, &src->where);
2694
2695   mpf_set (result->value.real, src->value.real);
2696
2697   rc = gfc_check_real_range (result->value.real, kind);
2698
2699   if (rc == ARITH_UNDERFLOW)
2700     {
2701       if (gfc_option.warn_underflow)
2702         gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2703       mpf_set_ui(result->value.real, 0);
2704     }
2705   else if (rc != ARITH_OK)
2706     {
2707       arith_error (rc, &src->ts, &result->ts, &src->where);
2708       gfc_free_expr (result);
2709       return NULL;
2710     }
2711
2712   return result;
2713 }
2714
2715
2716 /* Convert real to complex.  */
2717
2718 gfc_expr *
2719 gfc_real2complex (gfc_expr * src, int kind)
2720 {
2721   gfc_expr *result;
2722   arith rc;
2723
2724   result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2725
2726   mpf_set (result->value.complex.r, src->value.real);
2727   mpf_set_ui (result->value.complex.i, 0);
2728
2729   rc = gfc_check_real_range (result->value.complex.r, kind);
2730
2731   if (rc == ARITH_UNDERFLOW)
2732     {
2733       if (gfc_option.warn_underflow)
2734         gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2735       mpf_set_ui(result->value.complex.r, 0);
2736     }
2737   else if (rc != ARITH_OK)
2738     {
2739       arith_error (rc, &src->ts, &result->ts, &src->where);
2740       gfc_free_expr (result);
2741       return NULL;
2742     }
2743
2744   return result;
2745 }
2746
2747
2748 /* Convert complex to integer.  */
2749
2750 gfc_expr *
2751 gfc_complex2int (gfc_expr * src, int kind)
2752 {
2753   gfc_expr *result;
2754   arith rc;
2755
2756   result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2757
2758   mpz_set_f (result->value.integer, src->value.complex.r);
2759
2760   if ((rc = gfc_check_integer_range (result->value.integer, kind))
2761       != ARITH_OK)
2762     {
2763       arith_error (rc, &src->ts, &result->ts, &src->where);
2764       gfc_free_expr (result);
2765       return NULL;
2766     }
2767
2768   return result;
2769 }
2770
2771
2772 /* Convert complex to real.  */
2773
2774 gfc_expr *
2775 gfc_complex2real (gfc_expr * src, int kind)
2776 {
2777   gfc_expr *result;
2778   arith rc;
2779
2780   result = gfc_constant_result (BT_REAL, kind, &src->where);
2781
2782   mpf_set (result->value.real, src->value.complex.r);
2783
2784   rc = gfc_check_real_range (result->value.real, kind);
2785
2786   if (rc == ARITH_UNDERFLOW) 
2787     {
2788       if (gfc_option.warn_underflow)
2789         gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2790       mpf_set_ui(result->value.real, 0);
2791     }
2792   if (rc != ARITH_OK)
2793     {
2794       arith_error (rc, &src->ts, &result->ts, &src->where);
2795       gfc_free_expr (result);
2796       return NULL;
2797     }
2798
2799   return result;
2800 }
2801
2802
2803 /* Convert complex to complex.  */
2804
2805 gfc_expr *
2806 gfc_complex2complex (gfc_expr * src, int kind)
2807 {
2808   gfc_expr *result;
2809   arith rc;
2810
2811   result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2812
2813   mpf_set (result->value.complex.r, src->value.complex.r);
2814   mpf_set (result->value.complex.i, src->value.complex.i);
2815
2816   rc = gfc_check_real_range (result->value.complex.r, kind);
2817
2818   if (rc == ARITH_UNDERFLOW)
2819     {
2820       if (gfc_option.warn_underflow)
2821         gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2822       mpf_set_ui(result->value.complex.r, 0);
2823     }
2824   else if (rc != ARITH_OK)
2825     {
2826       arith_error (rc, &src->ts, &result->ts, &src->where);
2827       gfc_free_expr (result);
2828       return NULL;
2829     }
2830   
2831   rc = gfc_check_real_range (result->value.complex.i, kind);
2832
2833   if (rc == ARITH_UNDERFLOW)
2834     {
2835       if (gfc_option.warn_underflow)
2836         gfc_warning ("%s at %L", gfc_arith_error (rc), &src->where);
2837       mpf_set_ui(result->value.complex.i, 0);
2838     }
2839   else if (rc != ARITH_OK)
2840     {
2841       arith_error (rc, &src->ts, &result->ts, &src->where);
2842       gfc_free_expr (result);
2843       return NULL;
2844     }
2845
2846   return result;
2847 }
2848
2849
2850 /* Logical kind conversion.  */
2851
2852 gfc_expr *
2853 gfc_log2log (gfc_expr * src, int kind)
2854 {
2855   gfc_expr *result;
2856
2857   result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2858   result->value.logical = src->value.logical;
2859
2860   return result;
2861 }