1 /* hp2100_fp1.c: HP 1000 multiple-precision floating point routines
3 Copyright (c) 2005-2008, J. David Bryan
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of the author shall not be used
23 in advertising or otherwise to promote the sale, use or other dealings in
24 this Software without prior written authorization from the author.
26 10-May-08 JDB Fixed uninitialized return in fp_accum when setting
27 19-Mar-08 JDB Reworked "complement" to avoid inlining bug in gcc-4.x
28 01-Dec-06 JDB Reworked into generalized multiple-precision ops for FPP
29 12-Oct-06 JDB Altered x_trun for F-Series FFP compatibility
30 Added F-Series ..TCM FFP helpers
33 - HP 1000 M/E/F-Series Computers Engineering and Reference Documentation
34 (92851-90001, Mar-1981)
35 - HP 1000 M/E/F-Series Computers Technical Reference Handbook
37 - DOS/RTE Relocatable Library Reference Manual
38 (24998-90001, Oct-1981)
41 This module implements multiple-precision floating-point operations to
42 support the 1000 F-Series hardware Floating Point Processor. It employs
43 64-bit integer arithmetic for speed and simplicity of implementation. The
44 host compiler must support 64-bit integers, and the HAVE_INT64 symbol must be
45 defined during compilation. If this symbol is not defined, then FPP support
48 HP 2100/1000 computers used a proprietary floating-point format. The 2100
49 had optional firmware that provided two-word floating-point add, subtract,
50 multiply, and divide, as well as single-integer fix and float. The 1000-M/E
51 provided the same two-word firmware operations as standard equipment.
52 Three-word extended-precision instructions for the 2100 and 1000-M/E were
53 provided by the optional Fast FORTRAN Processor firmware.
55 The 1000-F substituted a hardware floating point processor for the firmware
56 in previous machines. In addition to the two- and three-word formats, the
57 F-Series introduced a four-word double-precision format. A five-word format
58 that provided extra range in the exponent by unpacking it from the mantissa
59 was also provided, although this capability was not documented in the user
60 manual. In addition, the FPP improved the accuracy of floating-point
61 calculations, as the firmware versions sacrificed a few bits of precision to
62 gain speed. Consequently, operations on the F-Series may return results that
63 differ slightly from the same operations on the M/E-Series or the 2100.
65 F-Series units after date code 1920 also provided two-word double-integer
66 instructions in firmware, as well as double-integer fix and float operations.
68 The original 32-bit floating-point format is as follows:
71 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
72 |MS| mantissa high | : M
73 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
74 | mantissa low | exponent |XS| : M + 1
75 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
78 Both 23-bit mantissa and 7-bit exponent are in twos-complement form. The
79 exponent sign bit has been rotated into the LSB of the second word.
81 The extended-precision floating-point format is a 48-bit extension of the
82 32-bit format used for single precision. A packed extended-precision value
83 consists of a 39-bit mantissa and a 7-bit exponent. The format is as
87 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
88 |MS| mantissa high | : M
89 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
90 | mantissa middle | : M + 1
91 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
92 | mantissa low | exponent |XS| : M + 2
93 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
96 The double-precision floating-point format is similar to the 48-bit
97 extended-precision format, although with a 55-bit mantissa:
100 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
101 |MS| mantissa high | : M
102 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
103 | mantissa middle high | : M + 1
104 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
105 | mantissa middle low | : M + 2
106 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
107 | mantissa low | exponent |XS| : M + 3
108 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
111 The FPP also supports a special five-word expanded-exponent format:
114 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
115 |MS| mantissa high | : M
116 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
117 | mantissa middle high | : M + 1
118 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
119 | mantissa middle low | : M + 2
120 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
121 | mantissa low | : M + 3
122 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
123 | exponent |XS| : M + 4
124 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
127 The exponent is a full 16-bit twos-complement value, but the allowed range is
128 only 10 bits, i.e., -512 to +511.
130 In a normalized value, the sign and MSB of the mantissa differ. Zero is
131 represented by all words = 0.
133 Internally, unpacked floating-point values are contained in a structure
134 having a signed 64-bit mantissa and a signed 32-bit exponent. Mantissas are
135 left-justified with the unused bits masked to zero. Exponents are
136 right-justified. The precision is indicated by the value of a structure
139 HP terminology for the three-word floating-point format is confused. Some
140 documents refer to it as "double precision," while others use "extended
141 precision." The instruction mnemonics begin with "X" (e.g., .XADD),
142 suggesting the extended-precision term.
144 HP apparently intended that the four-word double-precision format would be
145 called "triple-precision," as the instruction mnemonics begin with "T" (e.g.,
146 ".TADD" for the four-word add instruction). The source files for the
147 software simulations of these instructions for the M/E-Series also explicitly
148 refer to "triple precision math." However, the engineering documentation and
149 the F-Series reference manual both use the double-precision term.
151 This module adopts the single/extended/double terminology and uses the
152 initial letters of the instructions (F/X/T) to indicate the precision used.
154 The FPP hardware consisted of two circuit boards that interfaced to the main
155 CPU via the Microprogammable Processor Port (MPP) that had been introduced
156 with the 1000 E-Series. One board contained argument registers and ALUs,
157 split into separate mantissa and exponent parts. The other contained a state
158 machine sequencer. FPP results were copied automatically to the argument
159 registers in addition to being available over the MPP, so that chained
160 operations could be executed from these "accumulators" without reloading.
162 The FPP operated independently of the CPU. An opcode, specifying one of the
163 six operations (add, subtract, multiply, divide, fix, or float) was sent to
164 the FPP, and a start command was given. Operands of appropriate precision
165 were then supplied to the FPP. Once the operands were received, the FPP
166 would execute and set a flag when the operation was complete. The result
167 would then be retrieved from the FPP. The floating-point instruction
168 firmware in the CPU initiated the desired FPP operation and handled operand
169 reads from and result writes to main memory.
171 Under simulation, "fp_exec" provides the six arithmetic operations analogous
172 to FPP execution. The remainder of the functions are helpers that were
173 provided by firmware in the 1000-F but that can reuse code needed to simulate
174 the FPP hardware. As with the hardware, "fp_exec" retains the last result
175 in an internal accumulator that may be referenced in subsequent operations.
177 NOTE: this module also provides the floating-point support for the firmware
178 single-precision 1000-M/E base set and extended-precision FFP instructions.
179 Because the firmware and hardware implementations returned slightly different
180 results, particularly with respect to round-off, conditional checks are
181 implemented in the arithmetic routines. In some cases, entirely different
182 algorithms are used to ensure fidelity with the real machines. Functionally,
183 this means that the 2100/1000-M/E and 1000-F floating-point diagnostics are
184 not interchangeable, and failures are to be expected if a diagnostic is run
185 on the wrong machine.
188 #include "hp2100_defs.h"
189 #include "hp2100_cpu.h"
190 #include "hp2100_cpu1.h"
191 #include "hp2100_fp1.h"
194 #if defined (HAVE_INT64) /* we need int64 support */
199 #define IN_W_SMAGN 15
200 #define IN_W_DMAGN 31
203 #define FP_W_FMANT 23
204 #define FP_W_XMANT 39
205 #define FP_W_TMANT 55
206 #define FP_W_EMANT 55
207 #define FP_W_EXPANDEXP 9
211 /* Starting bit numbers. */
213 #define IN_V_SIGN (64 - IN_W_SIGN)
214 #define IN_V_SNUM (64 - IN_W_SIGN - IN_W_SMAGN)
215 #define IN_V_DNUM (64 - IN_W_SIGN - IN_W_DMAGN)
217 #define FP_V_FNUM (64 - FP_W_MSIGN - FP_W_FMANT - FP_W_EXP - FP_W_ESIGN)
218 #define FP_V_XNUM (64 - FP_W_MSIGN - FP_W_XMANT - FP_W_EXP - FP_W_ESIGN)
219 #define FP_V_TNUM (64 - FP_W_MSIGN - FP_W_TMANT - FP_W_EXP - FP_W_ESIGN)
220 #define FP_V_ENUM (64 - FP_W_MSIGN - FP_W_EMANT - FP_W_EXP - FP_W_ESIGN)
222 #define FP_V_MSIGN (64 - FP_W_MSIGN)
223 #define FP_V_FMANT (64 - FP_W_MSIGN - FP_W_FMANT)
224 #define FP_V_XMANT (64 - FP_W_MSIGN - FP_W_XMANT)
225 #define FP_V_TMANT (64 - FP_W_MSIGN - FP_W_TMANT)
226 #define FP_V_EMANT (64 - FP_W_MSIGN - FP_W_EMANT)
230 /* Right-aligned field masks. */
232 #define IN_M_SIGN (((t_uint64) 1 << IN_W_SIGN) - 1)
233 #define IN_M_SMAGN (((t_uint64) 1 << IN_W_SMAGN) - 1)
234 #define IN_M_DMAGN (((t_uint64) 1 << IN_W_DMAGN) - 1)
236 #define FP_M_MSIGN (((t_uint64) 1 << FP_W_MSIGN) - 1)
237 #define FP_M_FMANT (((t_uint64) 1 << FP_W_FMANT) - 1)
238 #define FP_M_XMANT (((t_uint64) 1 << FP_W_XMANT) - 1)
239 #define FP_M_TMANT (((t_uint64) 1 << FP_W_TMANT) - 1)
240 #define FP_M_EMANT (((t_uint64) 1 << FP_W_EMANT) - 1)
242 #define FP_M_EXPANDEXP ((1 << FP_W_EXPANDEXP) - 1)
243 #define FP_M_EXP ((1 << FP_W_EXP) - 1)
244 #define FP_M_ESIGN ((1 << FP_W_ESIGN) - 1)
246 /* In-place field masks. */
248 #define IN_SIGN (IN_M_SIGN << IN_V_SIGN)
249 #define IN_SMAGN (IN_M_SMAGN << IN_V_SNUM)
250 #define IN_DMAGN (IN_M_DMAGN << IN_V_DNUM)
252 #define FP_MSIGN (FP_M_MSIGN << FP_V_MSIGN)
253 #define FP_FMANT (FP_M_FMANT << FP_V_FMANT)
254 #define FP_XMANT (FP_M_XMANT << FP_V_XMANT)
255 #define FP_TMANT (FP_M_TMANT << FP_V_TMANT)
256 #define FP_EMANT (FP_M_EMANT << FP_V_EMANT)
257 #define FP_EXP (FP_M_EXP << FP_V_EXP)
258 #define FP_ESIGN (FP_M_ESIGN << FP_V_ESIGN)
260 /* In-place record masks. */
262 #define IN_SSMAGN (IN_SIGN | IN_SMAGN)
263 #define IN_SDMAGN (IN_SIGN | IN_DMAGN)
265 #define FP_SFMANT (FP_MSIGN | FP_FMANT)
266 #define FP_SXMANT (FP_MSIGN | FP_XMANT)
267 #define FP_STMANT (FP_MSIGN | FP_TMANT)
268 #define FP_SEMANT (FP_MSIGN | FP_EMANT)
269 #define FP_SEXP (FP_ESIGN | FP_EXP)
271 /* Minima and maxima. */
273 #define FP_ONEHALF ((t_int64) 1 << (FP_V_MSIGN - 1)) /* mantissa = 0.5 */
274 #define FP_MAXPMANT ((t_int64) FP_EMANT) /* maximum pos mantissa */
275 #define FP_MAXNMANT ((t_int64) FP_MSIGN) /* maximum neg mantissa */
276 #define FP_MAXPEXP (FP_M_EXPANDEXP) /* maximum pos expanded exponent */
277 #define FP_MAXNEXP (-(FP_MAXPEXP + 1)) /* maximum neg expanded exponent */
279 /* Floating-point helpers. */
281 #define DENORM(x) ((((x) ^ (x) << 1) & FP_MSIGN) == 0)
283 #define TO_EXP(e) (int8) ((e >> FP_V_EXP & FP_M_EXP) | \
284 (e & FP_M_ESIGN ? ~FP_M_EXP : 0))
286 /* Property constants. */
288 static const t_int64 p_half_lsb
[6] = { ((t_int64
) 1 << IN_V_SNUM
) - 1, /* different than FP! */
289 ((t_int64
) 1 << IN_V_DNUM
) - 1, /* different than FP! */
290 (t_int64
) 1 << (FP_V_FMANT
- 1),
291 (t_int64
) 1 << (FP_V_XMANT
- 1),
292 (t_int64
) 1 << (FP_V_TMANT
- 1),
293 (t_int64
) 1 << (FP_V_EMANT
- 1) };
295 static const t_int64 n_half_lsb
[6] = { 0,
297 ((t_int64
) 1 << (FP_V_FMANT
- 1)) - 1,
298 ((t_int64
) 1 << (FP_V_XMANT
- 1)) - 1,
299 ((t_int64
) 1 << (FP_V_TMANT
- 1)) - 1,
300 ((t_int64
) 1 << (FP_V_EMANT
- 1)) - 1 };
302 static const uint32 op_start
[6] = { IN_V_SNUM
,
309 static const t_int64 mant_mask
[6] = { IN_SSMAGN
,
316 static const uint32 op_bits
[6] = { IN_W_SMAGN
,
318 FP_W_FMANT
+ FP_W_MSIGN
,
319 FP_W_XMANT
+ FP_W_MSIGN
,
320 FP_W_TMANT
+ FP_W_MSIGN
,
321 FP_W_EMANT
+ FP_W_MSIGN
};
323 static const t_int64 op_mask
[6] = { ~(t_int64
) 0 << IN_V_SNUM
,
324 ~(t_int64
) 0 << IN_V_DNUM
,
325 ~(t_int64
) 0 << FP_V_FNUM
,
326 ~(t_int64
) 0 << FP_V_XNUM
,
327 ~(t_int64
) 0 << FP_V_TNUM
,
328 ~(t_int64
) 0 << FP_V_ENUM
};
330 static const uint32 int_p_max
[2] = { IN_M_SMAGN
,
334 /* Internal unpacked floating-point representation. */
344 /* Low-level helper routines. */
347 /* Arithmetic shift right for mantissa only.
349 Returns TRUE if any one-bits are shifted out (for F-series only).
352 static t_bool
asr (FPU
*operand
, int32 shift
)
357 if (UNIT_CPU_MODEL
== UNIT_1000_F
) { /* F-series? */
358 mask
= ((t_uint64
) 1 << shift
) - 1; /* mask for lost bits */
359 bits_lost
= ((operand
->mantissa
& mask
) != 0); /* flag if any lost */
364 operand
->mantissa
= operand
->mantissa
>> shift
; /* mantissa is int, so ASR */
369 /* Logical shift right for mantissa and exponent.
371 Shifts mantissa and corrects exponent for mantissa overflow.
372 Returns TRUE if any one-bits are shifted out (for F-series only).
375 static t_bool
lsrx (FPU
*operand
, int32 shift
)
380 if (UNIT_CPU_MODEL
== UNIT_1000_F
) { /* F-series? */
381 mask
= ((t_uint64
) 1 << shift
) - 1; /* mask for lost bits */
382 bits_lost
= ((operand
->mantissa
& mask
) != 0); /* flag if any lost */
387 operand
->mantissa
= (t_uint64
) operand
->mantissa
>> shift
; /* uint, so LSR */
388 operand
->exponent
= operand
->exponent
+ shift
; /* correct exponent */
393 /* Unpack an operand into a long integer.
395 Returns a left-aligned integer or mantissa. Does not mask to precision; this
396 should be done subsequently if desired.
399 static t_int64
unpack_int (OP packed
, OPSIZE precision
)
402 t_uint64 unpacked
= 0;
404 if (precision
== in_s
)
405 unpacked
= (t_uint64
) packed
.word
<< 48; /* unpack single integer */
407 else if (precision
== in_d
)
408 unpacked
= (t_uint64
) packed
.dword
<< 32; /* unpack double integer */
411 if (precision
== fp_e
) /* five word operand? */
412 precision
= fp_t
; /* only four mantissa words */
414 for (i
= 0; i
< 4; i
++) /* unpack fp 2 to 4 words */
415 if (i
< TO_COUNT (precision
))
416 unpacked
= unpacked
<< 16 | packed
.fpk
[i
];
418 unpacked
= unpacked
<< 16;
421 return (t_int64
) unpacked
;
425 /* Unpack a packed operand.
427 The packed value is split into separate mantissa and exponent variables. The
428 multiple words of the mantissa are concatenated into a single 64-bit signed
429 value, and the exponent is shifted with recovery of the sign.
432 static FPU
unpack (OP packed
, OPSIZE precision
)
436 unpacked
.precision
= precision
; /* set value's precision */
438 unpacked
.mantissa
= /* unpack and mask mantissa */
439 unpack_int (packed
, precision
) & mant_mask
[precision
];
446 unpacked
.exponent
= /* unpack exponent from correct word */
447 TO_EXP (packed
.fpk
[(uint32
) precision
- 1]);
451 unpacked
.exponent
= /* unpack expanded exponent */
452 (int16
) (packed
.fpk
[4] >> FP_V_EXP
| /* rotate sign into place */
453 (packed
.fpk
[4] & 1 ? SIGN
: 0));
456 case fp_a
: /* no action for value in accum */
457 case in_s
: /* integers don't use exponent */
458 case in_d
: /* integers don't use exponent */
460 unpacked
.exponent
= 0;
468 /* Pack a long integer into an operand. */
470 static OP
pack_int (t_int64 unpacked
, OPSIZE precision
)
475 if (precision
== in_s
)
476 packed
.word
= (uint16
) (unpacked
>> 48) & DMASK
; /* pack single integer */
478 else if (precision
== in_d
)
479 packed
.dword
= (uint32
) (unpacked
>> 32) & DMASK32
; /* pack double integer */
482 if (precision
== fp_e
) /* five word operand? */
483 precision
= fp_t
; /* only four mantissa words */
485 for (i
= 3; i
>= 0; i
--) { /* pack fp 2 to 4 words */
486 packed
.fpk
[i
] = (uint16
) unpacked
& DMASK
;
487 unpacked
= unpacked
>> 16;
495 /* Pack an unpacked floating-point number.
497 The 64-bit mantissa is split into the appropriate number of 16-bit words.
498 The exponent is rotated to incorporate the sign bit and merged into the
502 static OP
pack (FPU unpacked
)
507 packed
= pack_int (unpacked
.mantissa
, unpacked
.precision
); /* pack mantissa */
509 exp
= ((uint8
) unpacked
.exponent
<< FP_V_EXP
) | /* rotate exponent */
510 ((unpacked
.exponent
< 0) << FP_V_ESIGN
);
512 switch (unpacked
.precision
) { /* merge exponent into correct word */
514 case in_s
: /* no action for integers */
518 case fp_f
: /* merge into last word */
521 packed
.fpk
[(uint32
) unpacked
.precision
- 1] =
522 (packed
.fpk
[(uint32
) unpacked
.precision
- 1] & ~FP_SEXP
) | exp
;
525 case fp_e
: /* place in separate word */
526 packed
.fpk
[4] = ((uint16
) unpacked
.exponent
<< FP_V_EXP
) |
527 ((unpacked
.exponent
< 0) << FP_V_ESIGN
);
530 case fp_a
: /* no action for value in accum */
538 /* Normalize an unpacked floating-point number.
540 Floating-point numbers are in normal form if the sign bit and the MSB of the
541 mantissa differ. Unnormalized numbers are shifted as needed with appropriate
542 exponent modification.
545 static void normalize (FPU
*unpacked
)
548 if (unpacked
->mantissa
) /* non-zero? */
549 while (DENORM (unpacked
->mantissa
)) { /* normal form? */
550 unpacked
->exponent
= unpacked
->exponent
- 1; /* no, so left shift */
551 unpacked
->mantissa
= unpacked
->mantissa
<< 1;
554 unpacked
->exponent
= 0; /* clean for zero */
559 /* Round an unpacked floating-point number and check for overflow.
561 An unpacked floating-point number is rounded by adding one-half of the LSB
562 value, maintaining symmetry around zero. If rounding resulted in a mantissa
563 overflow, the result logically is shifted to the right with an appropriate
564 exponent modification. Finally, the result is checked for exponent underflow
565 or overflow, and the appropriate approximation (zero or infinity) is
568 Rounding in hardware involves a special mantissa extension register that
569 holds three "guard" bits and one "sticky" bit. These represent the value of
570 bits right-shifted out the mantissa register. Under simulation, we track
571 such right-shifts and utilize the lower eight bits of the 64-bit mantissa
572 value to simulate the extension register.
574 Overflow depends on whether the FPP expanded-exponent form is being used
575 (this expands the exponent range by two bits). If overflow is detected, the
576 value representing infinity is dependent on whether the operation is on
577 behalf of the Fast FORTRAN Processor. The F-Series FPP returns positive
578 infinity on both positive and negative overflow for all precisions. The 2100
579 and M/E-Series FFPs return negative infinity on negative overflow of
580 extended-precision values. Single-precision overflows on these machines
581 always return positive infinity.
583 The number to be rounded must be normalized upon entry.
586 static uint32
roundovf (FPU
*unpacked
, t_bool expand
)
591 sign
= (unpacked
->mantissa
< 0); /* save mantissa sign */
593 if (sign
) /* round and mask the number */
595 (unpacked
->mantissa
+ n_half_lsb
[unpacked
->precision
]) &
596 mant_mask
[unpacked
->precision
];
599 (unpacked
->mantissa
+ p_half_lsb
[unpacked
->precision
]) &
600 mant_mask
[unpacked
->precision
];
602 if (sign
!= (unpacked
->mantissa
< 0)) /* mantissa overflow? */
603 lsrx (unpacked
, 1); /* correct by shifting */
605 normalize (unpacked
); /* renorm may be needed */
607 if (unpacked
->mantissa
== 0) { /* result zero? */
608 unpacked
->mantissa
= 0; /* return zero */
609 unpacked
->exponent
= 0;
610 overflow
= 0; /* with overflow clear */
612 else if (unpacked
->exponent
< /* result underflow? */
613 (FP_MAXNEXP
>> (expand
? 0 : 2))) {
614 unpacked
->mantissa
= 0; /* return zero */
615 unpacked
->exponent
= 0;
616 overflow
= 1; /* and set overflow */
618 else if (unpacked
->exponent
> /* result overflow? */
619 (FP_MAXPEXP
>> (expand
? 0 : 2))) {
620 if (sign
&& /* negative value? */
621 (unpacked
->precision
== fp_x
) && /* extended precision? */
622 (UNIT_CPU_MODEL
!= UNIT_1000_F
)) { /* not F-series? */
623 unpacked
->mantissa
= FP_MAXNMANT
; /* return negative infinity */
624 unpacked
->exponent
= FP_MAXPEXP
& FP_M_EXP
;
627 unpacked
->mantissa
= FP_MAXPMANT
; /* return positive infinity */
628 unpacked
->exponent
= FP_MAXPEXP
& FP_M_EXP
;
630 overflow
= 1; /* and set overflow */
633 overflow
= 0; /* value is in range */
639 /* Normalize, round, and pack an unpacked floating-point number. */
641 static uint32
nrpack (OP
*packed
, FPU unpacked
, t_bool expand
)
645 normalize (&unpacked
); /* normalize for rounding */
646 overflow
= roundovf (&unpacked
, expand
); /* round and check for overflow */
647 *packed
= pack (unpacked
); /* pack result */
654 /* Low-level arithmetic routines. */
657 /* Complement an unpacked number. */
659 static void complement (FPU
*result
)
661 if (result
->mantissa
== FP_MAXNMANT
) { /* maximum negative? */
662 result
->mantissa
= FP_ONEHALF
; /* complement of -1.0 * 2 ^ n */
663 result
->exponent
= result
->exponent
+ 1; /* is 0.5 * 2 ^ (n + 1) */
666 result
->mantissa
= -result
->mantissa
; /* negate mantissa */
671 /* Add two unpacked numbers.
673 The mantissas are first aligned if necessary by scaling the smaller of the
674 two operands. If the magnitude of the difference between the exponents is
675 greater than the number of significant bits, then the smaller number has been
676 scaled to zero (swamped), and so the sum is simply the larger operand.
677 Otherwise, the sum is computed and checked for overflow, which has occured if
678 the signs of the operands are the same but differ from that of the result.
679 Scaling and renormalization is perfomed if overflow occurred.
682 static void add (FPU
*sum
, FPU augend
, FPU addend
)
687 if (augend
.mantissa
== 0)
688 *sum
= addend
; /* X + 0 = X */
690 else if (addend
.mantissa
== 0)
691 *sum
= augend
; /* 0 + X = X */
694 magn
= augend
.exponent
- addend
.exponent
; /* difference exponents */
696 if (magn
> 0) { /* addend smaller? */
697 *sum
= augend
; /* preset augend */
698 bits_lost
= asr (&addend
, magn
); /* align addend */
700 else { /* augend smaller? */
701 *sum
= addend
; /* preset addend */
702 magn
= -magn
; /* make difference positive */
703 bits_lost
= asr (&augend
, magn
); /* align augend */
706 if (magn
<= (int32
) op_bits
[augend
.precision
]) { /* value swamped? */
707 sum
->mantissa
= /* no, add mantissas */
708 addend
.mantissa
+ augend
.mantissa
;
710 if (((addend
.mantissa
< 0) == (augend
.mantissa
< 0)) && /* mantissa overflow? */
711 ((addend
.mantissa
< 0) != (sum
->mantissa
< 0))) {
712 bits_lost
= bits_lost
| lsrx (sum
, 1); /* restore value */
713 sum
->mantissa
= /* restore sign */
714 sum
-> mantissa
| (addend
.mantissa
& FP_MSIGN
);
717 if (bits_lost
) /* any bits lost? */
718 sum
->mantissa
= sum
->mantissa
| 1; /* include one for rounding */
725 /* Multiply two unpacked numbers.
727 The single-precision firmware (FMP) operates differently from the firmware
728 extended-precision (.XMPY) and the hardware multiplies of any precision.
729 Firmware implementations form 16-bit x 16-bit = 32-bit partial products and
730 sum them to form the result. The hardware uses a series of shifts and adds.
731 This means that firmware FMP and hardware FMP return slightly different
732 values, as may be seen by attempting to run the firmware FMP diagnostic on
735 The FMP microcode calls a signed multiply routine to calculate three partial
736 products (all but LSB * LSB). Because the LSBs are unsigned, i.e., all bits
737 significant, the two MSB * LSB products are calculated using LSB/2. The
738 unsigned right-shift ensures a positive LSB with no significant bits lost,
739 because the lower eight bits are unused (they held the vacated exponent). In
740 order to sum the partial products, the LSB of the result of MSB * MSB is also
741 right-shifted before addition. Note, though, that this loses a significant
742 bit. After summation, the result is left-shifted to correct for the original
745 The .XMPY microcode negates both operands as necessary to produce positive
746 values and then forms six of the nine 16-bit x 16-bit = 32-bit unsigned
747 multiplications required for a full 96-bit product. Given a 48-bit
748 multiplicand "a1a2a3" and a 48-bit multiplier "b1b2b3", the firmware performs
749 these calculations to develop a 48-bit product:
752 +-------+-------+-------+
754 +-------+-------+-------+
755 _________________________
769 _________________________________
772 +-------+-------+-------+
774 The least-significant words of partial products [p1], [p2], and [p4] are used
775 only to develop a carry bit into the 48-bit sum. The product is complemented
776 as necessary to restore the sign.
778 The basic FPP hardware algorithm scans the multiplier and adds a shifted copy
779 of the multiplicand whenever a one-bit is detected. To avoid successive adds
780 when a string of ones is encountered (because adds are more expensive than
781 shifts), the hardware instead adds the multiplicand shifted by N+1+P and
782 subtracts the multiplicand shifted by P to obtain the equivalent value with a
783 maximum of two operations.
785 Instead of implementing either the .XMPY firmware algorithm or the hardware
786 shift-and-add algorithm directly, it is more efficient under simulation to
787 use 32 x 32 = 64-bit multiplications, thereby reducing the number required
788 from six to four (64-bit "c1c2" x 64-bit "d1d2"):
804 _________________________________
809 However, the FMP algorithm is implemented directly from the microcode to
810 preserve the fidelity of the simulation, i.e., to lose the same amount
814 static void multiply (FPU
*product
, FPU multiplicand
, FPU multiplier
)
816 uint32 ah
, al
, bh
, bl
, sign
= 0;
817 t_uint64 hh
, hl
, lh
, ll
, carry
;
818 int16 ch
, cl
, dh
, dl
;
821 product
->precision
= multiplicand
.precision
; /* set precision */
823 if ((multiplicand
.mantissa
== 0) || /* 0 * X = 0 */
824 (multiplier
.mantissa
== 0)) /* X * 0 = 0 */
825 product
->mantissa
= product
->exponent
= 0;
828 firmware
= (UNIT_CPU_MODEL
!= UNIT_1000_F
); /* set firmware flag */
830 if (!firmware
|| (product
->precision
!= fp_f
)) { /* hardware? */
831 if (multiplicand
.mantissa
< 0) { /* negative? */
832 complement (&multiplicand
); /* complement operand */
833 sign
= ~sign
; /* track sign */
835 if (multiplier
.mantissa
< 0) { /* negative? */
836 complement (&multiplier
); /* complement operand */
837 sign
= ~sign
; /* track sign */
841 product
->exponent
= /* compute exponent */
842 multiplicand
.exponent
+ multiplier
.exponent
+ 1;
844 ah
= (uint32
) (multiplicand
.mantissa
>> 32); /* split multiplicand */
845 al
= (uint32
) (multiplicand
.mantissa
& DMASK32
); /* into high and low parts */
846 bh
= (uint32
) (multiplier
.mantissa
>> 32); /* split multiplier */
847 bl
= (uint32
) (multiplier
.mantissa
& DMASK32
); /* into high and low parts */
849 if (firmware
&& (product
->precision
== fp_f
)) { /* single-precision firmware? */
850 ch
= (int16
) (ah
>> 16) & DMASK
; /* split 32-bit multiplicand */
851 cl
= (int16
) (ah
& 0xfffe); /* into high and low parts */
852 dh
= (int16
) (bh
>> 16) & DMASK
; /* split 32-bit multiplier */
853 dl
= (int16
) (bh
& 0xfffe); /* into high and low parts */
855 hh
= (t_uint64
) (((int32
) ch
* dh
) & ~1); /* form cross products */
856 hl
= (t_uint64
) (((t_int64
) ch
* (t_int64
) (uint16
) dl
+
857 (t_int64
) dh
* (t_int64
) (uint16
) cl
) &
860 product
->mantissa
= (t_uint64
) (((t_int64
) hh
<< 32) + /* sum partials */
861 ((t_int64
) hl
<< 16));
865 hh
= ((t_uint64
) ah
* bh
); /* form four cross products */
866 hl
= ((t_uint64
) ah
* bl
); /* using 32 x 32 = */
867 lh
= ((t_uint64
) al
* bh
); /* 64-bit multiplies */
868 ll
= ((t_uint64
) al
* bl
);
870 carry
= ((ll
>> 32) + (uint32
) hl
+ (uint32
) lh
) >> 32; /* form carry */
872 product
->mantissa
= hh
+ (hl
>> 32) + (lh
>> 32) + carry
; /* sum partials */
874 if (sign
) /* negate if required */
875 complement (product
);
882 /* Divide two unpacked numbers.
884 As with multiply, the single-precision firmware (FDV) operates differently
885 from the firmware extended-precision (.XDIV) and the hardware divisions of
886 any precision. Firmware implementations utilize a "divide and correct"
887 algorithm, wherein the quotient is estimated and then corrected by comparing
888 the dividend to the product of the quotient and the divisor. The hardware
889 uses a series of shifts and subtracts. This means that firmware FDV and
890 hardware FDV once again return slightly different values.
892 Under simulation, the classic divide-and-correct method is employed, using
893 64-bit / 32-bit = 32-bit divisions. This involves dividing the 64-bit
894 dividend "a1a2a3a4" by the first 32-bit digit "b1b2" of the 64-bit divisor
895 "b1b2b3b4". The resulting 32-bit quotient is ...
897 The microcoded single-precision division avoids overflows by right-shifting
898 some values, which leads to a loss of precision in the LSBs. We duplicate
899 the firmware algorithm here to preserve the fidelity of the simulation.
902 static void divide (FPU
*quotient
, FPU dividend
, FPU divisor
)
905 t_int64 bh
, bl
, r1
, r0
, p1
, p0
;
909 int16 dh
, dl
, pq1
, pq2
, cq
;
911 quotient
->precision
= dividend
.precision
; /* set precision */
913 if (divisor
.mantissa
== 0) { /* division by zero? */
914 if (dividend
.mantissa
< 0)
915 quotient
->mantissa
= FP_MSIGN
; /* return minus infinity */
917 quotient
->mantissa
= ~FP_MSIGN
; /* or plus infinity */
918 quotient
->exponent
= FP_MAXPEXP
+ 1;
921 else if (dividend
.mantissa
== 0) /* dividend zero? */
922 quotient
->mantissa
= quotient
->exponent
= 0; /* yes; result is zero */
925 firmware
= (UNIT_CPU_MODEL
!= UNIT_1000_F
); /* set firmware flag */
927 if (!firmware
|| (quotient
->precision
!= fp_f
)) { /* hardware or FFP? */
928 if (dividend
.mantissa
< 0) { /* negative? */
929 complement (÷nd
); /* complement operand */
930 sign
= ~sign
; /* track sign */
932 if (divisor
.mantissa
< 0) { /* negative? */
933 complement (&divisor
); /* complement operand */
934 sign
= ~sign
; /* track sign */
938 quotient
->exponent
= /* division subtracts exponents */
939 dividend
.exponent
- divisor
.exponent
;
941 bh
= divisor
.mantissa
>> 32; /* split divisor */
942 bl
= divisor
.mantissa
& DMASK32
; /* into high and low parts */
944 if (firmware
&& (quotient
->precision
== fp_f
)) { /* single-precision firmware? */
945 quotient
->exponent
= quotient
->exponent
+ 1; /* fix exponent */
947 ah
= (int32
) (dividend
.mantissa
>> 32); /* split dividend */
948 dh
= (int16
) (bh
>> 16); /* split divisor again */
951 div
= ah
>> 2; /* ASR 2 to prevent overflow */
953 pq1
= div
/ dh
; /* form first partial quotient */
954 div
= ((div
% dh
) & ~1) << 15; /* ASR 1, move rem to upper */
955 pq2
= div
/ dh
; /* form second partial quotient */
957 div
= (uint16
) dl
<< 13; /* move divisor LSB to upper, LSR 3 */
958 cq
= div
/ dh
; /* form correction quotient */
959 cp
= -cq
* pq1
; /* and correction product */
961 cp
= (((cp
>> 14) & ~3) + (int32
) pq2
) << 1; /* add corr prod and 2nd partial quo */
962 quotient
->mantissa
= /* add 1st partial quo and align */
963 (t_uint64
) (((int32
) pq1
<< 16) + cp
) << 32;
966 else { /* hardware or FFP */
967 q1
= (t_uint64
) (dividend
.mantissa
/ bh
); /* form 1st trial quotient */
968 r1
= dividend
.mantissa
% bh
; /* and remainder */
969 p1
= (r1
<< 24) - (bl
>> 8) * q1
; /* calculate correction */
971 while (p1
< 0) { /* correction needed? */
972 q1
= q1
- 1; /* trial quotient too large */
973 p1
= p1
+ (divisor
.mantissa
>> 8); /* increase remainder */
976 q0
= (t_uint64
) ((p1
<< 8) / bh
); /* form 2nd trial quotient */
977 r0
= (p1
<< 8) % bh
; /* and remainder */
978 p0
= (r0
<< 24) - (bl
>> 8) * q0
; /* calculate correction */
980 while (p0
< 0) { /* correction needed? */
981 q0
= q0
- 1; /* trial quotient too large */
982 p0
= p0
+ (divisor
.mantissa
>> 8); /* increase remainder */
985 q
= (q1
<< 32) + q0
; /* sum quotient digits */
987 if (q1
& 0xffffffff00000000) { /* did we lose MSB? */
988 q
= (q
>> 1) | 0x8000000000000000; /* shift right and replace bit */
989 quotient
->exponent
= quotient
->exponent
+ 1;/* bump exponent for shift */
992 if (q
& 0x8000000000000000) /* lose normalization? */
993 q
= q
>> 1; /* correct */
995 quotient
->mantissa
= (t_int64
) q
;
999 complement (quotient
); /* negate if required */
1005 /* Fix an unpacked number.
1007 A floating-point value is converted to an integer. The desired precision of
1008 the result (single or double integer) must be set before calling.
1010 Values less than 0.5 (i.e., with negative exponents) underflow to zero. If
1011 the value exceeds the specified integer range, the maximum integer value is
1012 returned and overflow is set. Otherwise, the floating-point value is
1013 right-shifted to zero the exponent. The result is then rounded.
1016 static uint32
fix (FPU
*result
, FPU operand
)
1021 if (operand
.exponent
< 0) { /* value < 0.5? */
1022 result
->mantissa
= 0; /* result rounds to zero */
1023 overflow
= 0; /* clear for underflow */
1026 else if (operand
.exponent
> /* value > integer size? */
1027 (int32
) op_bits
[result
->precision
]) {
1028 result
->mantissa
= /* return max int value */
1029 (t_uint64
) int_p_max
[result
->precision
] <<
1030 op_start
[result
->precision
];
1031 overflow
= 1; /* and set overflow */
1034 else { /* value in range */
1035 bits_lost
= asr (&operand
, /* shift to zero exponent */
1036 op_bits
[result
->precision
] - operand
.exponent
);
1038 if (operand
.mantissa
< 0) { /* value negative? */
1039 if (bits_lost
) /* bits lost? */
1040 operand
.mantissa
= operand
.mantissa
| 1; /* include one for rounding */
1042 operand
.mantissa
= operand
.mantissa
+ /* round result */
1043 p_half_lsb
[result
->precision
];
1046 result
->mantissa
= operand
.mantissa
& /* mask to precision */
1047 op_mask
[result
->precision
];
1051 result
->exponent
= 0; /* tidy up for integer value */
1056 /* Float an integer to an unpacked number.
1058 An integer is converted to a floating-point value. The desired precision of
1059 the result must be set before calling.
1061 Conversion is simply a matter of copying the integer value, setting an
1062 exponent that reflects the right-aligned position of the bits, and
1066 static void ffloat (FPU
*result
, FPU operand
)
1068 result
->mantissa
= operand
.mantissa
; /* set value */
1069 result
->exponent
= op_bits
[operand
.precision
]; /* set exponent */
1070 normalize (result
); /* normalize */
1076 /* High-level floating-point routines. */
1079 /* Determine operand precisions.
1081 The precisions of the operands and result are determined by decoding an
1082 operation opcode and returned to the caller. Pass NULL for both of the
1083 operands if only the result precision is wanted. Pass NULL for the result if
1084 only the operand precisions are wanted.
1087 void fp_prec (uint16 opcode
, OPSIZE
*operand_l
, OPSIZE
*operand_r
, OPSIZE
*result
)
1089 OPSIZE fp_size
, int_size
;
1091 fp_size
= (OPSIZE
) ((opcode
& 0003) + 2); /* fp_f, fp_x, fp_t, fp_e */
1092 int_size
= (OPSIZE
) ((opcode
& 0004) >> 2); /* in_s, in_d */
1094 if (operand_l
&& operand_r
) { /* want operand precisions? */
1095 switch (opcode
& 0120) { /* mask out opcode bit 5 */
1096 case 0000: /* add/mpy */
1097 case 0020: /* sub/div */
1098 *operand_l
= fp_size
; /* assume first op is fp */
1100 if (opcode
& 0004) /* operand internal? */
1101 *operand_r
= fp_a
; /* second op is accum */
1103 *operand_r
= fp_size
; /* second op is fp */
1106 case 0100: /* fix/accum as integer */
1107 *operand_l
= fp_size
; /* first op is fp */
1108 *operand_r
= fp_a
; /* second op is always null */
1111 case 0120: /* flt/accum as float */
1112 *operand_l
= int_size
; /* first op is integer */
1113 *operand_r
= fp_a
; /* second op is always null */
1117 if (opcode
& 0010) /* operand internal? */
1118 *operand_l
= fp_a
; /* first op is accum */
1121 if (result
) /* want result precision? */
1122 if ((opcode
& 0120) == 0100) /* fix? */
1123 *result
= int_size
; /* result is integer */
1124 else /* all others */
1125 *result
= fp_size
; /* result is fp */
1131 /* Floating Point Processor executor.
1133 The executor simulates the MPP interface between the CPU and the FPP. The
1134 operation to be performed is specified by the supplied opcode, which conforms
1135 to the FPP hardware interface, as follows:
1138 ---- ----- ----------------------------------------------
1139 7 0 Exponent range is standard (+/-127)
1140 1 Exponent range is expanded (+/-511)
1151 3 0 Left operand is supplied
1152 1 Left operand in accumulator
1154 2 0 Right operand is supplied (ADD/SUB/MPY/DIV)
1155 Single integer operation (FIX/FLT)
1156 1 Right operand in accumulator (ADD/SUB/MPY/DIV)
1157 Double integer operation (FIX/FLT)
1159 1-0 00 2-word operation
1164 If the opcode specifies that the left (or right) operand is in the
1165 accumulator, then the value supplied for that parameter is not used. All
1166 results are automatically left in the accumulator. If the result is not
1167 needed externally, then NULL may be passed for the result parameter.
1169 To support accumulator set/get operations under simulation, the opcode is
1170 expanded to include a special mode, indicated by bit 15 = 1. In this mode,
1171 if the result parameter is NULL, then the accumulator is set from the value
1172 passed as operand_l. If the result parameter is not null, then the
1173 accumulator value is returned as the result, and operand_l is ignored. The
1174 precision of the operation is performed as specified by the OPSIZE value
1175 passed in bits 2-0 of the opcode.
1177 The function returns 1 if the operation overflows and 0 if not.
1180 uint32
fp_exec (uint16 opcode
, OP
*result
, OP operand_l
, OP operand_r
)
1182 static FPU accumulator
;
1183 FPU uoperand_l
, uoperand_r
;
1184 OPSIZE op_l_prec
, op_r_prec
, rslt_prec
;
1187 if (opcode
& SIGN
) { /* accumulator mode? */
1188 rslt_prec
= (OPSIZE
) (opcode
& 0017); /* get operation precision */
1190 if (result
) { /* get accumulator? */
1191 op_l_prec
= accumulator
.precision
; /* save accum prec temp */
1192 accumulator
.precision
= rslt_prec
; /* set desired precision */
1193 *result
= pack (accumulator
); /* pack accumulator */
1194 accumulator
.precision
= op_l_prec
; /* restore correct prec */
1196 else /* set accumulator */
1197 accumulator
= unpack (operand_l
, rslt_prec
); /* unpack from operand */
1199 return 0; /* no overflow from accum ops */
1202 fp_prec (opcode
, &op_l_prec
, &op_r_prec
, &rslt_prec
); /* calc precs from opcode */
1204 if (op_l_prec
== fp_a
) /* left operand in accum? */
1205 uoperand_l
= accumulator
; /* copy it */
1206 else /* operand supplied */
1207 uoperand_l
= unpack (operand_l
, op_l_prec
); /* unpack from parameter */
1209 if (op_r_prec
== fp_a
) /* right operand in accum? */
1210 uoperand_r
= accumulator
; /* copy it */
1211 else /* operand supplied */
1212 uoperand_r
= unpack (operand_r
, op_r_prec
); /* unpack from parameter */
1215 switch (opcode
& 0160) { /* dispatch operation */
1217 case 0000: /* add */
1218 add (&accumulator
, uoperand_l
, uoperand_r
);
1221 case 0020: /* subtract */
1222 complement (&uoperand_r
);
1223 add (&accumulator
, uoperand_l
, uoperand_r
);
1226 case 0040: /* multiply */
1227 multiply (&accumulator
, uoperand_l
, uoperand_r
);
1230 case 0060: /* divide */
1231 divide (&accumulator
, uoperand_l
, uoperand_r
);
1234 case 0100: /* fix */
1235 accumulator
.precision
= rslt_prec
;
1236 overflow
= fix (&accumulator
, uoperand_l
);
1238 if (result
) /* result wanted? */
1239 *result
= pack_int (accumulator
.mantissa
, /* pack integer */
1243 case 0120: /* float */
1244 accumulator
.precision
= rslt_prec
;
1245 ffloat (&accumulator
, uoperand_l
);
1247 if (result
) /* result wanted? */
1248 *result
= pack (accumulator
); /* pack FP (FLT does not round) */
1251 case 0140: /* (diagnostic) */
1252 case 0160: /* (diagnostic) */
1256 if (UNIT_CPU_MODEL
!= UNIT_1000_F
) /* firmware implementation? */
1257 accumulator
.mantissa
= accumulator
.mantissa
& /* mask to precision */
1258 op_mask
[accumulator
.precision
];
1260 normalize (&accumulator
); /* normalize */
1261 overflow
= roundovf (&accumulator
, opcode
& 0200); /* round and check for overflow */
1263 if (result
) /* result wanted? */
1264 *result
= pack (accumulator
); /* pack result */
1270 /* Set or get accumulator at desired precision.
1272 This function provides access to the FPP accumulator. In hardware, the
1273 accumulator may be read at a given precision by sending the FPP an opcode
1274 encoded with the desired precision and then reading words from the FPP
1275 /without/ initiating the operation, i.e., without starting the processor.
1277 Under simulation, pass this function a NULL operand and the desired
1278 precision to read the accumulator. Pass a pointer to an operand and the
1279 desired precision to set the accumulator; the return value in this case is
1283 OP
fp_accum (const OP
*operand
, OPSIZE precision
)
1286 uint16 opcode
= (uint16
) precision
| SIGN
; /* add special mode bit */
1289 fp_exec (opcode
, NULL
, *operand
, NOP
); /* set accum */
1291 fp_exec (opcode
, &result
, NOP
, NOP
); /* get accum */
1296 /* Pack an unpacked floating-point number.
1298 An unpacked mantissa is passed as a "packed" number with an unused exponent.
1299 The mantissa and separately-passed exponent are packed into the in-memory
1300 floating-point format. Note that all bits are significant in the mantissa
1301 (no masking is done).
1304 uint32
fp_pack (OP
*result
, OP mantissa
, int32 exponent
, OPSIZE precision
)
1308 unpacked
.mantissa
= unpack_int (mantissa
, precision
); /* unpack mantissa */
1309 unpacked
.exponent
= exponent
; /* set exponent */
1310 unpacked
.precision
= precision
; /* set precision */
1311 *result
= pack (unpacked
); /* pack them */
1316 /* Normalize, round, and pack an unpacked floating-point number.
1318 An unpacked mantissa is passed as a "packed" number with an unused exponent.
1319 The mantissa and separately-passed exponent are normalized, rounded, and
1320 packed into the in-memory floating-point format. Note that all bits are
1321 significant in the mantissa (no masking is done).
1324 uint32
fp_nrpack (OP
*result
, OP mantissa
, int32 exponent
, OPSIZE precision
)
1328 unpacked
.mantissa
= unpack_int (mantissa
, precision
); /* unpack mantissa */
1329 unpacked
.exponent
= exponent
; /* set exponent */
1330 unpacked
.precision
= precision
; /* set precision */
1331 return nrpack (result
, unpacked
, FALSE
); /* norm/rnd/pack them */
1335 /* Unpack a packed floating-point number.
1337 A floating-point number, packed into the in-memory format, is unpacked into
1338 separate mantissa and exponent values. The unpacked mantissa is returned in
1339 a "packed" structure with an exponent of zero. Mantissa or exponent may be
1340 null if that part isn't wanted.
1343 uint32
fp_unpack (OP
*mantissa
, int32
*exponent
, OP packed
, OPSIZE precision
)
1348 unpacked
= unpack (packed
, precision
); /* unpack mantissa and exponent */
1350 if (exponent
) /* exponent wanted? */
1351 *exponent
= unpacked
.exponent
; /* return exponent */
1353 if (mantissa
) /* mantissa wanted? */
1354 *mantissa
= pack_int (unpacked
.mantissa
, fp_t
); /* return full-size mantissa */
1359 /* Complement an unpacked mantissa.
1361 An unpacked mantissa is passed as a "packed" number with a zero exponent.
1362 The exponent increment, i.e., either zero or one, depending on whether a
1363 renormalization was required, is returned. Note that all bits are
1364 significant in the mantissa.
1367 uint32
fp_ucom (OP
*mantissa
, OPSIZE precision
)
1371 unpacked
.mantissa
= unpack_int (*mantissa
, precision
); /* unpack mantissa */
1372 unpacked
.exponent
= 0; /* clear undefined exponent */
1373 unpacked
.precision
= precision
; /* set precision */
1374 complement (&unpacked
); /* negate it */
1375 *mantissa
= pack_int (unpacked
.mantissa
, precision
); /* replace mantissa */
1376 return (uint32
) unpacked
.exponent
; /* return exponent increment */
1380 /* Complement a floating-point number. */
1382 uint32
fp_pcom (OP
*packed
, OPSIZE precision
)
1386 unpacked
= unpack (*packed
, precision
); /* unpack the number */
1387 complement (&unpacked
); /* negate it */
1388 return nrpack (packed
, unpacked
, FALSE
); /* and norm/rnd/pack */
1392 /* Truncate a floating-point number. */
1394 uint32
fp_trun (OP
*result
, OP source
, OPSIZE precision
)
1398 FPU one
= { FP_ONEHALF
, 1, 0 }; /* 0.5 * 2 ** 1 = 1.0 */
1399 OP zero
= { { 0, 0, 0, 0, 0 } }; /* 0.0 */
1400 t_uint64 mask
= mant_mask
[precision
] & ~FP_MSIGN
;
1402 unpacked
= unpack (source
, precision
);
1403 if (unpacked
.exponent
< 0) /* number < 0.5? */
1404 *result
= zero
; /* return 0 */
1405 else if (unpacked
.exponent
>= (int32
) op_bits
[precision
]) /* no fractional bits? */
1406 *result
= source
; /* already integer */
1408 mask
= (mask
>> unpacked
.exponent
) & mask
; /* mask fractional bits */
1409 bits_lost
= ((unpacked
.mantissa
& mask
) != 0); /* flag if bits lost */
1410 unpacked
.mantissa
= unpacked
.mantissa
& ~mask
; /* mask off fraction */
1411 if ((unpacked
.mantissa
< 0) && bits_lost
) /* negative? */
1412 add (&unpacked
, unpacked
, one
); /* truncate toward zero */
1413 nrpack (result
, unpacked
, FALSE
); /* (overflow cannot occur) */
1415 return 0; /* clear overflow on return */
1419 /* Convert a floating-point number from one precision to another. */
1421 uint32
fp_cvt (OP
*result
, OPSIZE source_precision
, OPSIZE dest_precision
)
1425 unpacked
= unpack (*result
, source_precision
);
1426 unpacked
.precision
= dest_precision
;
1427 return nrpack (result
, unpacked
, FALSE
); /* norm/rnd/pack */
1431 #endif /* end of int64 support */