blob: fd04a319577991739272559ad60898100a1527cb [file] [log] [blame]
Nicolas Capens68a82382018-10-02 13:16:55 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Half.hpp"
16
Nicolas Capens157ba262019-12-10 17:49:14 -050017namespace sw {
18
19half::half(float fp32)
Nicolas Capens68a82382018-10-02 13:16:55 -040020{
Ben Clayton595d9112019-12-17 20:37:57 +000021 unsigned int fp32i = *(unsigned int *)&fp32;
Nicolas Capens157ba262019-12-10 17:49:14 -050022 unsigned int sign = (fp32i & 0x80000000) >> 16;
23 unsigned int abs = fp32i & 0x7FFFFFFF;
24
Ben Clayton595d9112019-12-17 20:37:57 +000025 if(abs > 0x47FFEFFF) // Infinity
Nicolas Capens68a82382018-10-02 13:16:55 -040026 {
Nicolas Capens157ba262019-12-10 17:49:14 -050027 fp16i = sign | 0x7FFF;
28 }
Ben Clayton595d9112019-12-17 20:37:57 +000029 else if(abs < 0x38800000) // Denormal
Nicolas Capens157ba262019-12-10 17:49:14 -050030 {
31 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
Nicolas Capensfd8f0e22020-07-01 00:27:23 -040032 unsigned int e = 113 - (abs >> 23);
Nicolas Capens68a82382018-10-02 13:16:55 -040033
Nicolas Capens157ba262019-12-10 17:49:14 -050034 if(e < 24)
Nicolas Capens68a82382018-10-02 13:16:55 -040035 {
Nicolas Capens157ba262019-12-10 17:49:14 -050036 abs = mantissa >> e;
Nicolas Capens68a82382018-10-02 13:16:55 -040037 }
38 else
39 {
Nicolas Capens157ba262019-12-10 17:49:14 -050040 abs = 0;
Nicolas Capens68a82382018-10-02 13:16:55 -040041 }
42
Nicolas Capens157ba262019-12-10 17:49:14 -050043 fp16i = sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
Nicolas Capens68a82382018-10-02 13:16:55 -040044 }
Nicolas Capens157ba262019-12-10 17:49:14 -050045 else
Nicolas Capens68a82382018-10-02 13:16:55 -040046 {
Nicolas Capens157ba262019-12-10 17:49:14 -050047 fp16i = sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
Nicolas Capens68a82382018-10-02 13:16:55 -040048 }
49}
Nicolas Capens157ba262019-12-10 17:49:14 -050050
51half::operator float() const
52{
53 unsigned int fp32i;
54
55 int s = (fp16i >> 15) & 0x00000001;
56 int e = (fp16i >> 10) & 0x0000001F;
Ben Clayton595d9112019-12-17 20:37:57 +000057 int m = fp16i & 0x000003FF;
Nicolas Capens157ba262019-12-10 17:49:14 -050058
59 if(e == 0)
60 {
61 if(m == 0)
62 {
63 fp32i = s << 31;
64
Ben Clayton595d9112019-12-17 20:37:57 +000065 return (float &)fp32i;
Nicolas Capens157ba262019-12-10 17:49:14 -050066 }
67 else
68 {
69 while(!(m & 0x00000400))
70 {
71 m <<= 1;
Ben Clayton595d9112019-12-17 20:37:57 +000072 e -= 1;
Nicolas Capens157ba262019-12-10 17:49:14 -050073 }
74
75 e += 1;
76 m &= ~0x00000400;
77 }
78 }
79
80 e = e + (127 - 15);
81 m = m << 13;
82
83 fp32i = (s << 31) | (e << 23) | m;
84
Ben Clayton595d9112019-12-17 20:37:57 +000085 return (float &)fp32i;
Nicolas Capens157ba262019-12-10 17:49:14 -050086}
87
88half &half::operator=(half h)
89{
90 fp16i = h.fp16i;
91
92 return *this;
93}
94
Nicolas Capens157ba262019-12-10 17:49:14 -050095half &half::operator=(float f)
96{
97 *this = half(f);
98
99 return *this;
100}
101
102} // namespace sw