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