Last active
August 29, 2015 14:07
-
-
Save PhilipWitte/653d26e83d312293cae8 to your computer and use it in GitHub Desktop.
Nim xmmintrin wrap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StrUtils | |
# --- | |
type Vec4* {.incompleteStruct, importc:"__m128", header:"<xmmintrin.h>".} = object | |
type Vec4Array = array[4, float32] | |
# --- | |
proc newVec4 (w, z, y, x:float32): Vec4 {.importc:"_mm_set_ps".} | |
proc new*(T:type Vec4, x, y, z, w:float32): Vec4 {.inline, noInit.} = | |
newVec4(w, z, y, x) | |
# --- | |
template x* (v:Vec4): float32 = cast[Vec4Array](v)[0] | |
template y* (v:Vec4): float32 = cast[Vec4Array](v)[1] | |
template z* (v:Vec4): float32 = cast[Vec4Array](v)[2] | |
template w* (v:Vec4): float32 = cast[Vec4Array](v)[3] | |
# --- | |
proc sqrt* (v:Vec4): Vec4 {.importc: "_mm_sqrt_ps".} | |
proc rsqrt* (v:Vec4): Vec4 {.importc: "_mm_rsqrt_ps".} | |
proc rcp* (v:Vec4): Vec4 {.importc: "_mm_rcp_ps".} | |
proc min* (v:Vec4): Vec4 {.importc: "_mm_min_ps".} | |
proc max* (v:Vec4): Vec4 {.importc: "_mm_max_ps".} | |
proc `+`* (a, b:Vec4): Vec4 {.importc:"_mm_add_ps".} | |
proc `-`* (a, b:Vec4): Vec4 {.importc:"_mm_sub_ps".} | |
proc `*`* (a, b:Vec4): Vec4 {.importc:"_mm_mul_ps".} | |
proc `/`* (a, b:Vec4): Vec4 {.importc:"_mm_div_ps".} | |
proc `+=`* (v:var Vec4, b:Vec4) {.inline.} = v = v + b | |
proc `-=`* (v:var Vec4, b:Vec4) {.inline.} = v = v - b | |
proc `*=`* (v:var Vec4, b:Vec4) {.inline.} = v = v * b | |
proc `/=`* (v:var Vec4, b:Vec4) {.inline.} = v = v / b | |
# --- | |
proc `$`*(v:Vec4): string {.noInit.} = | |
return | |
v.x.formatFloat(FFDecimal, 3) & ", " & | |
v.y.formatFloat(FFDecimal, 3) & ", " & | |
v.z.formatFloat(FFDecimal, 3) & ", " & | |
v.w.formatFloat(FFDecimal, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment