source: trunk/third/gmp/tests/t-count_zeros.c @ 18191

Revision 18191, 2.1 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* Test count_leading_zeros and count_trailing_zeros.
2
3Copyright 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include "gmp.h"
25#include "gmp-impl.h"
26#include "longlong.h"
27#include "tests.h"
28
29void
30check_clz (int want, mp_limb_t n)
31{
32  int  got;
33  count_leading_zeros (got, n);
34  if (got != want)
35    {
36      printf ("count_leading_zeros wrong\n");
37      printf    ("  n    %lX\n", n);
38      printf    ("  want %d\n", want);
39      printf    ("  got  %d\n", got);
40      abort ();
41    }
42}
43
44void
45check_ctz (int want, mp_limb_t n)
46{
47  int  got;
48  count_trailing_zeros (got, n);
49  if (got != want)
50    {
51      printf ("count_trailing_zeros wrong\n");
52      mpn_trace ("  n    ", &n, (mp_size_t) 1);
53      printf    ("  want %d\n", want);
54      printf    ("  got  %d\n", got);
55      abort ();
56    }
57}
58
59void
60check_various (void)
61{
62  mp_limb_t  n;
63  int        i;
64
65#ifdef COUNT_LEADING_ZEROS_0
66  check_clz (COUNT_LEADING_ZEROS_0, CNST_LIMB(0));
67#endif
68
69  for (n=1, i=0; i < BITS_PER_MP_LIMB; n<<=1, i++)
70    {
71      check_clz (i, CNST_LIMB(1) << (BITS_PER_MP_LIMB-1-i));
72      check_ctz (i, CNST_LIMB(1) << i);
73
74      check_ctz (i, MP_LIMB_T_MAX << i);
75      check_clz (i, MP_LIMB_T_MAX >> i);
76    }
77}
78
79
80int
81main (int argc, char *argv[])
82{
83  tests_start ();
84  mp_trace_base = 16;
85
86  check_various ();
87
88  tests_end ();
89  exit (0);
90}
Note: See TracBrowser for help on using the repository browser.