| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* expt.d - exponentiate |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
#define ECL_INCLUDE_MATH_H |
| 17 |
#include <ecl/ecl.h> |
| 18 |
#include <ecl/ecl-inl.h> |
| 19 |
#include <ecl/internal.h> |
| 20 |
#include <ecl/impl/math_fenv.h> |
| 21 |
#include <ecl/impl/math_dispatch.h> |
| 22 |
|
| 23 |
#pragma STDC FENV_ACCESS ON |
| 24 |
|
| 25 |
cl_fixnum |
| 26 |
ecl_fixnum_expt(cl_fixnum x, cl_fixnum y) |
| 27 |
{ |
| 28 |
cl_fixnum z = 1; |
| 29 |
while (y > 0) |
| 30 |
if (y%2 == 0) { |
| 31 |
x *= x; |
| 32 |
y /= 2; |
| 33 |
} else { |
| 34 |
z *= x; |
| 35 |
--y; |
| 36 |
} |
| 37 |
return(z); |
| 38 |
} |
| 39 |
|
| 40 |
cl_object |
| 41 |
cl_expt(cl_object x, cl_object y) |
| 42 |
{ |
| 43 |
@(return ecl_expt(x, y)); |
| 44 |
} |
| 45 |
|
| 46 |
ecl_def_ct_single_float(singlefloat_one,1,static,const); |
| 47 |
ecl_def_ct_double_float(doublefloat_one,1,static,const); |
| 48 |
ecl_def_ct_long_float(longfloat_one,1,static,const); |
| 49 |
#ifdef ECL_COMPLEX_FLOAT |
| 50 |
ecl_def_ct_csfloat(csfloat_one,1,static,const); |
| 51 |
ecl_def_ct_cdfloat(cdfloat_one,1,static,const); |
| 52 |
ecl_def_ct_clfloat(clfloat_one,1,static,const); |
| 53 |
#endif |
| 54 |
|
| 55 |
static cl_object |
| 56 |
expt_zero(cl_object x, cl_object y) |
| 57 |
{ |
| 58 |
cl_type ty, tx; |
| 59 |
cl_object z; |
| 60 |
ty = ecl_t_of(y); |
| 61 |
tx = ecl_t_of(x); |
| 62 |
if (ecl_unlikely(!ECL_NUMBER_TYPE_P(tx))) { |
| 63 |
FEwrong_type_nth_arg(@[expt], 1, x, @[number]); |
| 64 |
} |
| 65 |
/* INV: The most specific numeric types come first. */ |
| 66 |
switch ((ty > tx)? ty : tx) { |
| 67 |
case t_fixnum: |
| 68 |
case t_bignum: |
| 69 |
case t_ratio: |
| 70 |
return ecl_make_fixnum(1); |
| 71 |
case t_singlefloat: |
| 72 |
return singlefloat_one; |
| 73 |
case t_doublefloat: |
| 74 |
return doublefloat_one; |
| 75 |
case t_longfloat: |
| 76 |
return longfloat_one; |
| 77 |
case t_complex: |
| 78 |
z = expt_zero((tx == t_complex)? x->gencomplex.real : x, |
| 79 |
(ty == t_complex)? y->gencomplex.real : y); |
| 80 |
return ecl_make_complex(z, ecl_make_fixnum(0)); |
| 81 |
#ifdef ECL_COMPLEX_FLOAT |
| 82 |
case t_csfloat: |
| 83 |
return csfloat_one; |
| 84 |
case t_cdfloat: |
| 85 |
return cdfloat_one; |
| 86 |
case t_clfloat: |
| 87 |
return clfloat_one; |
| 88 |
#endif |
| 89 |
default: |
| 90 |
/* We will never reach this */ |
| 91 |
if (ty > tx) FEwrong_type_nth_arg(@[expt], 1, x, @[number]); |
| 92 |
else FEwrong_type_nth_arg(@[expt], 2, y, @[number]); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
static cl_object |
| 97 |
ecl_expt_generic(cl_object x, cl_object y) { |
| 98 |
bool minusp = ecl_minusp(y); |
| 99 |
cl_object z = ecl_make_fixnum(1); |
| 100 |
if (minusp) { |
| 101 |
y = ecl_negate(y); |
| 102 |
} |
| 103 |
do { |
| 104 |
/* INV: ecl_integer_divide outputs an integer */ |
| 105 |
if (!ecl_evenp(y)) { |
| 106 |
z = ecl_times(z, x); |
| 107 |
} |
| 108 |
y = ecl_integer_divide(y, ecl_make_fixnum(2)); |
| 109 |
if (ecl_zerop(y)) { |
| 110 |
if (minusp) return ecl_divide(ecl_make_fixnum(1), z); |
| 111 |
else return z; |
| 112 |
} |
| 113 |
x = ecl_times(x, x); |
| 114 |
} while (1); |
| 115 |
} |
| 116 |
|
| 117 |
static cl_object |
| 118 |
ecl_expt_float(cl_object x, cl_object y) { |
| 119 |
cl_type |
| 120 |
tx = ecl_t_of(x), |
| 121 |
ty = ecl_t_of(y); |
| 122 |
cl_object ret; |
| 123 |
|
| 124 |
ECL_MATHERR_CLEAR; |
| 125 |
switch((ty > tx) ? ty : tx) { |
| 126 |
case t_longfloat: |
| 127 |
ret = ecl_make_long_float |
| 128 |
(powl(ecl_to_long_double(x), ecl_to_long_double(y))); |
| 129 |
break; |
| 130 |
case t_doublefloat: |
| 131 |
ret = ecl_make_double_float |
| 132 |
(pow(ecl_to_double(x), ecl_to_double(y))); |
| 133 |
break; |
| 134 |
default: |
| 135 |
ret = ecl_make_single_float |
| 136 |
(powf(ecl_to_float(x), ecl_to_float(y))); |
| 137 |
break; |
| 138 |
} |
| 139 |
ECL_MATHERR_TEST; |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
#ifdef ECL_COMPLEX_FLOAT |
| 144 |
static cl_object |
| 145 |
ecl_expt_complex_float(cl_object x, cl_object y) { |
| 146 |
cl_type |
| 147 |
tx = ecl_t_of(x), |
| 148 |
ty = ecl_t_of(y); |
| 149 |
cl_object ret; |
| 150 |
|
| 151 |
ECL_MATHERR_CLEAR; |
| 152 |
switch ((ty > tx)? ty : tx) { |
| 153 |
case t_clfloat: |
| 154 |
case t_longfloat: |
| 155 |
ret = ecl_make_clfloat |
| 156 |
(cpowl(ecl_to_clfloat(x), ecl_to_clfloat(y))); |
| 157 |
break; |
| 158 |
case t_cdfloat: |
| 159 |
case t_doublefloat: |
| 160 |
ret = ecl_make_cdfloat |
| 161 |
(cpow (ecl_to_cdfloat(x), ecl_to_cdfloat(y))); |
| 162 |
break; |
| 163 |
default: |
| 164 |
ret = ecl_make_csfloat |
| 165 |
(cpowf(ecl_to_csfloat(x), ecl_to_csfloat(y))); |
| 166 |
break; |
| 167 |
} |
| 168 |
ECL_MATHERR_TEST; |
| 169 |
return ret; |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
cl_object |
| 174 |
ecl_expt(cl_object x, cl_object y) |
| 175 |
{ |
| 176 |
cl_type ty, tx; |
| 177 |
/* 0 ^ 0 -> 1 */ |
| 178 |
if (ecl_unlikely(ecl_zerop(y))) { |
| 179 |
return expt_zero(x, y); |
| 180 |
} |
| 181 |
if (ecl_unlikely(ecl_zerop(x))) { |
| 182 |
/* 0 ^ -y = (1/0)^y */ |
| 183 |
if (!ecl_plusp(cl_realpart(y))) { |
| 184 |
/* Normally we would have signalled FEdivision_by_zero, but fpe |
| 185 |
may be disabled and then we want to work on infinity. */ |
| 186 |
return ecl_divide(ecl_make_fixnum(1), x); |
| 187 |
} |
| 188 |
/* 0 ^ +y = 0 */ |
| 189 |
/* We call ecl_times to ensure the most contagious type.*/ |
| 190 |
return ecl_times(x, y); |
| 191 |
} |
| 192 |
/* Here comes the order in which we handle cases: */ |
| 193 |
/* -----------------------------------FIRST IF--- */ |
| 194 |
/* rational ^ integer -> rational */ |
| 195 |
/* complex ^ integer -> complex/rational */ |
| 196 |
/* float ^ integer -> float */ |
| 197 |
/* cfloat ^ integer -> cfloat */ |
| 198 |
/* ----------------------------------SECOND IF--- */ |
| 199 |
/* number ^ complex -> cfloat */ |
| 200 |
/* complex ^ number -> cfloat */ |
| 201 |
/* negative ^ number -> cfloat */ |
| 202 |
/* ---------------------------------THIRD ELSE--- */ |
| 203 |
/* positive ^ number -> float */ |
| 204 |
/* ---------------------------------------------- */ |
| 205 |
/* x and y are already ensured to be a number by ecl_zerop above */ |
| 206 |
ty = ecl_t_of(y); |
| 207 |
tx = ecl_t_of(x); |
| 208 |
if (ty == t_fixnum || ty == t_bignum) { |
| 209 |
switch (tx) { |
| 210 |
case t_fixnum: |
| 211 |
case t_bignum: |
| 212 |
case t_ratio: |
| 213 |
case t_complex: |
| 214 |
return ecl_expt_generic(x, y); |
| 215 |
case t_longfloat: |
| 216 |
case t_doublefloat: |
| 217 |
case t_singlefloat: |
| 218 |
return ecl_expt_float(x, y); |
| 219 |
#ifdef ECL_COMPLEX_FLOAT |
| 220 |
case t_clfloat: |
| 221 |
case t_cdfloat: |
| 222 |
case t_csfloat: |
| 223 |
return ecl_expt_complex_float(x, y); |
| 224 |
#endif |
| 225 |
default: |
| 226 |
ecl_internal_error("expt: unhandled switch branch."); |
| 227 |
} |
| 228 |
} |
| 229 |
if (ECL_COMPLEXP(y) || ECL_COMPLEXP(x) || ecl_minusp(x)) { |
| 230 |
#ifdef ECL_COMPLEX_FLOAT |
| 231 |
return ecl_expt_complex_float(x, y); |
| 232 |
#else |
| 233 |
/* We call expt_zero to ensure the most contagious type.*/ |
| 234 |
cl_object z = ecl_log1(ecl_times(x, expt_zero(x, y))); |
| 235 |
z = ecl_times(z, y); |
| 236 |
z = ecl_exp(z); |
| 237 |
return z; |
| 238 |
#endif |
| 239 |
} |
| 240 |
return ecl_expt_float(x, y); |
| 241 |
} |