OpenCV 4.5.3(日本語機械翻訳)
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
89 struct CV_EXPORTS softfloat
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
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
226 struct CV_EXPORTS softdouble
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
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 {
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);
373CV_EXPORTS softdouble mulAdd( const softdouble& a, const softdouble& b, const softdouble& c);
374
376 CV_EXPORTS softfloat sqrt( const softfloat& a );
377CV_EXPORTS softdouble sqrt( const softdouble& a );
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
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
454 CV_EXPORTS softfloat exp( const softfloat& a);
455CV_EXPORTS softdouble exp( const softdouble& a);
456
463 CV_EXPORTS softfloat log( const softfloat& a );
464CV_EXPORTS softdouble log( const softdouble& a );
465
483 CV_EXPORTS softfloat pow( const softfloat& a, const softfloat& b);
484CV_EXPORTS softdouble pow( const softdouble& a, const softdouble& b);
485
492 CV_EXPORTS softfloat cbrt( const softfloat& a );
493
500 CV_EXPORTS softdouble sin( const softdouble& a );
501
508 CV_EXPORTS softdouble cos( const softdouble& a );
509
511
512} // cv::
513
514 #endif
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
CV_EXPORTS_W void sqrt(InputArray src, OutputArray dst)
Calculates a square root of array elements.
CV_EXPORTS_W void exp(InputArray src, OutputArray dst)
Calculates the exponent of every array element.
CV_EXPORTS_W void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
CV_EXPORTS_W void log(InputArray src, OutputArray dst)
Calculates the natural logarithm of every array element.
CV_INLINE v_reg< _Tp, n > operator/(const v_reg< _Tp, n > &a, const v_reg< _Tp, n > &b)
Divide values
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
softdouble(const uint32_t)
Construct from integer
static softfloat inf()
Positive infinity constant
Definition: softfloat.hpp:206
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
static softdouble zero()
Zero constant
Definition: softfloat.hpp:346
softfloat abs(softfloat a)
Absolute value
Definition: softfloat.hpp:444
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 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
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
static softfloat pi()
Correct pi approximation
Definition: softfloat.hpp:218
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
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:254
static _Tp saturate_cast(uchar v)
Template function for accurate conversion from one primitive type to another.
Definition: saturate.hpp:80
CV_INLINE int cvFloor(double value)
Rounds floating-point number to the nearest integer not larger than the original.
Definition: fast_math.hpp:234
Quat< T > cos(const Quat< T > &q)
Quat< T > sin(const Quat< T > &q)
cv
"black box" representation of the file storage associated with a file on disk.
Definition: aruco.hpp:75
Definition: softfloat.hpp:227
Definition: softfloat.hpp:90
Definition: cvdef.h:382
Definition: cvdef.h:390