]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/sort/example/shiftfloatsample.cpp
5488688c985ab53d6d689848d1d2da287e1975ec
1 // float_sort rightshift functor 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 //Casting to an integer before bitshifting
27 inline int operator()(const DATA_TYPE
&x
, const unsigned offset
) const {
28 return float_mem_cast
<DATA_TYPE
, int>(x
) >> offset
;
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
);
39 unsigned loopCount
= 1;
40 for (int u
= 1; u
< argc
; ++u
) {
41 if (std::string(argv
[u
]) == "-std")
44 loopCount
= atoi(argv
[u
]);
46 std::ifstream
input("input.txt", std::ios_base::in
| std::ios_base::binary
);
48 printf("input.txt could not be opened\n");
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
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
]))
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
));
79 //std::sort(&(array[0]), &(array[0]) + uCount);
80 std::sort(array
.begin(), array
.end());
82 //float_sort(&(array[0]), &(array[0]) + uCount, rightshift());
83 float_sort(array
.begin(), array
.end(), rightshift());
85 elapsed
= static_cast<double>(end
- start
) ;
88 ofile
.open("standard_sort_out.txt", std::ios_base::out
|
89 std::ios_base::binary
| std::ios_base::trunc
);
91 ofile
.open("boost_sort_out.txt", std::ios_base::out
|
92 std::ios_base::binary
| std::ios_base::trunc
);
94 for (unsigned v
= 0; v
< array
.size(); ++v
) {
95 ofile
.write(reinterpret_cast<char *>(&(array
[v
])), sizeof(array
[v
]) );
103 printf("std::sort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);
105 printf("spreadsort elapsed time %f\n", total
/ CLOCKS_PER_SEC
);