1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include <libpspp/float-format.h>
24 #include <language/command.h>
25 #include <language/lexer/lexer.h>
26 #include <libpspp/assertion.h>
27 #include <libpspp/message.h>
28 #include <libpspp/str.h>
30 #define _(msgid) gettext (msgid)
32 /* Maximum supported size of a floating-point number, in bytes. */
33 #define FP_MAX_SIZE 32
35 /* A floating-point number tagged with its representation. */
38 enum float_format format; /* Format. */
39 uint8_t data[FP_MAX_SIZE]; /* Representation. */
42 /* Associates a format name with its identifier. */
46 enum float_format format;
49 /* List of floating-point formats. */
50 static const struct assoc fp_formats[] =
52 {"ISL", FLOAT_IEEE_SINGLE_LE},
53 {"ISB", FLOAT_IEEE_SINGLE_BE},
54 {"IDL", FLOAT_IEEE_DOUBLE_LE},
55 {"IDB", FLOAT_IEEE_DOUBLE_BE},
59 {"ZS", FLOAT_Z_SHORT},
64 static const size_t format_cnt = sizeof fp_formats / sizeof *fp_formats;
66 /* Parses a floating-point format name into *FORMAT,
67 and returns success. */
69 parse_float_format (struct lexer *lexer, enum float_format *format)
73 for (i = 0; i < format_cnt; i++)
74 if (lex_match_id (lexer, fp_formats[i].name))
76 *format = fp_formats[i].format;
79 lex_error (lexer, "expecting floating-point format identifier");
83 /* Returns the name for the given FORMAT. */
85 get_float_format_name (enum float_format format)
89 for (i = 0; i < format_cnt; i++)
90 if (fp_formats[i].format == format)
91 return fp_formats[i].name;
96 /* Parses a number in the form FORMAT(STRING), where FORMAT is
97 the name of the format and STRING gives the number's
98 representation. Also supports ordinary floating-point numbers
99 written in decimal notation. Returns success. */
101 parse_fp (struct lexer *lexer, struct fp *fp)
103 if (lex_is_number (lexer))
105 double number = lex_number (lexer);
106 fp->format = FLOAT_NATIVE_DOUBLE;
107 memcpy (fp->data, &number, sizeof number);
110 else if (lex_token (lexer) == T_ID)
114 if (!parse_float_format (lexer, &fp->format)
115 || !lex_force_match (lexer, '(')
116 || !lex_force_string (lexer))
119 length = ds_length (lex_tokstr (lexer));
120 if (fp->format != FLOAT_HEX)
122 if (length != float_get_size (fp->format))
124 msg (SE, _("%zu-byte string needed but %zu-byte string "
126 float_get_size (fp->format), length);
129 assert (length <= sizeof fp->data);
130 memcpy (fp->data, ds_data (lex_tokstr (lexer)), length);
134 if (length >= sizeof fp->data)
136 msg (SE, _("Hexadecimal floating constant too long."));
139 strncpy ((char *) fp->data, ds_cstr (lex_tokstr (lexer)), sizeof fp->data);
143 if (!lex_force_match (lexer, ')'))
148 lex_error (lexer, NULL);
154 /* Renders SRC, which contains SRC_SIZE bytes of a floating-point
155 number in the given FORMAT, as relatively human-readable
156 null-terminated string in the DST_SIZE bytes in DST. DST_SIZE
157 must be be large enough to hold the output. */
159 make_printable (enum float_format format, const void *src_, size_t src_size,
160 char *dst, size_t dst_size)
162 assert (dst_size >= 2 * src_size + 1);
163 if (format != FLOAT_HEX)
165 const uint8_t *src = src_;
166 while (src_size-- > 0)
168 sprintf (dst, "%02x", *src++);
174 strncpy (dst, src_, src_size + 1);
177 /* Checks that RESULT is identical to TO.
178 If so, returns false.
179 If not, issues a helpful error message that includes the given
180 CONVERSION_TYPE and the value that was converted FROM, and
183 mismatch (const struct fp *from, const struct fp *to, char *result,
184 const char *conversion_type)
186 size_t to_size = float_get_size (to->format);
187 if (!memcmp (to->data, result, to_size))
191 size_t from_size = float_get_size (from->format);
192 char original[FP_MAX_SIZE * 2 + 1];
193 char expected[FP_MAX_SIZE * 2 + 1];
194 char actual[FP_MAX_SIZE * 2 + 1];
195 make_printable (from->format, from->data, from_size, original,
197 make_printable (to->format, to->data, to_size, expected,
199 make_printable (to->format, result, to_size, actual, sizeof actual);
201 _("%s conversion of %s from %s to %s should have produced %s "
202 "but actually produced %s."),
204 original, get_float_format_name (from->format),
205 get_float_format_name (to->format), expected,
211 /* Checks that converting FROM into the format of TO yields
212 exactly the data in TO. */
214 verify_conversion (const struct fp *from, const struct fp *to)
216 char tmp1[FP_MAX_SIZE], tmp2[FP_MAX_SIZE];
218 /* First try converting directly. */
219 float_convert (from->format, from->data, to->format, tmp1);
220 if (mismatch (from, to, tmp1, "Direct"))
223 /* Then convert via FLOAT_FP to prevent short-circuiting that
224 float_convert() does for some conversions (e.g. little<->big
225 endian for IEEE formats). */
226 float_convert (from->format, from->data, FLOAT_FP, tmp1);
227 float_convert (FLOAT_FP, tmp1, to->format, tmp2);
228 if (mismatch (from, to, tmp2, "Indirect"))
234 /* Executes the DEBUG FLOAT FORMAT command. */
236 cmd_debug_float_format (struct lexer *lexer, struct dataset *ds UNUSED)
240 bool bijective = false;
245 if (fp_cnt >= sizeof fp / sizeof *fp)
247 msg (SE, _("Too many values in single command."));
250 if (!parse_fp (lexer, &fp[fp_cnt++]))
253 if (lex_token (lexer) == '.' && fp_cnt > 1)
255 else if (!lex_force_match (lexer, '='))
260 if (lex_match (lexer, '='))
262 else if (lex_match (lexer, T_GT))
266 lex_error (lexer, NULL);
272 if ((bijective && !lex_force_match (lexer, '='))
273 || (!bijective && !lex_force_match (lexer, T_GT)))
283 for (i = 0; i < fp_cnt; i++)
284 for (j = 0; j < fp_cnt; j++)
285 if (!verify_conversion (&fp[i], &fp[j]))
292 for (i = 1; i < fp_cnt; i++)
293 if (!verify_conversion (&fp[i - 1], &fp[i]))
297 return ok ? CMD_SUCCESS : CMD_FAILURE;