Skip to content

Commit d1a7403

Browse files
author
Daniel Kroening
committed
support clang's __builtin_flt_rounds
1 parent f97c169 commit d1a7403

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This is C99, but currently only works with clang.
2+
// gcc and Visual Studio appear to hard-wire FLT_ROUNDS to 1.
3+
4+
#ifdef __clang__
5+
6+
#include <assert.h>
7+
#include <fenv.h>
8+
#include <float.h>
9+
10+
int main()
11+
{
12+
fesetround(FE_DOWNWARD);
13+
assert(FLT_ROUNDS==3);
14+
15+
fesetround(FE_TONEAREST);
16+
assert(FLT_ROUNDS==1);
17+
18+
fesetround(FE_TOWARDZERO);
19+
assert(FLT_ROUNDS==0);
20+
21+
fesetround(FE_UPWARD);
22+
assert(FLT_ROUNDS==2);
23+
}
24+
25+
#else
26+
27+
int main()
28+
{
29+
}
30+
31+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/clang_builtin_headers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
typedef float __gcc_v4sf __attribute__ ((__vector_size__ (16)));
22

33
__gcc_v4sf __builtin_shufflevector(__gcc_v4sf, __gcc_v4sf, ...);
4+
5+
int __builtin_flt_rounds(void);

src/ansi-c/library/float.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,19 @@ inline int _isnan(double x)
6767
{
6868
return __CPROVER_isnand(x);
6969
}
70+
71+
/* FUNCTION: __builtin_flt_rounds */
72+
73+
extern int __CPROVER_rounding_mode;
74+
75+
inline int __builtin_flt_rounds(void)
76+
{
77+
// This is a clang builtin for FLT_ROUNDS
78+
// The magic numbers are C99 and different from the
79+
// x86 encoding that CPROVER uses.
80+
return __CPROVER_rounding_mode==0?1: // to nearest
81+
__CPROVER_rounding_mode==1?3: // downward
82+
__CPROVER_rounding_mode==2?2: // upward
83+
__CPROVER_rounding_mode==3?0: // to zero
84+
-1;
85+
}

0 commit comments

Comments
 (0)