init pos cleanup
[babeltrace.git] / formats / ctf / types / float.c
index eb0291ec6eca080b11cfd42ee40efd7df78d9e1d..8bb3f743be2a27bc0e79cbf8380093ce5a6797ef 100644 (file)
@@ -3,28 +3,25 @@
  *
  * 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
  */
 
-#include <ctf/ctf-types.h>
+#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.
  */
 
@@ -67,119 +64,105 @@ union ldoubleIEEE754 {
 #endif
 };
 
+static struct declaration_float *static_ldouble_declaration;
+
 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,
+                    struct definition_float *dest_definition,
+                    struct stream_pos *srcp,
+                    const struct definition_float *src_definition)
 {
-       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;
+       /* Read */
+       if (src_definition->declaration->byte_order == LITTLE_ENDIAN) {
+               ctf_integer_read(srcp, &src_definition->mantissa->p);
+               ctf_integer_read(srcp, &src_definition->exp->p);
+               ctf_integer_read(srcp, &src_definition->sign->p);
        } else {
-               destpos.sign_start = 0;
-               destpos.exp_start = 1;
-               destpos.mantissa_start = destpos.exp_start + dest->exp_len;
+               ctf_integer_read(srcp, &src_definition->sign->p);
+               ctf_integer_read(srcp, &src_definition->exp->p);
+               ctf_integer_read(srcp, &src_definition->mantissa->p);
        }
 
-       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;
+       dest_definition->mantissa->value._unsigned =
+               src_definition->mantissa->value._unsigned;
+       dest_definition->exp->value._signed =
+               src_definition->exp->value._signed;
+       dest_definition->sign->value._unsigned =
+               src_definition->sign->value._unsigned;
+
+       /* Write */
+       if (dest_definition->declaration->byte_order == LITTLE_ENDIAN) {
+               ctf_integer_write(destp, &dest_definition->mantissa->p);
+               ctf_integer_write(destp, &dest_definition->exp->p);
+               ctf_integer_write(destp, &dest_definition->sign->p);
        } else {
-               srcpos.sign_start = 0;
-               srcpos.exp_start = 1;
-               srcpos.mantissa_start = srcpos.exp_start + src->exp_len;
+               ctf_integer_write(destp, &dest_definition->sign->p);
+               ctf_integer_write(destp, &dest_definition->exp->p);
+               ctf_integer_write(destp, &dest_definition->mantissa->p);
        }
-
-       /* sign */
-       tmp.u = bitfield_unsigned_read(ptr, srcpos.sign_start, 1,
-                                      src->byte_order);
-       bitfield_unsigned_write(&u.bits, destpos.sign_start, 1,
-                               dest->byte_order, tmp.u);
-
-       /* mantissa (without leading 1). No sign extend. */
-       tmp.u = bitfield_unsigned_read(ptr, srcpos.mantissa_start,
-                                      src->mantissa_len - 1, src->byte_order);
-       bitfield_unsigned_write(&u.bits, destpos.mantissa_start,
-                               dest->mantissa_len - 1, dest->byte_order, tmp.u);
-
-       /* exponent, with sign-extend. */
-       tmp.s = bitfield_signed_read(ptr, srcpos.exp_start, src->exp_len,
-                                    src->byte_order);
-       bitfield_signed_write(&u.bits, destpos.exp_start, dest->exp_len,
-                             dest->byte_order, tmp.s);
 }
 
-double ctf_double_read(const unsigned char *ptr, const struct ctf_float *src)
+void ctf_float_read(struct stream_pos *ppos, struct definition *definition)
 {
-       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,
-       };
-
-       float_copy(&u.bits, &dest, ptr, src);
-       return u.v;
+       struct definition_float *float_definition =
+               container_of(definition, struct definition_float, p);
+       const struct declaration_float *float_declaration =
+               float_definition->declaration;
+       struct ctf_stream_pos *pos = ctf_pos(ppos);
+       union ldoubleIEEE754 u;
+       struct definition *tmpdef =
+               static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p,
+                               NULL, 0, 0);
+       struct definition_float *tmpfloat =
+               container_of(tmpdef, struct definition_float, p);
+       struct ctf_stream_pos destp;
+
+       ctf_init_pos(&destp, -1, O_WRONLY);
+       destp.base = (char *) u.bits;
+
+       ctf_align_pos(pos, float_declaration->p.alignment);
+       _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition);
+       float_definition->value = u.v;
+       definition_unref(tmpdef);
 }
 
-size_t ctf_double_write(unsigned char *ptr, const struct ctf_float *dest,
-                       double v)
+void ctf_float_write(struct stream_pos *ppos, struct definition *definition)
 {
-       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;
-       u.v = v;
-       float_copy(ptr, dest, &u.bits, &src);
-end:
-       return len;
+       struct definition_float *float_definition =
+               container_of(definition, struct definition_float, p);
+       const struct declaration_float *float_declaration =
+               float_definition->declaration;
+       struct ctf_stream_pos *pos = ctf_pos(ppos);
+       union ldoubleIEEE754 u;
+       struct definition *tmpdef =
+               static_ldouble_declaration->p.definition_new(&static_ldouble_declaration->p,
+                               NULL, 0, 0);
+       struct definition_float *tmpfloat =
+               container_of(tmpdef, struct definition_float, p);
+       struct ctf_stream_pos srcp;
+
+       ctf_init_pos(&srcp, -1, O_RDONLY);
+       srcp.base = (char *) u.bits;
+
+       u.v = float_definition->value;
+       ctf_align_pos(pos, float_declaration->p.alignment);
+       _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat);
+       definition_unref(tmpdef);
 }
 
-long double ctf_ldouble_read(const unsigned char *ptr,
-                            const struct ctf_float *src)
+void __attribute__((constructor)) ctf_float_init(void)
 {
-       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,
-       };
-
-       float_copy(&u.bits, &dest, ptr, src);
-       return u.v;
+       static_ldouble_declaration =
+               float_declaration_new(LDBL_MANT_DIG,
+                               sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG,
+                               BYTE_ORDER,
+                               __alignof__(long double));
 }
 
-size_t ctf_ldouble_write(unsigned char *ptr, const struct ctf_float *dest,
-                        long double v)
+void __attribute__((destructor)) ctf_float_fini(void)
 {
-       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;
-       u.v = v;
-       float_copy(ptr, dest, &u.bits, &src);
-end:
-       return len;
+       declaration_unref(&static_ldouble_declaration->p);
 }
This page took 0.026354 seconds and 4 git commands to generate.