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>
20using namespace boost::sort::spreadsort;
21
22#define DATA_TYPE float
23#define CAST_TYPE int
24
25//Casting to an integer before bitshifting
26struct rightshift {
27 inline int operator()(const DATA_TYPE &x, const unsigned offset) const {
28 return float_mem_cast<DATA_TYPE, int>(data: 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
36int 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(nptr: argv[u]);
45 }
46 std::ifstream input("input.txt", std::ios_base::in | std::ios_base::binary);
47 if (input.fail()) {
48 printf(format: "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(new_size: uCount);
61 unsigned v = 0;
62 while (input.good() && v < uCount) {
63 input.read(s: reinterpret_cast<char *>(&(array[v])), n: 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>(data: array[v]) & 0x7f800000)) {
69 //Make the top exponent bit high
70 CAST_TYPE temp = 0x40000000 | float_mem_cast<float, int>(data: array[v]);
71 memcpy(dest: &(array[v]), src: &temp, n: 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(first: array.begin(), last: array.end());
81 else
82 //float_sort(&(array[0]), &(array[0]) + uCount, rightshift());
83 float_sort(first: array.begin(), last: array.end(), rshift: rightshift());
84 end = clock();
85 elapsed = static_cast<double>(end - start) ;
86 std::ofstream ofile;
87 if (stdSort)
88 ofile.open(s: "standard_sort_out.txt", mode: std::ios_base::out |
89 std::ios_base::binary | std::ios_base::trunc);
90 else
91 ofile.open(s: "boost_sort_out.txt", mode: 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(s: reinterpret_cast<char *>(&(array[v])), n: sizeof(array[v]) );
96 }
97 ofile.close();
98 }
99 total += elapsed;
100 array.clear();
101 }
102 if (stdSort)
103 printf(format: "std::sort elapsed time %f\n", total / CLOCKS_PER_SEC);
104 else
105 printf(format: "spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC);
106 return 0;
107}
108

source code of boost/libs/sort/example/shiftfloatsample.cpp