Skip to content

Instantly share code, notes, and snippets.

@sinkingsugar
Created July 2, 2019 05:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinkingsugar/48c17598cdd5e6532b81d4deb458d24c to your computer and use it in GitHub Desktop.
Save sinkingsugar/48c17598cdd5e6532b81d4deb458d24c to your computer and use it in GitHub Desktop.
Nim and Haldie
import nimline
import os
static:
let
halideDist = getenv("HALIDE_DIST")
assert halideDist != "" , "HALIDE_DIST environment variables not set!"
when defined windows:
# for dlls finding ease
static:
let path = getenv("PATH")
putenv("PATH", path & ";" & getenv("HALIDE_DIST") & "/bin")
let
includes {.compileTime.} = "-I" & getenv("HALIDE_DIST") & "/include"
lib {.compileTime, used.} = getenv("HALIDE_DIST") & "/lib/libHalide.a" & " -lz"
libWin {.compileTime, used.} = getenv("HALIDE_DIST") & "/lib/libHalide.a" & " -lz -lOle32 -luuid"
{.passC: includes.}
when not defined windows:
{.passL: lib.}
else:
{.passL: libWin.}
defineCppType(HalideInt32Buffer, "Halide::Buffer<int32_t>", "Halide.h")
defineCppType(HalideExpr, "Halide::Expr", "Halide.h")
defineCppType(HalideFunc, "Halide::Func", "Halide.h")
defineCppType(HalideFuncRef, "Halide::FuncRef", "Halide.h")
defineCppType(HalideVar, "Halide::Var", "Halide.h")
defineCppType(HalideExprVector, "std::vector<Halide::Expr>", "<vector>")
proc call*(f: HalideFunc; args: varargs[HalideVar]): HalideFuncRef {.inline, noinit.} =
var vargs: HalideExprVector
for arg in args:
vargs.push_back(arg).to(void)
f.invoke("operator()", vargs).to(HalideFuncRef)
proc call*(b: HalideInt32Buffer): int32 {.header:"Halide.h", importcpp: "#(@)", varargs.}
proc main() =
var
x, y: HalideVar
gradient: HalideFunc
e: HalideExpr = x.toCpp + y.toCpp
var
p = gradient.call(x, y)
p = e.toCpp
var res = gradient.realize(800, 600).to(HalideInt32Buffer)
for y in 0..<res.height().to(int):
for x in 0..<res.width().to(int):
var
computed = res.call(x, y)
actual = x + y
assert computed.int == actual
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment