VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTNoCrt-5.cpp@ 98496

Last change on this file since 98496 was 98496, checked in by vboxsync, 2 years ago

IPRT/vcc: Working on 64-bit integer support routines for the 32-bit Visual C++ compilers. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.5 KB
Line 
1/* $Id: tstRTNoCrt-5.cpp 98496 2023-02-07 15:56:27Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Testcase for the No-CRT 64-bit integer support.
4 */
5
6/*
7 * Copyright (C) 2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/uint64.h>
42#include <iprt/test.h>
43#include <iprt/string.h>
44#include <iprt/rand.h>
45
46
47/*********************************************************************************************************************************
48* Defined Constants And Macros *
49*********************************************************************************************************************************/
50#define RANDOM_LOOPS _256K
51
52
53/*********************************************************************************************************************************
54* Structures and Typedefs *
55*********************************************************************************************************************************/
56typedef struct TSTRTNOCRT5SHIFT
57{
58 uint64_t uValue;
59 uint8_t cShift;
60 uint64_t uExpected;
61} TSTRTNOCRT5SHIFT;
62
63typedef struct TSTRTNOCRT5MULT
64{
65 uint64_t uFactor1, uFactor2;
66 uint64_t uExpected;
67} TSTRTNOCRT5MULT;
68
69typedef struct TSTRTNOCRT5DIV
70{
71 uint64_t uDividend, uDivisor;
72 uint64_t uQuotient, uReminder;
73} TSTRTNOCRT5DIV;
74
75
76/*********************************************************************************************************************************
77* Global Variables *
78*********************************************************************************************************************************/
79RTTEST g_hTest;
80
81TSTRTNOCRT5SHIFT volatile const g_aShiftRight[] =
82{
83 { UINT64_C(0x8e7e6e5e4e3e2e1e), 0, UINT64_C(0x8e7e6e5e4e3e2e1e) },
84 { UINT64_C(0x8e7e6e5e4e3e2e1e), 8, UINT64_C(0x008e7e6e5e4e3e2e) },
85 { UINT64_C(0x8e7e6e5e4e3e2e1e), 16, UINT64_C(0x00008e7e6e5e4e3e) },
86 { UINT64_C(0x8e7e6e5e4e3e2e1e), 24, UINT64_C(0x0000008e7e6e5e4e) },
87 { UINT64_C(0x8e7e6e5e4e3e2e1e), 28, UINT64_C(0x00000008e7e6e5e4) },
88 { UINT64_C(0x8e7e6e5e4e3e2e1e), 32, UINT64_C(0x000000008e7e6e5e) },
89 { UINT64_C(0x8e7e6e5e4e3e2e1e), 36, UINT64_C(0x0000000008e7e6e5) },
90 { UINT64_C(0x8e7e6e5e4e3e2e1e), 40, UINT64_C(0x00000000008e7e6e) },
91 { UINT64_C(0x8e7e6e5e4e3e2e1e), 44, UINT64_C(0x000000000008e7e6) },
92 { UINT64_C(0x8e7e6e5e4e3e2e1e), 48, UINT64_C(0x0000000000008e7e) },
93 { UINT64_C(0x8e7e6e5e4e3e2e1e), 52, UINT64_C(0x00000000000008e7) },
94 { UINT64_C(0x8e7e6e5e4e3e2e1e), 56, UINT64_C(0x000000000000008e) },
95 { UINT64_C(0x8e7e6e5e4e3e2e1e), 60, UINT64_C(0x0000000000000008) },
96 { UINT64_C(0x8e7e6e5e4e3e2e1e), 64, UINT64_C(0x0000000000000000) },
97 { UINT64_C(0x8e7e6e5e4e3e2e1e), 65, UINT64_C(0x0000000000000000) },
98 { UINT64_C(0x8e7e6e5e4e3e2e1e), 99, UINT64_C(0x0000000000000000) },
99 { UINT64_C(0x8e7e6e5e4e3e2e1e), 127, UINT64_C(0x0000000000000000) },
100 { UINT64_C(0x8e7e6e5e4e3e2e1e), 132, UINT64_C(0x0000000000000000) },
101 { UINT64_C(0x8e7e6e5e4e3e2e1e), 255, UINT64_C(0x0000000000000000) },
102};
103
104
105static void tstShiftRight()
106{
107 RTTestSub(g_hTest, "64-bit unsigned shift right");
108
109 /* static tests from array. */
110 for (size_t i = 0; i < RT_ELEMENTS(g_aShiftRight); i++)
111 {
112 uint64_t const uResult = g_aShiftRight[i].uValue >> g_aShiftRight[i].cShift;
113 if (uResult != g_aShiftRight[i].uExpected)
114 RTTestFailed(g_hTest, "i=%u uValue=%#018RX64 SHR %u => %#018RX64, expected %#018RX64",
115 i, g_aShiftRight[i].uValue, g_aShiftRight[i].cShift, uResult, g_aShiftRight[i].uExpected);
116 }
117
118 /* Random values via uint64. */
119 for (size_t i = 0; i < RANDOM_LOOPS; i++)
120 {
121 uint64_t const uValue = RTRandU64();
122 uint8_t const cShift = (uint8_t)RTRandU32Ex(0, i & 3 ? 63 : 255);
123 uint64_t const uResult = uValue >> cShift;
124 RTUINT64U uExpected = { uValue };
125 if (cShift <= 63)
126 RTUInt64AssignShiftRight(&uExpected, cShift);
127 else
128 uExpected.u = 0;
129 if (uResult != uExpected.u)
130 RTTestFailed(g_hTest, "uValue=%#018RX64 SHR %u => %#018RX64, expected %#018RX64", uValue, cShift, uResult, uExpected.u);
131 }
132}
133
134
135TSTRTNOCRT5SHIFT volatile const g_aShiftSignedRight[] =
136{
137 { UINT64_C(0x8e7e6e5e4e3e2e1e), 0, UINT64_C(0x8e7e6e5e4e3e2e1e) },
138 { UINT64_C(0x8e7e6e5e4e3e2e1e), 8, UINT64_C(0xff8e7e6e5e4e3e2e) },
139 { UINT64_C(0x8e7e6e5e4e3e2e1e), 16, UINT64_C(0xffff8e7e6e5e4e3e) },
140 { UINT64_C(0x8e7e6e5e4e3e2e1e), 24, UINT64_C(0xffffff8e7e6e5e4e) },
141 { UINT64_C(0x8e7e6e5e4e3e2e1e), 28, UINT64_C(0xfffffff8e7e6e5e4) },
142 { UINT64_C(0x8e7e6e5e4e3e2e1e), 32, UINT64_C(0xffffffff8e7e6e5e) },
143 { UINT64_C(0x8e7e6e5e4e3e2e1e), 36, UINT64_C(0xfffffffff8e7e6e5) },
144 { UINT64_C(0x8e7e6e5e4e3e2e1e), 40, UINT64_C(0xffffffffff8e7e6e) },
145 { UINT64_C(0x8e7e6e5e4e3e2e1e), 44, UINT64_C(0xfffffffffff8e7e6) },
146 { UINT64_C(0x8e7e6e5e4e3e2e1e), 48, UINT64_C(0xffffffffffff8e7e) },
147 { UINT64_C(0x8e7e6e5e4e3e2e1e), 52, UINT64_C(0xfffffffffffff8e7) },
148 { UINT64_C(0x8e7e6e5e4e3e2e1e), 56, UINT64_C(0xffffffffffffff8e) },
149 { UINT64_C(0x8e7e6e5e4e3e2e1e), 60, UINT64_C(0xfffffffffffffff8) },
150 { UINT64_C(0x8e7e6e5e4e3e2e1e), 64, UINT64_C(0xffffffffffffffff) },
151 { UINT64_C(0x8e7e6e5e4e3e2e1e), 65, UINT64_C(0xffffffffffffffff) },
152 { UINT64_C(0x8e7e6e5e4e3e2e1e), 99, UINT64_C(0xffffffffffffffff) },
153 { UINT64_C(0x8e7e6e5e4e3e2e1e), 127, UINT64_C(0xffffffffffffffff) },
154 { UINT64_C(0x8e7e6e5e4e3e2e1e), 132, UINT64_C(0xffffffffffffffff) },
155 { UINT64_C(0x8e7e6e5e4e3e2e1e), 255, UINT64_C(0xffffffffffffffff) },
156
157 { UINT64_C(0x7e8e6e5e4e3e2e1e), 0, UINT64_C(0x7e8e6e5e4e3e2e1e) },
158 { UINT64_C(0x7e8e6e5e4e3e2e1e), 8, UINT64_C(0x007e8e6e5e4e3e2e) },
159 { UINT64_C(0x7e8e6e5e4e3e2e1e), 16, UINT64_C(0x00007e8e6e5e4e3e) },
160 { UINT64_C(0x7e8e6e5e4e3e2e1e), 24, UINT64_C(0x0000007e8e6e5e4e) },
161 { UINT64_C(0x7e8e6e5e4e3e2e1e), 28, UINT64_C(0x00000007e8e6e5e4) },
162 { UINT64_C(0x7e8e6e5e4e3e2e1e), 32, UINT64_C(0x000000007e8e6e5e) },
163 { UINT64_C(0x7e8e6e5e4e3e2e1e), 36, UINT64_C(0x0000000007e8e6e5) },
164 { UINT64_C(0x7e8e6e5e4e3e2e1e), 40, UINT64_C(0x00000000007e8e6e) },
165 { UINT64_C(0x7e8e6e5e4e3e2e1e), 44, UINT64_C(0x000000000007e8e6) },
166 { UINT64_C(0x7e8e6e5e4e3e2e1e), 48, UINT64_C(0x0000000000007e8e) },
167 { UINT64_C(0x7e8e6e5e4e3e2e1e), 52, UINT64_C(0x00000000000007e8) },
168 { UINT64_C(0x7e8e6e5e4e3e2e1e), 56, UINT64_C(0x000000000000007e) },
169 { UINT64_C(0x7e8e6e5e4e3e2e1e), 60, UINT64_C(0x0000000000000007) },
170 { UINT64_C(0x7e8e6e5e4e3e2e1e), 64, UINT64_C(0x0000000000000000) },
171 { UINT64_C(0x7e8e6e5e4e3e2e1e), 65, UINT64_C(0x0000000000000000) },
172 { UINT64_C(0x7e8e6e5e4e3e2e1e), 99, UINT64_C(0x0000000000000000) },
173 { UINT64_C(0x7e8e6e5e4e3e2e1e), 127, UINT64_C(0x0000000000000000) },
174 { UINT64_C(0x7e8e6e5e4e3e2e1e), 132, UINT64_C(0x0000000000000000) },
175 { UINT64_C(0x7e8e6e5e4e3e2e1e), 255, UINT64_C(0x0000000000000000) },
176};
177
178static void tstShiftRightArithmetic()
179{
180 RTTestSub(g_hTest, "64-bit signed shift right");
181
182 /* static tests from array. */
183 for (size_t i = 0; i < RT_ELEMENTS(g_aShiftSignedRight); i++)
184 {
185 int64_t const iResult = (int64_t)g_aShiftSignedRight[i].uValue >> g_aShiftSignedRight[i].cShift;
186 if ((uint64_t)iResult != g_aShiftSignedRight[i].uExpected)
187 RTTestFailed(g_hTest, "i=%u iValue=%#018RX64 SAR %u => %#018RX64, expected %#018RX64", i, g_aShiftSignedRight[i].uValue,
188 g_aShiftSignedRight[i].cShift, iResult, g_aShiftSignedRight[i].uExpected);
189 }
190
191 /* Random values via uint64. */
192 for (size_t i = 0; i < RANDOM_LOOPS; i++)
193 {
194 uint64_t const uValue = RTRandU64();
195 uint8_t const cShift = (uint8_t)RTRandU32Ex(0, i & 3 ? 63 : 255);
196 int64_t const iResult = (int64_t)uValue >> cShift;
197 RTUINT64U uExpected = { uValue };
198 if (cShift > 63)
199 uExpected.u = RT_BIT_64(63) & uValue ? UINT64_MAX : 0;
200 else
201 {
202 RTUInt64AssignShiftRight(&uExpected, cShift);
203 if (RT_BIT_64(63) & uValue)
204 uExpected.u |= UINT64_MAX << (63 - cShift);
205 }
206 if ((uint64_t)iResult != uExpected.u)
207 RTTestFailed(g_hTest, "uValue=%#018RX64 SHR %u => %#018RX64, expected %#018RX64", uValue, cShift, iResult, uExpected.u);
208 }
209}
210
211TSTRTNOCRT5SHIFT volatile const g_aShiftLeft[] =
212{
213 { UINT64_C(0x8e7d6c5e4a3e2b1e), 0, UINT64_C(0x8e7d6c5e4a3e2b1e) },
214 { UINT64_C(0x8e7d6c5e4a3e2b1e), 8, UINT64_C(0x7d6c5e4a3e2b1e00) },
215 { UINT64_C(0x8e7d6c5e4a3e2b1e), 16, UINT64_C(0x6c5e4a3e2b1e0000) },
216 { UINT64_C(0x8e7d6c5e4a3e2b1e), 24, UINT64_C(0x5e4a3e2b1e000000) },
217 { UINT64_C(0x8e7d6c5e4a3e2b1e), 28, UINT64_C(0xe4a3e2b1e0000000) },
218 { UINT64_C(0x8e7d6c5e4a3e2b1e), 32, UINT64_C(0x4a3e2b1e00000000) },
219 { UINT64_C(0x8e7d6c5e4a3e2b1e), 36, UINT64_C(0xa3e2b1e000000000) },
220 { UINT64_C(0x8e7d6c5e4a3e2b1e), 40, UINT64_C(0x3e2b1e0000000000) },
221 { UINT64_C(0x8e7d6c5e4a3e2b1e), 44, UINT64_C(0xe2b1e00000000000) },
222 { UINT64_C(0x8e7d6c5e4a3e2b1e), 48, UINT64_C(0x2b1e000000000000) },
223 { UINT64_C(0x8e7d6c5e4a3e2b1e), 52, UINT64_C(0xb1e0000000000000) },
224 { UINT64_C(0x8e7d6c5e4a3e2b1e), 56, UINT64_C(0x1e00000000000000) },
225 { UINT64_C(0x8e7d6c5e4a3e2b1e), 60, UINT64_C(0xe000000000000000) },
226 { UINT64_C(0x8e7d6c5e4a3e2b1e), 64, UINT64_C(0x0000000000000000) },
227 { UINT64_C(0x8e7d6c5e4a3e2b1e), 65, UINT64_C(0x0000000000000000) },
228 { UINT64_C(0x8e7d6c5e4a3e2b1e), 99, UINT64_C(0x0000000000000000) },
229 { UINT64_C(0x8e7d6c5e4a3e2b1e), 127, UINT64_C(0x0000000000000000) },
230 { UINT64_C(0x8e7d6c5e4a3e2b1e), 132, UINT64_C(0x0000000000000000) },
231 { UINT64_C(0x8e7d6c5e4a3e2b1e), 255, UINT64_C(0x0000000000000000) },
232};
233
234static void tstShiftLeft()
235{
236 RTTestSub(g_hTest, "64-bit shift left");
237
238 /* static tests from array. */
239 for (size_t i = 0; i < RT_ELEMENTS(g_aShiftLeft); i++)
240 {
241 uint64_t const uResult = g_aShiftLeft[i].uValue << g_aShiftLeft[i].cShift;
242 if (uResult != g_aShiftLeft[i].uExpected)
243 RTTestFailed(g_hTest, "i=%u iValue=%#018RX64 SHL %u => %#018RX64, expected %#018RX64", i, g_aShiftLeft[i].uValue,
244 g_aShiftLeft[i].cShift, uResult, g_aShiftLeft[i].uExpected);
245 }
246
247 for (size_t i = 0; i < RT_ELEMENTS(g_aShiftLeft); i++)
248 {
249 int64_t const iResult = (int64_t)g_aShiftLeft[i].uValue << g_aShiftLeft[i].cShift;
250 if ((uint64_t)iResult != g_aShiftLeft[i].uExpected)
251 RTTestFailed(g_hTest, "i=%u iValue=%#018RX64 SHL %u => %#018RX64, expected %#018RX64 [signed]", i, g_aShiftLeft[i].uValue,
252 g_aShiftLeft[i].cShift, iResult, g_aShiftLeft[i].uExpected);
253 }
254
255 /* Random values via uint64. */
256 for (size_t i = 0; i < RANDOM_LOOPS; i++)
257 {
258 uint64_t const uValue = RTRandU64();
259 uint8_t const cShift = (uint8_t)RTRandU32Ex(0, i & 3 ? 63 : 255);
260 uint64_t const uResult = uValue << cShift;
261 RTUINT64U uExpected = { uValue };
262 if (cShift > 63)
263 uExpected.u = 0;
264 else
265 RTUInt64AssignShiftLeft(&uExpected, cShift);
266
267 if (uResult != uExpected.u)
268 RTTestFailed(g_hTest, "uValue=%#018RX64 SHR %u => %#018RX64, expected %#018RX64", uValue, cShift, uResult, uExpected.u);
269 }
270
271 for (size_t i = 0; i < RANDOM_LOOPS; i++)
272 {
273 uint64_t const uValue = RTRandU64();
274 uint8_t const cShift = (uint8_t)RTRandU32Ex(0, i & 3 ? 63 : 255);
275 int64_t const iResult = (int64_t)uValue << cShift;
276 RTUINT64U uExpected = { uValue };
277 if (cShift > 63)
278 uExpected.u = 0;
279 else
280 RTUInt64AssignShiftLeft(&uExpected, cShift);
281
282 if ((uint64_t)iResult != uExpected.u)
283 RTTestFailed(g_hTest, "uValue=%#018RX64 SHR %u => %#018RX64, expected %#018RX64", uValue, cShift, iResult, uExpected.u);
284 }
285}
286
287
288TSTRTNOCRT5MULT volatile const g_aMult[] =
289{
290 { UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000), /* => */ UINT64_C(0x0000000000000000) },
291 { UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0x0000000000000001) },
292 { UINT64_C(0x000000001203879f), UINT64_C(0x0000000094585638), /* => */ UINT64_C(0x0A7041AFAAFD14C8) },
293 { UINT64_C(0x0000000100000000), UINT64_C(0x0000000010329484), /* => */ UINT64_C(0x1032948400000000) },
294 { UINT64_C(0x0958609457ad0f03), UINT64_C(0x8f9d0a07d9f83145), /* => */ UINT64_C(0xC77D9538D76C9ECF) },
295};
296
297static void tstMultiplication()
298{
299 RTTestSub(g_hTest, "64-bit multiplication");
300
301 /* static tests from array. */
302 for (size_t i = 0; i < RT_ELEMENTS(g_aMult); i++)
303 {
304 uint64_t const uResult = g_aMult[i].uFactor1 * g_aMult[i].uFactor2;
305 if (uResult != g_aMult[i].uExpected)
306 RTTestFailed(g_hTest, "i=%u %#018RX64 * %#018RX64 => %#018RX64, expected %#018RX64",
307 i, g_aMult[i].uFactor1, g_aMult[i].uFactor2, uResult, g_aMult[i].uExpected);
308 }
309 for (size_t i = 0; i < RT_ELEMENTS(g_aMult); i++)
310 {
311 uint64_t const uResult = g_aMult[i].uFactor2 * g_aMult[i].uFactor1;
312 if (uResult != g_aMult[i].uExpected)
313 RTTestFailed(g_hTest, "i=%u %#018RX64 * %#018RX64 => %#018RX64, expected %#018RX64 (f2*f1)",
314 i, g_aMult[i].uFactor2, g_aMult[i].uFactor1, uResult, g_aMult[i].uExpected);
315 }
316 for (size_t i = 0; i < RT_ELEMENTS(g_aMult); i++)
317 {
318 int64_t const iResult = (int64_t)g_aMult[i].uFactor1 * (int64_t)g_aMult[i].uFactor2;
319 if ((uint64_t)iResult != g_aMult[i].uExpected)
320 RTTestFailed(g_hTest, "i=%u %#018RX64 * %#018RX64 => %#018RX64, expected %#018RX64 (signed)",
321 i, g_aMult[i].uFactor1, g_aMult[i].uFactor2, iResult, g_aMult[i].uExpected);
322 }
323
324 /* Random values via uint64. */
325 for (size_t i = 0; i < RANDOM_LOOPS; i++)
326 {
327 RTUINT64U const uFactor1 = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
328 RTUINT64U const uFactor2 = { RTRandU64Ex(0, i & 3 ? UINT64_MAX : UINT32_MAX) };
329 uint64_t const uResult = uFactor1.u * uFactor2.u;
330 RTUINT64U uExpected;
331 RTUInt64Mul(&uExpected, &uFactor1, &uFactor2);
332
333 if (uResult != uExpected.u)
334 RTTestFailed(g_hTest, "%#018RX64 * %#018RX64 => %#018RX64, expected %#018RX64", uFactor1.u, uFactor2.u, uResult, uExpected.u);
335 }
336 for (size_t i = 0; i < RANDOM_LOOPS; i++)
337 {
338 RTUINT64U const uFactor1 = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
339 RTUINT64U const uFactor2 = { RTRandU64Ex(0, i & 3 ? UINT64_MAX : UINT32_MAX) };
340 int64_t const iResult = (int64_t)uFactor1.u * (int64_t)uFactor2.u;
341 RTUINT64U uExpected;
342 RTUInt64Mul(&uExpected, &uFactor1, &uFactor2);
343
344 if ((uint64_t)iResult != uExpected.u)
345 RTTestFailed(g_hTest, "%#018RX64 * %#018RX64 => %#018RX64, expected %#018RX64 (signed)",
346 uFactor1.u, uFactor2.u, iResult, uExpected.u);
347 }
348}
349
350
351TSTRTNOCRT5DIV volatile const g_aDivU[] =
352{
353 { UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000) },
354 { UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000) },
355 { UINT64_C(0x0000000000000002), UINT64_C(0x0000000000000002), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000) },
356 { UINT64_C(0x0000000000000003), UINT64_C(0x0000000000000002), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001) },
357 { UINT64_C(0x0000000009821348), UINT64_C(0x0000000023949583), /* => */ UINT64_C(0x0000000000000000), UINT64_C(0x0000000009821348) },
358 { UINT64_C(0x0000000079821348), UINT64_C(0x0000000023949583), /* => */ UINT64_C(0x0000000000000003), UINT64_C(0x000000000EC452BF) },
359 { UINT64_C(0x0439583044583049), UINT64_C(0x0984987539485732), /* => */ UINT64_C(0x0000000000000000), UINT64_C(0x0439583044583049) },
360 { UINT64_C(0xf439583044583049), UINT64_C(0x0984987539485732), /* => */ UINT64_C(0x0000000000000019), UINT64_C(0x064674BDAC47AC67) },
361 { UINT64_C(0xdf8305930df94306), UINT64_C(0x00000043d9dfa039), /* => */ UINT64_C(0x00000000034B4D9D), UINT64_C(0x0000000990EFDB11) },
362 { UINT64_C(0xff9f939d0f0302d9), UINT64_C(0x0000000000000042), /* => */ UINT64_C(0x03DF823C8FBE1B31), UINT64_C(0x0000000000000037) },
363};
364
365static void tstUnsignedDivision()
366{
367 RTTestSub(g_hTest, "64-bit unsigned division");
368
369 /*
370 * static tests from array.
371 */
372 for (size_t i = 0; i < RT_ELEMENTS(g_aDivU); i++)
373 {
374 uint64_t const uResult = g_aDivU[i].uDividend / g_aDivU[i].uDivisor; /* aulldiv */
375 if (uResult != g_aDivU[i].uQuotient)
376 RTTestFailed(g_hTest, "i=%u %#018RX64 / %#018RX64 => %#018RX64, expected %#018RX64",
377 i, g_aDivU[i].uDividend, g_aDivU[i].uDivisor, uResult, g_aDivU[i].uQuotient);
378 }
379 for (size_t i = 0; i < RT_ELEMENTS(g_aDivU); i++)
380 {
381 uint64_t const uResult = g_aDivU[i].uDividend % g_aDivU[i].uDivisor; /* aullrem */
382 if (uResult != g_aDivU[i].uReminder)
383 RTTestFailed(g_hTest, "i=%u %#018RX64 %% %#018RX64 => %#018RX64, expected %#018RX64",
384 i, g_aDivU[i].uDividend, g_aDivU[i].uDivisor, uResult, g_aDivU[i].uReminder);
385 }
386 for (size_t i = 0; i < RT_ELEMENTS(g_aDivU); i++)
387 {
388 uint64_t const uDividend = g_aDivU[i].uDividend;
389 uint64_t const uDivisor = g_aDivU[i].uDivisor;
390 uint64_t const uQuotient = uDividend / uDivisor; /* auldvrm hopefully - only not in unoptimized builds. */
391 uint64_t const uReminder = uDividend % uDivisor;
392 if ( uQuotient != g_aDivU[i].uQuotient
393 || uReminder != g_aDivU[i].uReminder)
394 RTTestFailed(g_hTest, "i=%u %#018RX64 / %#018RX64 => q=%#018RX64 r=%#018RX64, expected q=%#018RX64 r=%#018RX64",
395 i, g_aDivU[i].uDividend, g_aDivU[i].uDivisor,
396 uQuotient, uReminder, g_aDivU[i].uQuotient, g_aDivU[i].uReminder);
397 }
398
399 /*
400 * Same but with random values via uint64.
401 */
402 for (size_t i = 0; i < RANDOM_LOOPS; i++)
403 {
404 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
405 RTUINT64U const uDivisor = { RTRandU64Ex(0, i & 3 ? UINT64_MAX : UINT32_MAX) };
406 uint64_t const uResult = uDividend.u / uDivisor.u;
407 RTUINT64U uExpected;
408 RTUInt64Div(&uExpected, &uDividend, &uDivisor);
409 if (uResult != uExpected.u)
410 RTTestFailed(g_hTest, "%#018RX64 / %#018RX64 => %#018RX64, expected %#018RX64", uDividend.u, uDivisor.u, uResult, uExpected.u);
411 }
412 for (size_t i = 0; i < RANDOM_LOOPS; i++)
413 {
414 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
415 RTUINT64U const uDivisor = { RTRandU64Ex(0, i & 3 ? UINT64_MAX : UINT32_MAX) };
416 uint64_t const uResult = uDividend.u % uDivisor.u;
417 RTUINT64U uExpected;
418 RTUInt64Mod(&uExpected, &uDividend, &uDivisor);
419 if (uResult != uExpected.u)
420 RTTestFailed(g_hTest, "%#018RX64 %% %#018RX64 => %#018RX64, expected %#018RX64", uDividend.u, uDivisor.u, uResult, uExpected.u);
421 }
422 for (size_t i = 0; i < RANDOM_LOOPS; i++)
423 {
424 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
425 RTUINT64U const uDivisor = { RTRandU64Ex(0, i & 3 ? UINT64_MAX : UINT32_MAX) };
426 uint64_t const uReminder = uDividend.u % uDivisor.u;
427 uint64_t const uQuotient = uDividend.u / uDivisor.u;
428 RTUINT64U uExpectedQ, uExpectedR;
429 RTUInt64DivRem(&uExpectedQ, &uExpectedR, &uDividend, &uDivisor);
430 if ( uQuotient != uExpectedQ.u
431 || uReminder != uExpectedR.u)
432 RTTestFailed(g_hTest, "%#018RX64 / %#018RX64 => q=%#018RX64 r=%#018RX64, expected q=%#018RX64 r=%#018RX64",
433 uDividend.u, uDivisor.u, uQuotient, uReminder, uExpectedQ.u, uExpectedR.u);
434 }
435}
436
437
438TSTRTNOCRT5DIV volatile const g_aDivS[] =
439{
440 { UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000) },
441 { UINT64_C(0x0000000000000000), UINT64_C(0xffffffffffffffff), /* => */ UINT64_C(0x0000000000000000), UINT64_C(0x0000000000000000) },
442
443 { UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000) },
444 { UINT64_C(0x0000000000000001), UINT64_C(0xffffffffffffffff), /* => */ UINT64_C(0xffffffffffffffff), UINT64_C(0x0000000000000000) },
445 { UINT64_C(0xffffffffffffffff), UINT64_C(0x0000000000000001), /* => */ UINT64_C(0xffffffffffffffff), UINT64_C(0x0000000000000000) },
446 { UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000) },
447
448 { UINT64_C(0x0000000000000002), UINT64_C(0x0000000000000002), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000000) },
449
450 { UINT64_C(0x0000000000000003), UINT64_C(0x0000000000000002), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0x0000000000000001) },
451 { UINT64_C(0xfffffffffffffffd), UINT64_C(0x0000000000000002), /* => */ UINT64_C(0xffffffffffffffff), UINT64_C(0xffffffffffffffff) },
452 { UINT64_C(0x0000000000000003), UINT64_C(0xfffffffffffffffe), /* => */ UINT64_C(0xffffffffffffffff), UINT64_C(0x0000000000000001) },
453 { UINT64_C(0xfffffffffffffffd), UINT64_C(0xfffffffffffffffe), /* => */ UINT64_C(0x0000000000000001), UINT64_C(0xffffffffffffffff) },
454};
455
456static void tstSignedDivision()
457{
458 RTTestSub(g_hTest, "64-bit signed division");
459
460 /*
461 * static tests from array.
462 */
463 for (size_t i = 0; i < RT_ELEMENTS(g_aDivS); i++)
464 {
465 int64_t const iResult = (int64_t)g_aDivS[i].uDividend / (int64_t)g_aDivS[i].uDivisor; /* aulldiv */
466 if ((uint64_t)iResult != g_aDivS[i].uQuotient)
467 RTTestFailed(g_hTest, "i=%u %#018RX64 / %#018RX64 => %#018RX64, expected %#018RX64",
468 i, g_aDivS[i].uDividend, g_aDivS[i].uDivisor, iResult, g_aDivS[i].uQuotient);
469 }
470 for (size_t i = 0; i < RT_ELEMENTS(g_aDivS); i++)
471 {
472 int64_t const iResult = (int64_t)g_aDivS[i].uDividend % (int64_t)g_aDivS[i].uDivisor; /* aullrem */
473 if ((uint64_t)iResult != g_aDivS[i].uReminder)
474 RTTestFailed(g_hTest, "i=%u %#018RX64 %% %#018RX64 => %#018RX64, expected %#018RX64",
475 i, g_aDivS[i].uDividend, g_aDivS[i].uDivisor, iResult, g_aDivS[i].uReminder);
476 }
477 for (size_t i = 0; i < RT_ELEMENTS(g_aDivS); i++)
478 {
479 int64_t const iDividend = (int64_t)g_aDivS[i].uDividend;
480 int64_t const iDivisor = (int64_t)g_aDivS[i].uDivisor;
481 int64_t const iQuotient = iDividend / iDivisor; /* auldvrm hopefully - only not in unoptimized builds. */
482 int64_t const iReminder = iDividend % iDivisor;
483 if ( (uint64_t)iQuotient != g_aDivS[i].uQuotient
484 || (uint64_t)iReminder != g_aDivS[i].uReminder)
485 RTTestFailed(g_hTest, "i=%u %#018RX64 / %#018RX64 => q=%#018RX64 r=%#018RX64, expected q=%#018RX64 r=%#018RX64",
486 i, g_aDivS[i].uDividend, g_aDivS[i].uDivisor,
487 iQuotient, iReminder, g_aDivS[i].uQuotient, g_aDivS[i].uReminder);
488 }
489
490 /* Check that uint64 works: */
491 {
492 RTUINT64U Tmp = { 42 };
493 RTTEST_CHECK(g_hTest, !RTUInt64IsSigned(&Tmp));
494 RTUInt64AssignNeg(&Tmp);
495 int64_t iExpect = -42;
496 RTTEST_CHECK(g_hTest, Tmp.u == (uint64_t)iExpect);
497 RTTEST_CHECK(g_hTest, RTUInt64IsSigned(&Tmp));
498 }
499
500 /*
501 * Same but with random values via uint64.
502 */
503 for (size_t i = 0; i < RANDOM_LOOPS; i++)
504 {
505 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
506 RTUINT64U const uDivisor = { RTRandU64Ex(1, i & 3 ? UINT64_MAX : UINT32_MAX) };
507 int64_t const iResult = (int64_t)uDividend.u / (int64_t)uDivisor.u;
508 RTUINT64U uExpected;
509 RTUInt64DivSigned(&uExpected, &uDividend, &uDivisor);
510 if ((uint64_t)iResult != uExpected.u)
511 RTTestFailed(g_hTest, "%#018RX64 / %#018RX64 => %#018RX64, expected %#018RX64", uDividend.u, uDivisor.u, iResult, uExpected.u);
512 }
513 for (size_t i = 0; i < RANDOM_LOOPS; i++)
514 {
515 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
516 RTUINT64U const uDivisor = { RTRandU64Ex(1, i & 3 ? UINT64_MAX : UINT32_MAX) };
517 int64_t const iResult = (int64_t)uDividend.u % (int64_t)uDivisor.u;
518 RTUINT64U uExpected;
519 RTUInt64ModSigned(&uExpected, &uDividend, &uDivisor);
520 if ((uint64_t)iResult != uExpected.u)
521 RTTestFailed(g_hTest, "%#018RX64 %% %#018RX64 => %#018RX64, expected %#018RX64", uDividend.u, uDivisor.u, iResult, uExpected.u);
522 }
523 for (size_t i = 0; i < RANDOM_LOOPS; i++)
524 {
525 RTUINT64U const uDividend = { RTRandU64Ex(0, i & 7 ? UINT64_MAX : UINT32_MAX) };
526 RTUINT64U const uDivisor = { RTRandU64Ex(1, i & 3 ? UINT64_MAX : UINT32_MAX) };
527 int64_t const iReminder = (int64_t)uDividend.u % (int64_t)uDivisor.u;
528 int64_t const iQuotient = (int64_t)uDividend.u / (int64_t)uDivisor.u;
529 RTUINT64U uExpectedQ, uExpectedR;
530 RTUInt64DivRemSigned(&uExpectedQ, &uExpectedR, &uDividend, &uDivisor);
531 if ( (uint64_t)iQuotient != uExpectedQ.u
532 || (uint64_t)iReminder != uExpectedR.u)
533 RTTestFailed(g_hTest, "%#018RX64 / %#018RX64 => q=%#018RX64 r=%#018RX64, expected q=%#018RX64 r=%#018RX64",
534 uDividend.u, uDivisor.u, iQuotient, iReminder, uExpectedQ.u, uExpectedR.u);
535 }
536}
537
538
539int main()
540{
541 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTNoCrt-5", &g_hTest);
542 if (rcExit != RTEXITCODE_SUCCESS)
543 return rcExit;
544 tstShiftRight();
545 tstShiftRightArithmetic();
546 tstShiftLeft();
547 tstMultiplication();
548 tstUnsignedDivision();
549 tstSignedDivision();
550
551 return RTTestSummaryAndDestroy(g_hTest);
552}
553
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette