1 ///
2 /// @file  libdivide.cpp
3 /// @brief Test the branchfree divider of libdivide.h
4 ///
5 /// Copyright (C) 2018 Kim Walisch, <kim.walisch@gmail.com>
6 ///
7 /// This file is distributed under the BSD License. See the COPYING
8 /// file in the top level directory.
9 ///
10 
11 #include <libdivide.h>
12 #include <stdint.h>
13 
14 uint64_t dividends[20] =
15 {
16     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 63, 101, 511,
17     1 << 5, 1 << 9, 1 << 20,
18     (uint64_t) ((1ull << 31) - 1),
19     (uint64_t) ((1ull << 63) - 1),
20     (uint32_t) (~0ull),
21     (uint64_t) (~0ull)
22 };
23 
main()24 int main()
25 {
26   for (int i = 0; i < 20; i++)
27   {
28     for (int j = 2; j < 10000; j++)
29     {
30       libdivide::divider<uint64_t, libdivide::BRANCHFREE> fast_d(j);
31       uint64_t res1 = dividends[i] / j;
32       uint64_t res2 = dividends[i] / fast_d;
33 
34       if (res1 != res2)
35         return 1;
36     }
37   }
38 
39   for (int i = 0; i < 20; i++)
40   {
41     for (int j = 2; j < 20; j++)
42     {
43       libdivide::divider<uint64_t, libdivide::BRANCHFREE> fast_d(dividends[j]);
44       uint64_t res1 = dividends[i] / dividends[j];
45       uint64_t res2 = dividends[i] / fast_d;
46 
47       if (res1 != res2)
48         return 1;
49     }
50   }
51 
52   return 0;
53 }
54