added an extra SVP64 instruction, svstep, to replace setvl
[openpower-isa.git] / openpower / isafunctions / double2single.mdwn
1 # A.1 Floating-Point Round to Single-Precision Model
2
3 The following describes algorithmically the operation of the Floating
4 Round to Single-Precision instruction.
5
6 <!-- Power ISA Book I Version 3.0B Section A.1 page 775-778 -->
7
8 def Round_Single( sign, exp, frac, G, R, X, round_mode ):
9 inc <- 0
10 lsb <- frac[23]
11 gbit <- frac[24]
12 rbit <- frac[25]
13 xbit <- (frac[26:52] || G || R || X) != 0
14
15 if round_mode = 0b00 then # Round to Nearest
16 if (lsb = 1) & (gbit = 1) then inc <- 1
17 if (lsb = 0) & (gbit = 1) & (rbit = 1) then inc <- 1
18 if (lsb = 0) & (gbit = 1) & (xbit = 1) then inc <- 1
19 if round_mode = 0b10 then # Round toward + Infinity
20 if (sign = 0) & (gbit = 1) then inc <-1
21 if (sign = 0) & (rbit = 1) then inc <-1
22 if (sign = 0) & (xbit = 1) then inc <-1
23 if round_mode = 0b11 then # Round toward - Infinity
24 if (sign = 1) & (gbit = 1) then inc <-1
25 if (sign = 1) & (rbit = 1) then inc <-1
26 if (sign = 1) & (xbit = 1) then inc <-1
27
28 # add with carry-out
29 tmp <- [0]*25
30 tmp[1:24] <- frac[0:23]
31 tmp[0:24] <- tmp[0:24] + inc
32 carry_out <- tmp[0]
33 frac[0:23] <- tmp[1:24]
34 if carry_out = 1 then
35 exp[0:10] <- exp + 1
36 frac[0:23] <- 0b1 || frac[0:22]
37 frac[24:52] <- [0]*29
38 # TODO, later
39 # FPSCR[FR] <- inc
40 # FPSCR[FI] <- gbit || rbit || xbit
41
42 def DOUBLE2SINGLE(FR):
43 if (FR[1:11] <u 897) & (FR[1:63] >u 0) then
44 # exponent underflow
45 mode = 'disabled_exp_underflow'
46 # TODO if FPSCR_UE = 0 then mode = 'disabled_exp_underflow'
47 # TODO if FPSCR_UE = 1 then mode = 'enabled_exp_underflow'
48 else if (FR[1:11] >u 1150) & (FR[1:11] <u 2047) then
49 # exponent overflow
50 mode = 'disabled_exp_overflow'
51 # TODO if FPSCR_OE = 0 then mode = 'disabled_exp_overflow'
52 # TODO if FPSCR_OE = 1 then mode = 'enabled_exp_overflow'
53 else if (FR[1:11] >u 896) & (FR[1:11] <u 1151) then
54 # normal operand
55 mode <- 'normal_operand'
56 else if (FR[1:63] = 0) then
57 # zero operand
58 mode <- 'zero_operand'
59 else if (FR[1:11] = 2047) then
60 # inf / nan
61 if (FR[12:63] = 0) then
62 mode <- 'inf_operand'
63 else if (FR[12] = 1) then
64 mode <- 'qnan_operand'
65 else if (FR[12] = 0) & (FR[13:63] >u 0) then
66 mode <- 'snan_operand'
67
68 frac <- [0]*53
69 exp <- [0]*11
70 result <- [0] * 64
71
72 if mode = 'normal_operand' then
73 sign <- FR[0]
74 exp <- FR[1:11] - 1023
75 frac[0:52] <- 0b1 || FR[12:63]
76 RN <- 0b00 # TODO
77 Round_Single(sign, exp, frac, 0b0, 0b0, 0b0, RN)
78 # TODO FPSCR[XX] <- FPSCR[XX] || FPSCR[FI]
79 if exp > 127 then
80 mode = 'disabled_exp_overflow'
81 # if FPSCR_OE = 0 then mode = 'disabled_exp_overflow'
82 # if FPSCR_OE = 1 then mode = 'enabled_overflow'
83 else
84 result[0] <- sign
85 result[1:11] <- exp + 1023
86 result[12:63] <- frac[1:52]
87 # if sign = 0 then
88 # FPSCR[FPRF] <- '+ normal number'
89 # else
90 # FPSCR[FPRF] <- '- normal number'
91
92 if mode = 'enabled_exp_underflow':
93 sign <- FR[0]
94 if FR[1:11] = 0 then
95 exp <- 1022
96 frac[0:52] <- 0b0 || FRB[12:63]
97 if FR[1:11] >u 0 then
98 exp <- FRB[1:11] - 1023
99 frac[0:52] <- 0b1 || FRB[12:63]
100 # denormalise operand
101 G = 0b0
102 R = 0b0
103 X = 0b0
104 do while exp < -126
105 exp <- exp + 1
106 X <- R | X
107 R <- G
108 G <- frac[52]
109 frac[0:52] <- frac[1:52] || 0b0
110 # TODO
111 # FPCSR_UX <- (frac[0:52] || G || R || X) >u 0)
112 RN <- 0b00 # TODO
113 Round_Single(sign, exp, frac, G, R, X, RN)
114
115 if mode = 'zero_operand':
116 # Zero Operand
117 result[0] <- FR[0] # copy sign, the rest is zero
118 # TODO, FPSCR
119 #FPSCR[FR] <- 0b00
120 #FPSCR[FI] <- 0b00
121 #FPSCR[FPRF] <- '+ zero'
122
123 if mode = 'disabled_exp_underflow':
124 if sign = 1 then frac[0:63] <- ¬frac[0:63] + 1
125 # do the loop 0 times if FR = max negative 64-bit integer or
126 # if FR = max unsigned 64-bit integer
127 do while frac[0] = 0
128 frac[0:63] <- frac[1:63] || 0b0
129 exp <- exp - 1
130 # round to nearest
131 RN <- 0b00 # TODO, FPSCR[RN]
132 Round_Float( tgt_precision, sign, exp, frac, RN )
133 # TODO, FPSCR
134 #if sign = 0 then FPSCR[FPRF] <- '+normal number'
135 #if sign = 1 then FPSCR[FPRF] <- '-normal number'
136 result[0] <- sign
137 result[1:11] <- exp + 1023 # exp + bias
138 result[12:63] <- frac[1:52]
139
140 return result
141