Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

math_test.c 9.1KB

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