Add "stamp-h1", for automake 1.6.1, as per Joerg Mayer's suggestion.
[obnox/wireshark/wip.git] / ieee-float.h
1 /**********************************************************************
2  *
3  * ieee-float.h
4  *
5  * Implements simple stuff to convert from IEEE float types
6  * to 32-bit longs
7  *
8  * (C) Ashok Narayanan, 2000
9  *
10  * $Id: ieee-float.h,v 1.2 2001/02/04 08:21:35 guy Exp $
11  *
12  * For license details, see the COPYING file with this distribution
13  *
14  **********************************************************************/
15
16 #ifndef IEEE_FLOAT_H
17 #define IEEE_FLOAT_H
18
19 /* Stuff for IEEE float handling */
20
21 #define IEEE_NUMBER_WIDTH       32      /* bits in number */
22 #define IEEE_EXP_WIDTH          8       /* bits in exponent */
23 #define IEEE_MANTISSA_WIDTH     23      /* IEEE_NUMBER_WIDTH - 1 - IEEE_EXP_WIDTH */
24
25 #define IEEE_SIGN_MASK          0x80000000
26 #define IEEE_EXPONENT_MASK      0x7F800000
27 #define IEEE_MANTISSA_MASK      0x007FFFFF
28 #define IEEE_INFINITY           IEEE_EXPONENT_MASK
29
30 #define IEEE_IMPLIED_BIT (1 << IEEE_MANTISSA_WIDTH)
31 #define IEEE_INFINITE ((1 << IEEE_EXP_WIDTH) - 1)
32 #define IEEE_BIAS ((1 << (IEEE_EXP_WIDTH - 1)) - 1)
33
34 #define MINUS_INFINITY (signed)0x80000000L
35 #define PLUS_INFINITY  0x7FFFFFFF
36
37 static inline int ieee_float_is_zero (long number)
38 {
39     return(!(number & ~IEEE_SIGN_MASK));
40 }
41
42 /*
43  * simple conversion: ieee floating point to long
44  */
45 static long tvb_ieee_to_long (tvbuff_t *tvb, int offset)
46 {
47     long number;
48     long sign;
49     long exponent;
50     long mantissa;
51
52     number = tvb_get_ntohl(tvb, offset);
53     sign = number & IEEE_SIGN_MASK;
54     exponent = number & IEEE_EXPONENT_MASK;
55     mantissa = number & IEEE_MANTISSA_MASK;
56
57     if (ieee_float_is_zero(number)) {
58         /* number is zero, unnormalized, or not-a-number */
59         return 0;
60     }
61     if (IEEE_INFINITY == exponent) {
62         /* number is positive or negative infinity, or a special value */
63         return (sign? MINUS_INFINITY: PLUS_INFINITY);
64     }
65
66     exponent = (exponent >> IEEE_MANTISSA_WIDTH) - IEEE_BIAS;
67     if (exponent < 0) {
68         /* number is between zero and one */
69         return 0;
70     }
71
72     mantissa |= IEEE_IMPLIED_BIT;
73     if (exponent <= IEEE_MANTISSA_WIDTH)
74         mantissa >>= IEEE_MANTISSA_WIDTH - exponent;
75     else
76         mantissa <<= exponent - IEEE_MANTISSA_WIDTH;
77
78     if (sign)
79         return -mantissa;
80     else
81         return mantissa;
82 }
83
84 #endif