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