EstervQrCode 1.1.1
Library for qr code manipulation
softfloat.hpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html
4 
5 // This file is based on files from package issued with the following license:
6 
7 /*============================================================================
8 
9 This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic
10 Package, Release 3c, by John R. Hauser.
11 
12 Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
13 University of California. All rights reserved.
14 
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17 
18  1. Redistributions of source code must retain the above copyright notice,
19  this list of conditions, and the following disclaimer.
20 
21  2. Redistributions in binary form must reproduce the above copyright notice,
22  this list of conditions, and the following disclaimer in the documentation
23  and/or other materials provided with the distribution.
24 
25  3. Neither the name of the University nor the names of its contributors may
26  be used to endorse or promote products derived from this software without
27  specific prior written permission.
28 
29 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
30 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
32 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
33 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 =============================================================================*/
41 
42 #pragma once
43 #ifndef softfloat_h
44 #define softfloat_h 1
45 
46 #include "cvdef.h"
47 
48 namespace cv
49 {
50 
85 
86 struct softfloat;
87 struct softdouble;
88 
90 {
91 public:
93  softfloat() { v = 0; }
95  softfloat( const softfloat& c) { v = c.v; }
98  {
99  if(&c != this) v = c.v;
100  return *this;
101  }
106  static const softfloat fromRaw( const uint32_t a ) { softfloat x; x.v = a; return x; }
107 
109  explicit softfloat( const uint32_t );
110  explicit softfloat( const uint64_t );
111  explicit softfloat( const int32_t );
112  explicit softfloat( const int64_t );
113 
114 #ifdef CV_INT32_T_IS_LONG_INT
115  // for platforms with int32_t = long int
116  explicit softfloat( const int a ) { *this = softfloat(static_cast<int32_t>(a)); }
117 #endif
118 
120  explicit softfloat( const float a ) { Cv32suf s; s.f = a; v = s.u; }
121 
123  operator softdouble() const;
124  operator float() const { Cv32suf s; s.u = v; return s.f; }
125 
131  softfloat operator - () const { softfloat x; x.v = v ^ (1U << 31); return x; }
132 
146  softfloat operator % (const softfloat&) const;
147 
148  softfloat& operator += (const softfloat& a) { *this = *this + a; return *this; }
149  softfloat& operator -= (const softfloat& a) { *this = *this - a; return *this; }
150  softfloat& operator *= (const softfloat& a) { *this = *this * a; return *this; }
151  softfloat& operator /= (const softfloat& a) { *this = *this / a; return *this; }
152  softfloat& operator %= (const softfloat& a) { *this = *this % a; return *this; }
153 
160  bool operator == ( const softfloat& ) const;
161  bool operator != ( const softfloat& ) const;
162  bool operator > ( const softfloat& ) const;
163  bool operator >= ( const softfloat& ) const;
164  bool operator < ( const softfloat& ) const;
165  bool operator <= ( const softfloat& ) const;
166 
168  inline bool isNaN() const { return (v & 0x7fffffff) > 0x7f800000; }
170  inline bool isInf() const { return (v & 0x7fffffff) == 0x7f800000; }
172  inline bool isSubnormal() const { return ((v >> 23) & 0xFF) == 0; }
173 
175  inline bool getSign() const { return (v >> 31) != 0; }
177  inline softfloat setSign(bool sign) const { softfloat x; x.v = (v & ((1U << 31) - 1)) | ((uint32_t)sign << 31); return x; }
179  inline int getExp() const { return ((v >> 23) & 0xFF) - 127; }
181  inline softfloat setExp(int e) const { softfloat x; x.v = (v & 0x807fffff) | (((e + 127) & 0xFF) << 23 ); return x; }
182 
187  inline softfloat getFrac() const
188  {
189  uint_fast32_t vv = (v & 0x007fffff) | (127 << 23);
190  return softfloat::fromRaw(vv);
191  }
196  inline softfloat setFrac(const softfloat& s) const
197  {
198  softfloat x;
199  x.v = (v & 0xff800000) | (s.v & 0x007fffff);
200  return x;
201  }
202 
204  static softfloat zero() { return softfloat::fromRaw( 0 ); }
206  static softfloat inf() { return softfloat::fromRaw( 0xFF << 23 ); }
208  static softfloat nan() { return softfloat::fromRaw( 0x7fffffff ); }
210  static softfloat one() { return softfloat::fromRaw( 127 << 23 ); }
212  static softfloat min() { return softfloat::fromRaw( 0x01 << 23 ); }
214  static softfloat eps() { return softfloat::fromRaw( (127 - 23) << 23 ); }
216  static softfloat max() { return softfloat::fromRaw( (0xFF << 23) - 1 ); }
218  static softfloat pi() { return softfloat::fromRaw( 0x40490fdb ); }
219 
220  uint32_t v;
221 };
222 
223 /*----------------------------------------------------------------------------
224 *----------------------------------------------------------------------------*/
225 
227 {
228 public:
230  softdouble() : v(0) { }
232  softdouble( const softdouble& c) { v = c.v; }
235  {
236  if(&c != this) v = c.v;
237  return *this;
238  }
243  static softdouble fromRaw( const uint64_t a ) { softdouble x; x.v = a; return x; }
244 
246  explicit softdouble( const uint32_t );
247  explicit softdouble( const uint64_t );
248  explicit softdouble( const int32_t );
249  explicit softdouble( const int64_t );
250 
251 #ifdef CV_INT32_T_IS_LONG_INT
252  // for platforms with int32_t = long int
253  explicit softdouble( const int a ) { *this = softdouble(static_cast<int32_t>(a)); }
254 #endif
255 
257  explicit softdouble( const double a ) { Cv64suf s; s.f = a; v = s.u; }
258 
260  operator softfloat() const;
261  operator double() const { Cv64suf s; s.u = v; return s.f; }
262 
268  softdouble operator - () const { softdouble x; x.v = v ^ (1ULL << 63); return x; }
269 
283  softdouble operator % (const softdouble&) const;
284 
285  softdouble& operator += (const softdouble& a) { *this = *this + a; return *this; }
286  softdouble& operator -= (const softdouble& a) { *this = *this - a; return *this; }
287  softdouble& operator *= (const softdouble& a) { *this = *this * a; return *this; }
288  softdouble& operator /= (const softdouble& a) { *this = *this / a; return *this; }
289  softdouble& operator %= (const softdouble& a) { *this = *this % a; return *this; }
290 
297  bool operator == ( const softdouble& ) const;
298  bool operator != ( const softdouble& ) const;
299  bool operator > ( const softdouble& ) const;
300  bool operator >= ( const softdouble& ) const;
301  bool operator < ( const softdouble& ) const;
302  bool operator <= ( const softdouble& ) const;
303 
305  inline bool isNaN() const { return (v & 0x7fffffffffffffff) > 0x7ff0000000000000; }
307  inline bool isInf() const { return (v & 0x7fffffffffffffff) == 0x7ff0000000000000; }
309  inline bool isSubnormal() const { return ((v >> 52) & 0x7FF) == 0; }
310 
312  inline bool getSign() const { return (v >> 63) != 0; }
314  softdouble setSign(bool sign) const { softdouble x; x.v = (v & ((1ULL << 63) - 1)) | ((uint_fast64_t)(sign) << 63); return x; }
316  inline int getExp() const { return ((v >> 52) & 0x7FF) - 1023; }
318  inline softdouble setExp(int e) const
319  {
320  softdouble x;
321  x.v = (v & 0x800FFFFFFFFFFFFF) | ((uint_fast64_t)((e + 1023) & 0x7FF) << 52);
322  return x;
323  }
324 
329  inline softdouble getFrac() const
330  {
331  uint_fast64_t vv = (v & 0x000FFFFFFFFFFFFF) | ((uint_fast64_t)(1023) << 52);
332  return softdouble::fromRaw(vv);
333  }
338  inline softdouble setFrac(const softdouble& s) const
339  {
340  softdouble x;
341  x.v = (v & 0xFFF0000000000000) | (s.v & 0x000FFFFFFFFFFFFF);
342  return x;
343  }
344 
346  static softdouble zero() { return softdouble::fromRaw( 0 ); }
348  static softdouble inf() { return softdouble::fromRaw( (uint_fast64_t)(0x7FF) << 52 ); }
350  static softdouble nan() { return softdouble::fromRaw( CV_BIG_INT(0x7FFFFFFFFFFFFFFF) ); }
352  static softdouble one() { return softdouble::fromRaw( (uint_fast64_t)( 1023) << 52 ); }
354  static softdouble min() { return softdouble::fromRaw( (uint_fast64_t)( 0x01) << 52 ); }
356  static softdouble eps() { return softdouble::fromRaw( (uint_fast64_t)( 1023 - 52 ) << 52 ); }
358  static softdouble max() { return softdouble::fromRaw( ((uint_fast64_t)(0x7FF) << 52) - 1 ); }
360  static softdouble pi() { return softdouble::fromRaw( CV_BIG_INT(0x400921FB54442D18) ); }
361 
362  uint64_t v;
363 };
364 
365 /*----------------------------------------------------------------------------
366 *----------------------------------------------------------------------------*/
367 
372 CV_EXPORTS softfloat mulAdd( const softfloat& a, const softfloat& b, const softfloat & c);
374 
378 }
379 
380 /*----------------------------------------------------------------------------
381 | Ported from OpenCV and added for usability
382 *----------------------------------------------------------------------------*/
383 
385 CV_EXPORTS int cvTrunc(const cv::softfloat& a);
386 CV_EXPORTS int cvTrunc(const cv::softdouble& a);
387 
389 CV_EXPORTS int cvRound(const cv::softfloat& a);
390 CV_EXPORTS int cvRound(const cv::softdouble& a);
391 
393 CV_EXPORTS int64_t cvRound64(const cv::softdouble& a);
394 
396 CV_EXPORTS int cvFloor(const cv::softfloat& a);
397 CV_EXPORTS int cvFloor(const cv::softdouble& a);
398 
400 CV_EXPORTS int cvCeil(const cv::softfloat& a);
401 CV_EXPORTS int cvCeil(const cv::softdouble& a);
402 
403 namespace cv
404 {
406 template<typename _Tp> static inline _Tp saturate_cast(softfloat a) { return _Tp(a); }
407 template<typename _Tp> static inline _Tp saturate_cast(softdouble a) { return _Tp(a); }
408 
409 template<> inline uchar saturate_cast<uchar>(softfloat a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
410 template<> inline uchar saturate_cast<uchar>(softdouble a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
411 
412 template<> inline schar saturate_cast<schar>(softfloat a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
413 template<> inline schar saturate_cast<schar>(softdouble a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
414 
415 template<> inline ushort saturate_cast<ushort>(softfloat a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
416 template<> inline ushort saturate_cast<ushort>(softdouble a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
417 
418 template<> inline short saturate_cast<short>(softfloat a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
419 template<> inline short saturate_cast<short>(softdouble a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
420 
421 template<> inline int saturate_cast<int>(softfloat a) { return cvRound(a); }
422 template<> inline int saturate_cast<int>(softdouble a) { return cvRound(a); }
423 
424 template<> inline int64_t saturate_cast<int64_t>(softfloat a) { return cvRound(a); }
425 template<> inline int64_t saturate_cast<int64_t>(softdouble a) { return cvRound64(a); }
426 
430 template<> inline unsigned saturate_cast<unsigned>(softfloat a) { return cvRound(a); }
431 template<> inline unsigned saturate_cast<unsigned>(softdouble a) { return cvRound(a); }
432 
433 template<> inline uint64_t saturate_cast<uint64_t>(softfloat a) { return cvRound(a); }
434 template<> inline uint64_t saturate_cast<uint64_t>(softdouble a) { return cvRound64(a); }
435 
437 inline softfloat min(const softfloat& a, const softfloat& b) { return (a > b) ? b : a; }
438 inline softdouble min(const softdouble& a, const softdouble& b) { return (a > b) ? b : a; }
439 
440 inline softfloat max(const softfloat& a, const softfloat& b) { return (a > b) ? a : b; }
441 inline softdouble max(const softdouble& a, const softdouble& b) { return (a > b) ? a : b; }
442 
444 inline softfloat abs( softfloat a) { softfloat x; x.v = a.v & ((1U << 31) - 1); return x; }
445 inline softdouble abs( softdouble a) { softdouble x; x.v = a.v & ((1ULL << 63) - 1); return x; }
446 
456 
465 
485 
493 
501 
509 
511 
512 } // cv::
513 
514 #endif
static bool operator!=(const Matx< _Tp, m, n > &a, const Matx< _Tp, m, n > &b)
static bool operator==(const Matx< _Tp, m, n > &a, const Matx< _Tp, m, n > &b)
const CvArr CvArr * x
Definition: core_c.h:1195
signed char schar
Definition: interface.h:48
unsigned char uchar
Definition: interface.h:51
#define CV_BIG_INT(n)
Definition: interface.h:63
unsigned short ushort
Definition: interface.h:52
CV_INLINE v_reg< _Tp, n > & operator-=(v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
CV_INLINE v_reg< _Tp, n > & operator*=(v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
CV_INLINE v_reg< _Tp, n > & operator/=(v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
CV_INLINE v_reg< _Tp, n > operator/(const v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
Divide values.
CV_INLINE v_reg< _Tp, n > & operator+=(v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
int getExp() const
Get 0-based exponent.
Definition: softfloat.hpp:179
softfloat(const float a)
Construct from float.
Definition: softfloat.hpp:120
softfloat getFrac() const
Get a fraction part.
Definition: softfloat.hpp:187
CV_EXPORTS softfloat mulAdd(const softfloat &a, const softfloat &b, const softfloat &c)
Fused Multiplication and Addition.
uint64_t v
Definition: softfloat.hpp:362
softdouble(const uint32_t)
Construct from integer.
static softfloat inf()
Positive infinity constant.
Definition: softfloat.hpp:206
softfloat(const uint64_t)
softfloat(const softfloat &c)
Copy constructor.
Definition: softfloat.hpp:95
static softfloat one()
One constant.
Definition: softfloat.hpp:210
softdouble setExp(int e) const
Construct a copy with new 0-based exponent.
Definition: softfloat.hpp:318
static softfloat min()
Smallest normalized value.
Definition: softfloat.hpp:212
softfloat setSign(bool sign) const
Construct a copy with new sign bit.
Definition: softfloat.hpp:177
bool getSign() const
Get sign bit.
Definition: softfloat.hpp:312
softfloat setFrac(const softfloat &s) const
Construct a copy with provided significand.
Definition: softfloat.hpp:196
bool isInf() const
Inf state indicator.
Definition: softfloat.hpp:170
softfloat setExp(int e) const
Construct a copy with new 0-based exponent.
Definition: softfloat.hpp:181
softfloat()
Default constructor.
Definition: softfloat.hpp:93
static softdouble max()
Biggest finite value.
Definition: softfloat.hpp:358
static softdouble pi()
Correct pi approximation.
Definition: softfloat.hpp:360
softdouble setFrac(const softdouble &s) const
Construct a copy with provided significand.
Definition: softfloat.hpp:338
bool isInf() const
Inf state indicator.
Definition: softfloat.hpp:307
bool isNaN() const
NaN state indicator.
Definition: softfloat.hpp:168
bool isSubnormal() const
Subnormal number indicator.
Definition: softfloat.hpp:172
softdouble(const softdouble &c)
Copy constructor.
Definition: softfloat.hpp:232
static softfloat max()
Biggest finite value.
Definition: softfloat.hpp:216
softfloat(const int64_t)
static softdouble zero()
Zero constant.
Definition: softfloat.hpp:346
int64_t saturate_cast< int64_t >(softfloat a)
Definition: softfloat.hpp:424
softfloat max(const softfloat &a, const softfloat &b)
Definition: softfloat.hpp:440
softfloat(const uint32_t)
Construct from integer.
static softfloat eps()
Difference between 1 and next representable value.
Definition: softfloat.hpp:214
static softdouble one()
One constant.
Definition: softfloat.hpp:352
static softfloat zero()
Zero constant.
Definition: softfloat.hpp:204
softdouble(const int32_t)
softdouble setSign(bool sign) const
Construct a copy with new sign bit.
Definition: softfloat.hpp:314
softfloat & operator=(const softfloat &c)
Assign constructor.
Definition: softfloat.hpp:97
static softdouble eps()
Difference between 1 and next representable value.
Definition: softfloat.hpp:356
uint32_t v
Definition: softfloat.hpp:220
static softfloat nan()
Default NaN constant.
Definition: softfloat.hpp:208
CV_EXPORTS softfloat cbrt(const softfloat &a)
Cube root.
static softdouble min()
Smallest normalized value.
Definition: softfloat.hpp:354
bool isSubnormal() const
Subnormal number indicator.
Definition: softfloat.hpp:309
int getExp() const
Get 0-based exponent.
Definition: softfloat.hpp:316
softdouble(const double a)
Construct from double.
Definition: softfloat.hpp:257
softdouble getFrac() const
Get a fraction part.
Definition: softfloat.hpp:329
uint64_t saturate_cast< uint64_t >(softfloat a)
Definition: softfloat.hpp:433
static softfloat pi()
Correct pi approximation.
Definition: softfloat.hpp:218
softfloat min(const softfloat &a, const softfloat &b)
Min and Max functions.
Definition: softfloat.hpp:437
CV_EXPORTS softfloat pow(const softfloat &a, const softfloat &b)
Raising to the power.
softdouble(const int64_t)
softdouble(const uint64_t)
softfloat(const int32_t)
static softdouble inf()
Positive infinity constant.
Definition: softfloat.hpp:348
softdouble & operator=(const softdouble &c)
Assign constructor.
Definition: softfloat.hpp:234
bool isNaN() const
NaN state indicator.
Definition: softfloat.hpp:305
bool getSign() const
Get sign bit.
Definition: softfloat.hpp:175
static const softfloat fromRaw(const uint32_t a)
Construct from raw.
Definition: softfloat.hpp:106
softdouble()
Default constructor.
Definition: softfloat.hpp:230
static softdouble nan()
Default NaN constant.
Definition: softfloat.hpp:350
static softdouble fromRaw(const uint64_t a)
Construct from raw.
Definition: softfloat.hpp:243
int saturate_cast< int >(unsigned v)
Definition: saturate.hpp:138
#define CV_EXPORTS
Definition: cvdef.h:435
uchar saturate_cast< uchar >(schar v)
Definition: saturate.hpp:101
CV_INLINE int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:200
CV_INLINE int cvCeil(double value)
Rounds floating-point number to the nearest integer not smaller than the original.
Definition: fast_math.hpp:258
short saturate_cast< short >(ushort v)
Definition: saturate.hpp:130
ushort saturate_cast< ushort >(schar v)
Definition: saturate.hpp:121
unsigned saturate_cast< unsigned >(schar v)
Definition: saturate.hpp:144
static _Tp saturate_cast(uchar v)
Template function for accurate conversion from one primitive type to another.
Definition: saturate.hpp:81
CV_INLINE int cvFloor(double value)
Rounds floating-point number to the nearest integer not larger than the original.
Definition: fast_math.hpp:231
schar saturate_cast< schar >(uchar v)
Definition: saturate.hpp:111
Quat< T > sin(const Quat< T > &q)
Quat< S > sqrt(const Quat< S > &q, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT)
Quat< T > cos(const Quat< T > &q)
T max(T... args)
T min(T... args)
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
DualQuat< T > operator+(const T a, const DualQuat< T > &q)
Definition: dualquaternion.inl.hpp:243
static bool operator<(const FileNodeIterator &it1, const FileNodeIterator &it2)
Definition: persistence.hpp:1303
DualQuat< T > operator*(const T a, const DualQuat< T > &q)
Definition: dualquaternion.inl.hpp:274
DualQuat< T > log(const DualQuat< T > &dq, QuatAssumeType assumeUnit=QUAT_ASSUME_NOT_UNIT)
Definition: dualquaternion.inl.hpp:344
DualQuat< T > operator-(const DualQuat< T > &q, const T a)
Definition: dualquaternion.inl.hpp:255
DualQuat< T > exp(const DualQuat< T > &dq)
Definition: dualquaternion.inl.hpp:312
static uchar abs(uchar a)
Definition: cvstd.hpp:66
Definition: softfloat.hpp:227
Definition: softfloat.hpp:90
Definition: cvdef.h:402
unsigned u
Definition: cvdef.h:404
float f
Definition: cvdef.h:405
Definition: cvdef.h:410
double f
Definition: cvdef.h:413
uint64 u
Definition: cvdef.h:412