You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

math_test.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Mathematical self-tests
  23. *
  24. */
  25. /* Forcibly enable assertions */
  26. #undef NDEBUG
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <assert.h>
  30. #include <ipxe/test.h>
  31. #include <ipxe/isqrt.h>
  32. /**
  33. * Force a call to the non-constant implementation of flsl()
  34. *
  35. * @v value Value
  36. * @ret msb Most significant bit set in value (LSB=1), or zero
  37. */
  38. __attribute__ (( noinline )) int flsl_var ( long value ) {
  39. return flsl ( value );
  40. }
  41. /**
  42. * Check current stack pointer
  43. *
  44. * @ret stack A value at a fixed offset from the current stack pointer
  45. *
  46. * Used by check_divmod()
  47. */
  48. static __attribute__ (( noinline )) void * stack_check ( void ) {
  49. int a;
  50. void *ret;
  51. /* Hide the fact that we are returning the address of a local
  52. * variable, to prevent a compiler warning.
  53. */
  54. __asm__ ( "\n" : "=g" ( ret ) : "0" ( &a ) );
  55. return ret;
  56. }
  57. /**
  58. * Check division/modulus operation
  59. *
  60. * One aspect of the calling convention for the implicit arithmetic
  61. * functions (__udivmoddi4() etc) is whether the caller or the callee
  62. * is expected to pop any stack-based arguments. This distinction can
  63. * be masked if the compiler chooses to uses a frame pointer in the
  64. * caller, since the caller will then reload the stack pointer from
  65. * the frame pointer and so can mask an error in the value of the
  66. * stack pointer.
  67. *
  68. * We run the division operation in a loop, and check that the stack
  69. * pointer does not change value on the second iteration. To prevent
  70. * the compiler from performing various optimisations which might
  71. * invalidate our intended test (such as unrolling the loop, or moving
  72. * the division operation outside the loop), we include some dummy
  73. * inline assembly code.
  74. */
  75. #define check_divmod( dividend, divisor, OP ) ( { \
  76. uint64_t result; \
  77. int count = 2; \
  78. void *check = NULL; \
  79. \
  80. /* Prevent compiler from unrolling the loop */ \
  81. __asm__ ( "\n" : "=g" ( count ) : "0" ( count ) ); \
  82. \
  83. do { \
  84. /* Check that stack pointer does not change between \
  85. * loop iterations. \
  86. */ \
  87. if ( check ) { \
  88. assert ( check == stack_check() ); \
  89. } else { \
  90. check = stack_check(); \
  91. } \
  92. \
  93. /* Perform division, preventing the compiler from \
  94. * moving the division out of the loop. \
  95. */ \
  96. __asm__ ( "\n" : "=g" ( dividend ), "=g" ( divisor ) \
  97. : "0" ( dividend ), "1" ( divisor ) ); \
  98. result = ( dividend OP divisor ); \
  99. __asm__ ( "\n" : "=g" ( result ) : "0" ( result ) ); \
  100. \
  101. } while ( --count ); \
  102. result; } )
  103. /**
  104. * Force a use of runtime 64-bit unsigned integer division
  105. *
  106. * @v dividend Dividend
  107. * @v divisor Divisor
  108. * @ret quotient Quotient
  109. */
  110. __attribute__ (( noinline )) uint64_t u64div_var ( uint64_t dividend,
  111. uint64_t divisor ) {
  112. return check_divmod ( dividend, divisor, / );
  113. }
  114. /**
  115. * Force a use of runtime 64-bit unsigned integer modulus
  116. *
  117. * @v dividend Dividend
  118. * @v divisor Divisor
  119. * @ret remainder Remainder
  120. */
  121. __attribute__ (( noinline )) uint64_t u64mod_var ( uint64_t dividend,
  122. uint64_t divisor ) {
  123. return check_divmod ( dividend, divisor, % );
  124. }
  125. /**
  126. * Force a use of runtime 64-bit signed integer division
  127. *
  128. * @v dividend Dividend
  129. * @v divisor Divisor
  130. * @ret quotient Quotient
  131. */
  132. __attribute__ (( noinline )) int64_t s64div_var ( int64_t dividend,
  133. int64_t divisor ) {
  134. return check_divmod ( dividend, divisor, / );
  135. }
  136. /**
  137. * Force a use of runtime 64-bit unsigned integer modulus
  138. *
  139. * @v dividend Dividend
  140. * @v divisor Divisor
  141. * @ret remainder Remainder
  142. */
  143. __attribute__ (( noinline )) int64_t s64mod_var ( int64_t dividend,
  144. int64_t divisor ) {
  145. return check_divmod ( dividend, divisor, % );
  146. }
  147. /**
  148. * Report a flsl() test result
  149. *
  150. * @v value Value
  151. * @v msb Expected MSB
  152. * @v file Test code file
  153. * @v line Test code line
  154. */
  155. static inline __attribute__ (( always_inline )) void
  156. flsl_okx ( long value, int msb, const char *file, unsigned int line ) {
  157. /* Verify as a constant (requires to be inlined) */
  158. okx ( flsl ( value ) == msb, file, line );
  159. /* Verify as a non-constant */
  160. okx ( flsl_var ( value ) == msb, file, line );
  161. }
  162. #define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )
  163. /**
  164. * Report a 64-bit unsigned integer division test result
  165. *
  166. * @v dividend Dividend
  167. * @v divisor Divisor
  168. * @v quotient Quotient
  169. * @v remainder Remainder
  170. * @v file Test code file
  171. * @v line Test code line
  172. */
  173. static void u64divmod_okx ( uint64_t dividend, uint64_t divisor,
  174. uint64_t quotient, uint64_t remainder,
  175. const char *file, unsigned int line ) {
  176. /* Sanity check */
  177. okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
  178. /* Check division */
  179. okx ( u64div_var ( dividend, divisor ) == quotient, file, line );
  180. /* Check modulus */
  181. okx ( u64mod_var ( dividend, divisor ) == remainder, file, line );
  182. }
  183. #define u64divmod_ok( dividend, divisor, quotient, remainder ) \
  184. u64divmod_okx ( dividend, divisor, quotient, remainder, \
  185. __FILE__, __LINE__ )
  186. /**
  187. * Report a 64-bit signed integer division test result
  188. *
  189. * @v dividend Dividend
  190. * @v divisor Divisor
  191. * @v quotient Quotient
  192. * @v remainder Remainder
  193. * @v file Test code file
  194. * @v line Test code line
  195. */
  196. static void s64divmod_okx ( int64_t dividend, int64_t divisor,
  197. int64_t quotient, int64_t remainder,
  198. const char *file, unsigned int line ) {
  199. /* Sanity check */
  200. okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
  201. /* Check division */
  202. okx ( s64div_var ( dividend, divisor ) == quotient, file, line );
  203. /* Check modulus */
  204. okx ( s64mod_var ( dividend, divisor ) == remainder, file, line );
  205. }
  206. #define s64divmod_ok( dividend, divisor, quotient, remainder ) \
  207. s64divmod_okx ( dividend, divisor, quotient, remainder, \
  208. __FILE__, __LINE__ )
  209. /**
  210. * Perform mathematical self-tests
  211. *
  212. */
  213. static void math_test_exec ( void ) {
  214. /* Test flsl() */
  215. flsl_ok ( 0, 0 );
  216. flsl_ok ( 1, 1 );
  217. flsl_ok ( 255, 8 );
  218. flsl_ok ( 256, 9 );
  219. flsl_ok ( 257, 9 );
  220. flsl_ok ( 0x69505845, 31 );
  221. flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
  222. flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
  223. /* Test 64-bit arithmetic
  224. *
  225. * On a 64-bit machine, these tests are fairly meaningless.
  226. *
  227. * On a 32-bit machine, these tests verify the correct
  228. * operation of our libgcc functions __udivmoddi4()
  229. * etc. (including checking that the implicit calling
  230. * convention assumed by gcc matches our expectations).
  231. */
  232. u64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
  233. 0x2eef6ab4ULL, 0x0e12f089ULL );
  234. s64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
  235. 0x2eef6ab4ULL, 0x0e12f089ULL );
  236. u64divmod_ok ( 0xc09e00dcb9e34b54ULL, 0x35968185cdc744f3ULL,
  237. 3, 0x1fda7c4b508d7c7bULL );
  238. s64divmod_ok ( -0x3f61ff23461cb4acLL, 0x35968185cdc744f3ULL,
  239. -1LL, -0x9cb7d9d78556fb9LL );
  240. u64divmod_ok ( 0, 0x5b2f2737f4ffULL, 0, 0 );
  241. s64divmod_ok ( 0, 0xbb00ded72766207fULL, 0, 0 );
  242. /* Test integer square root */
  243. ok ( isqrt ( 0 ) == 0 );
  244. ok ( isqrt ( 1 ) == 1 );
  245. ok ( isqrt ( 255 ) == 15 );
  246. ok ( isqrt ( 256 ) == 16 );
  247. ok ( isqrt ( 257 ) == 16 );
  248. ok ( isqrt ( 0xa53df2adUL ) == 52652 );
  249. ok ( isqrt ( 0x123793c6UL ) == 17482 );
  250. ok ( isqrt ( -1UL ) == ( -1UL >> ( 8 * sizeof ( unsigned long ) / 2 )));
  251. }
  252. /** Mathematical self-tests */
  253. struct self_test math_test __self_test = {
  254. .name = "math",
  255. .exec = math_test_exec,
  256. };