summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/tests/math_test.c
blob: 1a244f1ebb53d32547457255a0823ebb1a60b06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * Mathematical self-tests
 *
 */

/* Forcibly enable assertions */
#undef NDEBUG

#include <string.h>
#include <strings.h>
#include <assert.h>
#include <ipxe/test.h>
#include <ipxe/isqrt.h>

/**
 * Force a call to the non-constant implementation of ffsl()
 *
 * @v value		Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
__attribute__ (( noinline )) int ffsl_var ( long value ) {
	return ffsl ( value );
}

/**
 * Force a call to the non-constant implementation of ffsll()
 *
 * @v value		Value
 * @ret lsb		Least significant bit set in value (LSB=1), or zero
 */
__attribute__ (( noinline )) int ffsll_var ( long long value ) {
	return ffsll ( value );
}

/**
 * Force a call to the non-constant implementation of flsl()
 *
 * @v value		Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
__attribute__ (( noinline )) int flsl_var ( long value ) {
	return flsl ( value );
}

/**
 * Force a call to the non-constant implementation of flsll()
 *
 * @v value		Value
 * @ret msb		Most significant bit set in value (LSB=1), or zero
 */
__attribute__ (( noinline )) int flsll_var ( long long value ) {
	return flsll ( value );
}

/**
 * Check current stack pointer
 *
 * @ret stack		A value at a fixed offset from the current stack pointer
 *
 * Used by check_divmod()
 */
static __attribute__ (( noinline )) void * stack_check ( void ) {
	int a;
	void *ret;

	/* Hide the fact that we are returning the address of a local
	 * variable, to prevent a compiler warning.
	 */
	__asm__ ( "\n" : "=g" ( ret ) : "0" ( &a ) );

	return ret;
}

/**
 * Check division/modulus operation
 *
 * One aspect of the calling convention for the implicit arithmetic
 * functions (__udivmoddi4() etc) is whether the caller or the callee
 * is expected to pop any stack-based arguments.  This distinction can
 * be masked if the compiler chooses to uses a frame pointer in the
 * caller, since the caller will then reload the stack pointer from
 * the frame pointer and so can mask an error in the value of the
 * stack pointer.
 *
 * We run the division operation in a loop, and check that the stack
 * pointer does not change value on the second iteration.  To prevent
 * the compiler from performing various optimisations which might
 * invalidate our intended test (such as unrolling the loop, or moving
 * the division operation outside the loop), we include some dummy
 * inline assembly code.
 */
#define check_divmod( dividend, divisor, OP ) ( {			\
	uint64_t result;						\
	int count = 2;							\
	void *check = NULL;						\
									\
	/* Prevent compiler from unrolling the loop */			\
	__asm__ ( "\n" : "=g" ( count ) : "0" ( count ) );		\
									\
	do {								\
		/* Check that stack pointer does not change between	\
		 * loop iterations.					\
		 */							\
		if ( check ) {						\
			assert ( check == stack_check() );		\
		} else {						\
			check = stack_check();				\
		}							\
									\
		/* Perform division, preventing the compiler from	\
		 * moving the division out of the loop.			\
		 */							\
		__asm__ ( "\n" : "=g" ( dividend ), "=g" ( divisor )	\
			  : "0" ( dividend ), "1" ( divisor ) );	\
	        result = ( dividend OP divisor );			\
		__asm__ ( "\n" : "=g" ( result ) : "0" ( result ) );	\
									\
	} while ( --count );						\
	result; } )

/**
 * Force a use of runtime 64-bit unsigned integer division
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @ret quotient	Quotient
 */
__attribute__ (( noinline )) uint64_t u64div_var ( uint64_t dividend,
						   uint64_t divisor ) {

	return check_divmod ( dividend, divisor, / );
}

/**
 * Force a use of runtime 64-bit unsigned integer modulus
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @ret remainder	Remainder
 */
__attribute__ (( noinline )) uint64_t u64mod_var ( uint64_t dividend,
						   uint64_t divisor ) {

	return check_divmod ( dividend, divisor, % );
}

/**
 * Force a use of runtime 64-bit signed integer division
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @ret quotient	Quotient
 */
__attribute__ (( noinline )) int64_t s64div_var ( int64_t dividend,
						  int64_t divisor ) {

	return check_divmod ( dividend, divisor, / );
}

/**
 * Force a use of runtime 64-bit unsigned integer modulus
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @ret remainder	Remainder
 */
__attribute__ (( noinline )) int64_t s64mod_var ( int64_t dividend,
						  int64_t divisor ) {

	return check_divmod ( dividend, divisor, % );
}

/**
 * Report a ffsl() test result
 *
 * @v value		Value
 * @v lsb		Expected LSB
 * @v file		Test code file
 * @v line		Test code line
 */
static inline __attribute__ (( always_inline )) void
ffsl_okx ( long value, int lsb, const char *file, unsigned int line ) {

	/* Verify as a constant (requires to be inlined) */
	okx ( ffsl ( value ) == lsb, file, line );

	/* Verify as a non-constant */
	okx ( ffsl_var ( value ) == lsb, file, line );
}
#define ffsl_ok( value, lsb ) ffsl_okx ( value, lsb, __FILE__, __LINE__ )

/**
 * Report a ffsll() test result
 *
 * @v value		Value
 * @v lsb		Expected LSB
 * @v file		Test code file
 * @v line		Test code line
 */
static inline __attribute__ (( always_inline )) void
ffsll_okx ( long long value, int lsb, const char *file, unsigned int line ) {

	/* Verify as a constant (requires to be inlined) */
	okx ( ffsll ( value ) == lsb, file, line );

	/* Verify as a non-constant */
	okx ( ffsll_var ( value ) == lsb, file, line );
}
#define ffsll_ok( value, lsb ) ffsll_okx ( value, lsb, __FILE__, __LINE__ )

/**
 * Report a flsl() test result
 *
 * @v value		Value
 * @v msb		Expected MSB
 * @v file		Test code file
 * @v line		Test code line
 */
static inline __attribute__ (( always_inline )) void
flsl_okx ( long value, int msb, const char *file, unsigned int line ) {

	/* Verify as a constant (requires to be inlined) */
	okx ( flsl ( value ) == msb, file, line );

	/* Verify as a non-constant */
	okx ( flsl_var ( value ) == msb, file, line );
}
#define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )

/**
 * Report a flsll() test result
 *
 * @v value		Value
 * @v msb		Expected MSB
 * @v file		Test code file
 * @v line		Test code line
 */
static inline __attribute__ (( always_inline )) void
flsll_okx ( long long value, int msb, const char *file, unsigned int line ) {

	/* Verify as a constant (requires to be inlined) */
	okx ( flsll ( value ) == msb, file, line );

	/* Verify as a non-constant */
	okx ( flsll_var ( value ) == msb, file, line );
}
#define flsll_ok( value, msb ) flsll_okx ( value, msb, __FILE__, __LINE__ )

/**
 * Report a 64-bit unsigned integer division test result
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @v quotient		Quotient
 * @v remainder		Remainder
 * @v file		Test code file
 * @v line		Test code line
 */
static void u64divmod_okx ( uint64_t dividend, uint64_t divisor,
			    uint64_t quotient, uint64_t remainder,
			    const char *file, unsigned int line ) {

	/* Sanity check */
	okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );

	/* Check division */
	okx ( u64div_var ( dividend, divisor ) == quotient, file, line );

	/* Check modulus */
	okx ( u64mod_var ( dividend, divisor ) == remainder, file, line );
}
#define u64divmod_ok( dividend, divisor, quotient, remainder )	\
	u64divmod_okx ( dividend, divisor, quotient, remainder,	\
			__FILE__, __LINE__ )

/**
 * Report a 64-bit signed integer division test result
 *
 * @v dividend		Dividend
 * @v divisor		Divisor
 * @v quotient		Quotient
 * @v remainder		Remainder
 * @v file		Test code file
 * @v line		Test code line
 */
static void s64divmod_okx ( int64_t dividend, int64_t divisor,
			    int64_t quotient, int64_t remainder,
			    const char *file, unsigned int line ) {

	/* Sanity check */
	okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );

	/* Check division */
	okx ( s64div_var ( dividend, divisor ) == quotient, file, line );

	/* Check modulus */
	okx ( s64mod_var ( dividend, divisor ) == remainder, file, line );
}
#define s64divmod_ok( dividend, divisor, quotient, remainder )	\
	s64divmod_okx ( dividend, divisor, quotient, remainder,	\
			__FILE__, __LINE__ )

/**
 * Perform mathematical self-tests
 *
 */
static void math_test_exec ( void ) {

	/* Test ffsl() */
	ffsl_ok ( 0, 0 );
	ffsl_ok ( 1, 1 );
	ffsl_ok ( 255, 1 );
	ffsl_ok ( 256, 9 );
	ffsl_ok ( 257, 1 );
	ffsl_ok ( 0x54850596, 2 );
	ffsl_ok ( 0x80000000, 32 );

	/* Test ffsll() */
	ffsll_ok ( 0, 0 );
	ffsll_ok ( 1, 1 );
	ffsll_ok ( 0x6d63623330ULL, 5 );
	ffsll_ok ( 0x80000000UL, 32 );
	ffsll_ok ( 0x8000000000000000ULL, 64 );

	/* Test flsl() */
	flsl_ok ( 0, 0 );
	flsl_ok ( 1, 1 );
	flsl_ok ( 255, 8 );
	flsl_ok ( 256, 9 );
	flsl_ok ( 257, 9 );
	flsl_ok ( 0x69505845, 31 );
	flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
	flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );

	/* Test flsll() */
	flsll_ok ( 0, 0 );
	flsll_ok ( 1, 1 );
	flsll_ok ( 0x6d63623330ULL, 39 );
	flsll_ok ( -1U, ( 8 * sizeof ( int ) ) );
	flsll_ok ( -1UL, ( 8 * sizeof ( long ) ) );
	flsll_ok ( -1ULL, ( 8 * sizeof ( long long ) ) );

	/* Test 64-bit arithmetic
	 *
	 * On a 64-bit machine, these tests are fairly meaningless.
	 *
	 * On a 32-bit machine, these tests verify the correct
	 * operation of our libgcc functions __udivmoddi4()
	 * etc. (including checking that the implicit calling
	 * convention assumed by gcc matches our expectations).
	 */
	u64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
		       0x2eef6ab4ULL, 0x0e12f089ULL );
	s64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
		       0x2eef6ab4ULL, 0x0e12f089ULL );
	u64divmod_ok ( 0xc09e00dcb9e34b54ULL, 0x35968185cdc744f3ULL,
		       3, 0x1fda7c4b508d7c7bULL );
	s64divmod_ok ( -0x3f61ff23461cb4acLL, 0x35968185cdc744f3ULL,
		       -1LL, -0x9cb7d9d78556fb9LL );
	u64divmod_ok ( 0, 0x5b2f2737f4ffULL, 0, 0 );
	s64divmod_ok ( 0, 0xbb00ded72766207fULL, 0, 0 );

	/* Test integer square root */
	ok ( isqrt ( 0 ) == 0 );
	ok ( isqrt ( 1 ) == 1 );
	ok ( isqrt ( 255 ) == 15 );
	ok ( isqrt ( 256 ) == 16 );
	ok ( isqrt ( 257 ) == 16 );
	ok ( isqrt ( 0xa53df2adUL ) == 52652 );
	ok ( isqrt ( 0x123793c6UL ) == 17482 );
	ok ( isqrt ( -1UL ) == ( -1UL >> ( 8 * sizeof ( unsigned long ) / 2 )));
}

/** Mathematical self-tests */
struct self_test math_test __self_test = {
	.name = "math",
	.exec = math_test_exec,
};