Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

math_test.c 9.3KB

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