mirror of
git://git.gnupg.org/gnupg.git
synced 2024-10-31 20:08:43 +01:00
134 lines
2.9 KiB
C
134 lines
2.9 KiB
C
|
/* mpihelp_udiv_w_sdiv -- implement udiv_qrnnd on machines with only signed
|
||
|
* division.
|
||
|
* Copyright (C) 1992, 1994, 1996, 1998 Free Software Foundation, Inc.
|
||
|
* Contributed by Peter L. Montgomery.
|
||
|
*
|
||
|
* This file is part of GnuPG.
|
||
|
*
|
||
|
* GnuPG is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 2 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* GnuPG is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||
|
*/
|
||
|
|
||
|
#include <config.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include "mpi-internal.h"
|
||
|
#include "longlong.h"
|
||
|
|
||
|
|
||
|
#if 0 /* not yet ported to MPI */
|
||
|
|
||
|
mpi_limb_t
|
||
|
mpihelp_udiv_w_sdiv( mpi_limp_t *rp,
|
||
|
mpi_limp_t *a1,
|
||
|
mpi_limp_t *a0,
|
||
|
mpi_limp_t *d )
|
||
|
{
|
||
|
mp_limb_t q, r;
|
||
|
mp_limb_t c0, c1, b1;
|
||
|
|
||
|
if ((mpi_limb_signed_t) d >= 0)
|
||
|
{
|
||
|
if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
|
||
|
{
|
||
|
/* dividend, divisor, and quotient are nonnegative */
|
||
|
sdiv_qrnnd (q, r, a1, a0, d);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
/* Compute c1*2^32 + c0 = a1*2^32 + a0 - 2^31*d */
|
||
|
sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
|
||
|
/* Divide (c1*2^32 + c0) by d */
|
||
|
sdiv_qrnnd (q, r, c1, c0, d);
|
||
|
/* Add 2^31 to quotient */
|
||
|
q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
b1 = d >> 1; /* d/2, between 2^30 and 2^31 - 1 */
|
||
|
c1 = a1 >> 1; /* A/2 */
|
||
|
c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
|
||
|
|
||
|
if (a1 < b1) /* A < 2^32*b1, so A/2 < 2^31*b1 */
|
||
|
{
|
||
|
sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
|
||
|
|
||
|
r = 2*r + (a0 & 1); /* Remainder from A/(2*b1) */
|
||
|
if ((d & 1) != 0)
|
||
|
{
|
||
|
if (r >= q)
|
||
|
r = r - q;
|
||
|
else if (q - r <= d)
|
||
|
{
|
||
|
r = r - q + d;
|
||
|
q--;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
r = r - q + 2*d;
|
||
|
q -= 2;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (c1 < b1) /* So 2^31 <= (A/2)/b1 < 2^32 */
|
||
|
{
|
||
|
c1 = (b1 - 1) - c1;
|
||
|
c0 = ~c0; /* logical NOT */
|
||
|
|
||
|
sdiv_qrnnd (q, r, c1, c0, b1); /* (A/2) / (d/2) */
|
||
|
|
||
|
q = ~q; /* (A/2)/b1 */
|
||
|
r = (b1 - 1) - r;
|
||
|
|
||
|
r = 2*r + (a0 & 1); /* A/(2*b1) */
|
||
|
|
||
|
if ((d & 1) != 0)
|
||
|
{
|
||
|
if (r >= q)
|
||
|
r = r - q;
|
||
|
else if (q - r <= d)
|
||
|
{
|
||
|
r = r - q + d;
|
||
|
q--;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
r = r - q + 2*d;
|
||
|
q -= 2;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else /* Implies c1 = b1 */
|
||
|
{ /* Hence a1 = d - 1 = 2*b1 - 1 */
|
||
|
if (a0 >= -d)
|
||
|
{
|
||
|
q = -1;
|
||
|
r = a0 + d;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
q = -2;
|
||
|
r = a0 + 2*d;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
*rp = r;
|
||
|
return q;
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|