2 // Copyright (C) 2008, 2009 Tim Blechmann
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.
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.
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.
19 #ifndef SIMD_SOFTCLIP_HPP
20 #define SIMD_SOFTCLIP_HPP
27 #include "detail/define_macros.hpp"
29 #if defined(__GNUC__) && defined(NDEBUG)
30 #define always_inline inline __attribute__((always_inline))
32 #define always_inline inline
40 template <typename FloatType
>
41 static FloatType
s_run(FloatType arg
)
43 FloatType abs
= std::fabs(arg
);
44 if (abs
< FloatType(0.5))
47 return (abs
- FloatType(0.25)) / arg
;
50 template <typename FloatType
>
51 always_inline FloatType
operator()(FloatType arg
) const
56 #if defined(__SSE__) || defined(__AVX__)
57 /* this computes both parts of the branch
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
65 * on intel nethalem cpus, the simdfied code is faster than all non-simd code
68 template <typename FloatType
>
69 always_inline vec
<FloatType
> operator()(vec
<FloatType
> arg
) const
71 typedef vec
<FloatType
> vec_type
;
73 const vec_type const05
= vec_type::gen_05();
74 const vec_type
const025 (0.25);
76 vec_type abs_
= abs(arg
);
77 vec_type selecter
= mask_lt(abs_
, const05
);
78 vec_type alt_ret
= (abs_
- const025
) / arg
;
80 return select(alt_ret
, arg
, selecter
);
83 template <typename FloatType
>
84 always_inline vec
<FloatType
> operator()(vec
<FloatType
> arg
) const
86 return arg
.collect(softclip::s_run
<FloatType
>);
91 } /* namespace detail */
93 NOVA_SIMD_DEFINE_UNARY_WRAPPER(softclip
, detail::softclip
)
96 } /* namespace nova */
100 #endif /* SIMD_SOFTCLIP_HPP */