OSDN Git Service

* acinclude.m4 (LIBGFOR_CHECK_ATTRIBUTE_VISIBILITY): New.
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / pow.m4
1 `/* Support routines for the intrinsic power (**) operator.
2    Copyright 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook
4
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
6
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "libgfortran.h"'
24 include(iparm.m4)dnl
25
26 /* Use Binary Method to calculate the powi. This is not an optimal but
27    a simple and reasonable arithmetic. See section 4.6.3, "Evaluation of
28    Powers" of Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art
29    of Computer Programming", 3rd Edition, 1998.  */
30
31 rtype_name `pow_'rtype_code`_'atype_code (rtype_name a, atype_name b);
32 export_proto(pow_`'rtype_code`_'atype_code);
33
34 rtype_name
35 `pow_'rtype_code`_'atype_code (rtype_name a, atype_name b)
36 {
37   rtype_name pow, x;
38   atype_name n, u;
39   
40   n = b;
41   x = a;
42   pow = 1;
43   if (n != 0)
44     {
45       if (n < 0)
46         {
47 ifelse(rtype_letter,i,`dnl
48           if (x == 1)
49             return 1;
50           if (x == -1)
51             return (n & 1) ? -1 : 1;
52           return (x == 0) ? 1 / x : 0;
53 ',`
54           n = -n;
55           x = pow / x;
56 ')dnl
57         }
58       u = n;
59       for (;;)
60         {
61           if (u & 1)
62             pow *= x;
63           u >>= 1;
64           if (u)
65             x *= x;
66           else
67             break;
68         }
69     }
70   return pow;
71 }