this is a sample for mbed(LPC1768)

Committer:
1
Date:
Thu Nov 19 10:17:55 2015 +0800
Revision:
0:3163adfd2cf1
????????????????????????
1.callback
2.thread
3.auto-connect,time-up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
1 0:3163adfd2cf1 1 /* Conversion routines for platforms that do not support 'double' directly. */
1 0:3163adfd2cf1 2
1 0:3163adfd2cf1 3 #include "double_conversion.h"
1 0:3163adfd2cf1 4 #include <math.h>
1 0:3163adfd2cf1 5
1 0:3163adfd2cf1 6 typedef union {
1 0:3163adfd2cf1 7 float f;
1 0:3163adfd2cf1 8 uint32_t i;
1 0:3163adfd2cf1 9 } conversion_t;
1 0:3163adfd2cf1 10
1 0:3163adfd2cf1 11 /* Note: IEE 754 standard specifies float formats as follows:
1 0:3163adfd2cf1 12 * Single precision: sign, 8-bit exp, 23-bit frac.
1 0:3163adfd2cf1 13 * Double precision: sign, 11-bit exp, 52-bit frac.
1 0:3163adfd2cf1 14 */
1 0:3163adfd2cf1 15
1 0:3163adfd2cf1 16 uint64_t float_to_double(float value)
1 0:3163adfd2cf1 17 {
1 0:3163adfd2cf1 18 conversion_t in;
1 0:3163adfd2cf1 19 in.f = value;
1 0:3163adfd2cf1 20 uint8_t sign;
1 0:3163adfd2cf1 21 int16_t exponent;
1 0:3163adfd2cf1 22 uint64_t mantissa;
1 0:3163adfd2cf1 23
1 0:3163adfd2cf1 24 /* Decompose input value */
1 0:3163adfd2cf1 25 sign = (in.i >> 31) & 1;
1 0:3163adfd2cf1 26 exponent = ((in.i >> 23) & 0xFF) - 127;
1 0:3163adfd2cf1 27 mantissa = in.i & 0x7FFFFF;
1 0:3163adfd2cf1 28
1 0:3163adfd2cf1 29 if (exponent == 128)
1 0:3163adfd2cf1 30 {
1 0:3163adfd2cf1 31 /* Special value (NaN etc.) */
1 0:3163adfd2cf1 32 exponent = 1024;
1 0:3163adfd2cf1 33 }
1 0:3163adfd2cf1 34 else if (exponent == -127)
1 0:3163adfd2cf1 35 {
1 0:3163adfd2cf1 36 if (!mantissa)
1 0:3163adfd2cf1 37 {
1 0:3163adfd2cf1 38 /* Zero */
1 0:3163adfd2cf1 39 exponent = -1023;
1 0:3163adfd2cf1 40 }
1 0:3163adfd2cf1 41 else
1 0:3163adfd2cf1 42 {
1 0:3163adfd2cf1 43 /* Denormalized */
1 0:3163adfd2cf1 44 mantissa <<= 1;
1 0:3163adfd2cf1 45 while (!(mantissa & 0x800000))
1 0:3163adfd2cf1 46 {
1 0:3163adfd2cf1 47 mantissa <<= 1;
1 0:3163adfd2cf1 48 exponent--;
1 0:3163adfd2cf1 49 }
1 0:3163adfd2cf1 50 mantissa &= 0x7FFFFF;
1 0:3163adfd2cf1 51 }
1 0:3163adfd2cf1 52 }
1 0:3163adfd2cf1 53
1 0:3163adfd2cf1 54 /* Combine fields */
1 0:3163adfd2cf1 55 mantissa <<= 29;
1 0:3163adfd2cf1 56 mantissa |= (uint64_t)(exponent + 1023) << 52;
1 0:3163adfd2cf1 57 mantissa |= (uint64_t)sign << 63;
1 0:3163adfd2cf1 58
1 0:3163adfd2cf1 59 return mantissa;
1 0:3163adfd2cf1 60 }
1 0:3163adfd2cf1 61
1 0:3163adfd2cf1 62 float double_to_float(uint64_t value)
1 0:3163adfd2cf1 63 {
1 0:3163adfd2cf1 64 uint8_t sign;
1 0:3163adfd2cf1 65 int16_t exponent;
1 0:3163adfd2cf1 66 uint32_t mantissa;
1 0:3163adfd2cf1 67 conversion_t out;
1 0:3163adfd2cf1 68
1 0:3163adfd2cf1 69 /* Decompose input value */
1 0:3163adfd2cf1 70 sign = (value >> 63) & 1;
1 0:3163adfd2cf1 71 exponent = ((value >> 52) & 0x7FF) - 1023;
1 0:3163adfd2cf1 72 mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
1 0:3163adfd2cf1 73
1 0:3163adfd2cf1 74 /* Figure if value is in range representable by floats. */
1 0:3163adfd2cf1 75 if (exponent == 1024)
1 0:3163adfd2cf1 76 {
1 0:3163adfd2cf1 77 /* Special value */
1 0:3163adfd2cf1 78 exponent = 128;
1 0:3163adfd2cf1 79 }
1 0:3163adfd2cf1 80 else if (exponent > 127)
1 0:3163adfd2cf1 81 {
1 0:3163adfd2cf1 82 /* Too large */
1 0:3163adfd2cf1 83 if (sign)
1 0:3163adfd2cf1 84 return -INFINITY;
1 0:3163adfd2cf1 85 else
1 0:3163adfd2cf1 86 return INFINITY;
1 0:3163adfd2cf1 87 }
1 0:3163adfd2cf1 88 else if (exponent < -150)
1 0:3163adfd2cf1 89 {
1 0:3163adfd2cf1 90 /* Too small */
1 0:3163adfd2cf1 91 if (sign)
1 0:3163adfd2cf1 92 return -0.0f;
1 0:3163adfd2cf1 93 else
1 0:3163adfd2cf1 94 return 0.0f;
1 0:3163adfd2cf1 95 }
1 0:3163adfd2cf1 96 else if (exponent < -126)
1 0:3163adfd2cf1 97 {
1 0:3163adfd2cf1 98 /* Denormalized */
1 0:3163adfd2cf1 99 mantissa |= 0x1000000;
1 0:3163adfd2cf1 100 mantissa >>= (-126 - exponent);
1 0:3163adfd2cf1 101 exponent = -127;
1 0:3163adfd2cf1 102 }
1 0:3163adfd2cf1 103
1 0:3163adfd2cf1 104 /* Round off mantissa */
1 0:3163adfd2cf1 105 mantissa = (mantissa + 1) >> 1;
1 0:3163adfd2cf1 106
1 0:3163adfd2cf1 107 /* Check if mantissa went over 2.0 */
1 0:3163adfd2cf1 108 if (mantissa & 0x800000)
1 0:3163adfd2cf1 109 {
1 0:3163adfd2cf1 110 exponent += 1;
1 0:3163adfd2cf1 111 mantissa &= 0x7FFFFF;
1 0:3163adfd2cf1 112 mantissa >>= 1;
1 0:3163adfd2cf1 113 }
1 0:3163adfd2cf1 114
1 0:3163adfd2cf1 115 /* Combine fields */
1 0:3163adfd2cf1 116 out.i = mantissa;
1 0:3163adfd2cf1 117 out.i |= (uint32_t)(exponent + 127) << 23;
1 0:3163adfd2cf1 118 out.i |= (uint32_t)sign << 31;
1 0:3163adfd2cf1 119
1 0:3163adfd2cf1 120 return out.f;
1 0:3163adfd2cf1 121 }
1 0:3163adfd2cf1 122
1 0:3163adfd2cf1 123