[m-rev.] for review: fix float field structure padding/packing problems

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Dec 8 14:06:44 AEDT 2011


On Tue, 6 Dec 2011, Peter Wang wrote:

> Branches: main
>
> Fix bug #240 and bug #211.  When MR_Float is wider than MR_Word, the C compiler
> may introduce padding for the structure field following the MR_Float.  When
> MR_Float is narrower than MR_Word the C compiler may pack consecutive MR_Float
> fields.  In both cases the C structure layout does not match what was intended
> by the Mercury compiler.

...

> diff --git a/runtime/mercury_float.h b/runtime/mercury_float.h
> index 049477f..d600045 100644
> --- a/runtime/mercury_float.h
> +++ b/runtime/mercury_float.h
> @@ -29,6 +29,23 @@
> #define MR_FLOAT_WORDS		((sizeof(MR_Float) + sizeof(MR_Word) - 1) \
> 					/ sizeof(MR_Word))
>
> +  /*
> +  ** MR_Float_Aligned and #pragma pack are used convince the C compiler to lay
> +  ** out structures containing MR_Float members as expected by the Mercury
> +  ** compiler, without additional padding or packing.
> +  **
> +  ** For MSVC, __declspec(align) only increases alignment, e.g. for single
> +  ** precision floats on 64-bit platforms. #pragma pack is required to
> +  ** reduce packing size, e.g. double precision floats on 32-bit platform.
> +  */
> +#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_BYTES_PER_WORD))) MR_Float MR_Float_Aligned;

s/sizeof(MR_BYTES_PER_WORD)/MR_BYTES_PER_WORD/

The rest looks fine.  I have tested it with MSVC and it works.

Julien.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list