533b388f6763e21c803a51ad756aed524be472af
[openpower-isa.git] / openpower / isafunctions / fpfromint.mdwn
1 # A.3 Floating-Point Convert from Integer Model
2
3 The following describes algorithmically the operation of the Floating
4 Convert From Integer instructions.
5
6 <!-- Power ISA Book I Version 3.0B Section A.3 page 782 -->
7
8 def Round_Float( tgt_precision, sign, exp, frac, round_mode ):
9 inc <- 0
10 if tgt_precision = 'single-precision' then
11 lsb <- frac[23]
12 gbit <- frac[24]
13 rbit <- frac[25]
14 xbit <- frac[26:63] > 0
15 else # tgt_precision = 'double-precision'
16 lsb <- frac[52]
17 gbit <- frac[53]
18 rbit <- frac[54]
19 xbit <- frac[55:63] > 0
20
21 if round_mode = 0b00 then # Round to Nearest
22 if (lsb = 1) & (gbit = 1) then inc <- 1
23 if (lsb = 0) & (gbit = 1) & (rbit = 1) then inc <- 1
24 if (lsb = 0) & (gbit = 1) & (xbit = 1) then inc <- 1
25 if round_mode = 0b10 then # Round toward + Infinity
26 if (sign = 0) & (gbit = 1) then inc <-1
27 if (sign = 0) & (rbit = 1) then inc <-1
28 if (sign = 0) & (xbit = 1) then inc <-1
29 if round_mode = 0b11 then # Round toward - Infinity
30 if (sign = 1) & (gbit = 1) then inc <-1
31 if (sign = 1) & (rbit = 1) then inc <-1
32 if (sign = 1) & (xbit = 1) then inc <-1
33
34 # increase fraction, record the top bit as a 'carry out'
35 if tgt_precision = 'single-precision' then
36 tmp <- [0]*25
37 tmp[1:24] <- frac[0:23]
38 tmp[0:24] <- tmp[0:24] + inc
39 carry_out = tmp[24]
40 frac[0:23] = tmp[1:24]
41 else # tgt_precision = 'double-precision'
42 tmp <- [0]*54
43 tmp[1:53] <- frac[0:52]
44 tmp[0:53] <- tmp[0:53] + inc
45 carry_out = tmp[53]
46 frac[0:52] = tmp[1:54]
47 if carry_out = 1 then exp <- exp + 1
48 # TODO, later
49 # FPSCR[FR] <- inc
50 # FPSCR[FI] <- gbit | rbit | xbit
51 # FPSCR[XX] <- FPSCR[XX] | FPSCR[FI]
52
53 def INT2FP(FR, cvt, RN):
54 if cvt = 'sint2double' then
55 tgt_precision = 'double-precision'
56 sign <- FR[0]
57 if cvt = 'sint2single' then
58 tgt_precision <- 'single-precision'
59 sign <- FR[0]
60 if cvt = 'uint2double' then
61 tgt_precision <- 'double-precision'
62 sign <- 0
63 if cvt = 'uint2single' then
64 tgt_precision <- 'single-precision'
65 sign <- 0
66
67 result = [0] * 64
68 exp <- 63
69 frac[0:63] <- FR
70
71 if frac[0:63] = 0 then
72 # Zero Operand
73 # TODO, FPSCR
74 #FPSCR[FR] <- 0b00
75 #FPSCR[FI] <- 0b00
76 #FPSCR[FPRF] <- '+ zero'
77 result = [0] * 64
78 else
79 if sign = 1 then frac[0:63] <- ¬frac[0:63] + 1
80 # do the loop 0 times if FR = max negative 64-bit integer or
81 # if FR = max unsigned 64-bit integer
82 do while frac[0] = 0
83 frac[0:63] <- frac[1:63] || 0b0
84 exp <- exp - 1
85 # round to nearest
86 Round_Float( tgt_precision, sign, exp, frac, 0b00 )
87 # TODO, FPSCR
88 #if sign = 0 then FPSCR[FPRF] <- '+normal number'
89 #if sign = 1 then FPSCR[FPRF] <- '-normal number'
90 result[0] <- sign
91 result[1:11] <- exp + 1023 # exp + bias
92 result[12:63] <- frac[1:52]
93
94 return result
95