summaryrefslogtreecommitdiffstats
path: root/third_party/xsimd/include/xsimd/arch/generic/xsimd_generic_rounding.hpp
blob: b6a79a4515b8cc8d3e4b1bdb2cd0b6d297bb87bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
 * Martin Renou                                                             *
 * Copyright (c) QuantStack                                                 *
 * Copyright (c) Serge Guelton                                              *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#ifndef XSIMD_GENERIC_ROUNDING_HPP
#define XSIMD_GENERIC_ROUNDING_HPP

#include "./xsimd_generic_details.hpp"

namespace xsimd
{

    namespace kernel
    {

        using namespace types;

        // ceil
        template <class A, class T>
        inline batch<T, A> ceil(batch<T, A> const& self, requires_arch<generic>) noexcept
        {
            batch<T, A> truncated_self = trunc(self);
            return select(truncated_self < self, truncated_self + 1, truncated_self);
        }

        // floor
        template <class A, class T>
        inline batch<T, A> floor(batch<T, A> const& self, requires_arch<generic>) noexcept
        {
            batch<T, A> truncated_self = trunc(self);
            return select(truncated_self > self, truncated_self - 1, truncated_self);
        }

        // round
        template <class A, class T>
        inline batch<T, A> round(batch<T, A> const& self, requires_arch<generic>) noexcept
        {
            auto v = abs(self);
            auto c = ceil(v);
            auto cp = select(c - 0.5 > v, c - 1, c);
            return select(v > constants::maxflint<batch<T, A>>(), self, copysign(cp, self));
        }

        // trunc
        template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
        inline batch<T, A> trunc(batch<T, A> const& self, requires_arch<generic>) noexcept
        {
            return self;
        }
        template <class A>
        inline batch<float, A> trunc(batch<float, A> const& self, requires_arch<generic>) noexcept
        {
            return select(abs(self) < constants::maxflint<batch<float, A>>(), to_float(to_int(self)), self);
        }
        template <class A>
        inline batch<double, A> trunc(batch<double, A> const& self, requires_arch<generic>) noexcept
        {
            return select(abs(self) < constants::maxflint<batch<double, A>>(), to_float(to_int(self)), self);
        }

    }

}

#endif