]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/floatsample.cpp
1 // spreadsort float sorting example.
3 // Copyright Steven Ross 2009-2014.
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)
9 // See http://www.boost.org/libs/sort for library home page.
11 #include <boost/sort/spreadsort/spreadsort.hpp>
20 using namespace boost::sort::spreadsort
;
22 #define DATA_TYPE float
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
);
31 unsigned loopCount
= 1;
32 for (int u
= 1; u
< argc
; ++u
) {
33 if (std::string(argv
[u
]) == "-std")
36 loopCount
= atoi(argv
[u
]);
38 std::ifstream
input("input.txt", std::ios_base::in
| std::ios_base::binary
);
40 printf("input.txt could not be opened\n");
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
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
));
62 //Testcase doesn't sort NaNs; they just cause confusion
63 if (!(array
[v
] < 0.0) && !(0.0 < array
[v
]))
71 //std::sort(&(array[0]), &(array[0]) + uCount);
72 std::sort(array
.begin(), array
.end());
74 //boost::sort::spreadsort::spreadsort(&(array[0]), &(array[0]) + uCount);
75 boost::sort::spreadsort::spreadsort(array
.begin(), array
.end());
77 elapsed
= static_cast<double>(end
- start
) ;
80 ofile
.open("standard_sort_out.txt", std::ios_base::out
|
81 std::ios_base::binary
| std::ios_base::trunc
);
83 ofile
.open("boost_sort_out.txt", std::ios_base::out
|
84 std::ios_base::binary
| std::ios_base::trunc
);
86 for (unsigned v
= 0; v
< array
.size(); ++v
) {
87 ofile
.write(reinterpret_cast<char *>(&(array
[v
])), sizeof(array
[v
]) );
96 printf("std::sort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);
98 printf("spreadsort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);