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