AAX SDK  2.4.1
Avid Audio Extensions Development Kit
AAX_Quantize.h
Go to the documentation of this file.
1 /*================================================================================================*/
2 /*
3  * Copyright 2013-2015 by Avid Technology, Inc.
4  * All rights reserved.
5  *
6  * CONFIDENTIAL: This document contains confidential information. Do not
7  * read or examine this document unless you are an Avid Technology employee
8  * or have signed a non-disclosure agreement with Avid Technology which protects
9  * the confidentiality of this document. DO NOT DISCLOSE ANY INFORMATION
10  * CONTAINED IN THIS DOCUMENT TO ANY THIRD-PARTY WITHOUT THE PRIOR WRITTEN CONSENT
11  * OF Avid Technology, INC.
12  */
13 
20 /*================================================================================================*/
21 #pragma once
22 
23 #ifndef AAX_QUANTIZE_H
24 #define AAX_QUANTIZE_H
25 
26 #include "AAX.h"
28 #include "AAX_Constants.h"
29 
30 #if ! defined( _TMS320C6X )
31 
32 #if _MSC_VER && !defined(__INTEL_COMPILER)
33 #define _MM_FUNCTIONALITY
34 #include <intrin.h>
35 #elif LINUX_VERSION
36 #elif TARGET_OS_IPHONE
37 #elif defined(__arm__)
38 #else
39 // GNU or INTEL:
40 #include <xmmintrin.h>
41 #include <pmmintrin.h>
42 #include <tmmintrin.h>
43 #endif
44 
45 #endif
46 
47 namespace AAX
48 {
49 
50 //This assumes the floating point processor is in 64 bit IEEE mode.
51 #if ! defined( _TMS320C6X )
52 
53 // For double->Int32 conversion
54 static const double cDouble2IntBias = ldexpf(1,52)*1.5;
55 static const double cOneHalfOffset = 0.5;
56 static const int32_t cMantisaWord = cBigEndian;
57 
58 // For double->Int64 conversion
59 static const double kExponentMagicDelta = 1.5e-8; //1e^(number of exp bit)
60 static const double kBigMantissaMagicFloat = 6755399441055744.0; //2^52 * 1.5, uses limited precisicion to floor
61 static const int64_t kBigMantissaMagicMask = 0x1fffffffffffffLL; //2^52 * 1.5 mask,
62 static const int64_t kBigMantissaMagicInt = 0x18000000000000LL; //2^52 * 1.5, uses limited precisicion to floor
63 #endif
64 
65 /*===================================================================================================*/
66 
75 inline int32_t FastRound2Int32(double iVal)
76 {
77 #if defined( _TMS320C6X )
78  // rounding mode set by the CSR register
79  const int32_t r = _dpint(iVal);
80  return r;
81 #else
82  iVal += cDouble2IntBias;
83  return (reinterpret_cast<int32_t*>(&iVal))[cMantisaWord];
84 #endif
85 }
86 
95 inline int32_t FastRound2Int32(float iVal)
96 {
97 #if defined( _TMS320C6X )
98  // rounding mode set by the CSR register
99  const int32_t r = _spint(iVal);
100  return r;
101 #else
102  return FastRound2Int32(double(iVal));
103 #endif
104 }
105 
106 
109 inline int32_t FastRndDbl2Int32(double iVal)
110 {
111  return FastRound2Int32(iVal);
112 }
113 
126 inline int32_t FastTrunc2Int32(double iVal)
127 {
128 #if defined( _TMS320C6X )
129  // rounding mode set by the CSR register
130  const int32_t r = _dpint(iVal - 0.5);
131  return r;
132 #else
133  return FastRound2Int32(iVal - 0.5);
134 #endif
135 }
136 
145 inline int32_t FastTrunc2Int32(float iVal)
146 {
147 #if defined( _TMS320C6X )
148  // rounding mode set by the CSR register
149  const int32_t r = _spint(iVal - 0.5f);
150  return r;
151 #elif _MSC_VER
152  int32_t r;
153  r = _mm_cvtt_ss2si( _mm_load_ss(&iVal) );
154  return r;
155 #else
156  return static_cast<int32_t>(iVal);
157 #endif
158 }
159 
169 inline int64_t FastRound2Int64(double iVal)
170 {
171 #if defined( _TMS320C6X )
172  return (int64_t)(iVal > 0.0 ? iVal + 0.5 : iVal - 0.5);
173 #else
174  iVal += kExponentMagicDelta; // Round to nearest
175  iVal += kBigMantissaMagicFloat; // Normalize to integer
176  int64_t result = *reinterpret_cast<int64_t*>(&iVal);
177  result &= kBigMantissaMagicMask; // Mask out upper bits of float (exponent, sign)
178  result -= kBigMantissaMagicInt; // Normalize to integer
179  return result;
180 #endif
181 }
182 
183 } // namespace AAX
184 
185 #endif // AAX_QUANTIZE_H
Various utility definitions for AAX.
Signal processing constants.
Definition: AAX_Exception.h:42
int64_t FastRound2Int64(double iVal)
Round to Int64.
Definition: AAX_Quantize.h:169
int32_t FastTrunc2Int32(double iVal)
Float to Int conversion with truncation.
Definition: AAX_Quantize.h:126
int32_t FastRndDbl2Int32(double iVal)
Definition: AAX_Quantize.h:109
int32_t FastRound2Int32(double iVal)
Round to Int32.
Definition: AAX_Quantize.h:75
const int cBigEndian
Definition: AAX_Constants.h:47