void m_apm_integer_pow_mt(M_APM rr, int places, M_APM aa, int mexp) { m_apm_enter(); m_apm_integer_pow(rr,places,aa,mexp); m_apm_leave(); }
void m_apm_exp(M_APM r, int places, M_APM x) { M_APM tmp7, tmp8, tmp9; int dplaces, nn, ii; if (MM_firsttime1) { MM_firsttime1 = FALSE; MM_exp_log2R = m_apm_init(); MM_exp_512R = m_apm_init(); m_apm_set_string(MM_exp_log2R, "1.44269504089"); /* ~ 1 / log(2) */ m_apm_set_string(MM_exp_512R, "1.953125E-3"); /* 1 / 512 */ } tmp7 = M_get_stack_var(); tmp8 = M_get_stack_var(); tmp9 = M_get_stack_var(); if (x->m_apm_sign == 0) /* if input == 0, return '1' */ { m_apm_copy(r, MM_One); M_restore_stack(3); return; } if (x->m_apm_exponent <= -3) /* already small enough so call _raw directly */ { M_raw_exp(tmp9, (places + 6), x); m_apm_round(r, places, tmp9); M_restore_stack(3); return; } /* From David H. Bailey's MPFUN Fortran package : exp (t) = (1 + r + r^2 / 2! + r^3 / 3! + r^4 / 4! ...) ^ q * 2 ^ n where q = 256, r = t' / q, t' = t - n Log(2) and where n is chosen so that -0.5 Log(2) < t' <= 0.5 Log(2). Reducing t mod Log(2) and dividing by 256 insures that -0.001 < r <= 0.001, which accelerates convergence in the above series. I use q = 512 and also limit how small 'r' can become. The 'r' used here is limited in magnitude from 1.95E-4 < |r| < 1.35E-3. Forcing 'r' into a narrow range keeps the algorithm 'well behaved'. ( the range is [0.1 / 512] to [log(2) / 512] ) */ if (M_exp_compute_nn(&nn, tmp7, x) != 0) { M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_exp\', Input too large, Overflow"); M_set_to_zero(r); M_restore_stack(3); return; } dplaces = places + 8; /* check to make sure our log(2) is accurate enough */ M_check_log_places(dplaces); m_apm_multiply(tmp8, tmp7, MM_lc_log2); m_apm_subtract(tmp7, x, tmp8); /* * guarantee that |tmp7| is between 0.1 and 0.9999999.... * (in practice, the upper limit only reaches log(2), 0.693... ) */ while (TRUE) { if (tmp7->m_apm_sign != 0) { if (tmp7->m_apm_exponent == 0) break; } if (tmp7->m_apm_sign >= 0) { nn++; m_apm_subtract(tmp8, tmp7, MM_lc_log2); m_apm_copy(tmp7, tmp8); } else { nn--; m_apm_add(tmp8, tmp7, MM_lc_log2); m_apm_copy(tmp7, tmp8); } } m_apm_multiply(tmp9, tmp7, MM_exp_512R); /* perform the series expansion ... */ M_raw_exp(tmp8, dplaces, tmp9); /* * raise result to the 512 power * * note : x ^ 512 = (((x ^ 2) ^ 2) ^ 2) ... 9 times */ ii = 9; while (TRUE) { m_apm_multiply(tmp9, tmp8, tmp8); m_apm_round(tmp8, dplaces, tmp9); if (--ii == 0) break; } /* now compute 2 ^ N */ m_apm_integer_pow(tmp7, dplaces, MM_Two, nn); m_apm_multiply(tmp9, tmp7, tmp8); m_apm_round(r, places, tmp9); M_restore_stack(3); /* restore the 3 locals we used here */ }
/* Calculate the POW function by calling EXP : Y A X = e where A = Y * log(X) */ void m_apm_pow(M_APM rr, int places, M_APM xx, M_APM yy) { int iflag, pflag; char sbuf[64]; M_APM tmp8, tmp9; /* if yy == 0, return 1 */ if (yy->m_apm_sign == 0) { m_apm_copy(rr, MM_One); return; } /* if xx == 0, return 0 */ if (xx->m_apm_sign == 0) { M_set_to_zero(rr); return; } if (M_size_flag == 0) /* init locals on first call */ { M_size_flag = M_get_sizeof_int(); M_last_log_digits = 0; M_last_xx_input = m_apm_init(); M_last_xx_log = m_apm_init(); } /* * if 'yy' is a small enough integer, call the more * efficient _integer_pow function. */ if (m_apm_is_integer(yy)) { iflag = FALSE; if (M_size_flag == 2) /* 16 bit compilers */ { if (yy->m_apm_exponent <= 4) iflag = TRUE; } else /* >= 32 bit compilers */ { if (yy->m_apm_exponent <= 7) iflag = TRUE; } if (iflag) { m_apm_to_integer_string(sbuf, yy); m_apm_integer_pow(rr, places, xx, atoi(sbuf)); return; } } tmp8 = M_get_stack_var(); tmp9 = M_get_stack_var(); /* * If parameter 'X' is the same this call as it * was the previous call, re-use the saved log * calculation from last time. */ pflag = FALSE; if (M_last_log_digits >= places) { if (m_apm_compare(xx, M_last_xx_input) == 0) pflag = TRUE; } if (pflag) { m_apm_round(tmp9, (places + 8), M_last_xx_log); } else { m_apm_log(tmp9, (places + 8), xx); M_last_log_digits = places + 2; /* save the 'X' input value and the log calculation */ m_apm_copy(M_last_xx_input, xx); m_apm_copy(M_last_xx_log, tmp9); } m_apm_multiply(tmp8, tmp9, yy); m_apm_exp(rr, places, tmp8); M_restore_stack(2); /* restore the 2 locals we used here */ }