Debian Codegraph @

lib/mlrisc-lib/MLRISC/mltree/mltree-mult.sml

raw
1 (*
2 * Generate multiplication/division by a constant.
3 * This module is mainly used for architectures without fast integer multiply.
4 *
5 * -- Allen
6 *)
7 functor MLTreeMult
8 (structure I : INSTRUCTIONS
9 structure T : MLTREE
10
11 structure CB : CELLS_BASIS (* = CellsBasis *)
12 where type CellSet.cellset = CellsBasis.CellSet.cellset
13 and type 'a ColorTable.hash_table = 'a CellsBasis.ColorTable.hash_table
14 and type 'a HashTable.hash_table = 'a CellsBasis.HashTable.hash_table
15 and type SortedCells.sorted_cells = CellsBasis.SortedCells.sorted_cells
16 and type cell = CellsBasis.cell
17 and type cellColor = CellsBasis.cellColor
18 and type cellkind = CellsBasis.cellkind
19 and type cellkindDesc = CellsBasis.cellkindDesc
20 and type cellkindInfo = CellsBasis.cellkindInfo
21 val intTy : int (* width of integer type *)
22
23 type argi = {r:CB.cell, i:int, d:CB.cell}
24 type arg = {r1:CB.cell, r2:CB.cell, d:CB.cell}
25
26 (* these are always non-overflow trapping *)
27 val mov : {r:CB.cell, d:CB.cell} -> I.instruction
28 val add : arg -> I.instruction
29 val slli : argi -> I.instruction list
30 val srli : argi -> I.instruction list
31 val srai : argi -> I.instruction list
32
33 val trapping : bool (* trap on overflow? *)
34 val multCost : int ref (* cost of multiplication *)
35
36 (* basic ops; these have to implemented by the architecture *)
37
38 (* if trapping = true, then the following MUST trap on overflow *)
39 val addv : arg -> I.instruction list
40 val subv : arg -> I.instruction list
41
42 (* some architectures, like the PA-RISC and the Alpha,
43 * have these types of special ops
44 * if trapping = true, then the following MUST also trap on overflow
45 *)
46 val sh1addv : (arg -> I.instruction list) option (* a*2 + b *)
47 val sh2addv : (arg -> I.instruction list) option (* a*4 + b *)
48 val sh3addv : (arg -> I.instruction list) option (* a*8 + b *)
49
50 val signed : bool (* signed? *)
51 ) : MLTREE_MULT_DIV =
52 struct
53 structure T = T
54 structure I = I
55 structure C = I.C
56 structure W = Word
57 structure A = Array
58
59 type arg = argi
60
61 infix << >> ~>> || &&
62 val itow = W.fromInt
63 val wtoi = W.toIntX
64 val op << = W.<<
65 val op >> = W.>>
66 val op ~>> = W.~>>
67 val op || = W.orb
68 val op && = W.andb
69
70 exception TooComplex
71
72 fun error msg = MLRiscErrorMsg.error("MLTreeMult",msg)
73
74 val zeroR = C.zeroReg CB.GP
75 val shiftri = if signed then srai else srli
76
77 fun isPowerOf2 w = ((w - 0w1) && w) = 0w0
78
79 fun log2 n = (* n must be > 0!!! *)
80 let fun loop(0w1,pow) = pow
81 | loop(w,pow) = loop(w >> 0w1,pow+1)
82 in loop(n,0) end
83
84 fun zeroBits(w,lowZeroBits) =
85 if (w && 0w1) = 0w1 then (w,lowZeroBits)
86 else zeroBits(w >> 0w1,lowZeroBits+0w1)
87
88 (* Non overflow trapping version of multiply:
89 * We can use add, shadd, shift, sub to perform the multiplication
90 *)
91 fun multiplyNonTrap{r,i,d} =
92 let fun mult(r,w,maxCost,d) =
93 if maxCost <= 0 then raise TooComplex
94 else if isPowerOf2 w then slli{r=r,i=log2 w,d=d}
95 else
96 (case (w,sh1addv,sh2addv,sh3addv) of
97 (* some base cases *)
98 (0w3,SOME f,_,_) => f{r1=r,r2=r,d=d}
99 | (0w5,_,SOME f,_) => f{r1=r,r2=r,d=d}
100 | (0w9,_,_,SOME f) => f{r1=r,r2=r,d=d}
101 | _ => (* recurse on the bit patterns of w *)
102 let val tmp = C.newReg()
103 in if (w && 0w1) = 0w1 then (* low order bit is 1 *)
104 if (w && 0w2) = 0w2 then (* second bit is 1 *)
105 mult(r,w+0w1,maxCost-1,tmp) @
106 subv{r1=tmp,r2=r,d=d}
107 else (* second bit is 0 *)
108 mult(r,w-0w1,maxCost-1,tmp) @
109 addv{r1=tmp,r2=r,d=d}
110 else (* low order bit is 0 *)
111 let val (w,lowZeroBits) = zeroBits(w,0w0)
112 in mult(r,w,maxCost-1,tmp) @
113 slli{r=tmp,i=wtoi lowZeroBits,d=d}
114 end
115 end
116 )
117 in if i <= 0 then raise TooComplex
118 else if i = 1 then [mov{r=r,d=d}]
119 else mult(r,itow i,!multCost,d)
120 end
121
122 (* The semantics of roundToZero{r,i,d} is:
123 * if r >= 0 then d <- r
124 * else d <- r + i
125 *)
126 fun roundToZero stm {ty,r,i,d} =
127 let val reg = T.REG(ty,r)
128 in stm(T.MV(ty,d,
129 T.COND(ty,T.CMP(ty,T.GE,reg, T.LI 0),reg,
130 T.ADD(ty,reg,T.LI(T.I.fromInt(intTy,i))))))
131 end
132
133
134 (*
135 * Simulate rounding towards zero for signed division
136 *)
137 fun roundDiv{mode=T.TO_NEGINF,r,...} = ([],r) (* no rounding necessary *)
138 | roundDiv{mode=T.TO_ZERO,stm,r,i} =
139 if signed then
140 let val d = C.newReg()
141 in if i = 2 then (* special case for division by 2 *)
142 let val tmpR = C.newReg()
143 in (srli{r=r,i=intTy - 1,d=tmpR}@[add{r1=r,r2=tmpR,d=d}], d)
144 end
145 else
146 (* invoke rounding callback *)
147 let val () = roundToZero stm {ty=intTy,r=r,i=i-1,d=d}
148 in ([],d) end
149 end
150 else ([],r) (* no rounding for unsigned division *)
151 | roundDiv{mode,...} =
152 error("Integer rounding mode "^
153 T.Basis.roundingModeToString mode^" is not supported")
154
155 fun divideNonTrap{mode,stm}{r,i,d} =
156 if i > 0 andalso isPowerOf2(itow i)
157 then
158 let val (code,r) = roundDiv{mode=mode,stm=stm,r=r,i=i}
159 in code@shiftri{r=r,i=log2(itow i),d=d} end (* won't overflow *)
160 else raise TooComplex
161
162 (* Overflow trapping version of multiply:
163 * We can use only add and shadd to perform the multiplication,
164 * because of overflow trapping problem.
165 *)
166 fun multiplyTrap{r,i,d} =
167 let fun mult(r,w,maxCost,d) =
168 if maxCost <= 0 then raise TooComplex
169 else
170 (case (w,sh1addv,sh2addv,sh3addv,zeroR) of
171 (* some simple base cases *)
172 (0w2,_,_,_,_) => addv{r1=r,r2=r,d=d}
173 | (0w3,SOME f,_,_,_) => f{r1=r,r2=r,d=d}
174 | (0w4,_,SOME f,_,SOME z) => f{r1=r,r2=z,d=d}
175 | (0w5,_,SOME f,_,_) => f{r1=r,r2=r,d=d}
176 | (0w8,_,_,SOME f,SOME z) => f{r1=r,r2=z,d=d}
177 | (0w9,_,_,SOME f,_) => f{r1=r,r2=r,d=d}
178 | _ => (* recurse on the bit patterns of w *)
179 let val tmp = C.newReg()
180 in if (w && 0w1) = 0w1 then
181 mult(r,w - 0w1,maxCost-1,tmp) @ addv{r1=tmp,r2=r,d=d}
182 else
183 case (w && 0w7, sh3addv, zeroR) of
184 (0w0, SOME f, SOME z) => (* times 8 *)
185 mult(r,w >> 0w3,maxCost-1,tmp) @ f{r1=tmp,r2=z,d=d}
186 | _ =>
187 case (w && 0w3, sh2addv, zeroR) of
188 (0w0, SOME f, SOME z) => (* times 4 *)
189 mult(r,w >> 0w2,maxCost-1,tmp) @ f{r1=tmp,r2=z,d=d}
190 | _ =>
191 mult(r,w >> 0w1,maxCost-1,tmp) @ addv{r1=tmp,r2=tmp,d=d}
192 end
193 )
194 in if i <= 0 then raise TooComplex
195 else if i = 1 then [mov{r=r,d=d}]
196 else mult(r,itow i,!multCost,d)
197 end
198
199 fun divideTrap{mode,stm}{r,i,d} =
200 if i > 0 andalso isPowerOf2(itow i)
201 then
202 let val (code,r) = roundDiv{mode=mode,stm=stm,r=r,i=i}
203 in code@shiftri{r=r,i=log2(itow i),d=d} end (* won't overflow *)
204 else raise TooComplex
205
206 fun multiply x = if trapping then multiplyTrap x else multiplyNonTrap x
207 fun divide x = if trapping then divideTrap x else divideNonTrap x
208
209 end