shiftfloatsample.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // float_sort rightshift functor sorting example
  2. //
  3. // Copyright Steven Ross 2009-2014.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/sort for library home page.
  9. #include <boost/sort/spreadsort/spreadsort.hpp>
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <string>
  16. #include <fstream>
  17. #include <iostream>
  18. using namespace boost::sort::spreadsort;
  19. #define DATA_TYPE float
  20. #define CAST_TYPE int
  21. //Casting to an integer before bitshifting
  22. struct rightshift {
  23. inline int operator()(const DATA_TYPE &x, const unsigned offset) const {
  24. return float_mem_cast<DATA_TYPE, int>(x) >> offset;
  25. }
  26. };
  27. //Pass in an argument to test std::sort
  28. //Note that this converts NaNs and -0.0 to 0.0, so that sorting results are
  29. //identical every time
  30. int main(int argc, const char ** argv) {
  31. size_t uCount,uSize=sizeof(DATA_TYPE);
  32. bool stdSort = false;
  33. unsigned loopCount = 1;
  34. for (int u = 1; u < argc; ++u) {
  35. if (std::string(argv[u]) == "-std")
  36. stdSort = true;
  37. else
  38. loopCount = atoi(argv[u]);
  39. }
  40. std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
  41. if (input.fail()) {
  42. printf("input.txt could not be opened\n");
  43. return 1;
  44. }
  45. double total = 0.0;
  46. std::vector<DATA_TYPE> array;
  47. input.seekg (0, std::ios_base::end);
  48. size_t length = input.tellg();
  49. uCount = length/uSize;
  50. //Run multiple loops, if requested
  51. for (unsigned u = 0; u < loopCount; ++u) {
  52. input.seekg (0, std::ios_base::beg);
  53. //Conversion to a vector
  54. array.resize(uCount);
  55. unsigned v = 0;
  56. while (input.good() && v < uCount) {
  57. input.read(reinterpret_cast<char *>(&(array[v])), uSize );
  58. //Testcase doesn't sort NaNs; they just cause confusion
  59. if (!(array[v] < 0.0) && !(0.0 < array[v]))
  60. array[v] = 0.0;
  61. //Checking for denormalized numbers
  62. if (!(float_mem_cast<float, int>(array[v]) & 0x7f800000)) {
  63. //Make the top exponent bit high
  64. CAST_TYPE temp = 0x40000000 | float_mem_cast<float, int>(array[v]);
  65. memcpy(&(array[v]), &temp, sizeof(DATA_TYPE));
  66. }
  67. ++v;
  68. }
  69. clock_t start, end;
  70. double elapsed;
  71. start = clock();
  72. if (stdSort)
  73. //std::sort(&(array[0]), &(array[0]) + uCount);
  74. std::sort(array.begin(), array.end());
  75. else
  76. //float_sort(&(array[0]), &(array[0]) + uCount, rightshift());
  77. float_sort(array.begin(), array.end(), rightshift());
  78. end = clock();
  79. elapsed = static_cast<double>(end - start) ;
  80. std::ofstream ofile;
  81. if (stdSort)
  82. ofile.open("standard_sort_out.txt", std::ios_base::out |
  83. std::ios_base::binary | std::ios_base::trunc);
  84. else
  85. ofile.open("boost_sort_out.txt", std::ios_base::out |
  86. std::ios_base::binary | std::ios_base::trunc);
  87. if (ofile.good()) {
  88. for (unsigned v = 0; v < array.size(); ++v) {
  89. ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );
  90. }
  91. ofile.close();
  92. }
  93. total += elapsed;
  94. array.clear();
  95. }
  96. if (stdSort)
  97. printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
  98. else
  99. printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
  100. return 0;
  101. }