xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deMath.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _DEMATH_HPP
2 #define _DEMATH_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Base Portability Library
5  * -------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Basic mathematical operations.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deFloat16.h"
27 #include "deMath.h"
28 
29 #include <limits>
30 
deToDouble(deFloat16 x)31 DE_INLINE double deToDouble(deFloat16 x)
32 {
33     return deFloat16To64(x);
34 }
deToDouble(float x)35 DE_INLINE double deToDouble(float x)
36 {
37     return x;
38 }
deToDouble(double x)39 DE_INLINE double deToDouble(double x)
40 {
41     return x;
42 }
43 
44 template <typename T>
deToFloatType(double x)45 inline T deToFloatType(double x)
46 {
47     return static_cast<T>(x);
48 }
49 
50 template <>
deToFloatType(double x)51 inline deFloat16 deToFloatType<deFloat16>(double x)
52 {
53     return deFloat64To16(x);
54 }
55 
56 // These helpers make the C helpers usable from templates.  Because some of
57 // these deal with signaling NaN, it's important that no implicit float
58 // conversion operations happen.
deIsPositiveZero(deFloat16 x)59 DE_INLINE bool deIsPositiveZero(deFloat16 x)
60 {
61     return deHalfIsPositiveZero(x);
62 }
deIsPositiveZero(float x)63 DE_INLINE bool deIsPositiveZero(float x)
64 {
65     return deFloatIsPositiveZero(x);
66 }
deIsPositiveZero(double x)67 DE_INLINE bool deIsPositiveZero(double x)
68 {
69     return deDoubleIsPositiveZero(x);
70 }
deIsNegativeZero(deFloat16 x)71 DE_INLINE bool deIsNegativeZero(deFloat16 x)
72 {
73     return deHalfIsNegativeZero(x);
74 }
deIsNegativeZero(float x)75 DE_INLINE bool deIsNegativeZero(float x)
76 {
77     return deFloatIsNegativeZero(x);
78 }
deIsNegativeZero(double x)79 DE_INLINE bool deIsNegativeZero(double x)
80 {
81     return deDoubleIsNegativeZero(x);
82 }
deIsIEEENaN(deFloat16 x)83 DE_INLINE bool deIsIEEENaN(deFloat16 x)
84 {
85     return deHalfIsIEEENaN(x);
86 }
deIsIEEENaN(float x)87 DE_INLINE bool deIsIEEENaN(float x)
88 {
89     return deFloatIsIEEENaN(x);
90 }
deIsIEEENaN(double x)91 DE_INLINE bool deIsIEEENaN(double x)
92 {
93     return deDoubleIsIEEENaN(x);
94 }
deIsSignalingNaN(deFloat16 x)95 DE_INLINE bool deIsSignalingNaN(deFloat16 x)
96 {
97     return deHalfIsSignalingNaN(x);
98 }
deIsSignalingNaN(float x)99 DE_INLINE bool deIsSignalingNaN(float x)
100 {
101     return deFloatIsSignalingNaN(x);
102 }
deIsSignalingNaN(double x)103 DE_INLINE bool deIsSignalingNaN(double x)
104 {
105     return deDoubleIsSignalingNaN(x);
106 }
deIsQuietNaN(deFloat16 x)107 DE_INLINE bool deIsQuietNaN(deFloat16 x)
108 {
109     return deHalfIsQuietNaN(x);
110 }
deIsQuietNaN(float x)111 DE_INLINE bool deIsQuietNaN(float x)
112 {
113     return deFloatIsQuietNaN(x);
114 }
deIsQuietNaN(double x)115 DE_INLINE bool deIsQuietNaN(double x)
116 {
117     return deDoubleIsQuietNaN(x);
118 }
119 
120 template <typename T>
deQuietNaN()121 inline T deQuietNaN()
122 {
123     return std::numeric_limits<T>::quiet_NaN();
124 }
125 
126 template <>
deQuietNaN()127 inline deFloat16 deQuietNaN<deFloat16>()
128 {
129     return deFloat16QuietNaN;
130 }
131 
132 template <typename T>
deSignalingNaN()133 inline T deSignalingNaN()
134 {
135     return std::numeric_limits<T>::signaling_NaN();
136 }
137 
138 template <>
deSignalingNaN()139 inline deFloat16 deSignalingNaN<deFloat16>()
140 {
141     return deFloat16SignalingNaN;
142 }
143 
144 #endif // _DEMATH_HPP
145