2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * Mathematical self-tests
28 /* Forcibly enable assertions */
34 #include <ipxe/test.h>
35 #include <ipxe/isqrt.h>
38 * Force a call to the non-constant implementation of flsl()
41 * @ret msb Most significant bit set in value (LSB=1), or zero
43 __attribute__ (( noinline )) int flsl_var ( long value ) {
44 return flsl ( value );
48 * Force a call to the non-constant implementation of flsll()
51 * @ret msb Most significant bit set in value (LSB=1), or zero
53 __attribute__ (( noinline )) int flsll_var ( long long value ) {
54 return flsll ( value );
58 * Check current stack pointer
60 * @ret stack A value at a fixed offset from the current stack pointer
62 * Used by check_divmod()
64 static __attribute__ (( noinline )) void * stack_check ( void ) {
68 /* Hide the fact that we are returning the address of a local
69 * variable, to prevent a compiler warning.
71 __asm__ ( "\n" : "=g" ( ret ) : "0" ( &a ) );
77 * Check division/modulus operation
79 * One aspect of the calling convention for the implicit arithmetic
80 * functions (__udivmoddi4() etc) is whether the caller or the callee
81 * is expected to pop any stack-based arguments. This distinction can
82 * be masked if the compiler chooses to uses a frame pointer in the
83 * caller, since the caller will then reload the stack pointer from
84 * the frame pointer and so can mask an error in the value of the
87 * We run the division operation in a loop, and check that the stack
88 * pointer does not change value on the second iteration. To prevent
89 * the compiler from performing various optimisations which might
90 * invalidate our intended test (such as unrolling the loop, or moving
91 * the division operation outside the loop), we include some dummy
92 * inline assembly code.
94 #define check_divmod( dividend, divisor, OP ) ( { \
99 /* Prevent compiler from unrolling the loop */ \
100 __asm__ ( "\n" : "=g" ( count ) : "0" ( count ) ); \
103 /* Check that stack pointer does not change between \
107 assert ( check == stack_check() ); \
109 check = stack_check(); \
112 /* Perform division, preventing the compiler from \
113 * moving the division out of the loop. \
115 __asm__ ( "\n" : "=g" ( dividend ), "=g" ( divisor ) \
116 : "0" ( dividend ), "1" ( divisor ) ); \
117 result = ( dividend OP divisor ); \
118 __asm__ ( "\n" : "=g" ( result ) : "0" ( result ) ); \
120 } while ( --count ); \
124 * Force a use of runtime 64-bit unsigned integer division
126 * @v dividend Dividend
128 * @ret quotient Quotient
130 __attribute__ (( noinline )) uint64_t u64div_var ( uint64_t dividend,
133 return check_divmod ( dividend, divisor, / );
137 * Force a use of runtime 64-bit unsigned integer modulus
139 * @v dividend Dividend
141 * @ret remainder Remainder
143 __attribute__ (( noinline )) uint64_t u64mod_var ( uint64_t dividend,
146 return check_divmod ( dividend, divisor, % );
150 * Force a use of runtime 64-bit signed integer division
152 * @v dividend Dividend
154 * @ret quotient Quotient
156 __attribute__ (( noinline )) int64_t s64div_var ( int64_t dividend,
159 return check_divmod ( dividend, divisor, / );
163 * Force a use of runtime 64-bit unsigned integer modulus
165 * @v dividend Dividend
167 * @ret remainder Remainder
169 __attribute__ (( noinline )) int64_t s64mod_var ( int64_t dividend,
172 return check_divmod ( dividend, divisor, % );
176 * Report a flsl() test result
179 * @v msb Expected MSB
180 * @v file Test code file
181 * @v line Test code line
183 static inline __attribute__ (( always_inline )) void
184 flsl_okx ( long value, int msb, const char *file, unsigned int line ) {
186 /* Verify as a constant (requires to be inlined) */
187 okx ( flsl ( value ) == msb, file, line );
189 /* Verify as a non-constant */
190 okx ( flsl_var ( value ) == msb, file, line );
192 #define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )
195 * Report a flsll() test result
198 * @v msb Expected MSB
199 * @v file Test code file
200 * @v line Test code line
202 static inline __attribute__ (( always_inline )) void
203 flsll_okx ( long long value, int msb, const char *file, unsigned int line ) {
205 /* Verify as a constant (requires to be inlined) */
206 okx ( flsll ( value ) == msb, file, line );
208 /* Verify as a non-constant */
209 okx ( flsll_var ( value ) == msb, file, line );
211 #define flsll_ok( value, msb ) flsll_okx ( value, msb, __FILE__, __LINE__ )
214 * Report a 64-bit unsigned integer division test result
216 * @v dividend Dividend
218 * @v quotient Quotient
219 * @v remainder Remainder
220 * @v file Test code file
221 * @v line Test code line
223 static void u64divmod_okx ( uint64_t dividend, uint64_t divisor,
224 uint64_t quotient, uint64_t remainder,
225 const char *file, unsigned int line ) {
228 okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
231 okx ( u64div_var ( dividend, divisor ) == quotient, file, line );
234 okx ( u64mod_var ( dividend, divisor ) == remainder, file, line );
236 #define u64divmod_ok( dividend, divisor, quotient, remainder ) \
237 u64divmod_okx ( dividend, divisor, quotient, remainder, \
241 * Report a 64-bit signed integer division test result
243 * @v dividend Dividend
245 * @v quotient Quotient
246 * @v remainder Remainder
247 * @v file Test code file
248 * @v line Test code line
250 static void s64divmod_okx ( int64_t dividend, int64_t divisor,
251 int64_t quotient, int64_t remainder,
252 const char *file, unsigned int line ) {
255 okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
258 okx ( s64div_var ( dividend, divisor ) == quotient, file, line );
261 okx ( s64mod_var ( dividend, divisor ) == remainder, file, line );
263 #define s64divmod_ok( dividend, divisor, quotient, remainder ) \
264 s64divmod_okx ( dividend, divisor, quotient, remainder, \
268 * Perform mathematical self-tests
271 static void math_test_exec ( void ) {
279 flsl_ok ( 0x69505845, 31 );
280 flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
281 flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
286 flsll_ok ( 0x6d63623330ULL, 39 );
287 flsll_ok ( -1U, ( 8 * sizeof ( int ) ) );
288 flsll_ok ( -1UL, ( 8 * sizeof ( long ) ) );
289 flsll_ok ( -1ULL, ( 8 * sizeof ( long long ) ) );
291 /* Test 64-bit arithmetic
293 * On a 64-bit machine, these tests are fairly meaningless.
295 * On a 32-bit machine, these tests verify the correct
296 * operation of our libgcc functions __udivmoddi4()
297 * etc. (including checking that the implicit calling
298 * convention assumed by gcc matches our expectations).
300 u64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
301 0x2eef6ab4ULL, 0x0e12f089ULL );
302 s64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
303 0x2eef6ab4ULL, 0x0e12f089ULL );
304 u64divmod_ok ( 0xc09e00dcb9e34b54ULL, 0x35968185cdc744f3ULL,
305 3, 0x1fda7c4b508d7c7bULL );
306 s64divmod_ok ( -0x3f61ff23461cb4acLL, 0x35968185cdc744f3ULL,
307 -1LL, -0x9cb7d9d78556fb9LL );
308 u64divmod_ok ( 0, 0x5b2f2737f4ffULL, 0, 0 );
309 s64divmod_ok ( 0, 0xbb00ded72766207fULL, 0, 0 );
311 /* Test integer square root */
312 ok ( isqrt ( 0 ) == 0 );
313 ok ( isqrt ( 1 ) == 1 );
314 ok ( isqrt ( 255 ) == 15 );
315 ok ( isqrt ( 256 ) == 16 );
316 ok ( isqrt ( 257 ) == 16 );
317 ok ( isqrt ( 0xa53df2adUL ) == 52652 );
318 ok ( isqrt ( 0x123793c6UL ) == 17482 );
319 ok ( isqrt ( -1UL ) == ( -1UL >> ( 8 * sizeof ( unsigned long ) / 2 )));
322 /** Mathematical self-tests */
323 struct self_test math_test __self_test = {
325 .exec = math_test_exec,