• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2013 The LibYuv Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS. All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14 
15 #include "../unit_test/unit_test.h"
16 #include "libyuv/basic_types.h"
17 #include "libyuv/cpu_id.h"
18 #include "libyuv/scale.h"
19 #include "libyuv/scale_row.h"
20 
21 namespace libyuv {
22 
TEST_F(LibYUVBaseTest,TestFixedDiv)23 TEST_F(LibYUVBaseTest, TestFixedDiv) {
24   int num[1280];
25   int div[1280];
26   int result_opt[1280];
27   int result_c[1280];
28 
29   EXPECT_EQ(0x10000, libyuv::FixedDiv(1, 1));
30   EXPECT_EQ(0x7fff0000, libyuv::FixedDiv(0x7fff, 1));
31   // TODO(fbarchard): Avoid the following that throw exceptions.
32   // EXPECT_EQ(0x100000000, libyuv::FixedDiv(0x10000, 1));
33   // EXPECT_EQ(0x80000000, libyuv::FixedDiv(0x8000, 1));
34 
35   EXPECT_EQ(0x20000, libyuv::FixedDiv(640 * 2, 640));
36   EXPECT_EQ(0x30000, libyuv::FixedDiv(640 * 3, 640));
37   EXPECT_EQ(0x40000, libyuv::FixedDiv(640 * 4, 640));
38   EXPECT_EQ(0x50000, libyuv::FixedDiv(640 * 5, 640));
39   EXPECT_EQ(0x60000, libyuv::FixedDiv(640 * 6, 640));
40   EXPECT_EQ(0x70000, libyuv::FixedDiv(640 * 7, 640));
41   EXPECT_EQ(0x80000, libyuv::FixedDiv(640 * 8, 640));
42   EXPECT_EQ(0xa0000, libyuv::FixedDiv(640 * 10, 640));
43   EXPECT_EQ(0x20000, libyuv::FixedDiv(960 * 2, 960));
44   EXPECT_EQ(0x08000, libyuv::FixedDiv(640 / 2, 640));
45   EXPECT_EQ(0x04000, libyuv::FixedDiv(640 / 4, 640));
46   EXPECT_EQ(0x20000, libyuv::FixedDiv(1080 * 2, 1080));
47   EXPECT_EQ(0x20000, libyuv::FixedDiv(200000, 100000));
48   EXPECT_EQ(0x18000, libyuv::FixedDiv(150000, 100000));
49   EXPECT_EQ(0x20000, libyuv::FixedDiv(40000, 20000));
50   EXPECT_EQ(0x20000, libyuv::FixedDiv(-40000, -20000));
51   EXPECT_EQ(-0x20000, libyuv::FixedDiv(40000, -20000));
52   EXPECT_EQ(-0x20000, libyuv::FixedDiv(-40000, 20000));
53   EXPECT_EQ(0x10000, libyuv::FixedDiv(4095, 4095));
54   EXPECT_EQ(0x10000, libyuv::FixedDiv(4096, 4096));
55   EXPECT_EQ(0x10000, libyuv::FixedDiv(4097, 4097));
56   EXPECT_EQ(123 * 65536, libyuv::FixedDiv(123, 1));
57 
58   for (int i = 1; i < 4100; ++i) {
59     EXPECT_EQ(0x10000, libyuv::FixedDiv(i, i));
60     EXPECT_EQ(0x20000, libyuv::FixedDiv(i * 2, i));
61     EXPECT_EQ(0x30000, libyuv::FixedDiv(i * 3, i));
62     EXPECT_EQ(0x40000, libyuv::FixedDiv(i * 4, i));
63     EXPECT_EQ(0x08000, libyuv::FixedDiv(i, i * 2));
64     EXPECT_NEAR(16384 * 65536 / i, libyuv::FixedDiv(16384, i), 1);
65   }
66   EXPECT_EQ(123 * 65536, libyuv::FixedDiv(123, 1));
67 
68   MemRandomize(reinterpret_cast<uint8*>(&num[0]), sizeof(num));
69   MemRandomize(reinterpret_cast<uint8*>(&div[0]), sizeof(div));
70   for (int j = 0; j < 1280; ++j) {
71     if (div[j] == 0) {
72       div[j] = 1280;
73     }
74     num[j] &= 0xffff;  // Clamp to avoid divide overflow.
75   }
76   for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
77     for (int j = 0; j < 1280; ++j) {
78       result_opt[j] = libyuv::FixedDiv(num[j], div[j]);
79     }
80   }
81   for (int j = 0; j < 1280; ++j) {
82     result_c[j] = libyuv::FixedDiv_C(num[j], div[j]);
83     EXPECT_NEAR(result_c[j], result_opt[j], 1);
84   }
85 }
86 
TEST_F(LibYUVBaseTest,TestFixedDiv_Opt)87 TEST_F(LibYUVBaseTest, TestFixedDiv_Opt) {
88   int num[1280];
89   int div[1280];
90   int result_opt[1280];
91   int result_c[1280];
92 
93   MemRandomize(reinterpret_cast<uint8*>(&num[0]), sizeof(num));
94   MemRandomize(reinterpret_cast<uint8*>(&div[0]), sizeof(div));
95   for (int j = 0; j < 1280; ++j) {
96     num[j] &= 4095;  // Make numerator smaller.
97     div[j] &= 4095;  // Make divisor smaller.
98     if (div[j] == 0) {
99       div[j] = 1280;
100     }
101   }
102 
103   int has_x86 = TestCpuFlag(kCpuHasX86);
104   for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
105     if (has_x86) {
106       for (int j = 0; j < 1280; ++j) {
107         result_opt[j] = libyuv::FixedDiv(num[j], div[j]);
108       }
109     } else {
110       for (int j = 0; j < 1280; ++j) {
111         result_opt[j] = libyuv::FixedDiv_C(num[j], div[j]);
112       }
113     }
114   }
115   for (int j = 0; j < 1280; ++j) {
116     result_c[j] = libyuv::FixedDiv_C(num[j], div[j]);
117     EXPECT_NEAR(result_c[j], result_opt[j], 1);
118   }
119 }
120 
TEST_F(LibYUVBaseTest,TestFixedDiv1_Opt)121 TEST_F(LibYUVBaseTest, TestFixedDiv1_Opt) {
122   int num[1280];
123   int div[1280];
124   int result_opt[1280];
125   int result_c[1280];
126 
127   MemRandomize(reinterpret_cast<uint8*>(&num[0]), sizeof(num));
128   MemRandomize(reinterpret_cast<uint8*>(&div[0]), sizeof(div));
129   for (int j = 0; j < 1280; ++j) {
130     num[j] &= 4095;  // Make numerator smaller.
131     div[j] &= 4095;  // Make divisor smaller.
132     if (div[j] <= 1) {
133       div[j] = 1280;
134     }
135   }
136 
137   int has_x86 = TestCpuFlag(kCpuHasX86);
138   for (int i = 0; i < benchmark_pixels_div1280_; ++i) {
139     if (has_x86) {
140       for (int j = 0; j < 1280; ++j) {
141         result_opt[j] = libyuv::FixedDiv1(num[j], div[j]);
142       }
143     } else {
144       for (int j = 0; j < 1280; ++j) {
145         result_opt[j] = libyuv::FixedDiv1_C(num[j], div[j]);
146       }
147     }
148   }
149   for (int j = 0; j < 1280; ++j) {
150     result_c[j] = libyuv::FixedDiv1_C(num[j], div[j]);
151     EXPECT_NEAR(result_c[j], result_opt[j], 1);
152   }
153 }
154 
155 }  // namespace libyuv
156