]>
Commit | Line | Data |
---|---|---|
5b200ac2 FW |
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 | ||
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. */ | |
29 | ||
30 | GFC_COMPLEX_8 | |
31 | prefix(pow_c8_i4) (GFC_COMPLEX_8 a, GFC_INTEGER_4 b) | |
32 | { | |
33 | GFC_COMPLEX_8 pow, x; | |
34 | GFC_INTEGER_4 n, u; | |
35 | ||
36 | n = b; | |
37 | x = a; | |
38 | pow = 1; | |
39 | if (n != 0) | |
40 | { | |
41 | if (n < 0) | |
42 | { | |
43 | ||
44 | n = -n; | |
45 | x = pow / x; | |
46 | } | |
47 | u = n; | |
48 | for (;;) | |
49 | { | |
50 | if (u & 1) | |
51 | pow *= x; | |
52 | u >>= 1; | |
53 | if (u) | |
54 | x *= x; | |
55 | else | |
56 | break; | |
57 | } | |
58 | } | |
59 | return pow; | |
60 | } |