1`/* Support routines for the intrinsic power (**) operator. 2 Copyright (C) 2004-2020 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 3 of the License, or (at your option) any later version. 11 12Libgfortran is distributed in the hope that it will be useful, 13but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15GNU General Public License for more details. 16 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/>. */ 25 26#include "libgfortran.h"' 27 28include(iparm.m4)dnl 29 30/* Use Binary Method to calculate the powi. This is not an optimal but 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 35`#if defined (HAVE_'rtype_name`) && defined (HAVE_'atype_name`)' 36 37rtype_name `pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b); 38export_proto(pow_'rtype_code`_'atype_code`); 39 40'rtype_name` 41pow_'rtype_code`_'atype_code` ('rtype_name` a, 'atype_name` b) 42{ 43 'rtype_name` pow, x; 44 'atype_name` n; 45 GFC_UINTEGER_'atype_kind` u; 46 47 n = b; 48 x = a; 49 pow = 1; 50 if (n != 0) 51 { 52 if (n < 0) 53 { 54'ifelse(rtype_letter,i,`dnl 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',` 61 u = -n; 62 x = pow / x; 63')dnl 64` } 65 else 66 { 67 u = n; 68 } 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} 82 83#endif' 84