Source

ocaml-llvm-phantom / examples / sqrt.ml

Full commit
(* Declare sqrt in libm, and use it *)

open Llvm_phantom.Std

let context = Llvm.global_context () 

include Create(struct let context = context end)
open Type
(* open Value *)

include CreateModule(struct
  let name = "mymodule"
  let opt = true
end)

(* Declare "sqrt" with its type double(double) *)
let sqrt = External.declare "sqrt" double (Phantom.c1 double)

let () = Value.dump sqrt

let run_sqrt = ExecutionEngine.run_function1 sqrt

let run_sqrt_ocaml x = Genvalue.as_float double (run_sqrt (Genvalue.of_float double x))

let () = Printf.eprintf "sqrt(3.0)=%f\n" (run_sqrt_ocaml 3.0)