3 * Encoding and decoding floating-point values
5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
37 /*----- External functions ------------------------------------------------*/
39 /* --- @buf_getf{32,64}{,l,b} --- *
41 * Arguments: @buf *b@ = a buffer to read from
42 * @float *x_out@, @double *x_out@ = where to put the result
44 * Returns: Zero on success, %$-1$% on failure (and the buffer is
47 * Use: Get an IEEE Binary32 or Binary64 value from the buffer.
48 * Conversion is performed using the `fltfmt' machinery, with
49 * the usual round-to-nearest/ties-to-even rounding mode.
52 int buf_getf32(buf *b, float *x_out)
56 p = buf_get(b, 4); if (!p) return (-1);
57 fltfmt_f32btoflt(x_out, p, FLTRND_NEAREVEN); return (0);
60 int buf_getf32l(buf *b, float *x_out)
64 p = buf_get(b, 4); if (!p) return (-1);
65 fltfmt_f32ltoflt(x_out, p, FLTRND_NEAREVEN); return (0);
68 int buf_getf32b(buf *b, float *x_out)
72 p = buf_get(b, 4); if (!p) return (-1);
73 fltfmt_f32ltoflt(x_out, p, FLTRND_NEAREVEN); return (0);
76 int (dbuf_getf32)(dbuf *db, float *x_out)
77 { return (dbuf_getf32(db, x_out)); }
78 int (dbuf_getf32l)(dbuf *db, float *x_out)
79 { return (dbuf_getf32l(db, x_out)); }
80 int (dbuf_getf32b)(dbuf *db, float *x_out)
81 { return (dbuf_getf32b(db, x_out)); }
83 int buf_getf64(buf *b, double *x_out)
87 p = buf_get(b, 8); if (!p) return (-1);
88 fltfmt_f64btodbl(x_out, p, FLTRND_NEAREVEN); return (0);
91 int buf_getf64l(buf *b, double *x_out)
95 p = buf_get(b, 8); if (!p) return (-1);
96 fltfmt_f64ltodbl(x_out, p, FLTRND_NEAREVEN); return (0);
99 int buf_getf64b(buf *b, double *x_out)
103 p = buf_get(b, 8); if (!p) return (-1);
104 fltfmt_f64ltodbl(x_out, p, FLTRND_NEAREVEN); return (0);
107 int (dbuf_getf64)(dbuf *db, double *x_out)
108 { return (dbuf_getf64(db, x_out)); }
109 int (dbuf_getf64l)(dbuf *db, double *x_out)
110 { return (dbuf_getf64l(db, x_out)); }
111 int (dbuf_getf64b)(dbuf *db, double *x_out)
112 { return (dbuf_getf64b(db, x_out)); }
114 /* --- @buf_putf{32,64}{,l,b} --- *
116 * Arguments: @buf *b@ = a buffer to write to
117 * @double x@ = a number to write
119 * Returns: Zero on success, %$-1$% on failure (and the buffer is
122 * Use: Get an IEEE Binary32 or Binary64 value from the buffer.
123 * Conversion is performed using the `fltfmt' machinery, with
124 * the usual round-to-nearest/ties-to-even rounding mode.
127 int buf_putf32(buf *b, float x)
131 p = buf_get(b, 4); if (!p) return (-1);
132 fltfmt_flttof32b(p, x, FLTRND_NEAREVEN); return (0);
135 int buf_putf32l(buf *b, float x)
139 p = buf_get(b, 4); if (!p) return (-1);
140 fltfmt_flttof32l(p, x, FLTRND_NEAREVEN); return (0);
143 int buf_putf32b(buf *b, float x)
147 p = buf_get(b, 4); if (!p) return (-1);
148 fltfmt_flttof32b(p, x, FLTRND_NEAREVEN); return (0);
151 int (dbuf_putf32)(dbuf *db, float x)
152 { return (dbuf_putf32(db, x)); }
153 int (dbuf_putf32l)(dbuf *db, float x)
154 { return (dbuf_putf32l(db, x)); }
155 int (dbuf_putf32b)(dbuf *db, float x)
156 { return (dbuf_putf32b(db, x)); }
158 int buf_putf64(buf *b, double x)
162 p = buf_get(b, 8); if (!p) return (-1);
163 fltfmt_dbltof64b(p, x, FLTRND_NEAREVEN); return (0);
166 int buf_putf64l(buf *b, double x)
170 p = buf_get(b, 8); if (!p) return (-1);
171 fltfmt_dbltof64l(p, x, FLTRND_NEAREVEN); return (0);
174 int buf_putf64b(buf *b, double x)
178 p = buf_get(b, 8); if (!p) return (-1);
179 fltfmt_dbltof64b(p, x, FLTRND_NEAREVEN); return (0);
182 int (dbuf_putf64)(dbuf *db, double x)
183 { return (dbuf_putf64(db, x)); }
184 int (dbuf_putf64l)(dbuf *db, double x)
185 { return (dbuf_putf64l(db, x)); }
186 int (dbuf_putf64b)(dbuf *db, double x)
187 { return (dbuf_putf64b(db, x)); }
189 /*----- That's all, folks -------------------------------------------------*/