Mercury Bugs - mercury
View Issue Details
0000211mercuryBugpublic2011-08-22 16:582011-12-09 10:26
Reporterwangp 
Assigned Towangp 
PrioritynormalSeverityminorReproducibilityhave not tried
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000211: single-precision float grades broken on 64-bit
DescriptionBoth LLDS (and now high-level C grades) generate structures like this for static data.

struct foo {
  MR_Word f1;
  MR_Float f2;
  MR_Float f3;
};

which is then treated as an array of MR_Word. However, if using MR_Float==float on a 64-bit platform, the C compiler will pack the two float members into a single word.

Two possible solutions are:

1. use C compiler extensions to force any MR_Float members to be word-aligned

2. replace the MR_Float members by a union, e.g.

union MR_Float_Word {
  MR_Float f;
  MR_Word w;
};

This requires additional braces in initialiser lists.
TagsNo tags attached.
Attached Files

Notes
(0000350)
juliensf   
2011-08-22 21:51   
I don't think (1) is a solution in this context. The source distribution .c files shouldn't
make assumptions about the availability or otherwise of compiler extensions.
(0000351)
wangp   
2011-08-23 10:31   
This only affects single-precision grades on 64-bit platforms, which likely no one uses anyway (why would they? Maybe we should just say it's not supported, and make the compiler issue an error.) Compiler extensions only need to be invoked a rare circumstance.

/* if defined(MR_USE_SINGLE_PRECISION) && sizeof(MR_Float) < sizeof(MR_Word) */
#if defined(MR_GNUC) || defined(MR_CLANG)
    typedef MR_Float MR_Float_Aligned __attribute__((aligned(sizeof(MR_Word))));
#elif defined(MR_MSVC)
    typedef _declspec(align(sizeof(MR_Word))) MR_Float MR_Float_Aligned;
#else
    #error MR_Float_Aligned not defined
#endif

/* otherwise */
typedef MR_Float MR_Float_Aligned;


(2) is problematic for high-level data grades. Presumably we would need to use union MR_Float_Word in structures where ever MR_Float currently is, so all field references will need an extra .f suffix.
(0000352)
juliensf   
2011-08-23 13:38   
Given that the relevant extensions are supported by gcc, clang and visual C, I don't have any objections
to (1).
(0000436)
wangp   
2011-12-09 10:26   
Fix committed.

Issue History
2011-08-22 16:58wangpNew Issue
2011-08-22 21:51juliensfNote Added: 0000350
2011-08-23 10:31wangpNote Added: 0000351
2011-08-23 13:38juliensfNote Added: 0000352
2011-12-09 10:26wangpNote Added: 0000436
2011-12-09 10:26wangpStatusnew => resolved
2011-12-09 10:26wangpResolutionopen => fixed
2011-12-09 10:26wangpAssigned To => wangp