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