1 /***************************************************************************
2 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
3 * Martin Renou                                                             *
4 * Copyright (c) QuantStack                                                 *
5 * Copyright (c) Serge Guelton                                              *
6 *                                                                          *
7 * Distributed under the terms of the BSD 3-Clause License.                 *
8 *                                                                          *
9 * The full license is in the file LICENSE, distributed with this software. *
10 ****************************************************************************/
11 
12 #ifndef XSIMD_GENERIC_ROUNDING_HPP
13 #define XSIMD_GENERIC_ROUNDING_HPP
14 
15 #include "./xsimd_generic_details.hpp"
16 
17 
18 namespace xsimd {
19 
20   namespace kernel {
21 
22 
23     using namespace types;
24 
25     // ceil
ceil(batch<T,A> const & self,requires_arch<generic>)26     template<class A, class T> batch<T, A> ceil(batch<T, A> const& self, requires_arch<generic>) {
27       batch<T, A> truncated_self = trunc(self);
28       return select(truncated_self < self, truncated_self + 1, truncated_self);
29     }
30 
31 
32     // floor
floor(batch<T,A> const & self,requires_arch<generic>)33     template<class A, class T> batch<T, A> floor(batch<T, A> const& self, requires_arch<generic>) {
34       batch<T, A> truncated_self = trunc(self);
35       return select(truncated_self > self, truncated_self - 1, truncated_self);
36     }
37 
38     // round
round(batch<T,A> const & self,requires_arch<generic>)39     template<class A, class T> batch<T, A> round(batch<T, A> const& self, requires_arch<generic>) {
40       auto v = abs(self);
41       auto c = ceil(v);
42       auto cp = select(c - 0.5 > v, c - 1, c);
43       return select(v > constants::maxflint<batch<T, A>>(), self, copysign(cp, self));
44     }
45 
46     // trunc
47     template<class A, class T, class=typename std::enable_if<std::is_integral<T>::value, void>::type>
trunc(batch<T,A> const & self,requires_arch<generic>)48     batch<T, A> trunc(batch<T, A> const& self, requires_arch<generic>) {
49       return self;
50     }
trunc(batch<float,A> const & self,requires_arch<generic>)51     template<class A> batch<float, A> trunc(batch<float, A> const& self, requires_arch<generic>) {
52       return select(abs(self) < constants::maxflint<batch<float, A>>(), to_float(to_int(self)), self);
53     }
trunc(batch<double,A> const & self,requires_arch<generic>)54     template<class A> batch<double, A> trunc(batch<double, A> const& self, requires_arch<generic>) {
55       return select(abs(self) < constants::maxflint<batch<double, A>>(), to_float(to_int(self)), self);
56     }
57 
58 
59   }
60 
61 }
62 
63 #endif
64 
65