Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1186 | savelij | 1 | /* milfloat.h */ |
2 | /*****************************************************************************/ |
||
3 | /* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only */ |
||
4 | /* */ |
||
5 | /* AS */ |
||
6 | /* */ |
||
7 | /* IEEE -> MIL STD 1750 Floating Point Conversion on host */ |
||
8 | /* */ |
||
9 | /*****************************************************************************/ |
||
10 | |||
11 | #include <stdio.h> |
||
12 | #include <errno.h> |
||
13 | #include "as_float.h" |
||
14 | #include "milfloat.h" |
||
15 | |||
16 | /*!------------------------------------------------------------------------ |
||
17 | * \fn as_float_2_mil1750(as_float_t inp, Word *p_dest, Boolean extended) |
||
18 | * \brief convert host float to MIL STD 1750 |
||
19 | * \param inp number to convert |
||
20 | * \param p_dest destination buffer |
||
21 | * \param extended convert to 32 or 48 bit format? |
||
22 | * \return # of words written or < 0 if error |
||
23 | * ------------------------------------------------------------------------ */ |
||
24 | |||
25 | int as_float_2_mil1750(as_float_t inp, Word *p_dest, Boolean extended) |
||
26 | { |
||
27 | as_float_dissect_t dissect; |
||
28 | Word or_sum; |
||
29 | unsigned req_mantissa_bits; |
||
30 | |||
31 | /* Dissect number: */ |
||
32 | |||
33 | as_float_dissect(&dissect, inp); |
||
34 | |||
35 | /* NaN and Infinity cannot be represented: */ |
||
36 | |||
37 | if ((dissect.fp_class == AS_FP_NAN) |
||
38 | || (dissect.fp_class == AS_FP_INFINITE)) |
||
39 | return -EINVAL; |
||
40 | |||
41 | /* Mantissa is in range [-1,+1) instead of [-2,+2): */ |
||
42 | |||
43 | dissect.exponent++; |
||
44 | |||
45 | /* If exponent is too small to represent, shift down mantissa |
||
46 | until exponent is large enough, or mantissa is all-zeroes: */ |
||
47 | |||
48 | while ((dissect.exponent < -128) && !as_float_mantissa_is_zero(&dissect)) |
||
49 | { |
||
50 | as_float_mantissa_shift_right(dissect.mantissa, 0, dissect.mantissa_bits); |
||
51 | dissect.exponent++; |
||
52 | } |
||
53 | |||
54 | /* exponent overflow? */ |
||
55 | |||
56 | if (dissect.exponent > 127) |
||
57 | return -E2BIG; |
||
58 | |||
59 | /* Form 2s complement of mantissa when sign is set: */ |
||
60 | |||
61 | as_float_mantissa_twos_complement(&dissect); |
||
62 | |||
63 | /* Normalize, so that topmost bits of mantissa are unequal. This happens |
||
64 | for powers of two, after negating: */ |
||
65 | |||
66 | switch (as_float_mantissa_extract(&dissect, 0, 2)) |
||
67 | { |
||
68 | case 0: |
||
69 | case 3: |
||
70 | if (dissect.exponent > -127) /* -128? */ |
||
71 | { |
||
72 | dissect.exponent--; |
||
73 | as_float_mantissa_shift_left(dissect.mantissa, 0, dissect.mantissa_bits); |
||
74 | } |
||
75 | break; |
||
76 | } |
||
77 | |||
78 | /* no rounding: */ |
||
79 | |||
80 | req_mantissa_bits = extended ? 40 : 24; |
||
81 | if (req_mantissa_bits > dissect.mantissa_bits) |
||
82 | as_float_append_mantissa_bits(&dissect, 0, req_mantissa_bits - dissect.mantissa_bits); |
||
83 | |||
84 | /* Write out result: */ |
||
85 | |||
86 | or_sum = (p_dest[0] = as_float_mantissa_extract(&dissect, 0, 16)); |
||
87 | or_sum |= (p_dest[1] = as_float_mantissa_extract(&dissect, 16, 8) << 8); |
||
88 | if (extended) |
||
89 | or_sum |= (p_dest[2] = as_float_mantissa_extract(&dissect, 24, 16)); |
||
90 | |||
91 | /* zero mantissa means zero exponent */ |
||
92 | |||
93 | if (or_sum) |
||
94 | p_dest[1] |= dissect.exponent & 0xff; |
||
95 | |||
96 | return extended ? 3 : 2; |
||
97 | } |