pxd: initial work
[pspp] / src / libpspp / float-format.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006, 2010-2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef LIBPSPP_FLOAT_FORMAT_H
18 #define LIBPSPP_FLOAT_FORMAT_H 1
19
20 #include <byteswap.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <stddef.h>
25 #include "libpspp/compiler.h"
26
27 /* A floating-point format. */
28 enum float_format
29   {
30     /* IEEE 754 formats. */
31     FLOAT_IEEE_SINGLE_LE,          /* 32 bit, little endian. */
32     FLOAT_IEEE_SINGLE_BE,          /* 32 bit, big endian. */
33     FLOAT_IEEE_DOUBLE_LE,          /* 64 bit, little endian. */
34     FLOAT_IEEE_DOUBLE_BE,          /* 64 bit, big endian. */
35
36     /* VAX formats. */
37     FLOAT_VAX_F,                   /* 32 bit VAX F format. */
38     FLOAT_VAX_D,                   /* 64 bit VAX D format. */
39     FLOAT_VAX_G,                   /* 64 bit VAX G format. */
40
41     /* IBM z architecture (390) hexadecimal formats. */
42     FLOAT_Z_SHORT,                 /* 32 bit format. */
43     FLOAT_Z_LONG,                  /* 64 bit format. */
44
45     /* Formats useful for testing. */
46     FLOAT_FP,                      /* Neutral intermediate format. */
47     FLOAT_HEX,                     /* C99 hexadecimal floating constant. */
48
49 #ifdef WORDS_BIGENDIAN
50     FLOAT_NATIVE_FLOAT = FLOAT_IEEE_SINGLE_BE,
51     FLOAT_NATIVE_DOUBLE = FLOAT_IEEE_DOUBLE_BE,
52     FLOAT_NATIVE_32_BIT = FLOAT_IEEE_SINGLE_BE,
53     FLOAT_NATIVE_64_BIT = FLOAT_IEEE_DOUBLE_BE
54 #else
55     FLOAT_NATIVE_FLOAT = FLOAT_IEEE_SINGLE_LE,
56     FLOAT_NATIVE_DOUBLE = FLOAT_IEEE_DOUBLE_LE,
57     FLOAT_NATIVE_32_BIT = FLOAT_IEEE_SINGLE_LE,
58     FLOAT_NATIVE_64_BIT = FLOAT_IEEE_DOUBLE_LE
59 #endif
60   };
61
62 static inline uint64_t double_to_ieee64le (double x);
63 static inline double ieee64le_to_double (uint64_t x);
64
65 void float_convert (enum float_format, const void *,
66                     enum float_format, void *);
67
68 double float_get_double (enum float_format, const void *);
69
70 size_t float_get_size (enum float_format) PURE_FUNCTION;
71
72 int float_identify (double expected_value, const void *, size_t,
73                     enum float_format *best_guess);
74
75 double float_get_lowest (void);
76 \f
77 static inline uint64_t
78 double_to_ieee64le (double x)
79 {
80   uint64_t y;
81
82 #if FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_LE
83   memcpy (&y, &x, sizeof y);
84 #elif FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_BE
85   memcpy (&y, &x, sizeof y);
86   y = bswap_64 (y);
87 #else
88   float_convert (&x, FLOAT_NATIVE_DOUBLE, &y, FLOAT_IEEE_DOUBLE_LE);
89 #endif
90
91   return y;
92 }
93
94 static inline double
95 ieee64le_to_double (uint64_t x)
96 {
97   double y;
98
99 #if FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_LE
100   memcpy (&y, &x, sizeof y);
101 #elif FLOAT_NATIVE_DOUBLE == FLOAT_IEEE_DOUBLE_BE
102   x = bswap_64 (x);
103   memcpy (&y, &x, sizeof y);
104 #else
105   float_convert (&x, FLOAT_IEEE_DOUBLE_LE, &y, FLOAT_NATIVE_DOUBLE);
106 #endif
107
108   return y;
109 }
110
111 #endif /* float-format.h */