[PATCH] Add getSwappedBytes implementations for float / double

Justin Bogner mail at justinbogner.com
Tue Jan 27 13:57:41 PST 2015


Matt Arsenault <Matthew.Arsenault at amd.com> writes:
> Alternatively these could be special cased in endian::Writer

Is anyone using endian::Writer with doubles or floats? That is, is this
a bug fix or is it a new feature?

If it's a bug fix it'd be nice if it came with a test. If a new feature,
it's probably better to commit it along with its first use.

> REPOSITORY
>   rL LLVM
>
> http://reviews.llvm.org/D6730
>
> Files:
>   include/llvm/Support/SwapByteOrder.h
>
> Index: include/llvm/Support/SwapByteOrder.h
> ===================================================================
> --- include/llvm/Support/SwapByteOrder.h
> +++ include/llvm/Support/SwapByteOrder.h
> @@ -19,6 +19,7 @@
>  #include "llvm/Support/DataTypes.h"
>  #include <cstddef>
>  #include <limits>
> +#include <string>
>  
>  namespace llvm {
>  namespace sys {
> @@ -77,6 +78,26 @@
>  inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
>  inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
>  
> +inline uint32_t getSwappedBytes(float C) {
> +  union {
> +    uint32_t I;
> +    float F;
> +  } Tmp;
> +
> +  std::memcpy(&Tmp, &C, sizeof(float));

Better to write &Tmp.F here, it's a little bit clearer. Similarly below.

> +  return SwapByteOrder_32(Tmp.I);
> +}
> +
> +inline uint64_t getSwappedBytes(double C) {
> +  union {
> +    uint64_t I;
> +    double F;
> +  } Tmp;
> +
> +  std::memcpy(&Tmp, &C, sizeof(double));
> +  return SwapByteOrder_64(Tmp.I);
> +}
> +
>  #if __LONG_MAX__ == __INT_MAX__
>  inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_32(C); }
>  inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_32(C); }
>
> EMAIL PREFERENCES
>   http://reviews.llvm.org/settings/panel/emailpreferences/
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits



More information about the llvm-commits mailing list