Work in progress generate io struct
[babeltrace.git] / formats / ctf / types / float.c
index a26e94cc20760e9fd88088ac464655964a371df0..72c779123bbd74e628e4a31b8eb0c847882c47cf 100644 (file)
@@ -3,21 +3,17 @@
  *
  * Floating point read/write functions.
  *
- * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
  * Reference: ISO C99 standard 5.2.4
  */
@@ -25,6 +21,7 @@
 #include <babeltrace/ctf/types.h>
 #include <glib.h>
 #include <float.h>     /* C99 floating point definitions */
+#include <limits.h>    /* C99 limits */
 #include <endian.h>
 
 /*
@@ -35,7 +32,7 @@
 
 /*
  * Aliasing float/double and unsigned long is not strictly permitted by strict
- * aliasing, but in practice type prunning is well supported, and this permits
+ * aliasing, but in practice declaration prunning is well supported, and this permits
  * us to use per-word read/writes rather than per-byte.
  */
 
@@ -71,117 +68,115 @@ struct pos_len {
        size_t sign_start, exp_start, mantissa_start, len;
 };
 
-void ctf_float_copy(unsigned char *destp, const struct ctf_float *dest,
-                   const unsigned char *src, const struct ctf_float *src)
+void _ctf_float_copy(struct stream_pos *destp,
+                    const struct declaration_float *dest_declaration,
+                    struct stream_pos *srcp,
+                    const struct declaration_float *src_declaration)
 {
-       struct pos_len destpos, srcpos;
-       union {
-               unsigned long long u;
-               long long s;
-       } tmp;
-
-       destpos.len = dest.exp_len + dest.mantissa_len;
-       if (dest.byte_order == LITTLE_ENDIAN) {
-               destpos.sign_start = destpos.len - 1;
-               destpos.exp_start = destpos.sign_start - dest->exp_len;
-               destpos.mantissa_start = 0;
+       uint8_t sign;
+       int64_t exp;
+       uint64_t mantissa;
+
+       /* Read */
+       if (src_declaration->byte_order == LITTLE_ENDIAN) {
+               mantissa = ctf_uint_read(srcp, src_declaration->mantissa);
+               exp = ctf_int_read(srcp, src_declaration->exp);
+               sign = ctf_uint_read(srcp, src_declaration->sign);
        } else {
-               destpos.sign_start = 0;
-               destpos.exp_start = 1;
-               destpos.mantissa_start = destpos.exp_start + dest->exp_len;
+               sign = ctf_uint_read(srcp, src_declaration->sign);
+               exp = ctf_int_read(srcp, src_declaration->exp);
+               mantissa = ctf_uint_read(srcp, src_declaration->mantissa);
        }
-
-       srcpos.len = src.exp_len + src.mantissa_len;
-       if (src.byte_order == LITTLE_ENDIAN) {
-               srcpos.sign_start = srcpos.len - 1;
-               srcpos.exp_start = srcpos.sign_start - src->exp_len;
-               srcpos.mantissa_start = 0;
+       /* Write */
+       if (dest_declaration->byte_order == LITTLE_ENDIAN) {
+               ctf_uint_write(destp, dest_declaration->mantissa, mantissa);
+               ctf_int_write(destp, dest_declaration->exp, exp);
+               ctf_uint_write(destp, dest_declaration->sign, sign);
        } else {
-               srcpos.sign_start = 0;
-               srcpos.exp_start = 1;
-               srcpos.mantissa_start = srcpos.exp_start + src->exp_len;
+               ctf_uint_write(destp, dest_declaration->sign, sign);
+               ctf_int_write(destp, dest_declaration->exp, exp);
+               ctf_uint_write(destp, dest_declaration->mantissa, mantissa);
        }
+}
 
-       /* sign */
-       tmp.u = ctf_bitfield_unsigned_read(ptr, srcpos.sign_start, 1,
-                                          src->byte_order);
-       ctf_bitfield_unsigned_write(&u.bits, destpos.sign_start, 1,
-                                   dest->byte_order, tmp.u);
-
-       /* mantissa (without leading 1). No sign extend. */
-       tmp.u = ctf_bitfield_unsigned_read(ptr, srcpos.mantissa_start,
-                                          src->mantissa_len - 1,
-                                          src->byte_order);
-       ctf_bitfield_unsigned_write(&u.bits, destpos.mantissa_start,
-                                   dest->mantissa_len - 1, dest->byte_order,
-                                   tmp.u);
-
-       /* exponent, with sign-extend. */
-       tmp.s = ctf_bitfield_signed_read(ptr, srcpos.exp_start, src->exp_len,
-                                        src->byte_order);
-       ctf_bitfield_signed_write(&u.bits, destpos.exp_start, dest->exp_len,
-                                 dest->byte_order, tmp.s);
+void ctf_float_copy(struct stream_pos *dest, struct stream_pos *src,
+                   const struct declaration_float *float_declaration)
+{
+       align_pos(src, float_declaration->p.alignment);
+       align_pos(dest, float_declaration->p.alignment);
+       _ctf_float_copy(dest, float_declaration, src, float_declaration);
 }
 
-double ctf_double_read(const unsigned char *ptr, const struct ctf_float *src)
+double ctf_double_read(struct stream_pos *srcp,
+                      const struct declaration_float *float_declaration)
 {
        union doubleIEEE754 u;
-       struct ctf_float dest = {
-               .exp_len = sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
-               .mantissa_len = DBL_MANT_DIG,
-               .byte_order = BYTE_ORDER,
-       };
-
-       ctf_float_copy(&u.bits, &dest, ptr, src);
+       struct declaration_float *dest_declaration =
+               float_declaration_new(DBL_MANT_DIG,
+                               sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
+                               BYTE_ORDER,
+                               __alignof__(double));
+       struct stream_pos destp;
+
+       align_pos(srcp, float_declaration->p.alignment);
+       init_pos(&destp, (char *) u.bits);
+       _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration);
+       declaration_unref(&dest_declaration->p);
        return u.v;
 }
 
-size_t ctf_double_write(unsigned char *ptr, const struct ctf_float *dest,
-                       double v)
+void ctf_double_write(struct stream_pos *destp,
+                     const struct declaration_float *float_declaration,
+                     double v)
 {
        union doubleIEEE754 u;
-       struct ctf_float src = {
-               .exp_len = sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
-               .mantissa_len = DBL_MANT_DIG,
-               .byte_order = BYTE_ORDER,
-       };
-
-       if (!ptr)
-               goto end;
+       struct declaration_float *src_declaration =
+               float_declaration_new(DBL_MANT_DIG,
+                               sizeof(double) * CHAR_BIT - DBL_MANT_DIG,
+                               BYTE_ORDER,
+                               __alignof__(double));
+       struct stream_pos srcp;
+
        u.v = v;
-       ctf_float_copy(ptr, dest, &u.bits, &src);
-end:
-       return len;
+       align_pos(destp, float_declaration->p.alignment);
+       init_pos(&srcp, (char *) u.bits);
+       _ctf_float_copy(destp, float_declaration, &srcp, src_declaration);
+       declaration_unref(&src_declaration->p);
 }
 
-long double ctf_ldouble_read(const unsigned char *ptr,
-                            const struct ctf_float *src)
+long double ctf_ldouble_read(struct stream_pos *srcp,
+                            const struct declaration_float *float_declaration)
 {
        union ldoubleIEEE754 u;
-       struct ctf_float dest = {
-               .exp_len = sizeof(double) * CHAR_BIT - LDBL_MANT_DIG,
-               .mantissa_len = LDBL_MANT_DIG,
-               .byte_order = BYTE_ORDER,
-       };
-
-       ctf_float_copy(&u.bits, &dest, ptr, src);
+       struct declaration_float *dest_declaration =
+               float_declaration_new(LDBL_MANT_DIG,
+                               sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG,
+                               BYTE_ORDER,
+                               __alignof__(long double));
+       struct stream_pos destp;
+
+       align_pos(srcp, float_declaration->p.alignment);
+       init_pos(&destp, (char *) u.bits);
+       _ctf_float_copy(&destp, dest_declaration, srcp, float_declaration);
+       declaration_unref(&dest_declaration->p);
        return u.v;
 }
 
-size_t ctf_ldouble_write(unsigned char *ptr, const struct ctf_float *dest,
-                        long double v)
+void ctf_ldouble_write(struct stream_pos *destp,
+                      const struct declaration_float *float_declaration,
+                      long double v)
 {
        union ldoubleIEEE754 u;
-       struct ctf_float src = {
-               .exp_len = sizeof(double) * CHAR_BIT - LDBL_MANT_DIG,
-               .mantissa_len = LDBL_MANT_DIG,
-               .byte_order = BYTE_ORDER,
-       };
-
-       if (!ptr)
-               goto end;
+       struct declaration_float *src_declaration =
+               float_declaration_new(LDBL_MANT_DIG,
+                               sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG,
+                               BYTE_ORDER,
+                               __alignof__(long double));
+       struct stream_pos srcp;
+
        u.v = v;
-       ctf_float_copy(ptr, dest, &u.bits, &src);
-end:
-       return len;
+       align_pos(destp, float_declaration->p.alignment);
+       init_pos(&srcp, (char *) u.bits);
+       _ctf_float_copy(destp, float_declaration, &srcp, src_declaration);
+       declaration_unref(&src_declaration->p);
 }
This page took 0.026652 seconds and 4 git commands to generate.