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