]>
Commit | Line | Data |
---|---|---|
76834664 | 1 | `/* Support routines for the intrinsic power (**) operator. |
fbd26352 | 2 | Copyright (C) 2004-2019 Free Software Foundation, Inc. |
76834664 | 3 | Contributed by Paul Brook |
4 | ||
b417ea8c | 5 | This file is part of the GNU Fortran 95 runtime library (libgfortran). |
76834664 | 6 | |
b417ea8c | 7 | Libgfortran is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public | |
76834664 | 9 | License as published by the Free Software Foundation; either |
6bc9506f | 10 | version 3 of the License, or (at your option) any later version. |
b417ea8c | 11 | |
12 | Libgfortran is distributed in the hope that it will be useful, | |
76834664 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
b417ea8c | 15 | GNU General Public License for more details. |
76834664 | 16 | |
6bc9506f | 17 | Under Section 7 of GPL version 3, you are granted additional |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
76834664 | 25 | |
76834664 | 26 | #include "libgfortran.h"' |
41f2d5e8 | 27 | |
76834664 | 28 | include(iparm.m4)dnl |
29 | ||
7b6cb5bd | 30 | /* Use Binary Method to calculate the powi. This is not an optimal but |
76834664 | 31 | a simple and reasonable arithmetic. See section 4.6.3, "Evaluation of |
32 | Powers" of Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art | |
33 | of Computer Programming", 3rd Edition, 1998. */ | |
34 | ||
920e54ef | 35 | `#if defined (HAVE_'rtype_name`) && defined (HAVE_'atype_name`)' |
36 | ||
0a6b5f6b | 37 | rtype_name `pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b); |
38 | export_proto(pow_'rtype_code`_'atype_code`); | |
7b6cb5bd | 39 | |
0a6b5f6b | 40 | 'rtype_name` |
41 | pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b) | |
76834664 | 42 | { |
0a6b5f6b | 43 | 'rtype_name` pow, x; |
44 | 'atype_name` n; | |
45 | GFC_UINTEGER_'atype_kind` u; | |
76834664 | 46 | |
47 | n = b; | |
48 | x = a; | |
49 | pow = 1; | |
50 | if (n != 0) | |
51 | { | |
52 | if (n < 0) | |
53 | { | |
0a6b5f6b | 54 | 'ifelse(rtype_letter,i,`dnl |
76834664 | 55 | if (x == 1) |
56 | return 1; | |
57 | if (x == -1) | |
58 | return (n & 1) ? -1 : 1; | |
59 | return (x == 0) ? 1 / x : 0; | |
60 | ',` | |
a6f8dac9 | 61 | u = -n; |
76834664 | 62 | x = pow / x; |
63 | ')dnl | |
0a6b5f6b | 64 | ` } |
a6f8dac9 | 65 | else |
66 | { | |
67 | u = n; | |
68 | } | |
76834664 | 69 | for (;;) |
70 | { | |
71 | if (u & 1) | |
72 | pow *= x; | |
73 | u >>= 1; | |
74 | if (u) | |
75 | x *= x; | |
76 | else | |
77 | break; | |
78 | } | |
79 | } | |
80 | return pow; | |
81 | } | |
920e54ef | 82 | |
0a6b5f6b | 83 | #endif' |