test_shuffle_roundtrip_avx2.c 3.8 KB

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