Skip to content

Instantly share code, notes, and snippets.

@apense
Created June 6, 2015 00:45
Show Gist options
  • Save apense/5fb468db7289b19474d2 to your computer and use it in GitHub Desktop.
Save apense/5fb468db7289b19474d2 to your computer and use it in GitHub Desktop.
FMA intrinsics with Nim
{.passC: "-march=native".}
type m128d {.importc: "__m128d", nodecl.} = object
proc fma(x,y,z: float32): float32 {.importc: "fma", header: "<math.h>".}
proc fma(x,y,z: float64): float64 {.importc: "fmaf", header: "<math.h>".}
proc fmadd(x,y,z: m128d): m128d {.importc: "_mm_fmadd_sd", header: "<intrin.h>".}
proc loadpd(x: pointer): m128d {.importc: "_mm_load_pd", header: "<intrin.h>".}
converter tom128d(f: float): m128d =
{.emit: "result = _mm_load_pd(&`f`);".}
proc makem128d(s: float): m128d =
{.emit: "result = _mm_load_pd(&`s`);".}
converter toFloat(m: m128d): float =
{.emit: "result = _mm_cvtsd_f64(`m`);".}
proc `$`(m: m128d): string =
result = $toFloat(m)
var
o: m128d = 1.0
t: m128d = 2.0
t2: m128d = 3.0
for i in 0..<1000:
echo (fmadd(i.toFloat,2.0,3.0))
@apense
Copy link
Author

apense commented Jun 6, 2015

this is not a good way to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment