pxd: initial work
[pspp] / src / libpspp / float-format.h
index 9cabf9bf2e228c0db8be76526096bc3c1694e4ba..028a4e8d0c7ad711ac3135285f2a798567d2f6bc 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2006, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2010-2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #ifndef LIBPSPP_FLOAT_FORMAT_H
 #define LIBPSPP_FLOAT_FORMAT_H 1
 
+#include <byteswap.h>
 #include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
 #include <stddef.h>
 #include "libpspp/compiler.h"
 
@@ -56,6 +59,9 @@ enum float_format
 #endif
   };
 
+static inline uint64_t double_to_ieee64le (double x);
+static inline double ieee64le_to_double (uint64_t x);
+
 void float_convert (enum float_format, const void *,
                     enum float_format, void *);
 
@@ -67,5 +73,39 @@ int float_identify (double expected_value, const void *, size_t,
                     enum float_format *best_guess);
 
 double float_get_lowest (void);
+\f
+static inline uint64_t
+double_to_ieee64le (double x)
+{
+  uint64_t y;
+
+#if FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_LE
+  memcpy (&y, &x, sizeof y);
+#elif FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_BE
+  memcpy (&y, &x, sizeof y);
+  y = bswap_64 (y);
+#else
+  float_convert (&x, FLOAT_NATIVE_DOUBLE, &y, FLOAT_IEEE_DOUBLE_LE);
+#endif
+
+  return y;
+}
+
+static inline double
+ieee64le_to_double (uint64_t x)
+{
+  double y;
+
+#if FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_LE
+  memcpy (&y, &x, sizeof y);
+#elif FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_BE
+  x = bswap_64 (x);
+  memcpy (&y, &x, sizeof y);
+#else
+  float_convert (&x, FLOAT_IEEE_DOUBLE_LE, &y, FLOAT_NATIVE_DOUBLE);
+#endif
+
+  return y;
+}
 
 #endif /* float-format.h */