]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/sort/example/double.cpp
update sources to v12.2.4
[ceph.git] / ceph / src / boost / libs / sort / example / double.cpp
CommitLineData
3a9019d9
FG
1// spreadsort double 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 double\r
23#define CAST_TYPE boost::int64_t\r
24\r
25//Pass in an argument to test std::sort\r
26//Note that this converts NaNs and -0.0 to 0.0, so that sorting results are\r
27//identical every time\r
28int main(int argc, const char ** argv) {\r
29 size_t uCount,uSize=sizeof(DATA_TYPE);\r
30 bool stdSort = false;\r
31 unsigned loopCount = 1;\r
32 for (int u = 1; u < argc; ++u) {\r
33 if (std::string(argv[u]) == "-std")\r
34 stdSort = true;\r
35 else\r
36 loopCount = atoi(argv[u]);\r
37 }\r
38 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);\r
39 if (input.fail()) {\r
40 printf("input.txt could not be opened\n");\r
41 return 1;\r
42 }\r
43 double total = 0.0;\r
44 std::vector<DATA_TYPE> array;\r
45 input.seekg (0, std::ios_base::end);\r
46 size_t length = input.tellg();\r
47 uCount = length/uSize;\r
48 //Using this to support compilers that don't support 64-bit constants\r
49 CAST_TYPE exponent_mask = 0x7ff00000;\r
50 exponent_mask <<= 32;\r
51 CAST_TYPE top_exponent_bit = 0x40000000;\r
52 top_exponent_bit <<= 32;\r
53 //Run multiple loops, if requested\r
54 for (unsigned u = 0; u < loopCount; ++u) {\r
55 input.seekg (0, std::ios_base::beg);\r
56 //Conversion to a vector\r
57 array.resize(uCount);\r
58 unsigned v = 0;\r
59 while (input.good() && v < uCount) {\r
60 input.read(reinterpret_cast<char *>(&(array[v])), uSize );\r
61 //Checking for denormalized numbers\r
62 if (!(float_mem_cast<DATA_TYPE, CAST_TYPE>(array[v]) & exponent_mask)) {\r
63 //Make the top exponent bit high\r
64 CAST_TYPE temp = top_exponent_bit |\r
65 float_mem_cast<DATA_TYPE, CAST_TYPE>(array[v]);\r
66 memcpy(&(array[v]), &temp, sizeof(DATA_TYPE));\r
67 }\r
68 //Testcase doesn't sort NaNs; they just cause confusion\r
69 if (!(array[v] < 0.0) && !(0.0 < array[v]))\r
70 array[v] = 0.0;\r
71 ++v;\r
72 }\r
73 clock_t start, end;\r
74 double elapsed;\r
75 start = clock();\r
76 if (stdSort)\r
77 //std::sort(&(array[0]), &(array[0]) + uCount);\r
78 std::sort(array.begin(), array.end());\r
79 else\r
80 //float_sort(&(array[0]), &(array[0]) + uCount);\r
81 float_sort(array.begin(), array.end());\r
82 end = clock();\r
83 elapsed = static_cast<double>(end - start) ;\r
84 std::ofstream ofile;\r
85 if (stdSort)\r
86 ofile.open("standard_sort_out.txt", std::ios_base::out |\r
87 std::ios_base::binary | std::ios_base::trunc);\r
88 else\r
89 ofile.open("boost_sort_out.txt", std::ios_base::out |\r
90 std::ios_base::binary | std::ios_base::trunc);\r
91 if (ofile.good()) {\r
92 for (unsigned v = 0; v < array.size(); ++v) {\r
93 ofile.write(reinterpret_cast<char *>(&(array[v])), sizeof(array[v]) );\r
94 }\r
95 ofile.close();\r
96 }\r
97 total += elapsed;\r
98 array.clear();\r
99 }\r
100 if (stdSort)\r
101 printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
102 else\r
103 printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);\r
104 return 0;\r
105}\r