Skip to content

Commit fdf85b1

Browse files
committed
Aded macro to compute "s", part of the numerator
1 parent 42117a6 commit fdf85b1

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

s_mp_faster_to_radix.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static int32_t s_pow(int32_t base, int32_t exponent)
3131
return result;
3232
}
3333

34+
#define MP_COMPUTE_ESS(T) ((int)((int32_t)((uint32_t)1 << (T)) * k))
35+
3436
static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_maxlen, size_t *part_written,
3537
int radix, int32_t k, int32_t t, bool pad, mp_int *P, mp_int *R)
3638
{
@@ -52,8 +54,7 @@ static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_
5254
See also: Modern Computer Arithmetic, version 0.5.9, page 59
5355
*/
5456

55-
/* If this cast-feast looks familiar: it is the numerator from computing the reciprocal*/
56-
Beta = (int)((int32_t)((uint32_t)1 << (t+1)) * k);
57+
Beta = MP_COMPUTE_ESS(t+1);
5758

5859
/* Q = floor(A1 * I / 2^Beta) */
5960
/* I = floor( (2^(2*Beta)) / B) Here we have R[t] = I, P[t] = B */
@@ -91,7 +92,6 @@ static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_
9192
return err;
9293
}
9394

94-
9595
mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix)
9696
{
9797
mp_err err;
@@ -228,34 +228,33 @@ mp_err s_mp_faster_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *w
228228
if (use_newton_raphson && (radix == 10)) {
229229
/* Use a round of Newton-Raphson to compute the next reciprocal */
230230
/* s = 2^t*k */
231-
s = (int)((int32_t)((uint32_t)1 << (t)) * k);
231+
s = MP_COMPUTE_ESS(t);
232232
/* M = R[t-1] * 2^g */
233-
if ((err = mp_mul_2d(&R[t-1], (mp_digit)3, &M2)) != MP_OKAY) goto LTM_ERR;
234-
if ((err = mp_sub_d(&M2, (mp_digit)2, &M2)) != MP_OKAY) goto LTM_ERR;
233+
if ((err = mp_mul_2d(&R[t-1], 3, &M2)) != MP_OKAY) goto LTM_ERR;
234+
if ((err = mp_sub_d(&M2, (mp_digit)2, &M2)) != MP_OKAY) goto LTM_ERR;
235235
/* Do the M-R round: M = floor( ( 2*M^2 - floor((n^2*M^4) / (2^(2*(s+g)))) ) / 2^(2*g)) + 1 */
236236
/* M2 = (M * 2^g)^2*/
237-
if ((err = mp_sqr(&M2, &M2)) != MP_OKAY) goto LTM_ERR;
237+
if ((err = mp_sqr(&M2, &M2)) != MP_OKAY) goto LTM_ERR;
238238
/* M4 = M2^2 */
239-
if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) goto LTM_ERR;
239+
if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) goto LTM_ERR;
240240
/* Compute numerator: n^2*M^4 = (P[t]) * M4*/
241-
if ((err = mp_mul(&P[t], &M4, &M4)) != MP_OKAY) goto LTM_ERR;
241+
if ((err = mp_mul(&P[t], &M4, &M4)) != MP_OKAY) goto LTM_ERR;
242242
/* Compute fraction by shifting right: M4>>2*(s+g) where 2*(s+g) < MAX_INT */
243-
if ((err = mp_div_2d(&M4, 2*(s+g), &M4, NULL)) != MP_OKAY) goto LTM_ERR;
244-
if ((err = mp_mul_2(&M2,&M2)) != MP_OKAY) goto LTM_ERR;
245-
if ((err = mp_sub(&M2,&M4,&M4)) != MP_OKAY) goto LTM_ERR;
243+
if ((err = mp_div_2d(&M4, 2*(s+g), &M4, NULL)) != MP_OKAY) goto LTM_ERR;
244+
if ((err = mp_mul_2(&M2,&M2)) != MP_OKAY) goto LTM_ERR;
245+
if ((err = mp_sub(&M2,&M4,&M4)) != MP_OKAY) goto LTM_ERR;
246246
/* R[t] = M / 2^(2*g) remove extra bits before storage */
247-
if ((err = mp_div_2d(&M4, 2*g, &(R[t]), &M4)) != MP_OKAY) goto LTM_ERR;
248-
if ((err = mp_incr(&R[t])) != MP_OKAY) goto LTM_ERR;
247+
if ((err = mp_div_2d(&M4, 2*g, &(R[t]), &M4)) != MP_OKAY) goto LTM_ERR;
248+
if ((err = mp_incr(&R[t])) != MP_OKAY) goto LTM_ERR;
249249
} else {
250250
#endif
251251
/* R[t] = R[t] << (2^t * k) The factor cannot overflow, we checked that above */
252-
/* TODO: these are more castings than in the ER in Mayrhofen at New Year's Eve! */
253-
if ((err = mp_2expt(&(R[t]), (int)((int32_t)((uint32_t)1 << (t+1)) * k))) != MP_OKAY) goto LTM_ERR;
252+
if ((err = mp_2expt(&(R[t]), MP_COMPUTE_ESS(t + 1) )) != MP_OKAY) goto LTM_ERR;
254253
/* Compute reciprocal */
255254
/* R[t] = floor(2^(2^t * k) / P[t] */
256-
if ((err = mp_div(&R[t], &P[t], &R[t], NULL)) != MP_OKAY) goto LTM_ERR;
255+
if ((err = mp_div(&R[t], &P[t], &R[t], NULL)) != MP_OKAY) goto LTM_ERR;
257256
/* Ceiling if P[t] is not a power of two but it is not a problem if P[t] is a power of two. */
258-
if ((err = mp_incr(&R[t])) != MP_OKAY) goto LTM_ERR;
257+
if ((err = mp_incr(&R[t])) != MP_OKAY) goto LTM_ERR;
259258
#ifdef MP_TO_RADIX_USE_NEWTON_RAPHSON
260259
}
261260
#endif

0 commit comments

Comments
 (0)