[llvm-commits] CVS: llvm/include/llvm/Support/MathExtras.h
Reid Spencer
reid at x10sys.com
Wed Aug 17 15:43:30 PDT 2005
Jim,
Is this going to work okay on all platforms? (see below)
On Wed, 2005-08-17 at 12:28 -0500, Jim Laskey wrote:
>
> Changes in directory llvm/include/llvm/Support:
>
> MathExtras.h updated: 1.23 -> 1.24
> ---
> Log message:
>
> Added support for converting raw bits to FP, and FP to raw bits. The intent
> is to avoid the distraction of the union declarations.
>
>
>
> ---
> Diffs of the changes: (+44 -0)
>
> MathExtras.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 44 insertions(+)
>
>
> Index: llvm/include/llvm/Support/MathExtras.h
> diff -u llvm/include/llvm/Support/MathExtras.h:1.23 llvm/include/llvm/Support/MathExtras.h:1.24
> --- llvm/include/llvm/Support/MathExtras.h:1.23 Wed Aug 3 15:53:19 2005
> +++ llvm/include/llvm/Support/MathExtras.h Wed Aug 17 12:27:47 2005
> @@ -168,6 +168,50 @@
> return 63 - CountLeadingZeros_64(Value);
> }
>
> +// BitsToDouble - This function takes a 64-bit integer and returns the bit
> +// equivalent double.
> +inline double BitsToDouble(uint64_t Bits) {
> + union {
> + uint64_t L;
> + double D;
> + } T;
> + T.L = Bits;
> + return T.D;
> +}
This one should be fine because double and uint64_t are 64-bits on all
platforms we care about.
> +
> +// BitsToFloat - This function takes a 32-bit integer and returns the bit
> +// equivalent float.
> +inline float BitsToFloat(unsigned Bits) {
> + union {
> + unsigned I;
> + float F;
> + } T;
> + T.I = Bits;
> + return T.F;
> +}
This one I'm not so sure about. "unsigned" could be 32 or 64-bits
depending on the platform. float is probably 32-bits on all platforms we
care about. If unsigned is 64-bits and big-endian, we would get the
wrong value for the float as it would be effectively shifted into the
high order bits. Truncation could occur too.
Shouldn't this use uin32_t instead of "unsigned" ?
> +
> +// DoubleToBits - This function takes a double and returns the bit
> +// equivalent 64-bit integer.
> +inline uint64_t DoubleToBits(double Double) {
> + union {
> + uint64_t L;
> + double D;
> + } T;
> + T.D = Double;
> + return T.L;
> +}
This should be okay.
> +
> +// FloatToBits - This function takes a float and returns the bit
> +// equivalent 32-bit integer.
> +inline unsigned FloatToBits(float Float) {
> + union {
> + unsigned I;
> + float F;
> + } T;
> + T.F = Float;
> + return T.I;
> +}
This one suffers same problem as for BitsToFloat
Reid
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20050817/6c0506b8/attachment.sig>
More information about the llvm-commits
mailing list