Skip to content

Commit e070228

Browse files
Let snan constants actually be properly declared.
1 parent d0332f3 commit e070228

File tree

5 files changed

+65
-33
lines changed

5 files changed

+65
-33
lines changed

Diff for: llvm/docs/LangRef.rst

+32-28
Original file line numberDiff line numberDiff line change
@@ -4612,34 +4612,38 @@ Simple Constants
46124612

46134613
Floating-point constants support the following kinds of strings:
46144614

4615-
+---------------+---------------------------------------------------+
4616-
| Syntax | Description |
4617-
+===============+===================================================+
4618-
| ``+4.5e-13`` | Common decimal literal. Signs are optional, as is |
4619-
| | the exponent portion. The decimal point is |
4620-
| | required, as is one or more leading digits before |
4621-
| | the decimal point. |
4622-
+---------------+---------------------------------------------------+
4623-
| ``-0x1.fp13`` | Common hexadecimal literal. Signs are optional. |
4624-
| | The decimal point is required, as is the exponent |
4625-
| | portion of the literal (after the ``p``). |
4626-
+---------------+---------------------------------------------------+
4627-
| ``+inf``, | Positive or negative infinity. The sign is |
4628-
| ``-inf`` | required. |
4629-
+---------------+---------------------------------------------------+
4630-
| ``+qnan``, | Positive or negative preferred quiet NaN, i.e., |
4631-
| ``-qnan`` | the quiet bit is set, and all other payload bits |
4632-
| | are 0. The sign is required. |
4633-
+---------------+---------------------------------------------------+
4634-
| ``+nan(0x1)`` | NaN value with a particular payload, specified as |
4635-
| | hexadecimal (including the quiet bit as part of |
4636-
| | the payload). The sign is required. |
4637-
+---------------+---------------------------------------------------+
4638-
| ``f0x3c00`` | Value of the floating-point number if bitcast to |
4639-
| | an integer. The number must have exactly as many |
4640-
| | hexadecimal digits as is necessary for the size |
4641-
| | of the floating-point number. |
4642-
+---------------+---------------------------------------------------+
4615+
+----------------+---------------------------------------------------+
4616+
| Syntax | Description |
4617+
+================+===================================================+
4618+
| ``+4.5e-13`` | Common decimal literal. Signs are optional, as is |
4619+
| | the exponent portion. The decimal point is |
4620+
| | required, as is one or more leading digits before |
4621+
| | the decimal point. |
4622+
+----------------+---------------------------------------------------+
4623+
| ``-0x1.fp13`` | Common hexadecimal literal. Signs are optional. |
4624+
| | The decimal point is required, as is the exponent |
4625+
| | portion of the literal (after the ``p``). |
4626+
+----------------+---------------------------------------------------+
4627+
| ``+inf``, | Positive or negative infinity. The sign is |
4628+
| ``-inf`` | required. |
4629+
+----------------+---------------------------------------------------+
4630+
| ``+qnan``, | Positive or negative preferred quiet NaN, i.e., |
4631+
| ``-qnan`` | the quiet bit is set, and all other payload bits |
4632+
| | are 0. The sign is required. |
4633+
+----------------+---------------------------------------------------+
4634+
| ``+nan(0x1)`` | qNaN value with a particular payload, specified |
4635+
| | as hexadecimal (not including the quiet bit as |
4636+
| | part of the payload). The sign is required. |
4637+
+----------------+---------------------------------------------------+
4638+
| ``+snan(0x1)`` | sNaN value with a particular payload, specified |
4639+
| | as hexadecimal (not including the quiet bit as |
4640+
| | part of the payload). The sign is required. |
4641+
+----------------+---------------------------------------------------+
4642+
| ``f0x3c00`` | Value of the floating-point number if bitcast to |
4643+
| | an integer. The number must have exactly as many |
4644+
| | hexadecimal digits as is necessary for the size |
4645+
| | of the floating-point number. |
4646+
+----------------+---------------------------------------------------+
46434647

46444648
There are no constants of type x86_amx.
46454649

Diff for: llvm/include/llvm/ADT/APFloat.h

+19
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,25 @@ class APFloat : public APFloatBase {
13471347
APFLOAT_DISPATCH_ON_SEMANTICS(
13481348
convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
13491349
}
1350+
1351+
/// Fill this APFloat with the result of a string conversion.
1352+
///
1353+
/// The following strings are accepted for conversion purposes:
1354+
/// * Decimal floating-point literals (e.g., `0.1e-5`)
1355+
/// * Hexadecimal floating-point literals (e.g., `0x1.0p-5`)
1356+
/// * Positive infinity via "inf", "INFINITY", "Inf", "+Inf", or "+inf".
1357+
/// * Negative infinity via "-inf", "-INFINITY", or "-Inf".
1358+
/// * Quiet NaNs via "nan", "NaN", "nan(...)", or "NaN(...)", where the
1359+
/// "..." is either a decimal or hexadecimal integer representing the
1360+
/// payload. A negative sign may be optionally provided.
1361+
/// * Signaling NaNs via "snan", "sNaN", "snan(...)", or "sNaN(...)", where
1362+
/// the "..." is either a decimal or hexadecimal integer representing the
1363+
/// payload. A negative sign may be optionally provided.
1364+
///
1365+
/// If the input string is none of these forms, then an error is returned.
1366+
///
1367+
/// If a floating-point exception occurs during conversion, then no error is
1368+
/// returned, and the exception is indicated via opStatus.
13501369
Expected<opStatus> convertFromString(StringRef, roundingMode);
13511370
APInt bitcastToAPInt() const {
13521371
APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt());

Diff for: llvm/lib/AsmParser/LLLexer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
12101210
/// Lex a floating point constant starting with +.
12111211
/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
12121212
/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1213-
/// HexFPSpecial [-+](inf|qnan|nan\(0x[0-9A-Fa-f]+\))
1213+
/// HexFPSpecial [-+](inf|qnan|s?nan\(0x[0-9A-Fa-f]+\))
12141214
lltok::Kind LLLexer::LexPositive() {
12151215
// If it's not numeric, check for special floating-point values.
12161216
if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
@@ -1251,7 +1251,7 @@ lltok::Kind LLLexer::LexPositive() {
12511251

12521252
/// Lex all tokens that start with a + or - that could be a float literal.
12531253
/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1254-
/// HexFPSpecial [-+](inf|qnan|nan\(0x[0-9A-Fa-f]+\))
1254+
/// HexFPSpecial [-+](inf|qnan|s?nan\(0x[0-9A-Fa-f]+\))
12551255
lltok::Kind LLLexer::LexFloatStr() {
12561256
// At the point we enter this function, we may have seen a few characters
12571257
// already, but how many differs based on the entry point. Rewind to the
@@ -1284,7 +1284,7 @@ lltok::Kind LLLexer::LexFloatStr() {
12841284
}
12851285

12861286
// NaN with payload.
1287-
if (Label == "nan" && *CurPtr == '(') {
1287+
if ((Label == "nan" || Label == "snan") && *CurPtr == '(') {
12881288
const char *Payload = ++CurPtr;
12891289
while (*CurPtr && *CurPtr != ')')
12901290
++CurPtr;

Diff for: llvm/test/Assembler/float-literals.ll

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
@3 = global bfloat +qnan
3636
; CHECK: @4 = global fp128 0xL00000000DEADBEEF7FFF800000000000
3737
@4 = global fp128 +nan(0xdeadbeef)
38-
; CHECK: @5 = global x86_fp80 0xK0001FFFF000000000000
39-
@5 = global x86_fp80 f0x0000ffff000000000000
38+
; CHECK: @5 = global float 0x7FF000002000000
39+
@5 = global float +snan(0x1)
40+
; CHECK: @6 = global x86_fp80 0xK0001FFFF000000000000
41+
@6 = global x86_fp80 f0x0000ffff000000000000
4042

Diff for: llvm/unittests/AsmParser/AsmParserTest.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ TEST(AsmParserTest, TypeAndConstantValueParsing) {
120120
ASSERT_TRUE(isa<ConstantFP>(V));
121121
EXPECT_TRUE(cast<ConstantFP>(V)->getValue().isNegInfinity());
122122

123+
const fltSemantics &Float = APFloatBase::IEEEsingle();
124+
V = parseConstantValue("float +nan(0x1)", Error, M);
125+
ASSERT_TRUE(V);
126+
ASSERT_TRUE(isa<ConstantFP>(V));
127+
EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(APFloat::getNaN(Float, false, 1)));
128+
EXPECT_TRUE(!cast<ConstantFP>(V)->getValue().isSignaling());
129+
123130
V = parseConstantValue("i32 42", Error, M);
124131
ASSERT_TRUE(V);
125132
EXPECT_TRUE(V->getType()->isIntegerTy());

0 commit comments

Comments
 (0)