1 /*********************************************************************
2 Copyright 2013 Karl Jones
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
33 using namespace DATA_TYPE
;
36 * [n].[m] digit fixed point binary number:
38 * records fractions of integer values to precision [m] binary places
40 * stored value (using integer container) = (int or float rhs) * 2^m
42 * "2^m" is equivalent to "1 << m" which is can be literal constant.
44 * Such code is used at many points in this file.
47 //////////////////////////////////////////////////////////////////////////
49 //////////////////////////////////////////////////////////////////////////
51 template<class Type
, KUINT8 BinaryPoint
>
52 Type KFIXED
<Type
, BinaryPoint
>::convert( KFLOAT32 V
) const
54 return V
* ( 1 << BinaryPoint
);
57 //////////////////////////////////////////////////////////////////////////
59 template<class Type
, KUINT8 BinaryPoint
>
60 Type KFIXED
<Type
, BinaryPoint
>::convert( KFLOAT64 V
) const
62 return V
* ( 1 << BinaryPoint
);
65 //////////////////////////////////////////////////////////////////////////
67 //////////////////////////////////////////////////////////////////////////
69 template<class Type
, KUINT8 BinaryPoint
>
70 KFIXED
<Type
, BinaryPoint
>::KFIXED() :
75 //////////////////////////////////////////////////////////////////////////
77 template<class Type
, KUINT8 BinaryPoint
>
78 KFIXED
<Type
, BinaryPoint
>::KFIXED( KFLOAT32 V
) :
83 //////////////////////////////////////////////////////////////////////////
85 template<class Type
, KUINT8 BinaryPoint
>
86 KFIXED
<Type
, BinaryPoint
>::KFIXED( KFLOAT64 V
) :
91 //////////////////////////////////////////////////////////////////////////
93 template<class Type
, KUINT8 BinaryPoint
>
94 KFIXED
<Type
, BinaryPoint
>::KFIXED( Type V
) :
99 //////////////////////////////////////////////////////////////////////////
101 template<class Type
, KUINT8 BinaryPoint
>
102 KFIXED
<Type
, BinaryPoint
>::KFIXED( KDataStream
& stream
)
107 //////////////////////////////////////////////////////////////////////////
109 template<class Type
, KUINT8 BinaryPoint
>
110 KFIXED
<Type
, BinaryPoint
>::~KFIXED()
114 //////////////////////////////////////////////////////////////////////////
116 template<class Type
, KUINT8 BinaryPoint
>
117 void KFIXED
<Type
, BinaryPoint
>::Set( KFLOAT32 V
)
119 m_Val
= convert( V
);
122 //////////////////////////////////////////////////////////////////////////
124 template<class Type
, KUINT8 BinaryPoint
>
125 void KFIXED
<Type
, BinaryPoint
>::Set( KFLOAT64 V
)
127 m_Val
= convert( V
);
130 //////////////////////////////////////////////////////////////////////////
132 template<class Type
, KUINT8 BinaryPoint
>
133 void KFIXED
<Type
, BinaryPoint
>::Set( Type V
)
138 //////////////////////////////////////////////////////////////////////////
140 template<class Type
, KUINT8 BinaryPoint
>
141 Type KFIXED
<Type
, BinaryPoint
>::Get() const
146 //////////////////////////////////////////////////////////////////////////
148 template<class Type
, KUINT8 BinaryPoint
>
149 KFLOAT32 KFIXED
<Type
, BinaryPoint
>::GetAsFloat32() const
151 return m_Val
* ( 1.0 / ( 1 << BinaryPoint
) );
154 //////////////////////////////////////////////////////////////////////////
156 template<class Type
, KUINT8 BinaryPoint
>
157 KString KFIXED
<Type
, BinaryPoint
>::GetAsString() const
161 ss
<< GetAsFloat32() << "\n";
166 //////////////////////////////////////////////////////////////////////////
168 template<class Type
, KUINT8 BinaryPoint
>
169 void KFIXED
<Type
, BinaryPoint
>::Decode( KDataStream
& stream
)
171 if( stream
.GetBufferSize() < sizeof( Type
) )throw KException( __FUNCTION__
, NOT_ENOUGH_DATA_IN_BUFFER
);
176 //////////////////////////////////////////////////////////////////////////
178 template<class Type
, KUINT8 BinaryPoint
>
179 KDataStream KFIXED
<Type
, BinaryPoint
>::Encode() const
183 KFIXED
<Type
, BinaryPoint
>::Encode( stream
);
188 //////////////////////////////////////////////////////////////////////////
190 template<class Type
, KUINT8 BinaryPoint
>
191 void KFIXED
<Type
, BinaryPoint
>::Encode( KDataStream
& stream
) const
196 //////////////////////////////////////////////////////////////////////////
198 //////////////////////////////////////////////////////////////////////////
200 // Some macros to reduce the code a little
202 // DEFINE_OPERATOR ******************************************************************************************
203 #define DEFINE_OPERATOR( OPERATOR ) \
205 template<class Type, KUINT8 BinaryPoint> \
206 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFLOAT32 Value ) \
208 return GetAsFloat32() OPERATOR Value; \
211 template<class Type, KUINT8 BinaryPoint> \
212 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFLOAT64 Value ) \
214 return GetAsFloat32() OPERATOR Value; \
217 template<class Type, KUINT8 BinaryPoint> \
218 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR ( Type Value ) \
220 return *this OPERATOR KFIXED( Value ).GetAsFloat32(); \
223 template<class Type, KUINT8 BinaryPoint> \
224 KFIXED<Type, BinaryPoint> KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFIXED<Type, BinaryPoint> Value ) \
226 return GetAsFloat32() OPERATOR Value.GetAsFloat32(); \
229 // DEFINE_COMPARISION_OPERATOR ******************************************************************************
230 #define DEFINE_COMPARISION_OPERATOR( OPERATOR ) \
232 template<class Type, KUINT8 BinaryPoint> \
233 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFLOAT32 Value ) const \
235 return m_Val OPERATOR convert( Value ); \
238 template<class Type, KUINT8 BinaryPoint> \
239 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFLOAT64 Value ) const \
241 return m_Val OPERATOR convert( Value ); \
244 template<class Type, KUINT8 BinaryPoint> \
245 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR ( Type Value ) const \
247 return m_Val OPERATOR m_Val; \
250 template<class Type, KUINT8 BinaryPoint> \
251 KBOOL KFIXED<Type, BinaryPoint>::operator OPERATOR ( KFIXED<Type, BinaryPoint> Value ) const \
253 return m_Val OPERATOR Value.m_Val; \
256 //***************************** End Macros ******************************************************************
258 template<class Type
, KUINT8 BinaryPoint
>
259 KFIXED
<Type
, BinaryPoint
> KFIXED
<Type
, BinaryPoint
>::operator = ( KFLOAT32 Value
)
261 m_Val
= convert( Value
);
265 //////////////////////////////////////////////////////////////////////////
267 template<class Type
, KUINT8 BinaryPoint
>
268 KFIXED
<Type
, BinaryPoint
> KFIXED
<Type
, BinaryPoint
>::operator = ( KFLOAT64 Value
)
270 m_Val
= convert( Value
);
274 //////////////////////////////////////////////////////////////////////////
276 template<class Type
, KUINT8 BinaryPoint
>
277 KFIXED
<Type
, BinaryPoint
> KFIXED
<Type
, BinaryPoint
>::operator = ( Type Value
)
283 //////////////////////////////////////////////////////////////////////////
285 template<class Type
, KUINT8 BinaryPoint
>
286 KFIXED
<Type
, BinaryPoint
> KFIXED
<Type
, BinaryPoint
>::operator = ( KFIXED
<Type
, BinaryPoint
> Value
)
292 //////////////////////////////////////////////////////////////////////////
299 //////////////////////////////////////////////////////////////////////////
301 DEFINE_COMPARISION_OPERATOR( == )
302 DEFINE_COMPARISION_OPERATOR( != )
303 DEFINE_COMPARISION_OPERATOR( < )
304 DEFINE_COMPARISION_OPERATOR( <= )
305 DEFINE_COMPARISION_OPERATOR( > )
306 DEFINE_COMPARISION_OPERATOR( >= )
308 //////////////////////////////////////////////////////////////////////////
310 template<class Type
, KUINT8 BinaryPoint
>
311 KFIXED
<Type
, BinaryPoint
>::operator KFLOAT32 () const
313 return GetAsFloat32();
316 //////////////////////////////////////////////////////////////////////////
318 template<class Type
, KUINT8 BinaryPoint
>
319 KFIXED
<Type
, BinaryPoint
>::operator Type () const
324 //////////////////////////////////////////////////////////////////////////
326 // When using a template class with implementation in a cpp file, linker
327 // errors will occur if you do not pre-declare the template types in that same file.
328 // This is good for us in this case as we only need to support a binary point of 3 and 8.
329 // If you do wish to add support for other binary point positions then simply add them
330 // below and re-compile.
332 template class KDIS::DATA_TYPE::KFIXED
<KINT16
, 3>;
333 template class KDIS::DATA_TYPE::KFIXED
<KINT16
, 8>;
334 template class KDIS::DATA_TYPE::KFIXED
<KINT8
, 3>;