Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created April 19, 2018 10:54
Show Gist options
  • Select an option

  • Save mratsim/e1879e3d271c15eb328d91971bc502f2 to your computer and use it in GitHub Desktop.

Select an option

Save mratsim/e1879e3d271c15eb328d91971bc502f2 to your computer and use it in GitHub Desktop.
import strutils
from os import DirSep
proc static_cast*[U](a: U, T: typedesc): T {.importcpp: "static_cast<'1>(#)".}
proc reinterpret_cast*[U](a: U, T: typedesc): T {.importcpp: "reinterpret_cast<'1>(#)".}
{.passC: "-std=c++11".}
const RecIntPath = currentSourcePath.rsplit(DirSep, 1)[0]
{.passC: "-I" & RecIntPath.}
const RecInt_Headers = RecIntPath & DirSep & "recint" & DirSep & "recint.h"
type
RecUInt*{.importcpp: "RecInt::ruint<'0>", header: RecInt_Headers.}[K: static[int]] = object
data*: array[(1 shl K) div 8, byte]
# ruint64 = ruint[6]
# ruint128 = ruint[7]
# Constructor
proc `shl`*[K: static[int]](x: RecUint[K], y: SomeInteger): RecUint[K] {.importcpp: "# << #", header: RecInt_Headers.}
proc divmod*(q, r: var RecUint, a, b: RecUint) {.importcpp: "div(@)", header: RecInt_Headers.}
when isMainModule:
var q, r: RecUint[6]
let a = static_cast(1'u64, RecUInt[6]) shl 32
let b = static_cast(3'u64, RecUInt[6])
divmod(q, r, a, b)
echo static_cast(q, uint64)
echo static_cast(r, uint64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment