s3:smb2_server: pass smbXsrv_connection to smbd_server_connection_terminate*()
[Samba.git] / source4 / heimdal / lib / hcrypto / libtommath / bn_s_mp_mul_high_digs.c
blob2b718f23ccfca6ca88b95e9400edebd7a7ca90bb
1 #include <tommath.h>
2 #ifdef BN_S_MP_MUL_HIGH_DIGS_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 /* multiplies |a| * |b| and does not compute the lower digs digits
19 * [meant to get the higher part of the product]
21 int
22 s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
24 mp_int t;
25 int res, pa, pb, ix, iy;
26 mp_digit u;
27 mp_word r;
28 mp_digit tmpx, *tmpt, *tmpy;
30 /* can we use the fast multiplier? */
31 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
32 if (((a->used + b->used + 1) < MP_WARRAY)
33 && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
34 return fast_s_mp_mul_high_digs (a, b, c, digs);
36 #endif
38 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
39 return res;
41 t.used = a->used + b->used + 1;
43 pa = a->used;
44 pb = b->used;
45 for (ix = 0; ix < pa; ix++) {
46 /* clear the carry */
47 u = 0;
49 /* left hand side of A[ix] * B[iy] */
50 tmpx = a->dp[ix];
52 /* alias to the address of where the digits will be stored */
53 tmpt = &(t.dp[digs]);
55 /* alias for where to read the right hand side from */
56 tmpy = b->dp + (digs - ix);
58 for (iy = digs - ix; iy < pb; iy++) {
59 /* calculate the double precision result */
60 r = ((mp_word)*tmpt) +
61 ((mp_word)tmpx) * ((mp_word)*tmpy++) +
62 ((mp_word) u);
64 /* get the lower part */
65 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
67 /* carry the carry */
68 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
70 *tmpt = u;
72 mp_clamp (&t);
73 mp_exch (&t, c);
74 mp_clear (&t);
75 return MP_OKAY;
77 #endif
79 /* $Source: /cvs/libtom/libtommath/bn_s_mp_mul_high_digs.c,v $ */
80 /* $Revision: 1.4 $ */
81 /* $Date: 2006/12/28 01:25:13 $ */