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 11KB

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