From: Mathieu Desnoyers Date: Mon, 4 Oct 2010 13:51:34 +0000 (-0400) Subject: Finish float X-Git-Tag: v0.1~225 X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=11d43b909baeee566511acfec577d4605386fa09 Finish float Signed-off-by: Mathieu Desnoyers --- diff --git a/formats/ctf/types/float.c b/formats/ctf/types/float.c index 09a20e3a..20cf7fa5 100644 --- a/formats/ctf/types/float.c +++ b/formats/ctf/types/float.c @@ -72,119 +72,119 @@ struct pos_len { size_t sign_start, exp_start, mantissa_start, len; }; -/* TODO */ - -void ctf_float_copy(unsigned char *destp, const struct type_class_float *dest, - const unsigned char *src, const struct type_class_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; + uint8_t sign; + int64_t exp; + uint64_t mantissa; - 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->byte_order == LITTLE_ENDIAN) { + mantissa = ctf_bitfield_unsigned_read(srcp, + src_class->mantissa); + exp = ctf_bitfield_signed_read(srcp, src_class->exp); + sign = ctf_bitfield_unsigned_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_bitfield_unsigned_read(srcp, src_class->sign); + exp = ctf_bitfield_signed_read(srcp, src_class->exp); + mantissa = ctf_bitfield_unsigned_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->byte_order == LITTLE_ENDIAN) { + ctf_bitfield_unsigned_write(destp, dest_class->mantissa, + mantissa); + ctf_bitfield_signed_write(destp, dest_class->exp, exp); + ctf_bitfield_unsigned_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_bitfield_unsigned_write(destp, dest_class->sign, sign); + ctf_bitfield_signed_write(destp, dest_class->exp, exp); + ctf_bitfield_unsigned_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 type_class_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 ctf_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, &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 type_class_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 ctf_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, &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 type_class_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 ctf_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, &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 type_class_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 ctf_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, &u.bits); + _ctf_float_copy(destp, float_class, &srcp, src_class); + float_type_free(src_class); } diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index 43b38eed..53919729 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -51,14 +51,13 @@ struct format { const struct type_class_bitfield *bitfield_class, int64_t v); - void (*float_copy)(struct stream_pos *destp, - const struct type_class_float *dest, - struct stream_pos *srcp, + void (*float_copy)(struct stream_pos *dest, + struct stream_pos *src, const struct type_class_float *src); double (*double_read)(struct stream_pos *pos, - const struct type_class_float *src); + const struct type_class_float *float_class); void (*double_write)(struct stream_pos *pos, - const struct type_class_float *dest, + const struct type_class_float *float_class, double v); void (*string_copy)(struct stream_pos *dest, struct stream_pos *src, diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index 86262408..3c66563f 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -111,6 +111,7 @@ struct type_class_bitfield { struct type_class_float { struct type_class p; + struct bitfield_class *sign; struct bitfield_class *mantissa; struct bitfield_class *exp; int byte_order; @@ -142,17 +143,21 @@ int ctf_register_type(struct type_class *type_class); /* Nameless types can be created by passing a NULL name */ struct type_class_integer *integer_type_new(const char *name, - size_t start_offset, size_t len, int byte_order, - int signedness); + int signedness, + size_t alignment); void integer_type_free(struct type_class_integer *int_class); struct type_class_bitfield *bitfield_type_new(const char *name, - size_t start_offset, size_t len, int byte_order, - int signedness); + int signedness, + size_t alignment); void bitfield_type_free(struct type_class_bitfield *bitfield_class); +/* + * mantissa_len is the length of the number of bytes represented by the mantissa + * (e.g. result of DBL_MANT_DIG). It includes the leading 1. + */ struct type_class_float *float_type_new(const char *name, size_t mantissa_len, size_t exp_len, int byte_order, @@ -175,7 +180,6 @@ void enum_unsigned_insert(struct type_class_enum *enum_class, uint64_t v, GQuark q); struct type_class_enum *enum_type_new(const char *name, - size_t start_offset, size_t len, int byte_order, int signedness, size_t alignment); diff --git a/types/enum.c b/types/enum.c index 0fbc1918..354603c4 100644 --- a/types/enum.c +++ b/types/enum.c @@ -196,7 +196,6 @@ void _enum_type_free(struct type_class *type_class) } struct type_class_enum *enum_type_new(const char *name, - size_t start_offset, size_t len, int byte_order, int signedness, size_t alignment) @@ -212,7 +211,6 @@ struct type_class_enum *enum_type_new(const char *name, g_direct_equal, NULL, enum_val_free); bitfield_class = &enum_class->p; - bitfield_class->start_offset = start_offset; int_class = &bitfield_class->p; int_class->p.name = g_quark_from_string(name); int_class->p.alignment = alignment; @@ -221,7 +219,6 @@ struct type_class_enum *enum_type_new(const char *name, int_class->len = len; int_class->byte_order = byte_order; int_class->signedness = signedness; - bitfield_class->start_offset = start_offset; if (int_class->p.name) { ret = register_type(&int_class->p); if (ret) { diff --git a/types/float.c b/types/float.c index 2f40d930..5c2e7f8d 100644 --- a/types/float.c +++ b/types/float.c @@ -29,12 +29,12 @@ void float_copy(struct stream_pos *dest, const struct format *fdest, container_of(type_class, struct type_class_float, p); if (fsrc->float_copy == fdest->float_copy) { - fsrc->float_copy(dest, float_class, src, float_class); + fsrc->float_copy(dest, src, float_class); } else { double v; - v = fsrc->double_read(src, fsrc); - fdest->double_write(dest, fdest, v); + v = fsrc->double_read(src, float_class); + fdest->double_write(dest, float_class, v); } } @@ -70,7 +70,11 @@ struct type_class_float *float_type_new(const char *name, type_class->free = _float_type_free; float_class->byte_order = byte_order; - float_class->mantissa = bitfield_type_new(NULL, mantissa_len, + float_class->sign = bitfield_type_new(NULL, 1, + byte_order, false, 1); + if (!float_class->mantissa) + goto error_sign; + float_class->mantissa = bitfield_type_new(NULL, mantissa_len - 1, byte_order, false, 1); if (!float_class->mantissa) goto error_mantissa; @@ -91,6 +95,8 @@ error_register: error_exp: bitfield_type_free(float_class->mantissa); error_mantissa: + bitfield_type_free(float_class->sign); +error_sign: g_free(float_class); return NULL; }