memcpy: hide some memory latencies
[nova-simd.git] / simd_unit_conversion.hpp
blob460f96860d01f1594a48847c00a20129fbe6de1d
1 // simdfied unit conversion functions
2 // Copyright (C) 2010 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_UNIT_CONVERSION_HPP
20 #define SIMD_UNIT_CONVERSION_HPP
22 #include "simd_unary_arithmetic.hpp"
23 #include "simd_math.hpp"
25 #if defined(__GNUC__) && defined(NDEBUG)
26 #define always_inline inline __attribute__((always_inline))
27 #else
28 #define always_inline inline
29 #endif
31 namespace nova {
32 namespace detail {
34 struct abs_log2
36 template <typename FloatType>
37 always_inline FloatType operator()(const FloatType & arg) const
39 return log2_()(fabs_()(arg));
43 struct midi2freq
45 template <typename FloatType>
46 always_inline FloatType operator()(const FloatType & midi) const
48 return (FloatType)440. * pow_()(FloatType(2.),
49 (midi - FloatType(69.)) * FloatType(0.083333333333));
53 struct freq2midi
55 template <typename FloatType>
56 always_inline FloatType operator()(const FloatType & freq) const
58 return abs_log2()(freq * FloatType(0.0022727272727)) * FloatType(12.) + FloatType(69.);
62 struct midi2ratio
64 template <typename FloatType>
65 always_inline FloatType operator()(const FloatType & midi) const
67 return pow_()(FloatType(2.), midi * FloatType(0.083333333333));
71 struct ratio2midi
73 template <typename FloatType>
74 always_inline FloatType operator()(const FloatType & ratio) const
76 return FloatType(12.) * log2_()(ratio);
80 struct oct2freq
82 template <typename FloatType>
83 always_inline FloatType operator()(const FloatType & note) const
85 return FloatType(440.) * pow_()(FloatType(2.), note - FloatType(4.75));
90 struct freq2oct
92 template <typename FloatType>
93 always_inline FloatType operator()(const FloatType & freq) const
95 return abs_log2()(freq * FloatType(0.0022727272727)) + FloatType(4.75);
99 struct amp2db
101 template <typename FloatType>
102 always_inline FloatType operator()(const FloatType & amp) const
104 return log10_()(fabs_()(amp)) * FloatType(20.);
108 struct db2amp
110 template <typename FloatType>
111 always_inline FloatType operator()(const FloatType & db) const
113 return pow_()(FloatType(10.), db * FloatType(0.05));
120 NOVA_SIMD_DEFINE_UNARY_WRAPPER(midi2freq, detail::midi2freq)
121 NOVA_SIMD_DEFINE_UNARY_WRAPPER(freq2midi, detail::freq2midi)
123 NOVA_SIMD_DEFINE_UNARY_WRAPPER(midi2ratio, detail::midi2ratio)
124 NOVA_SIMD_DEFINE_UNARY_WRAPPER(ratio2midi, detail::ratio2midi)
126 NOVA_SIMD_DEFINE_UNARY_WRAPPER(oct2freq, detail::oct2freq)
127 NOVA_SIMD_DEFINE_UNARY_WRAPPER(freq2oct, detail::freq2oct)
129 NOVA_SIMD_DEFINE_UNARY_WRAPPER(amp2db, detail::amp2db)
130 NOVA_SIMD_DEFINE_UNARY_WRAPPER(db2amp, detail::db2amp)
134 #undef always_inline
136 #endif /* SIMD_UNIT_CONVERSION_HPP */