1 //  softclip
2 //  Copyright (C) 2008, 2009 Tim Blechmann
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 2 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; see the file COPYING.  If not, write to
16 //  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 //  Boston, MA 02111-1307, USA.
18 
19 #ifndef SIMD_SOFTCLIP_HPP
20 #define SIMD_SOFTCLIP_HPP
21 
22 #include <cassert>
23 #include <cmath>
24 
25 #include "vec.hpp"
26 
27 #include "detail/define_macros.hpp"
28 
29 #if defined(__GNUC__) && defined(NDEBUG)
30 #define always_inline inline  __attribute__((always_inline))
31 #else
32 #define always_inline inline
33 #endif
34 
35 namespace nova   {
36 namespace detail {
37 
38 struct softclip
39 {
40     template <typename FloatType>
s_runnova::detail::softclip41     static FloatType s_run(FloatType arg)
42     {
43         FloatType abs = std::fabs(arg);
44         if (abs < FloatType(0.5))
45             return arg;
46         else
47             return (abs - FloatType(0.25)) / arg;
48     }
49 
50     template <typename FloatType>
operator ()nova::detail::softclip51     always_inline FloatType operator()(FloatType arg) const
52     {
53         return s_run(arg);
54     }
55 
56 #if defined(__SSE__) || defined(__AVX__)
57     /* this computes both parts of the branch
58      *
59      * benchmarks (core2) showed:
60      * 6.5 seconds for simdfied code
61      * 5.3 seconds for non-simd code for samples with abs(sample) < 0.5
62      * 17 seconds                    when 50% of the samples have abs(sample) < 0.5
63      * 26 seconds                    for samples with abs(sample) > 0.5
64      *
65      * on intel nethalem cpus, the simdfied code is faster than all non-simd code
66      * */
67 
68     template <typename FloatType>
operator ()nova::detail::softclip69     always_inline vec<FloatType> operator()(vec<FloatType> arg) const
70     {
71         typedef vec<FloatType> vec_type;
72 
73         const vec_type const05 = vec_type::gen_05();
74         const vec_type const025 (0.25);
75 
76         vec_type abs_ = abs(arg);
77         vec_type selecter = mask_lt(abs_, const05);
78         vec_type alt_ret = (abs_ - const025) / arg;
79 
80         return select(alt_ret, arg, selecter);
81     }
82 #else
83     template <typename FloatType>
operator ()nova::detail::softclip84     always_inline vec<FloatType> operator()(vec<FloatType> arg) const
85     {
86         return arg.collect(softclip::s_run<FloatType>);
87     }
88 #endif
89 };
90 
91 } /* namespace detail */
92 
93 NOVA_SIMD_DEFINE_UNARY_WRAPPER(softclip, detail::softclip)
94 
95 
96 } /* namespace nova */
97 
98 #undef always_inline
99 
100 #endif /* SIMD_SOFTCLIP_HPP */
101