MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / ParseAndEncodeFloatingPointNumber

Function ParseAndEncodeFloatingPointNumber

source/util/parse_number.cpp:147–238  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145 }
146}
147EncodeNumberStatus ParseAndEncodeFloatingPointNumber(
148 const char* text, const NumberType& type,
149 std::function<void(uint32_t)> emit, std::string* error_msg) {
150 if (!text) {
151 ErrorMsgStream(error_msg) << "The given text is a nullptr";
152 return EncodeNumberStatus::kInvalidText;
153 }
154
155 if (!IsFloating(type)) {
156 ErrorMsgStream(error_msg) << "The expected type is not a float type";
157 return EncodeNumberStatus::kInvalidUsage;
158 }
159
160 const auto bit_width = AssumedBitWidth(type);
161 switch (DeduceEncoding(type)) {
162 case SPV_FP_ENCODING_FLOAT8_E4M3: {
163 HexFloat<FloatProxy<Float8_E4M3>> hVal(0);
164 if (!ParseNumber(text, &hVal)) {
165 ErrorMsgStream(error_msg) << "Invalid E4M3 float literal: " << text;
166 return EncodeNumberStatus::kInvalidText;
167 }
168 // getAsFloat will return the Float16 value, and get_value
169 // will return a uint16_t representing the bits of the float.
170 // The encoding is therefore correct from the perspective of the SPIR-V
171 // spec since the top 16 bits will be 0.
172 emit(static_cast<uint32_t>(hVal.value().getAsFloat().get_value()));
173 return EncodeNumberStatus::kSuccess;
174 } break;
175 case SPV_FP_ENCODING_FLOAT8_E5M2: {
176 HexFloat<FloatProxy<Float8_E5M2>> hVal(0);
177 if (!ParseNumber(text, &hVal)) {
178 ErrorMsgStream(error_msg) << "Invalid E5M2 float literal: " << text;
179 return EncodeNumberStatus::kInvalidText;
180 }
181 // getAsFloat will return the Float16 value, and get_value
182 // will return a uint16_t representing the bits of the float.
183 // The encoding is therefore correct from the perspective of the SPIR-V
184 // spec since the top 16 bits will be 0.
185 emit(static_cast<uint32_t>(hVal.value().getAsFloat().get_value()));
186 return EncodeNumberStatus::kSuccess;
187 } break;
188 case SPV_FP_ENCODING_BFLOAT16: {
189 HexFloat<FloatProxy<BFloat16>> hVal(0);
190 if (!ParseNumber(text, &hVal)) {
191 ErrorMsgStream(error_msg) << "Invalid bfloat16 literal: " << text;
192 return EncodeNumberStatus::kInvalidText;
193 }
194 emit(static_cast<uint32_t>(hVal.value().getAsFloat().get_value()));
195 return EncodeNumberStatus::kSuccess;
196 } break;
197 case SPV_FP_ENCODING_IEEE754_BINARY16: {
198 HexFloat<FloatProxy<Float16>> hVal(0);
199 if (!ParseNumber(text, &hVal)) {
200 ErrorMsgStream(error_msg) << "Invalid 16-bit float literal: " << text;
201 return EncodeNumberStatus::kInvalidText;
202 }
203 // getAsFloat will return the Float16 value, and get_value
204 // will return a uint16_t representing the bits of the float.

Callers 2

ParseAndEncodeNumberFunction · 0.85
TESTFunction · 0.85

Calls 8

ErrorMsgStreamClass · 0.85
IsFloatingFunction · 0.85
AssumedBitWidthFunction · 0.85
DeduceEncodingFunction · 0.85
ParseNumberFunction · 0.85
get_valueMethod · 0.45
getAsFloatMethod · 0.45
valueMethod · 0.45

Tested by 1

TESTFunction · 0.68