Tizen 2.1 base
[external/gmp.git] / tests / devel / divrem.c
1 /*
2 Copyright 1996, 1997, 1998, 2000, 2001, 2007, 2009 Free Software Foundation,
3 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 #include "gmp.h"
23 #include "gmp-impl.h"
24
25 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
26 #include <time.h>
27
28 int
29 cputime ()
30 {
31   if (CLOCKS_PER_SEC < 100000)
32     return clock () * 1000 / CLOCKS_PER_SEC;
33   return clock () / (CLOCKS_PER_SEC / 1000);
34 }
35 #else
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39
40 int
41 cputime ()
42 {
43   struct rusage rus;
44
45   getrusage (0, &rus);
46   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
47 }
48 #endif
49
50 #define M * 1000000
51
52 #ifndef CLOCK
53 #error "Don't know CLOCK of your machine"
54 #endif
55
56 #ifndef OPS
57 #define OPS 20000000
58 #endif
59 #ifndef SIZE
60 #define SIZE 100
61 #endif
62 #ifndef TIMES
63 #define TIMES OPS/(SIZE+1)
64 #endif
65
66 int
67 main ()
68 {
69   mp_limb_t nptr[2 * SIZE];
70   mp_limb_t dptr[2 * SIZE];
71   mp_limb_t qptr[2 * SIZE];
72   mp_limb_t pptr[2 * SIZE + 1];
73   mp_limb_t rptr[2 * SIZE];
74   mp_size_t nsize, dsize, qsize, rsize, psize;
75   int test;
76   mp_limb_t qlimb;
77
78   for (test = 0; ; test++)
79     {
80       printf ("%d\n", test);
81 #ifdef RANDOM
82       nsize = random () % (2 * SIZE) + 1;
83       dsize = random () % nsize + 1;
84 #else
85       nsize = 2 * SIZE;
86       dsize = SIZE;
87 #endif
88
89       mpn_random2 (nptr, nsize);
90       mpn_random2 (dptr, dsize);
91       dptr[dsize - 1] |= (mp_limb_t) 1 << (GMP_LIMB_BITS - 1);
92
93       MPN_COPY (rptr, nptr, nsize);
94       qlimb = mpn_divrem (qptr, (mp_size_t) 0, rptr, nsize, dptr, dsize);
95       rsize = dsize;
96       qsize = nsize - dsize;
97       qptr[qsize] = qlimb;
98       qsize += qlimb;
99       if (qsize == 0 || qsize > 2 * SIZE)
100         {
101           continue;             /* bogus */
102         }
103       else
104         {
105           mp_limb_t cy;
106           if (qsize > dsize)
107             mpn_mul (pptr, qptr, qsize, dptr, dsize);
108           else
109             mpn_mul (pptr, dptr, dsize, qptr, qsize);
110           psize = qsize + dsize;
111           psize -= pptr[psize - 1] == 0;
112           cy = mpn_add (pptr, pptr, psize, rptr, rsize);
113           pptr[psize] = cy;
114           psize += cy;
115         }
116
117       if (nsize != psize || mpn_cmp (nptr, pptr, nsize) != 0)
118         abort ();
119     }
120 }