Skip to content

Commit 01b358b

Browse files
committed
Fix FE_INEXACT triggered by FORM TEAM on flang
1 parent 3ae8148 commit 01b358b

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/caffeine/caffeine.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stdio.h>
66
#include <stdbool.h>
77
#include <assert.h>
8+
#include <math.h>
9+
#include <fenv.h>
810
#include <gasnetex.h>
911
#include <gasnet_coll.h>
1012
#include <gasnet_vis.h>
@@ -34,6 +36,22 @@ typedef uint8_t byte;
3436
static void event_init(void);
3537
static void atomic_init(void);
3638

39+
// ---------------------------------------------------
40+
// Floating-point exception support
41+
42+
#ifndef IEEE_FE_MASK
43+
#define IEEE_FE_MASK FE_INEXACT
44+
#endif
45+
static fexcept_t fe_flag_save;
46+
void caf_fe_save(void) {
47+
fegetexceptflag(&fe_flag_save, IEEE_FE_MASK);
48+
}
49+
void caf_fe_restore(void) {
50+
fesetexceptflag(&fe_flag_save, IEEE_FE_MASK);
51+
}
52+
#define CHECK_INEXACT() \
53+
printf("%3i: inexact flag = %s\n",__LINE__,fetestexcept(FE_INEXACT) & FE_INEXACT ? "YES" : "no")
54+
3755
// ---------------------------------------------------
3856
int caf_this_image(gex_TM_t tm) {
3957
return gex_TM_QueryRank(tm) + 1;
@@ -142,7 +160,14 @@ void caf_allocate_remaining(mspace heap, void** allocated_space, size_t* allocat
142160
// nor necessarily the largest open space, but in practice is likely
143161
// to work out that way
144162
struct mallinfo heap_info = mspace_mallinfo(heap);
145-
*allocated_size = heap_info.keepcost * 0.9f;
163+
164+
// clang's implementation of nearbyint() raises FE_INEXACT,
165+
// in direct contradiction to its specified purpose.
166+
// Workaround this defect by saving and restoring the FE flags
167+
caf_fe_save();
168+
*allocated_size = (size_t)nearbyint(heap_info.keepcost * 0.9f);
169+
caf_fe_restore();
170+
146171
*allocated_space = mspace_memalign(heap, 8, *allocated_size);
147172
if (!*allocated_space) // uh-oh, something went wrong..
148173
gasnett_fatalerror("caf_allocate_remaining failed to mspace_memalign(%"PRIuSZ")",

0 commit comments

Comments
 (0)