test_shuffle_roundtrip_sse2.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Roundtrip tests for the SSE2-accelerated shuffle/unshuffle.
  4. Creation date: 2010-06-07
  5. Author: Francesc Alted <francesc@blosc.org>
  6. See LICENSES/BLOSC.txt for details about copyright and rights to use.
  7. **********************************************************************/
  8. #include "test_common.h"
  9. #include "../blosc/shuffle.h"
  10. #include "../blosc/shuffle-generic.h"
  11. /* Include SSE2-accelerated shuffle implementation if supported by this compiler.
  12. TODO: Need to also do run-time CPU feature support here. */
  13. #if defined(SHUFFLE_SSE2_ENABLED)
  14. #include "../blosc/shuffle-sse2.h"
  15. #else
  16. #if defined(_MSC_VER)
  17. #pragma message("SSE2 shuffle tests not enabled.")
  18. #else
  19. #warning SSE2 shuffle tests not enabled.
  20. #endif
  21. #endif /* defined(SHUFFLE_SSE2_ENABLED) */
  22. /** Roundtrip tests for the SSE2-accelerated shuffle/unshuffle. */
  23. static int test_shuffle_roundtrip_sse2(size_t type_size, size_t num_elements,
  24. size_t buffer_alignment, int test_type)
  25. {
  26. #if defined(SHUFFLE_SSE2_ENABLED)
  27. size_t buffer_size = type_size * num_elements;
  28. int exit_code;
  29. /* Allocate memory for the test. */
  30. void* original = blosc_test_malloc(buffer_alignment, buffer_size);
  31. void* shuffled = blosc_test_malloc(buffer_alignment, buffer_size);
  32. void* unshuffled = blosc_test_malloc(buffer_alignment, buffer_size);
  33. /* Fill the input data buffer with random values. */
  34. blosc_test_fill_random(original, buffer_size);
  35. /* Shuffle/unshuffle, selecting the implementations based on the test type. */
  36. switch(test_type)
  37. {
  38. case 0:
  39. /* sse2/sse2 */
  40. shuffle_sse2(type_size, buffer_size, original, shuffled);
  41. unshuffle_sse2(type_size, buffer_size, shuffled, unshuffled);
  42. break;
  43. case 1:
  44. /* generic/sse2 */
  45. shuffle_generic(type_size, buffer_size, original, shuffled);
  46. unshuffle_sse2(type_size, buffer_size, shuffled, unshuffled);
  47. break;
  48. case 2:
  49. /* sse2/generic */
  50. shuffle_sse2(type_size, buffer_size, original, shuffled);
  51. unshuffle_generic(type_size, buffer_size, shuffled, unshuffled);
  52. break;
  53. default:
  54. fprintf(stderr, "Invalid test type specified (%d).", test_type);
  55. return EXIT_FAILURE;
  56. }
  57. /* The round-tripped data matches the original data when the
  58. result of memcmp is 0. */
  59. exit_code = memcmp(original, unshuffled, buffer_size) ?
  60. EXIT_FAILURE : EXIT_SUCCESS;
  61. /* Free allocated memory. */
  62. blosc_test_free(original);
  63. blosc_test_free(shuffled);
  64. blosc_test_free(unshuffled);
  65. return exit_code;
  66. #else
  67. return EXIT_SUCCESS;
  68. #endif /* defined(SHUFFLE_SSE2_ENABLED) */
  69. }
  70. /** Required number of arguments to this test, including the executable name. */
  71. #define TEST_ARG_COUNT 5
  72. int main(int argc, char **argv)
  73. {
  74. uint32_t type_size;
  75. uint32_t num_elements;
  76. uint32_t buffer_align_size;
  77. uint32_t test_type;
  78. /* argv[1]: sizeof(element type)
  79. argv[2]: number of elements
  80. argv[3]: buffer alignment
  81. argv[4]: test type
  82. */
  83. /* Verify the correct number of command-line args have been specified. */
  84. if (TEST_ARG_COUNT != argc)
  85. {
  86. blosc_test_print_bad_argcount_msg(TEST_ARG_COUNT, argc);
  87. return EXIT_FAILURE;
  88. }
  89. /* Parse arguments */
  90. if (!blosc_test_parse_uint32_t(argv[1], &type_size) || (type_size < 1))
  91. {
  92. blosc_test_print_bad_arg_msg(1);
  93. return EXIT_FAILURE;
  94. }
  95. if (!blosc_test_parse_uint32_t(argv[2], &num_elements) || (num_elements < 1))
  96. {
  97. blosc_test_print_bad_arg_msg(2);
  98. return EXIT_FAILURE;
  99. }
  100. if (!blosc_test_parse_uint32_t(argv[3], &buffer_align_size)
  101. || (buffer_align_size & (buffer_align_size - 1))
  102. || (buffer_align_size < sizeof(void*)))
  103. {
  104. blosc_test_print_bad_arg_msg(3);
  105. return EXIT_FAILURE;
  106. }
  107. if (!blosc_test_parse_uint32_t(argv[4], &test_type) || (test_type > 2))
  108. {
  109. blosc_test_print_bad_arg_msg(4);
  110. return EXIT_FAILURE;
  111. }
  112. /* Run the test. */
  113. return test_shuffle_roundtrip_sse2(type_size, num_elements, buffer_align_size, test_type);
  114. }