]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/pow_i16_i4.c
re PR libfortran/19308 (I/O library should support more real and integer kinds)
[thirdparty/gcc.git] / libgfortran / generated / pow_i16_i4.c
CommitLineData
644cb69f
FXC
1/* Support routines for the intrinsic power (**) operator.
2 Copyright 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook
4
5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
9License as published by the Free Software Foundation; either
10version 2 of the License, or (at your option) any later version.
11
12In addition to the permissions in the GNU General Public License, the
13Free Software Foundation gives you unlimited permission to link the
14compiled version of this file into combinations with other programs,
15and to distribute those combinations without any restriction coming
16from the use of this file. (The General Public License restrictions
17do apply in other respects; for example, they cover modification of
18the file, and distribution when not linked into a combine
19executable.)
20
21Libgfortran is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public
27License along with libgfortran; see the file COPYING. If not,
28write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29Boston, MA 02110-1301, USA. */
30
31#include "config.h"
32#include "libgfortran.h"
33
34/* Use Binary Method to calculate the powi. This is not an optimal but
35 a simple and reasonable arithmetic. See section 4.6.3, "Evaluation of
36 Powers" of Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art
37 of Computer Programming", 3rd Edition, 1998. */
38
39#if defined (HAVE_GFC_INTEGER_16) && defined (HAVE_GFC_INTEGER_4)
40
41GFC_INTEGER_16 pow_i16_i4 (GFC_INTEGER_16 a, GFC_INTEGER_4 b);
42export_proto(pow_i16_i4);
43
44GFC_INTEGER_16
45pow_i16_i4 (GFC_INTEGER_16 a, GFC_INTEGER_4 b)
46{
47 GFC_INTEGER_16 pow, x;
48 GFC_INTEGER_4 n, u;
49
50 n = b;
51 x = a;
52 pow = 1;
53 if (n != 0)
54 {
55 if (n < 0)
56 {
57 if (x == 1)
58 return 1;
59 if (x == -1)
60 return (n & 1) ? -1 : 1;
61 return (x == 0) ? 1 / x : 0;
62 }
63 u = n;
64 for (;;)
65 {
66 if (u & 1)
67 pow *= x;
68 u >>= 1;
69 if (u)
70 x *= x;
71 else
72 break;
73 }
74 }
75 return pow;
76}
77
78#endif