mpi-genesis 1
mpi-genesis 2 * division.
mpi-genesis 3 * Copyright (C) 1992, 1994, 1996, 1998 Free Software Foundation, Inc.
mpi-genesis 4 * Contributed by Peter L. Montgomery.
mpi-genesis 5 *
mpi-genesis 6 * This file is part of GnuPG.
mpi-genesis 7 *
mpi-genesis 8 * GnuPG is free software; you can redistribute it and/or modify
mpi-genesis 9 * it under the terms of the GNU General Public License as published by
mpi-genesis 10 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 11 * (at your option) any later version.
mpi-genesis 12 *
mpi-genesis 13 * GnuPG is distributed in the hope that it will be useful,
mpi-genesis 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 16 * GNU General Public License for more details.
mpi-genesis 17 *
mpi-genesis 18 * You should have received a copy of the GNU General Public License
mpi-genesis 19 * along with this program; if not, see <http:
mpi-genesis 20 */
mpi-genesis 21
mpi-genesis 22 #include <config.h>
mpi-genesis 23 #include <stdio.h>
mpi-genesis 24 #include <stdlib.h>
mpi-genesis 25 #include "mpi-internal.h"
mpi-genesis 26 #include "longlong.h"
mpi-genesis 27
mpi-genesis 28
mpi-genesis 29 #if 0 /* not yet ported to MPI */
mpi-genesis 30
mpi-genesis 31 mpi_limb_t
mpi-genesis 32 mpihelp_udiv_w_sdiv( mpi_limp_t *rp,
mpi-genesis 33 mpi_limp_t *a1,
mpi-genesis 34 mpi_limp_t *a0,
mpi-genesis 35 mpi_limp_t *d )
mpi-genesis 36 {
mpi-genesis 37 mp_limb_t q, r;
mpi-genesis 38 mp_limb_t c0, c1, b1;
mpi-genesis 39
mpi-genesis 40 if ((mpi_limb_signed_t) d >= 0)
mpi-genesis 41 {
mpi-genesis 42 if (a1 < d - a1 - (a0 >> (BITS_PER_MP_LIMB - 1)))
mpi-genesis 43 {
mpi-genesis 44
mpi-genesis 45 sdiv_qrnnd (q, r, a1, a0, d);
mpi-genesis 46 }
mpi-genesis 47 else
mpi-genesis 48 {
mpi-genesis 49
mpi-genesis 50 sub_ddmmss (c1, c0, a1, a0, d >> 1, d << (BITS_PER_MP_LIMB - 1));
mpi-genesis 51
mpi-genesis 52 sdiv_qrnnd (q, r, c1, c0, d);
mpi-genesis 53
mpi-genesis 54 q += (mp_limb_t) 1 << (BITS_PER_MP_LIMB - 1);
mpi-genesis 55 }
mpi-genesis 56 }
mpi-genesis 57 else
mpi-genesis 58 {
mpi-genesis 59 b1 = d >> 1;
mpi-genesis 60 c1 = a1 >> 1;
mpi-genesis 61 c0 = (a1 << (BITS_PER_MP_LIMB - 1)) + (a0 >> 1);
mpi-genesis 62
mpi-genesis 63 if (a1 < b1)
mpi-genesis 64 {
mpi-genesis 65 sdiv_qrnnd (q, r, c1, c0, b1);
mpi-genesis 66
mpi-genesis 67 r = 2*r + (a0 & 1);
mpi-genesis 68 if ((d & 1) != 0)
mpi-genesis 69 {
mpi-genesis 70 if (r >= q)
mpi-genesis 71 r = r - q;
mpi-genesis 72 else if (q - r <= d)
mpi-genesis 73 {
mpi-genesis 74 r = r - q + d;
mpi-genesis 75 q--;
mpi-genesis 76 }
mpi-genesis 77 else
mpi-genesis 78 {
mpi-genesis 79 r = r - q + 2*d;
mpi-genesis 80 q -= 2;
mpi-genesis 81 }
mpi-genesis 82 }
mpi-genesis 83 }
mpi-genesis 84 else if (c1 < b1)
mpi-genesis 85 {
mpi-genesis 86 c1 = (b1 - 1) - c1;
mpi-genesis 87 c0 = ~c0;
mpi-genesis 88
mpi-genesis 89 sdiv_qrnnd (q, r, c1, c0, b1);
mpi-genesis 90
mpi-genesis 91 q = ~q;
mpi-genesis 92 r = (b1 - 1) - r;
mpi-genesis 93
mpi-genesis 94 r = 2*r + (a0 & 1);
mpi-genesis 95
mpi-genesis 96 if ((d & 1) != 0)
mpi-genesis 97 {
mpi-genesis 98 if (r >= q)
mpi-genesis 99 r = r - q;
mpi-genesis 100 else if (q - r <= d)
mpi-genesis 101 {
mpi-genesis 102 r = r - q + d;
mpi-genesis 103 q--;
mpi-genesis 104 }
mpi-genesis 105 else
mpi-genesis 106 {
mpi-genesis 107 r = r - q + 2*d;
mpi-genesis 108 q -= 2;
mpi-genesis 109 }
mpi-genesis 110 }
mpi-genesis 111 }
mpi-genesis 112 else
mpi-genesis 113 {
mpi-genesis 114 if (a0 >= -d)
mpi-genesis 115 {
mpi-genesis 116 q = -1;
mpi-genesis 117 r = a0 + d;
mpi-genesis 118 }
mpi-genesis 119 else
mpi-genesis 120 {
mpi-genesis 121 q = -2;
mpi-genesis 122 r = a0 + 2*d;
mpi-genesis 123 }
mpi-genesis 124 }
mpi-genesis 125 }
mpi-genesis 126
mpi-genesis 127 *rp = r;
mpi-genesis 128 return q;
mpi-genesis 129 }
mpi-genesis 130
mpi-genesis 131 #endif
mpi-genesis 132