1 /* mpihelp_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
3 * Copyright (C) 1992, 1994, 1996, 1998 Free Software Foundation, Inc.
4 * Contributed by Peter L. Montgomery.
6 * This file is part of GnuPG.
8 * GnuPG is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * GnuPG is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
23 #include "mpi-internal.h"
27 #if 0 /* not yet ported to MPI */
30 mpihelp_udiv_w_sdiv( mpi_limp_t *rp,
38 if ((mpi_limb_signed_t) d >= 0)
40 if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
42 /* dividend, divisor, and quotient are nonnegative */
43 sdiv_qrnnd (q, r, a1, a0, d);
47 /* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
48 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
49 /* Divide (c1*2^32 + c0) by d */
50 sdiv_qrnnd (q, r, c1, c0, d);
51 /* Add 2^31 to quotient */
52 q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
57 b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
58 c1 = a1 >> 1; /* A/2 */
59 c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
61 if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
63 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
65 r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
82 else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
85 c0 = ~c0; /* logical NOT */
87 sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
89 q = ~q; /* (A/2)/b1 */
92 r = 2*r + (a0 & 1); /* A/(2*b1) */
110 else /* Implies c1 = b1 */
111 { /* Hence a1 = d - 1 = 2*b1 - 1 */