X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf%2Ftypes%2Ffloat.c;h=be068316eb9241084abd4b0e662d6f2acd1880a9;hp=ce094747113f762c3f8785a9af913d7128de0882;hb=4c8bfb7e0a9cef6e74cefa38ed54bf8cbd424183;hpb=a3dbc7949a29a2abe9c7e75f2d893f99d36d6256 diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index ce094747..be068316 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -3,21 +3,17 @@ * * Floating point read/write functions. * - * Copyright (c) 2010 Mathieu Desnoyers + * Copyright 2010 - Mathieu Desnoyers * - * 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 */ @@ -72,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 type_class_float *dest_class, + struct stream_pos *srcp, + const struct type_class_float *src_class) { - 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_class->byte_order == LITTLE_ENDIAN) { + mantissa = ctf_uint_read(srcp, src_class->mantissa); + exp = ctf_int_read(srcp, src_class->exp); + sign = ctf_uint_read(srcp, src_class->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_class->sign); + exp = ctf_int_read(srcp, src_class->exp); + mantissa = ctf_uint_read(srcp, src_class->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_class->byte_order == LITTLE_ENDIAN) { + ctf_uint_write(destp, dest_class->mantissa, mantissa); + ctf_int_write(destp, dest_class->exp, exp); + ctf_uint_write(destp, dest_class->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_class->sign, sign); + ctf_int_write(destp, dest_class->exp, exp); + ctf_uint_write(destp, dest_class->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 type_class_float *float_class) +{ + align_pos(src, float_class->p.alignment); + align_pos(dest, float_class->p.alignment); + _ctf_float_copy(dest, float_class, src, float_class); } -double ctf_double_read(const unsigned char *ptr, const struct ctf_float *src) +double ctf_double_read(struct stream_pos *srcp, + const struct type_class_float *float_class) { 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 type_class_float *dest_class = float_type_new(NULL, + DBL_MANT_DIG, + sizeof(double) * CHAR_BIT - DBL_MANT_DIG, + BYTE_ORDER, + __alignof__(double)); + struct stream_pos destp; + + align_pos(srcp, float_class->p.alignment); + init_pos(&destp, (unsigned char *) u.bits); + _ctf_float_copy(&destp, dest_class, srcp, float_class); + float_type_free(dest_class); 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 type_class_float *float_class, + 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 type_class_float *src_class = float_type_new(NULL, + 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_class->p.alignment); + init_pos(&srcp, (unsigned char *) u.bits); + _ctf_float_copy(destp, float_class, &srcp, src_class); + float_type_free(src_class); } -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 type_class_float *float_class) { 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 type_class_float *dest_class = float_type_new(NULL, + LDBL_MANT_DIG, + sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG, + BYTE_ORDER, + __alignof__(long double)); + struct stream_pos destp; + + align_pos(srcp, float_class->p.alignment); + init_pos(&destp, (unsigned char *) u.bits); + _ctf_float_copy(&destp, dest_class, srcp, float_class); + float_type_free(dest_class); 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 type_class_float *float_class, + 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 type_class_float *src_class = float_type_new(NULL, + 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_class->p.alignment); + init_pos(&srcp, (unsigned char *) u.bits); + _ctf_float_copy(destp, float_class, &srcp, src_class); + float_type_free(src_class); }