Home | History | Annotate | Line # | Download | only in mpn
t-divrem_1.c revision 1.1.1.1
      1 /* Test mpn_divrem_1 and mpn_preinv_divrem_1.
      2 
      3 Copyright 2003 Free Software Foundation, Inc.
      4 
      5 This file is part of the GNU MP Library.
      6 
      7 The GNU MP Library is free software; you can redistribute it and/or modify
      8 it under the terms of the GNU Lesser General Public License as published by
      9 the Free Software Foundation; either version 3 of the License, or (at your
     10 option) any later version.
     11 
     12 The GNU MP Library is distributed in the hope that it will be useful, but
     13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     15 License for more details.
     16 
     17 You should have received a copy of the GNU Lesser General Public License
     18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     19 
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 
     23 #include "gmp.h"
     24 #include "gmp-impl.h"
     25 #include "tests.h"
     26 
     27 
     28 void
     29 check_data (void)
     30 {
     31   static const struct {
     32     mp_limb_t  n[1];
     33     mp_size_t  nsize;
     34     mp_limb_t  d;
     35     mp_size_t  qxn;
     36     mp_limb_t  want_q[5];
     37     mp_limb_t  want_r;
     38   } data[] = {
     39     { { 0 }, 1, 1, 0,
     40       { 0 }, 0},
     41 
     42     { { 5 }, 1, 2, 0,
     43       { 2 }, 1},
     44 
     45 #if GMP_NUMB_BITS == 32
     46     { { 0x3C }, 1, 0xF2, 1,
     47       { 0x3F789854, 0 }, 0x98 },
     48 #endif
     49 
     50 #if GMP_NUMB_BITS == 64
     51     { { 0x3C }, 1, 0xF2, 1,
     52       { CNST_LIMB(0x3F789854A0CB1B81), 0 }, 0x0E },
     53 
     54     /* This case exposed some wrong code generated by SGI cc on mips64 irix
     55        6.5 with -n32 -O2, in the fractional loop for normalized divisor
     56        using udiv_qrnnd_preinv.  A test "x>al" in one of the sub_ddmmss
     57        expansions came out wrong, leading to an incorrect quotient.  */
     58     { { CNST_LIMB(0x3C00000000000000) }, 1, CNST_LIMB(0xF200000000000000), 1,
     59       { CNST_LIMB(0x3F789854A0CB1B81), 0 }, CNST_LIMB(0x0E00000000000000) },
     60 #endif
     61   };
     62 
     63   mp_limb_t  dinv, got_r, got_q[numberof(data[0].want_q)];
     64   mp_size_t  qsize;
     65   int        i, shift;
     66 
     67   for (i = 0; i < numberof (data); i++)
     68     {
     69       qsize = data[i].nsize + data[i].qxn;
     70       ASSERT_ALWAYS (qsize <= numberof (got_q));
     71 
     72       got_r = mpn_divrem_1 (got_q, data[i].qxn, data[i].n, data[i].nsize,
     73                             data[i].d);
     74       if (got_r != data[i].want_r
     75           || refmpn_cmp (got_q, data[i].want_q, qsize) != 0)
     76         {
     77           printf        ("mpn_divrem_1 wrong at data[%d]\n", i);
     78         bad:
     79           mpn_trace     ("  n", data[i].n, data[i].nsize);
     80           printf        ("  nsize=%ld\n", (long) data[i].nsize);
     81           mp_limb_trace ("  d", data[i].d);
     82           printf        ("  qxn=%ld\n", (long) data[i].qxn);
     83           mpn_trace     ("  want q", data[i].want_q, qsize);
     84           mpn_trace     ("  got  q", got_q, qsize);
     85           mp_limb_trace ("  want r", data[i].want_r);
     86           mp_limb_trace ("  got  r", got_r);
     87           abort ();
     88         }
     89 
     90       /* test if available */
     91 #if USE_PREINV_DIVREM_1 || HAVE_NATIVE_mpn_preinv_divrem_1
     92       shift = refmpn_count_leading_zeros (data[i].d);
     93       dinv = refmpn_invert_limb (data[i].d << shift);
     94       got_r = mpn_preinv_divrem_1 (got_q, data[i].qxn,
     95                                    data[i].n, data[i].nsize,
     96                                    data[i].d, dinv, shift);
     97       if (got_r != data[i].want_r
     98           || refmpn_cmp (got_q, data[i].want_q, qsize) != 0)
     99         {
    100           printf        ("mpn_preinv divrem_1 wrong at data[%d]\n", i);
    101           printf        ("  shift=%d\n", shift);
    102           mp_limb_trace ("  dinv", dinv);
    103           goto bad;
    104         }
    105 #endif
    106     }
    107 }
    108 
    109 int
    110 main (void)
    111 {
    112   tests_start ();
    113   mp_trace_base = -16;
    114 
    115   check_data ();
    116 
    117   tests_end ();
    118   exit (0);
    119 }
    120