1
/* mpih-w-sdiv -- implement udiv_qrnnd on machines with only signed
3
* Copyright (C) 1992, 1994, 1996, 1998, 2002 Free Software Foundation, Inc.
4
* Contributed by Peter L. Montgomery.
6
* This file is part of Libgcrypt.
8
* Libgcrypt is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* Libgcrypt 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 Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26
#include "mpi-internal.h"
30
#if 0 /* not yet ported to MPI */
33
mpihelp_udiv_w_sdiv( mpi_limp_t *rp,
41
if ((mpi_limb_signed_t) d >= 0)
43
if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
45
/* dividend, divisor, and quotient are nonnegative */
46
sdiv_qrnnd (q, r, a1, a0, d);
50
/* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
51
sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
52
/* Divide (c1*2^32 + c0) by d */
53
sdiv_qrnnd (q, r, c1, c0, d);
54
/* Add 2^31 to quotient */
55
q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
60
b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
61
c1 = a1 >> 1; /* A/2 */
62
c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
64
if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
66
sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
68
r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
85
else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
88
c0 = ~c0; /* logical NOT */
90
sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
92
q = ~q; /* (A/2)/b1 */
95
r = 2*r + (a0 & 1); /* A/(2*b1) */
113
else /* Implies c1 = b1 */
114
{ /* Hence a1 = d - 1 = 2*b1 - 1 */