AAX SDK 2.8.0
Avid Audio Extensions Development Kit
Loading...
Searching...
No Matches
AAX_Quantize.h
Go to the documentation of this file.
1/*================================================================================================*/
2/*
3 * Copyright 2013-2015, 2019, 2021, 2023-2024 Avid Technology, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the Avid AAX SDK.
7 *
8 * The AAX SDK is subject to commercial or open-source licensing.
9 *
10 * By using the AAX SDK, you agree to the terms of both the Avid AAX SDK License
11 * Agreement and Avid Privacy Policy.
12 *
13 * AAX SDK License: https://developer.avid.com/aax
14 * Privacy Policy: https://www.avid.com/legal/privacy-policy-statement
15 *
16 * Or: You may also use this code under the terms of the GPL v3 (see
17 * www.gnu.org/licenses).
18 *
19 * THE AAX SDK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 * EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 * DISCLAIMED.
22 */
23
30/*================================================================================================*/
31#pragma once
32
33#ifndef AAX_QUANTIZE_H
34#define AAX_QUANTIZE_H
35
36#include "AAX.h"
38#include "AAX_Constants.h"
39
40#if ! defined( _TMS320C6X )
41
42#if _MSC_VER && !defined(__INTEL_COMPILER)
43#define _MM_FUNCTIONALITY
44#include <intrin.h>
45#elif LINUX_VERSION
46#elif TARGET_OS_IPHONE
47#elif defined(__arm__)
48#elif defined(__arm64__)
49#else
50// GNU or INTEL:
51#include <xmmintrin.h>
52#include <pmmintrin.h>
53#include <tmmintrin.h>
54#endif
55
56#endif
57
58namespace AAX
59{
60
61//This assumes the floating point processor is in 64 bit IEEE mode.
62#if ! defined( _TMS320C6X )
63
64// For double->Int32 conversion
65static const double cDouble2IntBias = ldexpf(1,52)*1.5;
66static const double cOneHalfOffset = 0.5;
67static const int32_t cMantisaWord = cBigEndian;
68
69// For double->Int64 conversion
70static const double kExponentMagicDelta = 1.5e-8; //1e^(number of exp bit)
71static const double kBigMantissaMagicFloat = 6755399441055744.0; //2^52 * 1.5, uses limited precisicion to floor
72static const int64_t kBigMantissaMagicMask = 0x1fffffffffffffLL; //2^52 * 1.5 mask,
73static const int64_t kBigMantissaMagicInt = 0x18000000000000LL; //2^52 * 1.5, uses limited precisicion to floor
74#endif
75
76/*===================================================================================================*/
77
86inline int32_t FastRound2Int32(double iVal)
87{
88#if defined( _TMS320C6X )
89 // rounding mode set by the CSR register
90 const int32_t r = _dpint(iVal);
91 return r;
92#else
93 iVal += cDouble2IntBias;
94 return (reinterpret_cast<int32_t*>(&iVal))[cMantisaWord];
95#endif
96}
97
106inline int32_t FastRound2Int32(float iVal)
107{
108#if defined( _TMS320C6X )
109 // rounding mode set by the CSR register
110 const int32_t r = _spint(iVal);
111 return r;
112#else
113 return FastRound2Int32(double(iVal));
114#endif
115}
116
117
120inline int32_t FastRndDbl2Int32(double iVal)
121{
122 return FastRound2Int32(iVal);
123}
124
137inline int32_t FastTrunc2Int32(double iVal)
138{
139#if defined( _TMS320C6X )
140 // rounding mode set by the CSR register
141 const int32_t r = _dpint(iVal - 0.5);
142 return r;
143#else
144 return FastRound2Int32(iVal - 0.5);
145#endif
146}
147
156inline int32_t FastTrunc2Int32(float iVal)
157{
158#if defined( _TMS320C6X )
159 // rounding mode set by the CSR register
160 const int32_t r = _spint(iVal - 0.5f);
161 return r;
162#elif _MSC_VER
163 int32_t r;
164 r = _mm_cvtt_ss2si( _mm_load_ss(&iVal) );
165 return r;
166#else
167 return static_cast<int32_t>(iVal);
168#endif
169}
170
180inline int64_t FastRound2Int64(double iVal)
181{
182#if defined( _TMS320C6X )
183 return (int64_t)(iVal > 0.0 ? iVal + 0.5 : iVal - 0.5);
184#else
185 iVal += kExponentMagicDelta; // Round to nearest
186 iVal += kBigMantissaMagicFloat; // Normalize to integer
187 int64_t result = *reinterpret_cast<int64_t*>(&iVal);
188 result &= kBigMantissaMagicMask; // Mask out upper bits of float (exponent, sign)
189 result -= kBigMantissaMagicInt; // Normalize to integer
190 return result;
191#endif
192}
193
194} // namespace AAX
195
196#endif // AAX_QUANTIZE_H
Various utility definitions for AAX.
Signal processing constants.
Definition: AAX_EnvironmentUtilities.h:72
int64_t FastRound2Int64(double iVal)
Round to Int64.
Definition: AAX_Quantize.h:180
int32_t FastTrunc2Int32(double iVal)
Float to Int conversion with truncation.
Definition: AAX_Quantize.h:137
int32_t FastRndDbl2Int32(double iVal)
Definition: AAX_Quantize.h:120
int32_t FastRound2Int32(double iVal)
Round to Int32.
Definition: AAX_Quantize.h:86
const int cBigEndian
Definition: AAX_Constants.h:57