* internal.h (HAVE_ICONV): Undefine if we do not have HAVE_ICONV_H
[official-gcc.git] / libgfortran / generated / pow_r8_i8.c
blob7f75c82f79ba3807cacdd74d0b01c6cd29d6cb0e
1 /* Support routines for the intrinsic power (**) operator.
2 Copyright 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
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.
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.
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. */
22 #include "config.h"
23 #include "libgfortran.h"
25 /* Uuse Binary Method to calculate the powi. This is not an optimal but
26 a simple and reasonable arithmetic. See section 4.6.3, "Evaluation of
27 Powers" of Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art
28 of Computer Programming", 3rd Edition, 1998. */
30 GFC_REAL_8
31 prefix(pow_r8_i8) (GFC_REAL_8 a, GFC_INTEGER_8 b)
33 GFC_REAL_8 pow, x;
34 GFC_INTEGER_8 n, u;
36 n = b;
37 x = a;
38 pow = 1;
39 if (n != 0)
41 if (n < 0)
44 n = -n;
45 x = pow / x;
47 u = n;
48 for (;;)
50 if (u & 1)
51 pow *= x;
52 u >>= 1;
53 if (u)
54 x *= x;
55 else
56 break;
59 return pow;