xref: /netbsd-src/external/lgpl3/gmp/dist/tests/t-count_zeros.c (revision 212397c69a103ae7e5eafa8731ddfae671d2dee7)
1 /* Test count_leading_zeros and count_trailing_zeros.
2 
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  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 #include "longlong.h"
25 #include "tests.h"
26 
27 void
28 check_clz (int want, mp_limb_t n)
29 {
30   int  got;
31   count_leading_zeros (got, n);
32   if (got != want)
33     {
34       printf        ("count_leading_zeros wrong\n");
35       mp_limb_trace ("  n    ", n);
36       printf        ("  want %d\n", want);
37       printf        ("  got  %d\n", got);
38       abort ();
39     }
40 }
41 
42 void
43 check_ctz (int want, mp_limb_t n)
44 {
45   int  got;
46   count_trailing_zeros (got, n);
47   if (got != want)
48     {
49       printf ("count_trailing_zeros wrong\n");
50       mpn_trace ("  n    ", &n, (mp_size_t) 1);
51       printf    ("  want %d\n", want);
52       printf    ("  got  %d\n", got);
53       abort ();
54     }
55 }
56 
57 void
58 check_various (void)
59 {
60   int        i;
61 
62 #ifdef COUNT_LEADING_ZEROS_0
63   check_clz (COUNT_LEADING_ZEROS_0, CNST_LIMB(0));
64 #endif
65 
66   for (i=0; i < GMP_LIMB_BITS; i++)
67     {
68       check_clz (i, CNST_LIMB(1) << (GMP_LIMB_BITS-1-i));
69       check_ctz (i, CNST_LIMB(1) << i);
70 
71       check_ctz (i, MP_LIMB_T_MAX << i);
72       check_clz (i, MP_LIMB_T_MAX >> i);
73     }
74 }
75 
76 
77 int
78 main (int argc, char *argv[])
79 {
80   tests_start ();
81   mp_trace_base = 16;
82 
83   check_various ();
84 
85   tests_end ();
86   exit (0);
87 }
88