Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /trunk/src/compiler/mid-to-low/mid-to-low.sml
ViewVC logotype

Diff of /trunk/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 464, Thu Oct 28 12:57:20 2010 UTC revision 465, Thu Oct 28 22:17:34 2010 UTC
# Line 81  Line 81 
81                  end                  end
82            (* end case *))            (* end case *))
83    
84    (* expand the EvalKernel operations into vector operations.  The parameters are    (* expand the EvalKernel operations into vector operations.  The parameters
85       * are
86     *    result  -- the lhs variable to store the result     *    result  -- the lhs variable to store the result
87     *    d       -- the vector width of the operation, which should be equal to twice the     *    d       -- the vector width of the operation, which should be equal
88     *               support of the kernel     *               to twice the support of the kernel
89     *    h       -- the kernel     *    h       -- the kernel
90     *    k       -- the derivative of the kernel to evaluate     *    k       -- the derivative of the kernel to evaluate
91       *
92       * The generated code is computing
93       *
94       *    result = a_0 + x*(a_1 + x*(a_2 + ... x*a_n) ... )
95       *
96       * as a d-wide vector operation, where n is the degree of the kth derivative
97       * of h and the a_i are coefficient vectors that have an element for each
98       * piece of h.  The computation is implemented as follows
99       *
100       *    m_n     = x * a_n
101       *    s_{n-1} = a_{n-1} + m_n
102       *    m_{n-1} = x * s_{n-1}
103       *    s_{n-2} = a_{n-2} + m_{n-1}
104       *    m_{n-2} = x * s_{n-2}
105       *    ...
106       *    s_1     = a_1 + m_2
107       *    m_1     = x * s_1
108       *    result  = a_0 + m_1
109     *)     *)
110      fun expandEvalKernel (result, d, h, k, [x]) = let      fun expandEvalKernel (result, d, h, k, [x]) = let
111            val {isCont, segs} = Kernel.curve (h, k)            val {isCont, segs} = Kernel.curve (h, k)
112            val deg = List.length segs - 1          (* degree of polynomial *)
113              val deg = List.length(hd segs) - 1
114          (* convert to a vector of vectors to give fast access *)          (* convert to a vector of vectors to give fast access *)
115            val segs = Vector.fromList (List.map Vector.fromList segs)            val segs = Vector.fromList (List.map Vector.fromList segs)
116          (* get the kernel coefficient value for the d'th term of the i'th          (* get the kernel coefficient value for the d'th term of the i'th
117           * segment.           * segment.
118           *)           *)
119            fun coefficient d i = Literal.Float(ratToFloat (Vector.sub (Vector.sub(segs, i), d)))            fun coefficient d i =
120                    Literal.Float(ratToFloat (Vector.sub (Vector.sub(segs, i), d)))
121            val ty = DstTy.VecTy d            val ty = DstTy.VecTy d
122            val coeffs = List.tabulate (deg+1,            val coeffs = List.tabulate (deg+1,
123                  fn i => DstIL.Var.new(str(chr(ord #"a" + (deg - i))), ty))                  fn i => DstIL.Var.new("a"^Int.toString i, ty))
124          (* code to define the coefficient vectors *)          (* code to define the coefficient vectors *)
125            val coeffVecs = let            val coeffVecs = let
126                  fun mk (x, (i, code)) = let                  fun mk (x, (i, code)) = let
# Line 115  Line 136 
136                    #2 (List.foldr mk (deg, []) coeffs)                    #2 (List.foldr mk (deg, []) coeffs)
137                  end                  end
138          (* build the evaluation of the polynomials in reverse order *)          (* build the evaluation of the polynomials in reverse order *)
139            fun eval [coeff] = (coeff, [])            fun pTmp i = DstIL.Var.new("prod" ^ Int.toString i, ty)
140              | eval (coeff::r) = let            fun sTmp i = DstIL.Var.new("sum" ^ Int.toString i, ty)
141                  val (t1, stms) = eval r            fun eval (i, [coeff]) = let
142                  val t2 = DstIL.Var.new ("_t", ty)                  val m = pTmp i
143                  val t3 = DstIL.Var.new ("_s", ty)                  in
144                      (m, [(m, DstIL.OP(DstOp.Mul ty, [x, coeff]))])
145                    end
146                | eval (i, coeff:r) = let
147                    val (m, stms) = eval(i+1, r)
148                    val s = sTmp i
149                    val m' = mTmp i
150                  val stms =                  val stms =
151                        (t3, DstIL.OP(DstOp.Add ty, [coeff, t2])) ::                        (m', DstIL.OP(DstOp.Mul ty, [x, s])) ::
152                        (t2, DstIL.OP(DstOp.Mul ty, [x, t1])) ::                        (s, DstIL.OP(DstOp.Add ty, [coeff, m])) ::
153                        stms                        stms
154                  in                  in
155                    (t3, stms)                    (m', stms)
156                    end
157              val evalCode = let
158                    val a0::r = coeffs
159                    val (m, stms) = eval (1, r)
160                    in
161                      List.rev ((result, DstIL.OP(DstOp.Add ty, [a0, m]))::stms)
162                  end                  end
 (* FIXME: need to get result into "result" variable! *)  
           val (t, evalCode) = eval coeffs  
163            in            in
164              coeffVecs @ evalCode              coeffVecs @ evalCode
165            end            end
166    
167    (* compute the load address for a given set of voxels indices *)    (* compute the load address for a given set of voxels indices.  For the
168      fun expandVoxelAddress (result, info) = raise Fail "unimplemented"     * operation
169       *
170       *    VoxelAddress<info>(i_1, ..., i_d)
171       *
172       * the address is given by
173       *
174       *    base + szb * (i_1 + N_2 * (i_2 + N_3 * (... + N_d * i_d) ...))
175       *
176       * where
177       *    base    -- base address of the image data
178       *    szb     -- image-element size in bytes
179       *    N_i     -- size of ith axis in elements
180       *)
181        fun expandVoxelAddress (result, info, indices) = raise Fail "unimplemented"
182    
183      fun expandOp (env, y, rator, args) = let      fun expandOp (env, y, rator, args) = let
184            fun assign rator' =            val args' = renameList(env, args)
185                  [(y, DstIL.OP(rator', renameList(env, args)))]            fun assign rator' = [(y, DstIL.OP(rator', args'))]
186            in            in
187              case rator              case rator
188               of SrcOp.Add ty => assign (DstOp.Add ty)               of SrcOp.Add ty => assign (DstOp.Add ty)
# Line 173  Line 217 
217                | SrcOp.RoundToInt d => assign (DstOp.RoundToInt d)                | SrcOp.RoundToInt d => assign (DstOp.RoundToInt d)
218                | SrcOp.CeilToInt d => assign (DstOp.CeilToInt d)                | SrcOp.CeilToInt d => assign (DstOp.CeilToInt d)
219                | SrcOp.FloorToInt d => assign (DstOp.FloorToInt d)                | SrcOp.FloorToInt d => assign (DstOp.FloorToInt d)
220                | SrcOp.VoxelAddress info => expandVoxelAddress (y, info)                | SrcOp.VoxelAddress info => expandVoxelAddress (y, info, args')
221                | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))                | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
222                | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)                | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
223                | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)                | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)
224                | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k, renameList(env, args))                | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k, args')
225                | SrcOp.LoadImage info => assign (DstOp.LoadImage info)                | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
226                | SrcOp.Inside info => assign (DstOp.Inside info)                | SrcOp.Inside info => assign (DstOp.Inside info)
227                | SrcOp.Input(ty, name) => assign (DstOp.Input(ty, name))                | SrcOp.Input(ty, name) => assign (DstOp.Input(ty, name))

Legend:
Removed from v.464  
changed lines
  Added in v.465

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0