Clang Project

clang_source_code/test/Driver/fast-math.c
1// Test that the GCC fast-math floating point flags get lowered to the correct
2// permutation of Clang frontend flags. This is non-trivial for a few reasons.
3// First, the GCC flags have many different and surprising effects. Second,
4// LLVM only supports three switches which is more coarse grained than GCC's
5// support.
6//
7// Both of them use gcc driver for as.
8// REQUIRES: clang-driver
9//
10// RUN: %clang -### -fno-honor-infinities -c %s 2>&1 \
11// RUN:   | FileCheck --check-prefix=CHECK-NO-INFS %s
12// infinites [sic] is a supported alternative spelling of infinities.
13// RUN: %clang -### -fno-honor-infinites -c %s 2>&1 \
14// RUN:   | FileCheck --check-prefix=CHECK-NO-INFS %s
15// CHECK-NO-INFS: "-cc1"
16// CHECK-NO-INFS: "-menable-no-infs"
17//
18// RUN: %clang -### -fno-fast-math -fno-honor-infinities -c %s 2>&1 \
19// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH-NO-INFS %s
20// CHECK-NO-FAST-MATH-NO-INFS: "-cc1"
21// CHECK-NO-FAST-MATH-NO-INFS: "-menable-no-infs"
22//
23// RUN: %clang -### -fno-honor-infinities -fno-fast-math -c %s 2>&1 \
24// RUN:   | FileCheck --check-prefix=CHECK-NO-INFS-NO-FAST-MATH %s
25// CHECK-NO-INFS-NO-FAST-MATH: "-cc1"
26// CHECK-NO-INFS-NO-FAST-MATH-NOT: "-menable-no-infs"
27//
28// RUN: %clang -### -fno-signed-zeros -c %s 2>&1 \
29// RUN:   | FileCheck --check-prefix=CHECK-NO-SIGNED-ZEROS %s
30// CHECK-NO-SIGNED-ZEROS: "-cc1"
31// CHECK-NO-SIGNED-ZEROS: "-fno-signed-zeros"
32//
33// RUN: %clang -### -fno-fast-math -fno-signed-zeros -c %s 2>&1 \
34// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH-NO-SIGNED-ZEROS %s
35// CHECK-NO-FAST-MATH-NO-SIGNED-ZEROS: "-cc1"
36// CHECK-NO-FAST-MATH-NO-SIGNED-ZEROS: "-fno-signed-zeros"
37//
38// RUN: %clang -### -fno-signed-zeros -fno-fast-math -c %s 2>&1 \
39// RUN:   | FileCheck --check-prefix=CHECK-NO-SIGNED-ZEROS-NO-FAST-MATH %s
40// CHECK-NO-SIGNED-ZEROS-NO-FAST-MATH: "-cc1"
41// CHECK-NO-SIGNED-ZEROS-NO-FAST-MATH-NOT: "-fno-signed-zeros"
42//
43// RUN: %clang -### -freciprocal-math -c %s 2>&1 \
44// RUN:   | FileCheck --check-prefix=CHECK-RECIPROCAL-MATH %s
45// CHECK-RECIPROCAL-MATH: "-cc1"
46// CHECK-RECIPROCAL-MATH: "-freciprocal-math"
47//
48// RUN: %clang -### -fno-fast-math -freciprocal-math -c %s 2>&1 \
49// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH-RECIPROCAL-MATH %s
50// CHECK-NO-FAST-MATH-RECIPROCAL-MATH: "-cc1"
51// CHECK-NO-FAST-MATH-RECIPROCAL-MATH: "-freciprocal-math"
52//
53// RUN: %clang -### -freciprocal-math -fno-fast-math -c %s 2>&1 \
54// RUN:   | FileCheck --check-prefix=CHECK-RECIPROCAL-MATH-NO-FAST-MATH %s
55// CHECK-RECIPROCAL-MATH-NO-FAST-MATH: "-cc1"
56// CHECK-RECIPROCAL-MATH-NO-FAST-MATH-NOT: "-freciprocal-math"
57//
58// RUN: %clang -### -fno-honor-nans -c %s 2>&1 \
59// RUN:   | FileCheck --check-prefix=CHECK-NO-NANS %s
60// CHECK-NO-NANS: "-cc1"
61// CHECK-NO-NANS: "-menable-no-nans"
62//
63// RUN: %clang -### -fno-fast-math -fno-honor-nans -c %s 2>&1 \
64// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH-NO-NANS %s
65// CHECK-NO-FAST-MATH-NO-NANS: "-cc1"
66// CHECK-NO-FAST-MATH-NO-NANS: "-menable-no-nans"
67//
68// RUN: %clang -### -fno-honor-nans -fno-fast-math -c %s 2>&1 \
69// RUN:   | FileCheck --check-prefix=CHECK-NO-NANS-NO-FAST-MATH %s
70// CHECK-NO-NANS-NO-FAST-MATH: "-cc1"
71// CHECK-NO-NANS-NO-FAST-MATH-NOT: "-menable-no-nans"
72//
73// RUN: %clang -### -fmath-errno -c %s 2>&1 \
74// RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
75// CHECK-MATH-ERRNO: "-cc1"
76// CHECK-MATH-ERRNO: "-fmath-errno"
77//
78// RUN: %clang -### -fmath-errno -fno-math-errno -c %s 2>&1 \
79// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
80// CHECK-NO-MATH-ERRNO: "-cc1"
81// CHECK-NO-MATH-ERRNO-NOT: "-fmath-errno"
82//
83// Target defaults for -fmath-errno (reusing the above checks).
84// RUN: %clang -### -target i686-unknown-linux -c %s 2>&1 \
85// RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
86// RUN: %clang -### -target i686-apple-darwin -c %s 2>&1 \
87// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
88// RUN: %clang -### -target x86_64-unknown-freebsd -c %s 2>&1 \
89// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
90// RUN: %clang -### -target x86_64-unknown-netbsd -c %s 2>&1 \
91// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
92// RUN: %clang -### -target x86_64-unknown-openbsd -c %s 2>&1 \
93// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
94// RUN: %clang -### -target x86_64-unknown-dragonfly -c %s 2>&1 \
95// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
96// RUN: %clang -### -target x86_64-fuchsia -c %s 2>&1 \
97// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
98// RUN: %clang -### -target x86_64-linux-android -c %s 2>&1 \
99// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
100//
101// Check that -ffast-math disables -fmath-errno, and -fno-fast-math merely
102// preserves the target default. Also check various flag set operations between
103// the two flags. (Resuses above checks.)
104// RUN: %clang -### -ffast-math -c %s 2>&1 \
105// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
106// RUN: %clang -### -fmath-errno -ffast-math -c %s 2>&1 \
107// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
108// RUN: %clang -### -ffast-math -fmath-errno -c %s 2>&1 \
109// RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
110// RUN: %clang -### -target i686-unknown-linux -fno-fast-math -c %s 2>&1 \
111// RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
112// RUN: %clang -### -target i686-unknown-linux -fno-math-errno -fno-fast-math -c %s 2>&1 \
113// RUN:   | FileCheck --check-prefix=CHECK-MATH-ERRNO %s
114// RUN: %clang -### -target i686-apple-darwin -fno-fast-math -c %s 2>&1 \
115// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
116// RUN: %clang -### -target i686-apple-darwin -fno-math-errno -fno-fast-math -c %s 2>&1 \
117// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
118// RUN: %clang -### -fno-fast-math -fno-math-errno -c %s 2>&1 \
119// RUN:   | FileCheck --check-prefix=CHECK-NO-MATH-ERRNO %s
120//
121// RUN: %clang -### -fno-math-errno -fassociative-math -freciprocal-math \
122// RUN:     -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
123// RUN:   | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s
124// CHECK-UNSAFE-MATH: "-cc1"
125// CHECK-UNSAFE-MATH: "-menable-unsafe-fp-math"
126// CHECK-UNSAFE-MATH: "-mreassociate"
127//
128// RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \
129// RUN:     -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
130// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH-UNSAFE-MATH %s
131// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-cc1"
132// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-menable-unsafe-fp-math"
133// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-mreassociate"
134
135// The 2nd -fno-fast-math overrides -fassociative-math.
136
137// RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \
138// RUN:     -fno-fast-math -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
139// RUN:   | FileCheck --check-prefix=CHECK-UNSAFE-MATH-NO-FAST-MATH %s
140// CHECK-UNSAFE-MATH-NO-FAST-MATH: "-cc1"
141// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-menable-unsafe-fp-math"
142// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-mreassociate"
143//
144// Check that various umbrella flags also enable these frontend options.
145// RUN: %clang -### -ffast-math -c %s 2>&1 \
146// RUN:   | FileCheck --check-prefix=CHECK-NO-INFS %s
147// RUN: %clang -### -ffast-math -c %s 2>&1 \
148// RUN:   | FileCheck --check-prefix=CHECK-NO-NANS %s
149// RUN: %clang -### -ffast-math -c %s 2>&1 \
150// RUN:   | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s
151// RUN: %clang -### -ffinite-math-only -c %s 2>&1 \
152// RUN:   | FileCheck --check-prefix=CHECK-NO-INFS %s
153// RUN: %clang -### -ffinite-math-only -c %s 2>&1 \
154// RUN:   | FileCheck --check-prefix=CHECK-NO-NANS %s
155// RUN: %clang -### -funsafe-math-optimizations -fno-math-errno -c %s 2>&1 \
156// RUN:   | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s
157//
158// One umbrella flag is *really* weird and also changes the semantics of the
159// program by adding a special preprocessor macro. Check that the frontend flag
160// modeling this semantic change is provided. Also check that the flag is not
161// present if any of the optimizations are disabled.
162// RUN: %clang -### -ffast-math -c %s 2>&1 \
163// RUN:   | FileCheck --check-prefix=CHECK-FAST-MATH %s
164// RUN: %clang -### -fno-fast-math -ffast-math -c %s 2>&1 \
165// RUN:   | FileCheck --check-prefix=CHECK-FAST-MATH %s
166// RUN: %clang -### -funsafe-math-optimizations -ffinite-math-only \
167// RUN:     -fno-math-errno -ffp-contract=fast -c %s 2>&1 \
168// RUN:   | FileCheck --check-prefix=CHECK-FAST-MATH %s
169// RUN: %clang -### -fno-honor-infinities -fno-honor-nans -fno-math-errno \
170// RUN:     -fassociative-math -freciprocal-math -fno-signed-zeros \
171// RUN:     -fno-trapping-math -ffp-contract=fast -c %s 2>&1 \
172// RUN:   | FileCheck --check-prefix=CHECK-FAST-MATH %s
173// CHECK-FAST-MATH: "-cc1"
174// CHECK-FAST-MATH: "-ffast-math"
175// CHECK-FAST-MATH: "-ffinite-math-only"
176//
177// RUN: %clang -### -ffast-math -fno-fast-math -c %s 2>&1 \
178// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
179// RUN: %clang -### -ffast-math -fno-finite-math-only -c %s 2>&1 \
180// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
181// RUN: %clang -### -ffast-math -fno-unsafe-math-optimizations -c %s 2>&1 \
182// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
183// RUN: %clang -### -ffast-math -fmath-errno -c %s 2>&1 \
184// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH %s
185// RUN: %clang -### -ffast-math -fno-associative-math -c %s 2>&1 \
186// RUN:   | FileCheck --check-prefix=CHECK-NO-FAST-MATH --check-prefix=CHECK-ASSOC-MATH %s
187// CHECK-NO-FAST-MATH: "-cc1"
188// CHECK-NO-FAST-MATH-NOT: "-ffast-math"
189// CHECK-ASSOC-MATH-NOT: "-mreassociate"
190//
191// Check various means of disabling these flags, including disabling them after
192// they've been enabled via an umbrella flag.
193// RUN: %clang -### -fno-honor-infinities -fhonor-infinities -c %s 2>&1 \
194// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-INFS %s
195// RUN: %clang -### -ffinite-math-only -fhonor-infinities -c %s 2>&1 \
196// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-INFS %s
197// RUN: %clang -### -ffinite-math-only -fno-finite-math-only -c %s 2>&1 \
198// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-INFS %s
199// RUN: %clang -### -ffast-math -fhonor-infinities -c %s 2>&1 \
200// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-INFS %s
201// RUN: %clang -### -ffast-math -fno-finite-math-only -c %s 2>&1 \
202// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-INFS %s
203// CHECK-NO-NO-INFS: "-cc1"
204// CHECK-NO-NO-INFS-NOT: "-menable-no-infs"
205// CHECK-NO-NO-INFS-NOT: "-ffinite-math-only"
206// CHECK-NO-NO-INFS: "-o"
207//
208// RUN: %clang -### -fno-honor-nans -fhonor-nans -c %s 2>&1 \
209// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-NANS %s
210// RUN: %clang -### -ffinite-math-only -fhonor-nans -c %s 2>&1 \
211// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-NANS %s
212// RUN: %clang -### -ffinite-math-only -fno-finite-math-only -c %s 2>&1 \
213// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-NANS %s
214// RUN: %clang -### -ffast-math -fhonor-nans -c %s 2>&1 \
215// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-NANS %s
216// RUN: %clang -### -ffast-math -fno-finite-math-only -c %s 2>&1 \
217// RUN:   | FileCheck --check-prefix=CHECK-NO-NO-NANS %s
218// CHECK-NO-NO-NANS: "-cc1"
219// CHECK-NO-NO-NANS-NOT: "-menable-no-nans"
220// CHECK-NO-NO-NANS-NOT: "-ffinite-math-only"
221// CHECK-NO-NO-NANS: "-o"
222
223// A later inverted option overrides an earlier option.
224
225// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
226// RUN:     -fno-trapping-math -fno-associative-math -c %s 2>&1 \
227// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
228
229// RUN: %clang -### -funsafe-math-optimizations -fno-associative-math -c %s \
230// RUN:   2>&1 | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
231
232// RUN: %clang -### -funsafe-math-optimizations -fno-reciprocal-math -c %s \
233// RUN:   2>&1 | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
234// RUN: %clang -### -funsafe-math-optimizations -fsigned-zeros -c %s 2>&1 \
235// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
236// RUN: %clang -### -funsafe-math-optimizations -ftrapping-math -c %s 2>&1 \
237// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
238// RUN: %clang -### -funsafe-math-optimizations -fno-unsafe-math-optimizations \
239// RUN:     -c %s 2>&1 \
240// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
241// RUN: %clang -### -ffast-math -fno-associative-math -c %s 2>&1 \
242// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
243
244// RUN: %clang -### -ffast-math -fno-reciprocal-math -c %s 2>&1 \
245// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
246// RUN: %clang -### -ffast-math -fsigned-zeros -c %s 2>&1 \
247// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
248// RUN: %clang -### -ffast-math -ftrapping-math -c %s 2>&1 \
249// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
250// RUN: %clang -### -ffast-math -fno-unsafe-math-optimizations -c %s 2>&1 \
251// RUN:   | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
252
253// CHECK-NO-UNSAFE-MATH: "-cc1"
254// CHECK-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
255// CHECK-NO_UNSAFE-MATH-NOT: "-mreassociate"
256// CHECK-NO-UNSAFE-MATH: "-o"
257
258
259// Reassociate is allowed because it does not require reciprocal-math.
260
261// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
262// RUN:     -fno-trapping-math -fno-reciprocal-math -c %s 2>&1 \
263// RUN:   | FileCheck --check-prefix=CHECK-REASSOC-NO-UNSAFE-MATH %s
264
265// CHECK-REASSOC-NO-UNSAFE-MATH: "-cc1"
266// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
267// CHECK-REASSOC-NO_UNSAFE-MATH: "-mreassociate"
268// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
269// CHECK-REASSOC-NO-UNSAFE-MATH: "-o"
270
271
272// In these runs, reassociate is not allowed because both no-signed-zeros and no-trapping-math are required.
273
274// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
275// RUN:     -fno-trapping-math -fsigned-zeros -c %s 2>&1 \
276// RUN:   | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s
277
278// RUN: %clang -### -fassociative-math -freciprocal-math -fno-signed-zeros \
279// RUN:     -fno-trapping-math -ftrapping-math -c %s 2>&1 \
280// RUN:   | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s
281
282// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-cc1"
283// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
284// CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-mreassociate"
285// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
286// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-o"
287
288
289// RUN: %clang -### -ftrapping-math -fno-trapping-math -c %s 2>&1 \
290// RUN:   | FileCheck --check-prefix=CHECK-NO-TRAPPING-MATH %s
291// CHECK-NO-TRAPPING-MATH: "-fno-trapping-math"
292
293// This isn't fast-math, but the option is handled in the same place as other FP params.
294// Last option wins, and strict behavior is assumed by default. 
295
296// RUN: %clang -### -fno-strict-float-cast-overflow -c %s 2>&1 \
297// RUN:   | FileCheck --check-prefix=CHECK-FPOV-WORKAROUND %s
298// CHECK-FPOV-WORKAROUND: "-cc1"
299// CHECK-FPOV-WORKAROUND: "-fno-strict-float-cast-overflow"
300
301// RUN: %clang -### -c %s 2>&1 \
302// RUN:   | FileCheck --check-prefix=CHECK-FPOV-WORKAROUND-DEFAULT %s
303// CHECK-FPOV-WORKAROUND-DEFAULT: "-cc1"
304// CHECK-FPOV-WORKAROUND-DEFAULT-NOT: "strict-float-cast-overflow"
305
306// RUN: %clang -### -fstrict-float-cast-overflow -c %s 2>&1 \
307// RUN:   | FileCheck --check-prefix=CHECK-NO-FPOV-WORKAROUND %s
308// CHECK-NO-FPOV-WORKAROUND: "-cc1"
309// CHECK-NO-FPOV-WORKAROUND-NOT: "strict-float-cast-overflow"
310
311// RUN: %clang -### -fno-strict-float-cast-overflow -fstrict-float-cast-overflow -c %s 2>&1 \
312// RUN:   | FileCheck --check-prefix=CHECK-NO-FPOV-WORKAROUND-OVERRIDE %s
313// CHECK-NO-FPOV-WORKAROUND-OVERRIDE: "-cc1"
314// CHECK-NO-FPOV-WORKAROUND-OVERRIDE-NOT: "strict-float-cast-overflow"
315
316