--- xds_engine_xdr.c 2001/08/22 20:23:48 1.7
+++ xds_engine_xdr.c 2001/08/23 08:42:50 1.8
@@ -297,17 +297,96 @@
* Encode/decode floating point values.
*/
+typedef struct
+ {
+ unsigned int sign :1;
+ unsigned int fraction :23;
+ int exponent :8;
+ }
+myfloat_t;
+
+static int float2myfloat(myfloat_t* new_num, float num)
+ {
+ const static unsigned int base = 2;
+ size_t i;
+ float tmp;
+
+ /* Handle zero as a special case. */
+
+ if (num == 0.0)
+ {
+ new_num->sign = 0;
+ new_num->fraction = 0;
+ new_num->exponent = -127;
+ return 0;
+ }
+
+ /* Determine the sign of the number. */
+
+ if (num < 0)
+ {
+ new_num->sign = 1;
+ num = 0 - num;
+ }
+ else
+ new_num->sign = 0;
+
+ /* Canonify the number before we convert it. */
+
+ new_num->exponent = 0;
+ while (num < 1)
+ {
+ num *= base;
+ --new_num->exponent;
+ }
+
+ /* Find the exponent. */
+
+ for (i = 0, tmp = 1; i <= 128; ++i, tmp *= base)
+ {
+ if (tmp*base > num)
+ break;
+ }
+ if (i <= 128)
+ {
+ num = num / tmp - 1;
+ new_num->exponent += i;
+ }
+ else
+ return 1;
+
+ /* Calculate the fraction part. */
+
+ for (new_num->fraction = 0, i = 0; i < 23; ++i)
+ {
+ new_num->fraction *= base;
+ if (num >= 1.0 / base)
+ {
+ new_num->fraction |= 1;
+ num = num * base - 1;
+ }
+ else
+ {
+ new_num->fraction |= 0;
+ num *= base;
+ }
+ }
+
+ return 0;
+ }
+
+
int xdr_encode_float(xds_t *xds, void *engine_context,
void *buffer, size_t buffer_size,
size_t *used_buffer_size, va_list *args)
{
- xds_float_t value;
+ myfloat_t value;
xds_uint8_t tmp;
xds_init_encoding_engine(4);
/* Get value and format it into the structure. */
- float2xds_float(&value, (float)va_arg(*args, double));
+ float2myfloat(&value, (float)va_arg(*args, double));
memset(buffer, 0, 4);
|