1 /* PSPP - computes sample statistics.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <libpspp/float-format.h>
26 #include <language/command.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/assertion.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
32 #define _(msgid) gettext (msgid)
34 /* Maximum supported size of a floating-point number, in bytes. */
35 #define FP_MAX_SIZE 32
37 /* A floating-point number tagged with its representation. */
40 enum float_format format; /* Format. */
41 uint8_t data[FP_MAX_SIZE]; /* Representation. */
44 /* Associates a format name with its identifier. */
48 enum float_format format;
51 /* List of floating-point formats. */
52 static const struct assoc fp_formats[] =
54 {"ISL", FLOAT_IEEE_SINGLE_LE},
55 {"ISB", FLOAT_IEEE_SINGLE_BE},
56 {"IDL", FLOAT_IEEE_DOUBLE_LE},
57 {"IDB", FLOAT_IEEE_DOUBLE_BE},
61 {"ZS", FLOAT_Z_SHORT},
66 static const size_t format_cnt = sizeof fp_formats / sizeof *fp_formats;
68 /* Parses a floating-point format name into *FORMAT,
69 and returns success. */
71 parse_float_format (struct lexer *lexer, enum float_format *format)
75 for (i = 0; i < format_cnt; i++)
76 if (lex_match_id (lexer, fp_formats[i].name))
78 *format = fp_formats[i].format;
81 lex_error (lexer, "expecting floating-point format identifier");
85 /* Returns the name for the given FORMAT. */
87 get_float_format_name (enum float_format format)
91 for (i = 0; i < format_cnt; i++)
92 if (fp_formats[i].format == format)
93 return fp_formats[i].name;
98 /* Parses a number in the form FORMAT(STRING), where FORMAT is
99 the name of the format and STRING gives the number's
100 representation. Also supports ordinary floating-point numbers
101 written in decimal notation. Returns success. */
103 parse_fp (struct lexer *lexer, struct fp *fp)
105 if (lex_is_number (lexer))
107 double number = lex_number (lexer);
108 fp->format = FLOAT_NATIVE_DOUBLE;
109 memcpy (fp->data, &number, sizeof number);
112 else if (lex_token (lexer) == T_ID)
116 if (!parse_float_format (lexer, &fp->format)
117 || !lex_force_match (lexer, '(')
118 || !lex_force_string (lexer))
121 length = ds_length (lex_tokstr (lexer));
122 if (fp->format != FLOAT_HEX)
124 if (length != float_get_size (fp->format))
126 msg (SE, _("%d-byte string needed but %d-byte string supplied."),
127 (int) float_get_size (fp->format), (int) length);
130 assert (length <= sizeof fp->data);
131 memcpy (fp->data, ds_data (lex_tokstr (lexer)), length);
135 if (length >= sizeof fp->data)
137 msg (SE, _("Hexadecimal floating constant too long."));
140 strncpy ((char *) fp->data, ds_cstr (lex_tokstr (lexer)), sizeof fp->data);
144 if (!lex_force_match (lexer, ')'))
149 lex_error (lexer, NULL);
155 /* Renders SRC, which contains SRC_SIZE bytes of a floating-point
156 number in the given FORMAT, as relatively human-readable
157 null-terminated string in the DST_SIZE bytes in DST. DST_SIZE
158 must be be large enough to hold the output. */
160 make_printable (enum float_format format, const void *src_, size_t src_size,
161 char *dst, size_t dst_size)
163 assert (dst_size >= 2 * src_size + 1);
164 if (format != FLOAT_HEX)
166 const uint8_t *src = src_;
167 while (src_size-- > 0)
169 sprintf (dst, "%02x", *src++);
175 strncpy (dst, src_, src_size + 1);
178 /* Checks that RESULT is identical to TO.
179 If so, returns false.
180 If not, issues a helpful error message that includes the given
181 CONVERSION_TYPE and the value that was converted FROM, and
184 mismatch (const struct fp *from, const struct fp *to, char *result,
185 const char *conversion_type)
187 size_t to_size = float_get_size (to->format);
188 if (!memcmp (to->data, result, to_size))
192 size_t from_size = float_get_size (from->format);
193 char original[FP_MAX_SIZE * 2 + 1];
194 char expected[FP_MAX_SIZE * 2 + 1];
195 char actual[FP_MAX_SIZE * 2 + 1];
196 make_printable (from->format, from->data, from_size, original,
198 make_printable (to->format, to->data, to_size, expected,
200 make_printable (to->format, result, to_size, actual, sizeof actual);
202 _("%s conversion of %s from %s to %s should have produced %s "
203 "but actually produced %s."),
205 original, get_float_format_name (from->format),
206 get_float_format_name (to->format), expected,
212 /* Checks that converting FROM into the format of TO yields
213 exactly the data in TO. */
215 verify_conversion (const struct fp *from, const struct fp *to)
217 char tmp1[FP_MAX_SIZE], tmp2[FP_MAX_SIZE];
219 /* First try converting directly. */
220 float_convert (from->format, from->data, to->format, tmp1);
221 if (mismatch (from, to, tmp1, "Direct"))
224 /* Then convert via FLOAT_FP to prevent short-circuiting that
225 float_convert() does for some conversions (e.g. little<->big
226 endian for IEEE formats). */
227 float_convert (from->format, from->data, FLOAT_FP, tmp1);
228 float_convert (FLOAT_FP, tmp1, to->format, tmp2);
229 if (mismatch (from, to, tmp2, "Indirect"))
235 /* Executes the DEBUG FLOAT FORMAT command. */
237 cmd_debug_float_format (struct lexer *lexer, struct dataset *ds UNUSED)
241 bool bijective = false;
246 if (fp_cnt >= sizeof fp / sizeof *fp)
248 msg (SE, _("Too many values in single command."));
251 if (!parse_fp (lexer, &fp[fp_cnt++]))
254 if (lex_token (lexer) == '.' && fp_cnt > 1)
256 else if (!lex_force_match (lexer, '='))
261 if (lex_match (lexer, '='))
263 else if (lex_match (lexer, T_GT))
267 lex_error (lexer, NULL);
273 if ((bijective && !lex_force_match (lexer, '='))
274 || (!bijective && !lex_force_match (lexer, T_GT)))
284 for (i = 0; i < fp_cnt; i++)
285 for (j = 0; j < fp_cnt; j++)
286 if (!verify_conversion (&fp[i], &fp[j]))
293 for (i = 1; i < fp_cnt; i++)
294 if (!verify_conversion (&fp[i - 1], &fp[i]))
298 return ok ? CMD_SUCCESS : CMD_FAILURE;