memcpy: hide some memory latencies
[nova-simd.git] / softclip.hpp
blob1530bc30bb86029961d1b63774faa1c8673d3015
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.
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
22 #include <cassert>
23 #include <cmath>
25 #include "vec.hpp"
27 #include "detail/define_macros.hpp"
29 #if defined(__GNUC__) && defined(NDEBUG)
30 #define always_inline inline __attribute__((always_inline))
31 #else
32 #define always_inline inline
33 #endif
35 namespace nova {
36 namespace detail {
38 struct softclip
40 template <typename FloatType>
41 static FloatType s_run(FloatType arg)
43 FloatType abs = std::fabs(arg);
44 if (abs < FloatType(0.5))
45 return arg;
46 else
47 return (abs - FloatType(0.25)) / arg;
50 template <typename FloatType>
51 always_inline FloatType operator()(FloatType arg) const
53 return s_run(arg);
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
66 * */
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);
82 #else
83 template <typename FloatType>
84 always_inline vec<FloatType> operator()(vec<FloatType> arg) const
86 return arg.collect(softclip::s_run<FloatType>);
88 #endif
91 } /* namespace detail */
93 NOVA_SIMD_DEFINE_UNARY_WRAPPER(softclip, detail::softclip)
96 } /* namespace nova */
98 #undef always_inline
100 #endif /* SIMD_SOFTCLIP_HPP */