/************************************************************* ************************************************************* * ROUNDED TO NEAREST * ************************************************************* *************************************************************/ double scs_cos_rn(double x){ scs_t sc1, sc2; double resd; int N; #if EVAL_PERF crlibm_second_step_taken++; #endif scs_set_d(sc1, x); N = rem_pio2_scs(sc2, sc1); N = N & 0x0000003; /* extract the 2 last bits of N */ switch (N){ case 0: cosine(sc2); scs_get_d(&resd, sc2); return resd; case 1: sine(sc2); scs_get_d(&resd, sc2); return -resd; case 2: cosine(sc2); scs_get_d(&resd, sc2); return -resd; case 3: sine(sc2); scs_get_d(&resd, sc2); return resd; default: fprintf(stderr,"ERREUR: %d is not a valid value in s_cos \n", N); return 0.0; } }
double scs_atanpi_ru(double x){ scs_t sc1; scs_t res_scs; db_number res; int sign = 1; res.d = x; /* Filter cases */ if (x < 0){ sign = -1; x *= -1; } scs_set_d(sc1, x); scs_atanpi(res_scs, sc1); if (sign == -1){ scs_get_d_minf(&res.d, res_scs); res.d *= -1; return res.d; } else{ scs_get_d_pinf(&res.d, res_scs); return res.d; } }
static void scs_tan(double x, scs_ptr res_scs){ scs_t x_scs; scs_t x2; int i; scs_t y_scs; int N; scs_set_d(x_scs, x); N = rem_pio2_scs(y_scs, x_scs); /* x (=sc2) is in [-Pi/4,Pi/4] */ N = N & 1; /* extract the last bit of N */ scs_square(x2, y_scs); scs_mul(res_scs, tan_scs_poly_ptr[0], x2); for(i=1; i<(DEGREE_TAN_SCS-1)/2; i++){ /* The last coeff is not read from the file. */ scs_add(res_scs, tan_scs_poly_ptr[i], res_scs); scs_mul(res_scs, res_scs, x2); } scs_mul(res_scs, res_scs, y_scs); scs_add(res_scs, y_scs, res_scs); if(N==1) { scs_inv(res_scs, res_scs); res_scs->sign = -res_scs->sign; } }
void scs_log(scs_ptr res, db_number y, int E) { scs_t R, sc_ln2_times_E, res1, addi; scs_ptr ti, inv_wi; db_number z, wi; int i; #if EVAL_PERF crlibm_second_step_taken++; #endif /* to normalize y.d and round to nearest */ /* + (1-trunc(sqrt(2.)/2 * 2^(4))*2^(-4) )+2.^(-(4+1))*/ z.d = y.d + norm_number.d; i = (z.i[HI_ENDIAN] & 0x000fffff); i = i >> 16; /* 0<= i <=11 */ wi.d = ((double)(11+i))*0.0625; /* (1+f-w_i) */ y.d -= wi.d; /* Table reduction */ ti = table_ti_ptr[i]; inv_wi = table_inv_wi_ptr[i]; /* R = (1+f-w_i)/w_i */ scs_set_d(R, y.d); scs_mul(R, R, inv_wi); /* * Polynomial evaluation of log(1 + R) with an error less than 2^(-130) */ scs_mul(res1, constant_poly_ptr[0], R); for(i=1; i<20; i++) { scs_add(addi, constant_poly_ptr[i], res1); scs_mul(res1, addi, R); } if(E==0) { scs_add(res, res1, ti); } else { /* sc_ln2_times_E = E*log(2) */ scs_set(sc_ln2_times_E, sc_ln2_ptr); if (E >= 0) { scs_mul_ui(sc_ln2_times_E, (unsigned int) E); } else { scs_mul_ui(sc_ln2_times_E, (unsigned int) -E); sc_ln2_times_E->sign = -1; } scs_add(addi, res1, ti); scs_add(res, addi, sc_ln2_times_E); } }
double scs_atanpi_rn(double x){ /* This function does NOT compute atanpi(x) correctly if it isn't * called in atanpi_rn() */ scs_t sc1; scs_t res_scs; db_number res; int sign =1; res.d = x; if (x < 0){ sign = -1; x *= -1; } scs_set_d(sc1, x); scs_atanpi(res_scs, sc1); scs_get_d(&res.d, res_scs); res.d *= sign; return res.d; }
/************************************************************* ************************************************************* * ROUNDED TO NEAREST ************************************************************* *************************************************************/ double log10_rn(double x) { scs_t R, res1; scs_t sc_ln2_r10_times_E; scs_ptr inv_wi, ti; db_number nb, nb2, wi, resd; int i, E=0; nb.d = x; /* Filter cases */ if (nb.i[HI_ENDIAN] < 0x00100000){ /* x < 2^(-1022) */ if (((nb.i[HI_ENDIAN] & 0x7fffffff)|nb.i[LO_ENDIAN])==0) /* return 1.0/0.0; */ /* log(+/-0) = -Inf */ return NInf.d; if (nb.i[HI_ENDIAN] < 0) /* return (x-x)/0; */ /* log(-x) = Nan */ return NaN.d; /* Subnormal number */ E -= (SCS_NB_BITS*2); /* keep in mind that x is a subnormal number */ nb.d *=SCS_RADIX_TWO_DOUBLE; /* make x as normal number */ /* We may just want add 2 to the scs number.index */ /* may be .... we will see */ } if (nb.i[HI_ENDIAN] >= 0x7ff00000) return x+x; /* Inf or Nan */ /* find n, nb.d such that sqrt(2)/2 < nb.d < sqrt(2) */ E += (nb.i[HI_ENDIAN]>>20)-1023; nb.i[HI_ENDIAN] = (nb.i[HI_ENDIAN] & 0x000fffff) | 0x3ff00000; if (nb.d > SQRT_2){ nb.d *= 0.5; E++; } /* to normalize nb.d and round to nearest */ /* +((2^4 - trunc(sqrt(2)/2) *2^4 )*2 + 1)/2^5 */ nb2.d = nb.d + norm_number.d; i = (nb2.i[HI_ENDIAN] & 0x000fffff); i = i >> 16; /* 0<= i <=11 */ wi.d = (11+i)*(double)0.6250e-1; /* (1+f-w_i) */ nb.d -= wi.d; /* Table reduction */ ti = table_ti_ptr[i]; inv_wi = table_inv_wi_ptr[i]; /* R = (1+f-w_i)/w_i */ scs_set_d(R, nb.d); scs_mul(R, R, inv_wi); /* sc_ln2_r10_times_E = E*log10(2) */ scs_set(sc_ln2_r10_times_E, sc_ln2_r10_ptr); if (E >= 0){ scs_mul_ui(sc_ln2_r10_times_E, (unsigned int) E); }else{ scs_mul_ui(sc_ln2_r10_times_E, (unsigned int) -E); sc_ln2_r10_times_E->sign = -1; } /* * Polynomial evaluation of log10(1 + R) with an error less than 2^(-130) */ scs_mul(res1, constant_poly_ptr[0], R); for(i=1; i<20; i++){ scs_add(res1, constant_poly_ptr[i], res1); scs_mul(res1, res1, R); } scs_add(res1, res1, ti); scs_add(res1, res1, sc_ln2_r10_times_E); scs_get_d(&resd.d, res1); return resd.d; }
static void scs_atan(scs_ptr res_scs, scs_ptr x){ scs_t X_scs, denom1_scs, denom2_scs, poly_scs, X2; scs_t atanbhihi,atanbhilo, atanblo, atanbhi, atanb; scs_t bsc_ptr; db_number db; double test; int k, i=31; scs_get_d(&db.d, x); #if EVAL_PERF crlibm_second_step_taken++; #endif /* test if x as to be reduced */ if (db.d > MIN_REDUCTION_NEEDED) { /* Compute i so that x E [a[i],a[i+1]] */ if (db.d < arctan_table[i][A].d) i-= 16; else i+=16; if (db.d < arctan_table[i][A].d) i-= 8; else i+= 8; if (db.d < arctan_table[i][A].d) i-= 4; else i+= 4; if (db.d < arctan_table[i][A].d) i-= 2; else i+= 2; if (db.d < arctan_table[i][A].d) i-= 1; else if (i<61) i+= 1; if (db.d < arctan_table[i][A].d) i-= 1; /* evaluate X = (x - b(i)) / (1 + x*b(i)) */ scs_set_d(bsc_ptr, arctan_table[i][B].d); scs_mul(denom1_scs,bsc_ptr,x); scs_add(denom2_scs,denom1_scs,SCS_ONE); scs_sub(X_scs,x,bsc_ptr); scs_div(X_scs,X_scs,denom2_scs); scs_get_d(&test,X_scs); /* Polynomial evaluation of atan(X) , X = (x-b(i)) / (1+ x*b(i)) */ scs_square(X2, X_scs); scs_set(res_scs, constant_poly_ptr[0]); for(k=1; k < 10; k++) { /* we use Horner expression */ scs_mul(res_scs, res_scs, X2); scs_add(res_scs, constant_poly_ptr[k], res_scs); } scs_mul(poly_scs, res_scs, X_scs); /* reconstruction : */ /* 1st we load atan ( b[i] ) in a scs*/ scs_set_d( atanbhihi , arctan_table[i][ATAN_BHI].d); scs_set_d( atanbhilo , arctan_table[i][ATAN_BLO].d); scs_set_d( atanblo , atan_blolo[i].d); scs_add(atanbhi,atanbhihi,atanbhilo); scs_add(atanb,atanbhi,atanblo); scs_add(res_scs,atanb, poly_scs); return; } else { /* no reduction needed */ /* Polynomial evaluation of atan(x) */ scs_square(X2, x); scs_set(res_scs, constant_poly_ptr[0]); for(k=1; k < 10; k++) { /* we use Horner expression */ scs_mul(res_scs, res_scs, X2); scs_add(res_scs, constant_poly_ptr[k], res_scs); } scs_mul(res_scs, res_scs, x); return; } }