EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
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
9This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic
10Package, Release 3c, by John R. Hauser.
11
12Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
13University of California. All rights reserved.
14
15Redistribution and use in source and binary forms, with or without
16modification, 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
29THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
30EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
32DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
33DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36ON 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
38SOFTWARE, 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
48namespace cv
49{
50
85
86struct softfloat;
87struct softdouble;
88
90{
91public:
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
127 softfloat operator + (const softfloat&) const;
128 softfloat operator - (const softfloat&) const;
129 softfloat operator * (const softfloat&) const;
130 softfloat operator / (const softfloat&) const;
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{
228public:
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
264 softdouble operator + (const softdouble&) const;
265 softdouble operator - (const softdouble&) const;
266 softdouble operator * (const softdouble&) const;
267 softdouble operator / (const softdouble&) const;
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 {
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 {
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
372CV_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
385CV_EXPORTS int cvTrunc(const cv::softfloat& a);
386CV_EXPORTS int cvTrunc(const cv::softdouble& a);
387
389CV_EXPORTS int cvRound(const cv::softfloat& a);
390CV_EXPORTS int cvRound(const cv::softdouble& a);
391
393CV_EXPORTS int64_t cvRound64(const cv::softdouble& a);
394
396CV_EXPORTS int cvFloor(const cv::softfloat& a);
397CV_EXPORTS int cvFloor(const cv::softdouble& a);
398
400CV_EXPORTS int cvCeil(const cv::softfloat& a);
401CV_EXPORTS int cvCeil(const cv::softdouble& a);
402
403namespace cv
404{
406template<typename _Tp> static inline _Tp saturate_cast(softfloat a) { return _Tp(a); }
407template<typename _Tp> static inline _Tp saturate_cast(softdouble a) { return _Tp(a); }
408
409template<> inline uchar saturate_cast<uchar>(softfloat a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
410template<> inline uchar saturate_cast<uchar>(softdouble a) { return (uchar)std::max(std::min(cvRound(a), (int)UCHAR_MAX), 0); }
411
412template<> inline schar saturate_cast<schar>(softfloat a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
413template<> inline schar saturate_cast<schar>(softdouble a) { return (schar)std::min(std::max(cvRound(a), (int)SCHAR_MIN), (int)SCHAR_MAX); }
414
415template<> inline ushort saturate_cast<ushort>(softfloat a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
416template<> inline ushort saturate_cast<ushort>(softdouble a) { return (ushort)std::max(std::min(cvRound(a), (int)USHRT_MAX), 0); }
417
418template<> inline short saturate_cast<short>(softfloat a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
419template<> inline short saturate_cast<short>(softdouble a) { return (short)std::min(std::max(cvRound(a), (int)SHRT_MIN), (int)SHRT_MAX); }
420
421template<> inline int saturate_cast<int>(softfloat a) { return cvRound(a); }
422template<> inline int saturate_cast<int>(softdouble a) { return cvRound(a); }
423
424template<> inline int64_t saturate_cast<int64_t>(softfloat a) { return cvRound(a); }
425template<> inline int64_t saturate_cast<int64_t>(softdouble a) { return cvRound64(a); }
426
430template<> inline unsigned saturate_cast<unsigned>(softfloat a) { return cvRound(a); }
431template<> inline unsigned saturate_cast<unsigned>(softdouble a) { return cvRound(a); }
432
433template<> inline uint64_t saturate_cast<uint64_t>(softfloat a) { return cvRound(a); }
434template<> inline uint64_t saturate_cast<uint64_t>(softdouble a) { return cvRound64(a); }
435
437inline softfloat min(const softfloat& a, const softfloat& b) { return (a > b) ? b : a; }
438inline softdouble min(const softdouble& a, const softdouble& b) { return (a > b) ? b : a; }
439
440inline softfloat max(const softfloat& a, const softfloat& b) { return (a > b) ? a : b; }
441inline softdouble max(const softdouble& a, const softdouble& b) { return (a > b) ? a : b; }
442
444inline softfloat abs( softfloat a) { softfloat x; x.v = a.v & ((1U << 31) - 1); return x; }
445inline softdouble abs( softdouble a) { softdouble x; x.v = a.v & ((1ULL << 63) - 1); return x; }
446
456
465
483CV_EXPORTS softfloat pow( const softfloat& a, const softfloat& b);
485
493
501
509
511
512} // cv::
513
514#endif
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
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
softdouble & operator=(const softdouble &c)
Assign constructor.
Definition softfloat.hpp:234
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
softfloat abs(softfloat a)
Absolute value.
Definition softfloat.hpp:444
int64_t saturate_cast< int64_t >(softfloat a)
Definition softfloat.hpp:424
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
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
softdouble(const int64_t)
softdouble(const uint64_t)
softfloat(const int32_t)
static softdouble inf()
Positive infinity constant.
Definition softfloat.hpp:348
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
softfloat & operator=(const softfloat &c)
Assign constructor.
Definition softfloat.hpp:97
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 > cos(const Quat< T > &q)
Quat< T > sin(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
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