Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 09:11:28

0001 /***************************************************************************
0002  * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
0003  * Martin Renou                                                             *
0004  * Copyright (c) QuantStack                                                 *
0005  * Copyright (c) Serge Guelton                                              *
0006  *                                                                          *
0007  * Distributed under the terms of the BSD 3-Clause License.                 *
0008  *                                                                          *
0009  * The full license is in the file LICENSE, distributed with this software. *
0010  ****************************************************************************/
0011 
0012 #ifndef XSIMD_GENERIC_ROUNDING_HPP
0013 #define XSIMD_GENERIC_ROUNDING_HPP
0014 
0015 #include "./xsimd_generic_details.hpp"
0016 
0017 namespace xsimd
0018 {
0019 
0020     namespace kernel
0021     {
0022 
0023         using namespace types;
0024 
0025         // ceil
0026         template <class A, class T>
0027         XSIMD_INLINE batch<T, A> ceil(batch<T, A> const& self, requires_arch<generic>) noexcept
0028         {
0029             batch<T, A> truncated_self = trunc(self);
0030             return select(truncated_self < self, truncated_self + 1, truncated_self);
0031         }
0032 
0033         // floor
0034         template <class A, class T>
0035         XSIMD_INLINE batch<T, A> floor(batch<T, A> const& self, requires_arch<generic>) noexcept
0036         {
0037             batch<T, A> truncated_self = trunc(self);
0038             return select(truncated_self > self, truncated_self - 1, truncated_self);
0039         }
0040 
0041         // round
0042         template <class A, class T>
0043         XSIMD_INLINE batch<T, A> round(batch<T, A> const& self, requires_arch<generic>) noexcept
0044         {
0045             auto v = abs(self);
0046             auto c = ceil(v);
0047             auto cp = select(c - 0.5 > v, c - 1, c);
0048             return select(v > constants::maxflint<batch<T, A>>(), self, copysign(cp, self));
0049         }
0050 
0051         // trunc
0052         template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
0053         XSIMD_INLINE batch<T, A> trunc(batch<T, A> const& self, requires_arch<generic>) noexcept
0054         {
0055             return self;
0056         }
0057         template <class A>
0058         XSIMD_INLINE batch<float, A> trunc(batch<float, A> const& self, requires_arch<generic>) noexcept
0059         {
0060             return select(abs(self) < constants::maxflint<batch<float, A>>(), to_float(to_int(self)), self);
0061         }
0062         template <class A>
0063         XSIMD_INLINE batch<double, A> trunc(batch<double, A> const& self, requires_arch<generic>) noexcept
0064         {
0065             return select(abs(self) < constants::maxflint<batch<double, A>>(), to_float(to_int(self)), self);
0066         }
0067 
0068     }
0069 
0070 }
0071 
0072 #endif