Skip to content

Commit 19e8b1d

Browse files
committed
Explicitely handle single, double, double extended
1 parent f2784ed commit 19e8b1d

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,23 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) {
263263
}
264264

265265
std::string GetNumAsString(llvm::APFloat value) {
266-
if (&value.getSemantics() == &llvm::APFloat::IEEEsingle()) {
267-
return std::format("{}_f32", value.convertToFloat());
268-
}
269-
bool loses_info = false;
270-
value.convert(llvm::APFloat::IEEEdouble(),
271-
llvm::APFloat::rmNearestTiesToEven, &loses_info);
272-
return value.isInfinity() ? "f64::INFINITY"
273-
: std::format("{}_f64", value.convertToDouble());
266+
switch (llvm::APFloat::SemanticsToEnum(value.getSemantics())) {
267+
case llvm::APFloat::S_IEEEsingle:
268+
return value.isInfinity() ? "f32::INFINITY"
269+
: std::format("{}_f32", value.convertToFloat());
270+
case llvm::APFloat::S_x87DoubleExtended: {
271+
bool loses_info = false;
272+
value.convert(llvm::APFloat::IEEEdouble(),
273+
llvm::APFloat::rmNearestTiesToEven, &loses_info);
274+
}
275+
[[fallthrough]];
276+
case llvm::APFloat::S_IEEEdouble:
277+
return value.isInfinity() ? "f64::INFINITY"
278+
: std::format("{}_f64", value.convertToDouble());
279+
default:
280+
assert(false && "unsupported floating-point semantics");
281+
return {};
282+
}
274283
}
275284

276285
bool IsUserDefinedDecl(const clang::Decl *decl) {

0 commit comments

Comments
 (0)