comments in fp convert, fix carry out, comment out FPSCR for now
[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 and gbit = 1 then inc <- 1
23 if lsb = 0 and gbit = 1 and rbit = 1 then inc <- 1
24 if lsb = 0 and gbit = 1 and xbit = 1 then inc <- 1
25 end
26 if round_mode = 0b10 then # Round toward + Infinity
27 if sign = 0 and gbit = 1 then inc <-1
28 if sign = 0 and rbit = 1 then inc <-1
29 if sign = 0 and xbit = 1 then inc <-1
30 end
31 if round_mode = 0b11 then # Round toward - Infinity
32 if sign = 1 and gbit = 1 then inc <-1
33 if sign = 1 and rbit = 1 then inc <-1
34 if sign = 1 and xbit = 1 then inc <-1
35 end
36 # increase fraction, record the top bit as a "carry out"
37 if tgt_precision = "single-precision" then
38 tmp = [0]*25
39 tmp[1:24] = frac[0:23]
40 tmp[0:24] <- tmp[0:24] + inc
41 carry_out = tmp[24]
42 frac[0:23] = tmp[1:24]
43 else # tgt_precision = "double-precision"
44 tmp = [0]*54
45 tmp[1:53] = frac[0:52]
46 tmp[0:53] <- tmp[0:53] + inc
47 carry_out = tmp[53]
48 frac[0:52] = tmp[1:54]
49 if carry_out = 1 then exp <- exp + 1
50 # TODO, later
51 # FPSCR[FR] <- inc
52 # FPSCR[FI] <- gbit | rbit | xbit
53 # FPSCR[XX] <- FPSCR[XX] | FPSCR[FI]
54
55 def INT2FP(FR, cvt, RN):
56 if cvt = "sint2double" then
57 tgt_precision = "double-precision"
58 sign <- FR[0]
59 if cvt = "sint2single" then
60 tgt_precision <- "single-precision"
61 sign <- FR[0]
62 if cvt = "uint2double" then
63 tgt_precision <- "double-precision"
64 sign <- 0
65 if cvt = "uint2single" then
66 tgt_precision <- "single-precision"
67 sign <- 0
68
69 result = [0] * 64
70 exp <- 63
71 frac[0:63] <- FR
72
73 if frac[0:63] = 0 then
74 # Zero Operand
75 # TODO, FPSCR
76 #FPSCR[FR] <- 0b00
77 #FPSCR[FI] <- 0b00
78 #FPSCR[FPRF] <- "+ zero"
79 result = [0] * 64
80 else
81 if sign = 1 then frac[0:63] <- ¬frac[0:63] + 1
82 # do the loop 0 times if FR = max negative 64-bit integer or
83 # if FR = max unsigned 64-bit integer
84 do while frac[0] = 0
85 frac[0:63] <- frac[1:63] || 0b0
86 exp <- exp - 1
87 # round to nearest
88 Round_Float( tgt_precision, sign, exp, frac, 0b00 )
89 # TODO, FPSCR
90 #if sign = 0 then FPSCR[FPRF] <- "+normal number"
91 #if sign = 1 then FPSCR[FPRF] <- "-normal number"
92 result[0] <- sign
93 result[1:11] <- exp + 1023 # exp + bias
94 result[12:63] <- frac[1:52]
95
96 return result
97