Back to index

glibc  2.9
e_pow.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1999 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <math.h>
00020 #include "math_private.h"
00021 #include "mathimpl.h"
00022 
00023 #ifndef SUFF
00024 #define SUFF
00025 #endif
00026 #ifndef float_type
00027 #define float_type double
00028 #endif
00029 
00030 #define CONCATX(a,b) __CONCAT(a,b)
00031 #define s(name) CONCATX(name,SUFF)
00032 #define m81(func) __m81_u(s(func))
00033 
00034 float_type
00035 s(__ieee754_pow) (float_type x, float_type y)
00036 {
00037   float_type z;
00038   float_type ax;
00039   unsigned long x_cond, y_cond;
00040 
00041   y_cond = __m81_test (y);
00042   if (y_cond & __M81_COND_ZERO)
00043     return 1.0;
00044   if (y_cond & __M81_COND_NAN)
00045     return x == 1.0 ? x : x + y;
00046 
00047   x_cond = __m81_test (x);
00048   if (x_cond & __M81_COND_NAN)
00049     return x + y;
00050 
00051   if (y_cond & __M81_COND_INF)
00052     {
00053       ax = s(fabs) (x);
00054       if (ax == 1.0)
00055        return ax;
00056       if (ax > 1.0)
00057        return y_cond & __M81_COND_NEG ? 0 : y;
00058       else
00059        return y_cond & __M81_COND_NEG ? -y : 0;
00060     }
00061 
00062   if (s(fabs) (y) == 1.0)
00063     return y_cond & __M81_COND_NEG ? 1 / x : x;
00064 
00065   if (y == 2)
00066     return x * x;
00067   if (y == 0.5 && !(x_cond & __M81_COND_NEG))
00068     return m81(__ieee754_sqrt) (x);
00069 
00070   if (x == 10.0)
00071     {
00072       __asm ("ftentox%.x %1, %0" : "=f" (z) : "f" (y));
00073       return z;
00074     }
00075   if (x == 2.0)
00076     {
00077       __asm ("ftwotox%.x %1, %0" : "=f" (z) : "f" (y));
00078       return z;
00079     }
00080 
00081   ax = s(fabs) (x);
00082   if (x_cond & (__M81_COND_INF | __M81_COND_ZERO) || ax == 1.0)
00083     {
00084       z = ax;
00085       if (y_cond & __M81_COND_NEG)
00086        z = 1 / z;
00087       if (x_cond & __M81_COND_NEG)
00088        {
00089          if (y != m81(__rint) (y))
00090            {
00091              if (x == -1)
00092               z = (z - z) / (z - z);
00093            }
00094          else
00095            goto maybe_negate;
00096        }
00097       return z;
00098     }
00099 
00100   if (x_cond & __M81_COND_NEG)
00101     {
00102       if (y == m81(__rint) (y))
00103        {
00104          z = m81(__ieee754_exp) (y * m81(__ieee754_log) (-x));
00105        maybe_negate:
00106          /* We always use the long double format, since y is already in
00107             this format and rounding won't change the result.  */
00108          {
00109            int32_t exponent;
00110            u_int32_t i0, i1;
00111            GET_LDOUBLE_WORDS (exponent, i0, i1, y);
00112            exponent = (exponent & 0x7fff) - 0x3fff;
00113            if (exponent <= 31
00114               ? i0 & (1 << (31 - exponent))
00115               : (exponent <= 63
00116                  && i1 & (1 << (63 - exponent))))
00117              z = -z;
00118          }
00119        }
00120       else
00121        z = (y - y) / (y - y);
00122     }
00123   else
00124     z = m81(__ieee754_exp) (y * m81(__ieee754_log) (x));
00125   return z;
00126 }