iPXE
Macros | Functions | Variables
math_test.c File Reference

Mathematical self-tests. More...

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

Go to the source code of this file.

Macros

#define check_divmod(dividend, divisor, OP)
 Check division/modulus operation. More...
 
#define ffsl_ok(value, lsb)   ffsl_okx ( value, lsb, __FILE__, __LINE__ )
 
#define ffsll_ok(value, lsb)   ffsll_okx ( value, lsb, __FILE__, __LINE__ )
 
#define flsl_ok(value, msb)   flsl_okx ( value, msb, __FILE__, __LINE__ )
 
#define flsll_ok(value, msb)   flsll_okx ( value, msb, __FILE__, __LINE__ )
 
#define lsl64_ok(value, shift, expected)   lsl64_okx ( value, shift, expected, __FILE__, __LINE__ )
 
#define lsr64_ok(value, shift, expected)   lsr64_okx ( value, shift, expected, __FILE__, __LINE__ )
 
#define asr64_ok(value, shift, expected)   asr64_okx ( value, shift, expected, __FILE__, __LINE__ )
 
#define u64divmod_ok(dividend, divisor, quotient, remainder)
 
#define s64divmod_ok(dividend, divisor, quotient, remainder)
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int ffsl_var (long value)
 Force a call to the non-constant implementation of ffsl() More...
 
int ffsll_var (long long value)
 Force a call to the non-constant implementation of ffsll() More...
 
int flsl_var (long value)
 Force a call to the non-constant implementation of flsl() More...
 
int flsll_var (long long value)
 Force a call to the non-constant implementation of flsll() More...
 
uint64_t lsl64_var (uint64_t value, unsigned int shift)
 Force a use of runtime 64-bit shift left. More...
 
uint64_t lsr64_var (uint64_t value, unsigned int shift)
 Force a use of runtime 64-bit logical shift right. More...
 
int64_t asr64_var (int64_t value, unsigned int shift)
 Force a use of runtime 64-bit arithmetic shift right. More...
 
static void * stack_check (void)
 Check current stack pointer. More...
 
uint64_t u64div_var (uint64_t dividend, uint64_t divisor)
 Force a use of runtime 64-bit unsigned integer division. More...
 
uint64_t u64mod_var (uint64_t dividend, uint64_t divisor)
 Force a use of runtime 64-bit unsigned integer modulus. More...
 
int64_t s64div_var (int64_t dividend, int64_t divisor)
 Force a use of runtime 64-bit signed integer division. More...
 
int64_t s64mod_var (int64_t dividend, int64_t divisor)
 Force a use of runtime 64-bit unsigned integer modulus. More...
 
static void ffsl_okx (long value, int lsb, const char *file, unsigned int line)
 Report a ffsl() test result. More...
 
static void ffsll_okx (long long value, int lsb, const char *file, unsigned int line)
 Report a ffsll() test result. More...
 
static void flsl_okx (long value, int msb, const char *file, unsigned int line)
 Report a flsl() test result. More...
 
static void flsll_okx (long long value, int msb, const char *file, unsigned int line)
 Report a flsll() test result. More...
 
static void lsl64_okx (uint64_t value, unsigned int shift, uint64_t expected, const char *file, unsigned int line)
 Report a 64-bit shift left test result. More...
 
static void lsr64_okx (uint64_t value, unsigned int shift, uint64_t expected, const char *file, unsigned int line)
 Report a 64-bit logical shift right test result. More...
 
static void asr64_okx (int64_t value, unsigned int shift, int64_t expected, const char *file, unsigned int line)
 Report a 64-bit arithmetic shift right test result. More...
 
static void u64divmod_okx (uint64_t dividend, uint64_t divisor, uint64_t quotient, uint64_t remainder, const char *file, unsigned int line)
 Report a 64-bit unsigned integer division test result. More...
 
static void s64divmod_okx (int64_t dividend, int64_t divisor, int64_t quotient, int64_t remainder, const char *file, unsigned int line)
 Report a 64-bit signed integer division test result. More...
 
static void math_test_exec (void)
 Perform mathematical self-tests. More...
 

Variables

struct self_test math_test __self_test
 Mathematical self-tests. More...
 

Detailed Description

Mathematical self-tests.

Definition in file math_test.c.

Macro Definition Documentation

◆ check_divmod

#define check_divmod (   dividend,
  divisor,
  OP 
)
Value:
( { \
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; } )
uint16_t count
Number of entries.
Definition: ena.h:22
static void * stack_check(void)
Check current stack pointer.
Definition: math_test.c:124
uint16_t result
Definition: hyperv.h:33
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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.

Definition at line 154 of file math_test.c.

◆ ffsl_ok

#define ffsl_ok (   value,
  lsb 
)    ffsl_okx ( value, lsb, __FILE__, __LINE__ )

Definition at line 252 of file math_test.c.

◆ ffsll_ok

#define ffsll_ok (   value,
  lsb 
)    ffsll_okx ( value, lsb, __FILE__, __LINE__ )

Definition at line 271 of file math_test.c.

◆ flsl_ok

#define flsl_ok (   value,
  msb 
)    flsl_okx ( value, msb, __FILE__, __LINE__ )

Definition at line 290 of file math_test.c.

◆ flsll_ok

#define flsll_ok (   value,
  msb 
)    flsll_okx ( value, msb, __FILE__, __LINE__ )

Definition at line 309 of file math_test.c.

◆ lsl64_ok

#define lsl64_ok (   value,
  shift,
  expected 
)    lsl64_okx ( value, shift, expected, __FILE__, __LINE__ )

Definition at line 330 of file math_test.c.

◆ lsr64_ok

#define lsr64_ok (   value,
  shift,
  expected 
)    lsr64_okx ( value, shift, expected, __FILE__, __LINE__ )

Definition at line 352 of file math_test.c.

◆ asr64_ok

#define asr64_ok (   value,
  shift,
  expected 
)    asr64_okx ( value, shift, expected, __FILE__, __LINE__ )

Definition at line 374 of file math_test.c.

◆ u64divmod_ok

#define u64divmod_ok (   dividend,
  divisor,
  quotient,
  remainder 
)
Value:
u64divmod_okx ( dividend, divisor, quotient, remainder, \
__FILE__, __LINE__ )
static void u64divmod_okx(uint64_t dividend, uint64_t divisor, uint64_t quotient, uint64_t remainder, const char *file, unsigned int line)
Report a 64-bit unsigned integer division test result.
Definition: math_test.c:387

Definition at line 400 of file math_test.c.

◆ s64divmod_ok

#define s64divmod_ok (   dividend,
  divisor,
  quotient,
  remainder 
)
Value:
s64divmod_okx ( dividend, divisor, quotient, remainder, \
__FILE__, __LINE__ )
static void s64divmod_okx(int64_t dividend, int64_t divisor, int64_t quotient, int64_t remainder, const char *file, unsigned int line)
Report a 64-bit signed integer division test result.
Definition: math_test.c:414

Definition at line 427 of file math_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ ffsl_var()

int ffsl_var ( long  value)

Force a call to the non-constant implementation of ffsl()

Parameters
valueValue
Return values
lsbLeast significant bit set in value (LSB=1), or zero

Definition at line 47 of file math_test.c.

47  {
48  return ffsl ( value );
49 }
#define ffsl(x)
Find first (i.e.
Definition: strings.h:131
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References ffsl, and value.

Referenced by ffsl_okx().

◆ ffsll_var()

int ffsll_var ( long long  value)

Force a call to the non-constant implementation of ffsll()

Parameters
valueValue
Return values
lsbLeast significant bit set in value (LSB=1), or zero

Definition at line 57 of file math_test.c.

57  {
58  return ffsll ( value );
59 }
#define ffsll(x)
Find first (i.e.
Definition: strings.h:122
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References ffsll, and value.

Referenced by ffsll_okx().

◆ flsl_var()

int flsl_var ( long  value)

Force a call to the non-constant implementation of flsl()

Parameters
valueValue
Return values
msbMost significant bit set in value (LSB=1), or zero

Definition at line 67 of file math_test.c.

67  {
68  return flsl ( value );
69 }
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define flsl(x)
Find last (i.e.
Definition: strings.h:157

References flsl, and value.

Referenced by flsl_okx().

◆ flsll_var()

int flsll_var ( long long  value)

Force a call to the non-constant implementation of flsll()

Parameters
valueValue
Return values
msbMost significant bit set in value (LSB=1), or zero

Definition at line 77 of file math_test.c.

77  {
78  return flsll ( value );
79 }
#define flsll(x)
Find last (i.e.
Definition: strings.h:148
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References flsll, and value.

Referenced by flsll_okx().

◆ lsl64_var()

uint64_t lsl64_var ( uint64_t  value,
unsigned int  shift 
)

Force a use of runtime 64-bit shift left.

Parameters
valueValue
shiftShift amount
Return values
valueShifted value

Definition at line 88 of file math_test.c.

89  {
90  return ( value << shift );
91 }
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References value.

Referenced by lsl64_okx().

◆ lsr64_var()

uint64_t lsr64_var ( uint64_t  value,
unsigned int  shift 
)

Force a use of runtime 64-bit logical shift right.

Parameters
valueValue
shiftShift amount
Return values
valueShifted value

Definition at line 100 of file math_test.c.

101  {
102  return ( value >> shift );
103 }
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References value.

Referenced by lsr64_okx().

◆ asr64_var()

int64_t asr64_var ( int64_t  value,
unsigned int  shift 
)

Force a use of runtime 64-bit arithmetic shift right.

Parameters
valueValue
shiftShift amount
Return values
valueShifted value

Definition at line 112 of file math_test.c.

113  {
114  return ( value >> shift );
115 }
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References value.

Referenced by asr64_okx().

◆ stack_check()

static void* stack_check ( void  )
static

Check current stack pointer.

Return values
stackA value at a fixed offset from the current stack pointer

Used by check_divmod()

Definition at line 124 of file math_test.c.

124  {
125  int a;
126  void *ret;
127 
128  /* Hide the fact that we are returning the address of a local
129  * variable, to prevent a compiler warning.
130  */
131  __asm__ ( "\n" : "=g" ( ret ) : "0" ( &a ) );
132 
133  return ret;
134 }
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")

References __asm__().

◆ u64div_var()

uint64_t u64div_var ( uint64_t  dividend,
uint64_t  divisor 
)

Force a use of runtime 64-bit unsigned integer division.

Parameters
dividendDividend
divisorDivisor
Return values
quotientQuotient

Definition at line 190 of file math_test.c.

191  {
192 
193  return check_divmod ( dividend, divisor, / );
194 }
#define check_divmod(dividend, divisor, OP)
Check division/modulus operation.
Definition: math_test.c:154

References check_divmod.

Referenced by u64divmod_okx().

◆ u64mod_var()

uint64_t u64mod_var ( uint64_t  dividend,
uint64_t  divisor 
)

Force a use of runtime 64-bit unsigned integer modulus.

Parameters
dividendDividend
divisorDivisor
Return values
remainderRemainder

Definition at line 203 of file math_test.c.

204  {
205 
206  return check_divmod ( dividend, divisor, % );
207 }
#define check_divmod(dividend, divisor, OP)
Check division/modulus operation.
Definition: math_test.c:154

References check_divmod.

Referenced by u64divmod_okx().

◆ s64div_var()

int64_t s64div_var ( int64_t  dividend,
int64_t  divisor 
)

Force a use of runtime 64-bit signed integer division.

Parameters
dividendDividend
divisorDivisor
Return values
quotientQuotient

Definition at line 216 of file math_test.c.

217  {
218 
219  return check_divmod ( dividend, divisor, / );
220 }
#define check_divmod(dividend, divisor, OP)
Check division/modulus operation.
Definition: math_test.c:154

References check_divmod.

Referenced by s64divmod_okx().

◆ s64mod_var()

int64_t s64mod_var ( int64_t  dividend,
int64_t  divisor 
)

Force a use of runtime 64-bit unsigned integer modulus.

Parameters
dividendDividend
divisorDivisor
Return values
remainderRemainder

Definition at line 229 of file math_test.c.

230  {
231 
232  return check_divmod ( dividend, divisor, % );
233 }
#define check_divmod(dividend, divisor, OP)
Check division/modulus operation.
Definition: math_test.c:154

References check_divmod.

Referenced by s64divmod_okx().

◆ ffsl_okx()

static void ffsl_okx ( long  value,
int  lsb,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a ffsl() test result.

Parameters
valueValue
lsbExpected LSB
fileTest code file
lineTest code line

Definition at line 244 of file math_test.c.

244  {
245 
246  /* Verify as a constant (requires to be inlined) */
247  okx ( ffsl ( value ) == lsb, file, line );
248 
249  /* Verify as a non-constant */
250  okx ( ffsl_var ( value ) == lsb, file, line );
251 }
#define ffsl(x)
Find first (i.e.
Definition: strings.h:131
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int ffsl_var(long value)
Force a call to the non-constant implementation of ffsl()
Definition: math_test.c:47

References ffsl, ffsl_var(), okx, and value.

◆ ffsll_okx()

static void ffsll_okx ( long long  value,
int  lsb,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a ffsll() test result.

Parameters
valueValue
lsbExpected LSB
fileTest code file
lineTest code line

Definition at line 263 of file math_test.c.

263  {
264 
265  /* Verify as a constant (requires to be inlined) */
266  okx ( ffsll ( value ) == lsb, file, line );
267 
268  /* Verify as a non-constant */
269  okx ( ffsll_var ( value ) == lsb, file, line );
270 }
int ffsll_var(long long value)
Force a call to the non-constant implementation of ffsll()
Definition: math_test.c:57
#define okx(success, file, line)
Report test result.
Definition: test.h:44
#define ffsll(x)
Find first (i.e.
Definition: strings.h:122
pseudo_bit_t value[0x00020]
Definition: arbel.h:13

References ffsll, ffsll_var(), okx, and value.

◆ flsl_okx()

static void flsl_okx ( long  value,
int  msb,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a flsl() test result.

Parameters
valueValue
msbExpected MSB
fileTest code file
lineTest code line

Definition at line 282 of file math_test.c.

282  {
283 
284  /* Verify as a constant (requires to be inlined) */
285  okx ( flsl ( value ) == msb, file, line );
286 
287  /* Verify as a non-constant */
288  okx ( flsl_var ( value ) == msb, file, line );
289 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define flsl(x)
Find last (i.e.
Definition: strings.h:157
int flsl_var(long value)
Force a call to the non-constant implementation of flsl()
Definition: math_test.c:67

References flsl, flsl_var(), okx, and value.

◆ flsll_okx()

static void flsll_okx ( long long  value,
int  msb,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a flsll() test result.

Parameters
valueValue
msbExpected MSB
fileTest code file
lineTest code line

Definition at line 301 of file math_test.c.

301  {
302 
303  /* Verify as a constant (requires to be inlined) */
304  okx ( flsll ( value ) == msb, file, line );
305 
306  /* Verify as a non-constant */
307  okx ( flsll_var ( value ) == msb, file, line );
308 }
#define flsll(x)
Find last (i.e.
Definition: strings.h:148
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int flsll_var(long long value)
Force a call to the non-constant implementation of flsll()
Definition: math_test.c:77

References flsll, flsll_var(), okx, and value.

◆ lsl64_okx()

static void lsl64_okx ( uint64_t  value,
unsigned int  shift,
uint64_t  expected,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a 64-bit shift left test result.

Parameters
valueValue
shiftShift amount
expectedExpected value
fileTest code file
lineTest code line

Definition at line 321 of file math_test.c.

322  {
323 
324  /* Verify as a compile-time calculation */
325  okx ( ( value << shift ) == expected, file, line );
326 
327  /* Verify as a runtime calculation */
328  okx ( lsl64_var ( value, shift ) == expected, file, line );
329 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint64_t lsl64_var(uint64_t value, unsigned int shift)
Force a use of runtime 64-bit shift left.
Definition: math_test.c:88

References lsl64_var(), okx, and value.

◆ lsr64_okx()

static void lsr64_okx ( uint64_t  value,
unsigned int  shift,
uint64_t  expected,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a 64-bit logical shift right test result.

Parameters
valueValue
shiftShift amount
expectedExpected value
fileTest code file
lineTest code line

Definition at line 343 of file math_test.c.

344  {
345 
346  /* Verify as a compile-time calculation */
347  okx ( ( value >> shift ) == expected, file, line );
348 
349  /* Verify as a runtime calculation */
350  okx ( lsr64_var ( value, shift ) == expected, file, line );
351 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint64_t lsr64_var(uint64_t value, unsigned int shift)
Force a use of runtime 64-bit logical shift right.
Definition: math_test.c:100

References lsr64_var(), okx, and value.

◆ asr64_okx()

static void asr64_okx ( int64_t  value,
unsigned int  shift,
int64_t  expected,
const char *  file,
unsigned int  line 
)
inlinestatic

Report a 64-bit arithmetic shift right test result.

Parameters
valueValue
shiftShift amount
expectedExpected value
fileTest code file
lineTest code line

Definition at line 365 of file math_test.c.

366  {
367 
368  /* Verify as a compile-time calculation */
369  okx ( ( value >> shift ) == expected, file, line );
370 
371  /* Verify as a runtime calculation */
372  okx ( asr64_var ( value, shift ) == expected, file, line );
373 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int64_t asr64_var(int64_t value, unsigned int shift)
Force a use of runtime 64-bit arithmetic shift right.
Definition: math_test.c:112

References asr64_var(), okx, and value.

◆ u64divmod_okx()

static void u64divmod_okx ( uint64_t  dividend,
uint64_t  divisor,
uint64_t  quotient,
uint64_t  remainder,
const char *  file,
unsigned int  line 
)
static

Report a 64-bit unsigned integer division test result.

Parameters
dividendDividend
divisorDivisor
quotientQuotient
remainderRemainder
fileTest code file
lineTest code line

Definition at line 387 of file math_test.c.

389  {
390 
391  /* Sanity check */
392  okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
393 
394  /* Check division */
395  okx ( u64div_var ( dividend, divisor ) == quotient, file, line );
396 
397  /* Check modulus */
398  okx ( u64mod_var ( dividend, divisor ) == remainder, file, line );
399 }
uint64_t u64div_var(uint64_t dividend, uint64_t divisor)
Force a use of runtime 64-bit unsigned integer division.
Definition: math_test.c:190
#define okx(success, file, line)
Report test result.
Definition: test.h:44
uint64_t u64mod_var(uint64_t dividend, uint64_t divisor)
Force a use of runtime 64-bit unsigned integer modulus.
Definition: math_test.c:203

References okx, u64div_var(), and u64mod_var().

◆ s64divmod_okx()

static void s64divmod_okx ( int64_t  dividend,
int64_t  divisor,
int64_t  quotient,
int64_t  remainder,
const char *  file,
unsigned int  line 
)
static

Report a 64-bit signed integer division test result.

Parameters
dividendDividend
divisorDivisor
quotientQuotient
remainderRemainder
fileTest code file
lineTest code line

Definition at line 414 of file math_test.c.

416  {
417 
418  /* Sanity check */
419  okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
420 
421  /* Check division */
422  okx ( s64div_var ( dividend, divisor ) == quotient, file, line );
423 
424  /* Check modulus */
425  okx ( s64mod_var ( dividend, divisor ) == remainder, file, line );
426 }
int64_t s64div_var(int64_t dividend, int64_t divisor)
Force a use of runtime 64-bit signed integer division.
Definition: math_test.c:216
#define okx(success, file, line)
Report test result.
Definition: test.h:44
int64_t s64mod_var(int64_t dividend, int64_t divisor)
Force a use of runtime 64-bit unsigned integer modulus.
Definition: math_test.c:229

References okx, s64div_var(), and s64mod_var().

◆ math_test_exec()

static void math_test_exec ( void  )
static

Perform mathematical self-tests.

Definition at line 435 of file math_test.c.

435  {
436 
437  /* Test ffsl() */
438  ffsl_ok ( 0, 0 );
439  ffsl_ok ( 1, 1 );
440  ffsl_ok ( 255, 1 );
441  ffsl_ok ( 256, 9 );
442  ffsl_ok ( 257, 1 );
443  ffsl_ok ( 0x54850596, 2 );
444  ffsl_ok ( 0x80000000, 32 );
445 
446  /* Test ffsll() */
447  ffsll_ok ( 0, 0 );
448  ffsll_ok ( 1, 1 );
449  ffsll_ok ( 0x6d63623330ULL, 5 );
450  ffsll_ok ( 0x80000000UL, 32 );
451  ffsll_ok ( 0x8000000000000000ULL, 64 );
452 
453  /* Test flsl() */
454  flsl_ok ( 0, 0 );
455  flsl_ok ( 1, 1 );
456  flsl_ok ( 255, 8 );
457  flsl_ok ( 256, 9 );
458  flsl_ok ( 257, 9 );
459  flsl_ok ( 0x69505845, 31 );
460  flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
461  flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
462 
463  /* Test flsll() */
464  flsll_ok ( 0, 0 );
465  flsll_ok ( 1, 1 );
466  flsll_ok ( 0x6d63623330ULL, 39 );
467  flsll_ok ( -1U, ( 8 * sizeof ( int ) ) );
468  flsll_ok ( -1UL, ( 8 * sizeof ( long ) ) );
469  flsll_ok ( -1ULL, ( 8 * sizeof ( long long ) ) );
470 
471  /* Test 64-bit arithmetic
472  *
473  * On a 64-bit machine, these tests are fairly meaningless.
474  *
475  * On a 32-bit machine, these tests verify the correct
476  * operation of our libgcc functions __udivmoddi4()
477  * etc. (including checking that the implicit calling
478  * convention assumed by gcc matches our expectations).
479  */
480  lsl64_ok ( 0x06760c14710540c2ULL, 0, 0x06760c14710540c2ULL );
481  lsr64_ok ( 0x06760c14710540c2ULL, 0, 0x06760c14710540c2ULL );
482  asr64_ok ( 0x06760c14710540c2ULL, 0, 0x06760c14710540c2ULL );
483  lsl64_ok ( 0xccafd1a8cb724c13ULL, 20, 0x1a8cb724c1300000ULL );
484  lsr64_ok ( 0xccafd1a8cb724c13ULL, 20, 0x00000ccafd1a8cb7ULL );
485  asr64_ok ( 0xccafd1a8cb724c13ULL, 20, 0xfffffccafd1a8cb7ULL );
486  lsl64_ok ( 0x83567264b1234518ULL, 32, 0xb123451800000000ULL );
487  lsr64_ok ( 0x83567264b1234518ULL, 32, 0x0000000083567264ULL );
488  asr64_ok ( 0x83567264b1234518ULL, 32, 0xffffffff83567264ULL );
489  lsl64_ok ( 0x69ee42fcbf1a4ea4ULL, 47, 0x2752000000000000ULL );
490  lsr64_ok ( 0x69ee42fcbf1a4ea4ULL, 47, 0x000000000000d3dcULL );
491  asr64_ok ( 0x69ee42fcbf1a4ea4ULL, 47, 0x000000000000d3dcULL );
492  lsl64_ok ( 0xaa20b8caddee4269ULL, 63, 0x8000000000000000ULL );
493  lsr64_ok ( 0xaa20b8caddee4269ULL, 63, 0x0000000000000001ULL );
494  asr64_ok ( 0xaa20b8caddee4269ULL, 63, 0xffffffffffffffffULL );
495  u64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
496  0x2eef6ab4ULL, 0x0e12f089ULL );
497  s64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
498  0x2eef6ab4ULL, 0x0e12f089ULL );
499  u64divmod_ok ( 0xc09e00dcb9e34b54ULL, 0x35968185cdc744f3ULL,
500  3, 0x1fda7c4b508d7c7bULL );
501  s64divmod_ok ( -0x3f61ff23461cb4acLL, 0x35968185cdc744f3ULL,
502  -1LL, -0x9cb7d9d78556fb9LL );
503  u64divmod_ok ( 0, 0x5b2f2737f4ffULL, 0, 0 );
504  s64divmod_ok ( 0, 0xbb00ded72766207fULL, 0, 0 );
505 
506  /* Test integer square root */
507  ok ( isqrt ( 0 ) == 0 );
508  ok ( isqrt ( 1 ) == 1 );
509  ok ( isqrt ( 255 ) == 15 );
510  ok ( isqrt ( 256 ) == 16 );
511  ok ( isqrt ( 257 ) == 16 );
512  ok ( isqrt ( 0xa53df2adUL ) == 52652 );
513  ok ( isqrt ( 0x123793c6UL ) == 17482 );
514  ok ( isqrt ( -1UL ) == ( -1UL >> ( 8 * sizeof ( unsigned long ) / 2 )));
515 }
#define s64divmod_ok(dividend, divisor, quotient, remainder)
Definition: math_test.c:427
#define ffsl_ok(value, lsb)
Definition: math_test.c:252
#define lsl64_ok(value, shift, expected)
Definition: math_test.c:330
unsigned long isqrt(unsigned long value)
Find integer square root.
Definition: isqrt.c:40
#define u64divmod_ok(dividend, divisor, quotient, remainder)
Definition: math_test.c:400
#define ffsll_ok(value, lsb)
Definition: math_test.c:271
#define flsll_ok(value, msb)
Definition: math_test.c:309
#define asr64_ok(value, shift, expected)
Definition: math_test.c:374
#define lsr64_ok(value, shift, expected)
Definition: math_test.c:352
#define ok(success)
Definition: test.h:46
#define flsl_ok(value, msb)
Definition: math_test.c:290

References asr64_ok, ffsl_ok, ffsll_ok, flsl_ok, flsll_ok, isqrt(), lsl64_ok, lsr64_ok, ok, s64divmod_ok, and u64divmod_ok.

Variable Documentation

◆ __self_test

struct self_test math_test __self_test
Initial value:
= {
.name = "math",
.exec = math_test_exec,
}
static void math_test_exec(void)
Perform mathematical self-tests.
Definition: math_test.c:435

Mathematical self-tests.

Definition at line 518 of file math_test.c.