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

SCM Repository

[diderot] View of /trunk/src/compiler/c-util/c-names.sml
ViewVC logotype

View of /trunk/src/compiler/c-util/c-names.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2636 - (download) (annotate)
Tue May 27 16:18:36 2014 UTC (5 years, 1 month ago) by jhr
File size: 5671 byte(s)
  Merging changes from vis12 branch (via staging).  The main change is the new
  syntax for inputs (especially image inputs).
(* c-names.sml
 *
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *)

structure CNames =
  struct

    local
      structure Ty = TreeIL.Ty
      structure F = Format
    in

    open RuntimeNames

  (* globals that specify the target characteristics.  These should be initialized
   * when the program object is created.
   *)
    val doublePrecision = ref false
    val longInt = ref false
    val gVectorWid = ref 4
    val gIntTy = ref CLang.int32
    val gRealTy = ref CLang.float
    val gRealSuffix = ref "?"
    val gIntSuffix = ref "?"
    val gIntFormat = ref "?"

  (* initialize globals based on target precision *)
    fun initTargetSpec {double, long} = (
	  doublePrecision := double;
          longInt := long;
	  if double
	    then (
	      gVectorWid := 2;
	      gRealTy := CLang.double;
	      gRealSuffix := "d")
	    else (
	      gVectorWid := 4;
	      gRealTy := CLang.float;
	      gRealSuffix := "f");
          if long
            then (
              gIntTy := CLang.int64;
              gIntSuffix := "l";
	      gIntFormat := "%ld")
            else (
	      gIntTy := CLang.int32;
              gIntSuffix := "i";
	      gIntFormat := "%d"))

    fun addVecSuffix stem n = concat[stem, Int.toString n]
    fun addIVecSuffix stem n = concat[stem, Int.toString n]

    fun addTySuffix (stem, 1) = stem
      | addTySuffix (stem, n) = addVecSuffix stem n

    fun vecTy n = concat["vec", Int.toString n, !gRealSuffix, "_t"]
    fun ivecTy n = concat["vec", Int.toString n, !gIntSuffix, "_t"]
    fun unionTy n = concat["union", Int.toString n, !gRealSuffix, "_t"]
    fun iunionTy n = concat["union", Int.toString n, !gIntSuffix, "_t"]
    fun imageTy n = concat["Diderot_image", Int.toString n, "D_t"]
    fun matTy (n, m) = concat["Diderot_Mat", Int.toString n, "x", Int.toString m, "_t"]
    fun ten3Ty (n, m, l) = concat["Diderot_Ten", Int.toString n, "x", Int.toString m, "x", Int.toString l, "_t"]

    val dynSeqTy = "Diderot_DynSeq_t"

  (* scalar math functions *)
    fun max () = "max"
    fun min () = "min"
    fun fabs () = "fabs"

  (* clamp and lerp *)
    fun clamp n = addTySuffix("clamp", n)
    fun lerp n = addTySuffix("lerp", n)

  (* vector math functions *)
    val loadVec = addVecSuffix "loadVec"
    val mkVec = addVecSuffix "vec"
    val mkIVec = addVecSuffix "ivec"
    val scale = addVecSuffix "scale"
    val truncToInt = addVecSuffix "truncToInt"
    val dot = addVecSuffix "dot"
    fun cross () = addVecSuffix "cross" 3
    val length = addVecSuffix "length"
    val normalize = addVecSuffix "normalize"
    fun vecitof n = concat["vec", Int.toString n, "itor"]
    fun vecftoi n = concat["vec", Int.toString n, "rtoi"]

  (* matrix operations *)
    fun zeroMat (n, m) = concat["zero", Int.toString n, "x", Int.toString m]
    fun identityMat n = concat["identity", Int.toString n, "x", Int.toString n]
    fun trace n = concat["trace", Int.toString n, "x", Int.toString n]
    fun normMat (n, m) = concat["norm", Int.toString n, "x", Int.toString m]
    fun copyMat (n, m) = concat["copy", Int.toString n, "x", Int.toString m]
    fun scaleMat (n, m) = concat["scale", Int.toString n, "x", Int.toString m]
    fun addMat (n, m) = concat["add", Int.toString n, "x", Int.toString m]
    fun subMat (n, m) = concat["sub", Int.toString n, "x", Int.toString m]
    fun mulVecMat (m, n) = F.format "mulVec%dMat%dx%d" [F.INT m, F.INT m, F.INT n]
    fun mulMatVec (m, n) = F.format "mulMat%dx%dVec%d" [F.INT n, F.INT m, F.INT n]
    fun mulMatMat (m, n, p) = F.format "mulMat%dx%dMat%dx%d" [F.INT m, F.INT n, F.INT n, F.INT p]
    fun transposeMat (n, m) = concat["transpose", Int.toString n, "x", Int.toString m]

  (* 3rd-order tensor operations *)
    fun copyTen3 (n, m, l) = F.format "copyTen%dx%dx%d" [F.INT n, F.INT m, F.INT l]
    fun normTen3 (n, m, p) = concat["normTen", Int.toString n, "x", Int.toString m, "x", Int.toString p]
    fun mulVecTen3 (m, n, p) = F.format "mulVec%dTen%dx%dx%d" [F.INT m, F.INT m, F.INT n, F.INT p]
    fun mulTen3Vec (m, n, p) = F.format "mulTen%dx%dx%dVec%d" [F.INT m, F.INT n, F.INT p, F.INT p]

  (* colon-product operators *)
    local
      fun chkDim (d1 : int, d2 : int, d1', d2') =
	    if (d1 = d1') andalso (d2 = d2')
	      then ()
	      else raise Fail "mismatched dimensions for \":\" operator"
    fun fmtName (fmt, l) = F.format fmt (List.map F.INT l)
    in
    fun colonMul ([d1, d2], [d1', d2']) = (
	  chkDim (d1, d2, d1', d2');
	  fmtName ("colonProdMat%dx%dMat%dx%d", [d1, d2, d1', d2']))
      | colonMul ([d1, d2], [d1', d2', d3']) = (
	  chkDim (d1, d2, d1', d2');
	  fmtName ("colonProdMat%dx%dTen%dx%dx%d", [d1, d2, d1', d2', d3']))
      | colonMul ([d1, d2, d3], [d2', d3']) = (
	  chkDim (d2, d3, d2', d3');
	  fmtName ("colonProdTen%dx%dx%dMat%dx%d", [d1, d2, d3, d2', d3']))
      | colonMul ([d1, d2, d3], [d2', d3', d4']) = (
	  chkDim (d2, d3, d2', d3');
	  fmtName ("colonProdTen%dx%dx%dTen%dx%dx%d", [d1, d2, d3, d2', d3', d4']))
      | colonMul _ = raise Fail "unsupported type for \":\" operator"
    end

  (* Status_t symbols *)
    val kActive = "DIDEROT_ACTIVE"
    val kDie = "DIDEROT_DIE"
    val kStabilize = "DIDEROT_STABILIZE"

    val toImageSpace = addVecSuffix "ToImageSpace"
    fun toWorldSpace ty = let
	  val suffix = (case ty
		 of Ty.TensorTy[d] => [Int.toString d]
		  | Ty.TensorTy[m,n] => [Int.toString n, "x", Int.toString n]
		  | Ty.TensorTy[m,n,p] => [Int.toString n, "x", Int.toString n, "x", Int.toString p]
		(* end case *))
	  in
	    concat("ToWorldSpace" :: suffix)
	  end
    val inside = addVecSuffix "Inside"

    end (* local *)
  end

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