Skip to content

Commit ad49657

Browse files
authored
[clang] Add fixed point precision macros (#81207)
This defines the builtin macros specified in `7.18a.3 Precision macros` of ISO/IEC TR 18037:2008. These are the `__*__` versions of them and the formal definitions in stdfix.h can use them.
1 parent 0fc5786 commit ad49657

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ DWARF Support in Clang
305305
Floating Point Support in Clang
306306
-------------------------------
307307

308+
Fixed Point Support in Clang
309+
----------------------------
310+
311+
- Support fixed point precision macros according to ``7.18a.3`` of
312+
`ISO/IEC TR 18037:2008 <https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip>`_.
313+
308314
AST Matchers
309315
------------
310316

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,60 @@ void InitializeOpenCLFeatureTestMacros(const TargetInfo &TI,
768768
Builder.defineMacro("__opencl_c_int64");
769769
}
770770

771+
llvm::SmallString<32> ConstructFixedPointLiteral(llvm::APFixedPoint Val,
772+
llvm::StringRef Suffix) {
773+
if (Val.isSigned() && Val == llvm::APFixedPoint::getMin(Val.getSemantics())) {
774+
// When representing the min value of a signed fixed point type in source
775+
// code, we cannot simply write `-<lowest value>`. For example, the min
776+
// value of a `short _Fract` cannot be written as `-1.0hr`. This is because
777+
// the parser will read this (and really any negative numerical literal) as
778+
// a UnaryOperator that owns a FixedPointLiteral with a positive value
779+
// rather than just a FixedPointLiteral with a negative value. Compiling
780+
// `-1.0hr` results in an overflow to the maximal value of that fixed point
781+
// type. The correct way to represent a signed min value is to instead split
782+
// it into two halves, like `(-0.5hr-0.5hr)` which is what the standard
783+
// defines SFRACT_MIN as.
784+
llvm::SmallString<32> Literal;
785+
Literal.push_back('(');
786+
llvm::SmallString<32> HalfStr =
787+
ConstructFixedPointLiteral(Val.shr(1), Suffix);
788+
Literal += HalfStr;
789+
Literal += HalfStr;
790+
Literal.push_back(')');
791+
return Literal;
792+
}
793+
794+
llvm::SmallString<32> Str(Val.toString());
795+
Str += Suffix;
796+
return Str;
797+
}
798+
799+
void DefineFixedPointMacros(const TargetInfo &TI, MacroBuilder &Builder,
800+
llvm::StringRef TypeName, llvm::StringRef Suffix,
801+
unsigned Width, unsigned Scale, bool Signed) {
802+
// Saturation doesn't affect the size or scale of a fixed point type, so we
803+
// don't need it here.
804+
llvm::FixedPointSemantics FXSema(
805+
Width, Scale, Signed, /*IsSaturated=*/false,
806+
!Signed && TI.doUnsignedFixedPointTypesHavePadding());
807+
llvm::SmallString<32> MacroPrefix("__");
808+
MacroPrefix += TypeName;
809+
Builder.defineMacro(MacroPrefix + "_EPSILON__",
810+
ConstructFixedPointLiteral(
811+
llvm::APFixedPoint::getEpsilon(FXSema), Suffix));
812+
Builder.defineMacro(MacroPrefix + "_FBIT__", Twine(Scale));
813+
Builder.defineMacro(
814+
MacroPrefix + "_MAX__",
815+
ConstructFixedPointLiteral(llvm::APFixedPoint::getMax(FXSema), Suffix));
816+
817+
// ISO/IEC TR 18037:2008 doesn't specify MIN macros for unsigned types since
818+
// they're all just zero.
819+
if (Signed)
820+
Builder.defineMacro(
821+
MacroPrefix + "_MIN__",
822+
ConstructFixedPointLiteral(llvm::APFixedPoint::getMin(FXSema), Suffix));
823+
}
824+
771825
static void InitializePredefinedMacros(const TargetInfo &TI,
772826
const LangOptions &LangOpts,
773827
const FrontendOptions &FEOpts,
@@ -1097,6 +1151,47 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
10971151
TI.getTypeWidth(TI.getIntMaxType()) &&
10981152
"uintmax_t and intmax_t have different widths?");
10991153

1154+
if (LangOpts.FixedPoint) {
1155+
// Each unsigned type has the same width as their signed type.
1156+
DefineFixedPointMacros(TI, Builder, "SFRACT", "HR", TI.getShortFractWidth(),
1157+
TI.getShortFractScale(), /*Signed=*/true);
1158+
DefineFixedPointMacros(TI, Builder, "USFRACT", "UHR",
1159+
TI.getShortFractWidth(),
1160+
TI.getUnsignedShortFractScale(), /*Signed=*/false);
1161+
DefineFixedPointMacros(TI, Builder, "FRACT", "R", TI.getFractWidth(),
1162+
TI.getFractScale(), /*Signed=*/true);
1163+
DefineFixedPointMacros(TI, Builder, "UFRACT", "UR", TI.getFractWidth(),
1164+
TI.getUnsignedFractScale(), /*Signed=*/false);
1165+
DefineFixedPointMacros(TI, Builder, "LFRACT", "LR", TI.getLongFractWidth(),
1166+
TI.getLongFractScale(), /*Signed=*/true);
1167+
DefineFixedPointMacros(TI, Builder, "ULFRACT", "ULR",
1168+
TI.getLongFractWidth(),
1169+
TI.getUnsignedLongFractScale(), /*Signed=*/false);
1170+
DefineFixedPointMacros(TI, Builder, "SACCUM", "HK", TI.getShortAccumWidth(),
1171+
TI.getShortAccumScale(), /*Signed=*/true);
1172+
DefineFixedPointMacros(TI, Builder, "USACCUM", "UHK",
1173+
TI.getShortAccumWidth(),
1174+
TI.getUnsignedShortAccumScale(), /*Signed=*/false);
1175+
DefineFixedPointMacros(TI, Builder, "ACCUM", "K", TI.getAccumWidth(),
1176+
TI.getAccumScale(), /*Signed=*/true);
1177+
DefineFixedPointMacros(TI, Builder, "UACCUM", "UK", TI.getAccumWidth(),
1178+
TI.getUnsignedAccumScale(), /*Signed=*/false);
1179+
DefineFixedPointMacros(TI, Builder, "LACCUM", "LK", TI.getLongAccumWidth(),
1180+
TI.getLongAccumScale(), /*Signed=*/true);
1181+
DefineFixedPointMacros(TI, Builder, "ULACCUM", "ULK",
1182+
TI.getLongAccumWidth(),
1183+
TI.getUnsignedLongAccumScale(), /*Signed=*/false);
1184+
1185+
Builder.defineMacro("__SACCUM_IBIT__", Twine(TI.getShortAccumIBits()));
1186+
Builder.defineMacro("__USACCUM_IBIT__",
1187+
Twine(TI.getUnsignedShortAccumIBits()));
1188+
Builder.defineMacro("__ACCUM_IBIT__", Twine(TI.getAccumIBits()));
1189+
Builder.defineMacro("__UACCUM_IBIT__", Twine(TI.getUnsignedAccumIBits()));
1190+
Builder.defineMacro("__LACCUM_IBIT__", Twine(TI.getLongAccumIBits()));
1191+
Builder.defineMacro("__ULACCUM_IBIT__",
1192+
Twine(TI.getUnsignedLongAccumIBits()));
1193+
}
1194+
11001195
if (TI.hasFloat16Type())
11011196
DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16");
11021197
DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");

clang/test/Preprocessor/fixed-point.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// Assert the fixed point precision macros according to ISO/IEC TR 18037:2008 7.18a.3 are
2+
/// defined when -ffixed-point is provided.
3+
4+
// RUN: %clang_cc1 -triple=x86_64 -E -dM -ffixed-point -x c < /dev/null | FileCheck -match-full-lines %s
5+
// RUN: %clang_cc1 -triple=x86_64 -E -dM -ffixed-point -x c++ < /dev/null | FileCheck -match-full-lines %s
6+
7+
/// These are the implementation-defined values for x86_64.
8+
// CHECK-DAG:#define __SFRACT_EPSILON__ 0.0078125HR
9+
// CHECK-DAG:#define __SFRACT_FBIT__ 7
10+
// CHECK-DAG:#define __SFRACT_MAX__ 0.9921875HR
11+
// CHECK-DAG:#define __SFRACT_MIN__ (-0.5HR-0.5HR)
12+
13+
// CHECK-DAG:#define __USFRACT_EPSILON__ 0.00390625UHR
14+
// CHECK-DAG:#define __USFRACT_FBIT__ 8
15+
// CHECK-DAG:#define __USFRACT_MAX__ 0.99609375UHR
16+
17+
// CHECK-DAG:#define __FRACT_EPSILON__ 0.000030517578125R
18+
// CHECK-DAG:#define __FRACT_FBIT__ 15
19+
// CHECK-DAG:#define __FRACT_MAX__ 0.999969482421875R
20+
// CHECK-DAG:#define __FRACT_MIN__ (-0.5R-0.5R)
21+
22+
// CHECK-DAG:#define __UFRACT_EPSILON__ 0.0000152587890625UR
23+
// CHECK-DAG:#define __UFRACT_FBIT__ 16
24+
// CHECK-DAG:#define __UFRACT_MAX__ 0.9999847412109375UR
25+
26+
// CHECK-DAG:#define __LFRACT_EPSILON__ 0.0000000004656612873077392578125LR
27+
// CHECK-DAG:#define __LFRACT_FBIT__ 31
28+
// CHECK-DAG:#define __LFRACT_MAX__ 0.9999999995343387126922607421875LR
29+
// CHECK-DAG:#define __LFRACT_MIN__ (-0.5LR-0.5LR)
30+
31+
// CHECK-DAG:#define __ULFRACT_EPSILON__ 0.00000000023283064365386962890625ULR
32+
// CHECK-DAG:#define __ULFRACT_FBIT__ 32
33+
// CHECK-DAG:#define __ULFRACT_MAX__ 0.99999999976716935634613037109375ULR
34+
35+
// CHECK-DAG:#define __SACCUM_EPSILON__ 0.0078125HK
36+
// CHECK-DAG:#define __SACCUM_FBIT__ 7
37+
// CHECK-DAG:#define __SACCUM_MAX__ 255.9921875HK
38+
// CHECK-DAG:#define __SACCUM_MIN__ (-128.0HK-128.0HK)
39+
40+
// CHECK-DAG:#define __USACCUM_EPSILON__ 0.00390625UHK
41+
// CHECK-DAG:#define __USACCUM_FBIT__ 8
42+
// CHECK-DAG:#define __USACCUM_MAX__ 255.99609375UHK
43+
44+
// CHECK-DAG:#define __ACCUM_EPSILON__ 0.000030517578125K
45+
// CHECK-DAG:#define __ACCUM_FBIT__ 15
46+
// CHECK-DAG:#define __ACCUM_MAX__ 65535.999969482421875K
47+
// CHECK-DAG:#define __ACCUM_MIN__ (-32768.0K-32768.0K)
48+
49+
// CHECK-DAG:#define __UACCUM_EPSILON__ 0.0000152587890625UK
50+
// CHECK-DAG:#define __UACCUM_FBIT__ 16
51+
// CHECK-DAG:#define __UACCUM_MAX__ 65535.9999847412109375UK
52+
53+
// CHECK-DAG:#define __LACCUM_EPSILON__ 0.0000000004656612873077392578125LK
54+
// CHECK-DAG:#define __LACCUM_FBIT__ 31
55+
// CHECK-DAG:#define __LACCUM_MAX__ 4294967295.9999999995343387126922607421875LK
56+
// CHECK-DAG:#define __LACCUM_MIN__ (-2147483648.0LK-2147483648.0LK)
57+
58+
// CHECK-DAG:#define __ULACCUM_EPSILON__ 0.00000000023283064365386962890625ULK
59+
// CHECK-DAG:#define __ULACCUM_FBIT__ 32
60+
// CHECK-DAG:#define __ULACCUM_MAX__ 4294967295.99999999976716935634613037109375ULK
61+
62+
// CHECK-DAG:#define __SACCUM_IBIT__ 8
63+
// CHECK-DAG:#define __USACCUM_IBIT__ 8
64+
// CHECK-DAG:#define __ACCUM_IBIT__ 16
65+
// CHECK-DAG:#define __UACCUM_IBIT__ 16
66+
// CHECK-DAG:#define __LACCUM_IBIT__ 32
67+
// CHECK-DAG:#define __ULACCUM_IBIT__ 32
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// Assert the fixed point precision macros according to ISO/IEC TR 18037:2008 7.18a.3 are not
2+
/// defined when -ffixed-point is not provided.
3+
4+
// RUN: %clang_cc1 -triple=x86_64 -E -dM -x c < /dev/null | FileCheck -match-full-lines %s
5+
// RUN: %clang_cc1 -triple=x86_64 -E -dM -x c++ < /dev/null | FileCheck -match-full-lines %s
6+
7+
// CHECK-NOT:#define __SFRACT_FBIT__ 7

llvm/include/llvm/ADT/APFixedPoint.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ class APFixedPoint {
260260

261261
static APFixedPoint getMax(const FixedPointSemantics &Sema);
262262
static APFixedPoint getMin(const FixedPointSemantics &Sema);
263+
static APFixedPoint getEpsilon(const FixedPointSemantics &Sema);
263264

264265
/// Given a floating point semantic, return the next floating point semantic
265266
/// with a larger exponent and larger or equal mantissa.

llvm/lib/Support/APFixedPoint.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ APFixedPoint APFixedPoint::getMin(const FixedPointSemantics &Sema) {
129129
return APFixedPoint(Val, Sema);
130130
}
131131

132+
APFixedPoint APFixedPoint::getEpsilon(const FixedPointSemantics &Sema) {
133+
APSInt Val(Sema.getWidth(), !Sema.isSigned());
134+
Val.setBit(/*BitPosition=*/0);
135+
return APFixedPoint(Val, Sema);
136+
}
137+
132138
bool FixedPointSemantics::fitsInFloatSemantics(
133139
const fltSemantics &FloatSema) const {
134140
// A fixed point semantic fits in a floating point semantic if the maximum

0 commit comments

Comments
 (0)