]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/m4/pow.m4
Update copyright years.
[thirdparty/gcc.git] / libgfortran / m4 / pow.m4
CommitLineData
5b200ac2 1`/* Support routines for the intrinsic power (**) operator.
a5544970 2 Copyright (C) 2004-2019 Free Software Foundation, Inc.
5b200ac2
FW
3 Contributed by Paul Brook
4
57dea9f6 5This file is part of the GNU Fortran 95 runtime library (libgfortran).
5b200ac2 6
57dea9f6
TM
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
5b200ac2 9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
57dea9f6
TM
11
12Libgfortran is distributed in the hope that it will be useful,
5b200ac2
FW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57dea9f6 15GNU General Public License for more details.
5b200ac2 16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
5b200ac2 25
5b200ac2 26#include "libgfortran.h"'
36ae8a61 27
5b200ac2
FW
28include(iparm.m4)dnl
29
7d7b8bfe 30/* Use Binary Method to calculate the powi. This is not an optimal but
5b200ac2
FW
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
644cb69f
FXC
35`#if defined (HAVE_'rtype_name`) && defined (HAVE_'atype_name`)'
36
adea5e16
TK
37rtype_name `pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b);
38export_proto(pow_'rtype_code`_'atype_code`);
7d7b8bfe 39
adea5e16
TK
40'rtype_name`
41pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b)
5b200ac2 42{
adea5e16
TK
43 'rtype_name` pow, x;
44 'atype_name` n;
45 GFC_UINTEGER_'atype_kind` u;
5b200ac2
FW
46
47 n = b;
48 x = a;
49 pow = 1;
50 if (n != 0)
51 {
52 if (n < 0)
53 {
adea5e16 54'ifelse(rtype_letter,i,`dnl
5b200ac2
FW
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',`
3d1f465a 61 u = -n;
5b200ac2
FW
62 x = pow / x;
63')dnl
adea5e16 64` }
3d1f465a
TK
65 else
66 {
67 u = n;
68 }
5b200ac2
FW
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}
644cb69f 82
adea5e16 83#endif'