Skip to content

Instantly share code, notes, and snippets.

View mratsim's full-sized avatar
:shipit:

Mamy Ratsimbazafy mratsim

:shipit:
  • Paris
View GitHub Profile
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".}
@mratsim
mratsim / div3n2n.nim
Last active April 18, 2018 22:41
Debugging Burnikel and ziegler
## From: https://stackoverflow.com/questions/43648718/burnikel-and-ziegler-algorithm-2-not-working
#############################################################################################
import bitops, strformat
proc divmod*(x, y: SomeInteger): tuple[quot, rem: SomeInteger] {.noSideEffect, inline.}=
# hopefully the compiler fuse that in a single op
(x div y, x mod y)
template defineIntConstructor(typ: typedesc, name: untyped{nkIdent}) =
template name*(a: int64): typ = initInt[typ](a)
template name*(a: cstring): typ = initInt[typ](a)
template `+`*(a: typ, b: int{lit}): typ = a + initInt[typ](b)
template `+`*(a: int{lit}, b: typ): typ = initInt[typ](a) + b
template `-`*(a: typ, b: int{lit}): typ = a - initInt[typ](b)
template `-`*(a: int{lit}, b: typ): typ = initInt[typ](a) - b
defineIntConstructor(Int256, i256)
defineIntConstructor(Int512, i512)
@mratsim
mratsim / nimbus_stack_test.cpp
Created April 3, 2018 09:36
Nimbus Stack Test - Flexible array member data with non-trivial destruction
/* Generated by Nim Compiler v0.18.1 */
/* (c) 2018 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: MacOSX, amd64, clang */
/* Command for C compiler:
clang++ -c -w -I/Users/<User>/.nimble/pkgs/ttmath-0.5.0 -I'/Users/<User>/.choosenim/toolchains/nim-#devel/lib' -o /Users/<User>/Programming/Status/nimbus/tests/nimcache/nimbus_stack_test.o /Users/<User>/Programming/Status/nimbus/tests/nimcache/nimbus_stack_test.cpp */
#define NIM_NEW_MANGLING_RULES
#define NIM_INTBITS 64
#include "nimbase.h"
@mratsim
mratsim / divmod_BZ.nim
Created March 26, 2018 19:05
(WIP) integer division by Burnikel and Zeigler in Nim
proc div3n2n(a12, a3, b, b1, b2: BaseUint, n: int): tuple[quot, rem: BaseUint] {.noinit, noSideEffect.}
# Forward declaration
proc div2n1n[T, U: BaseUint](a: T, b: U, n: int): tuple[quot, rem: U] {.noinit, noSideEffect.}=
## Divide a 2n-bit uint with a n-bit uint.
## This is part of the implementation of
## a recursive asymptotically fast division algorithm by Burnikel and Ziegler
if n <= 1000:
return divmod(a, b)
@mratsim
mratsim / undistinct.nim
Created March 26, 2018 08:08
Remove distinct from a complex type
proc replaceSymNodes(ast: NimNode): NimNode =
# For undistinct we need to replace the sym "MpUintImpl" or "uint64"
# By a newIdentNode
proc inspect(node: NimNode): NimNode =
case node.kind:
of nnkSym:
return newIdentNode($node)
of {nnkIdent, nnkEmpty}:
return node
else:
@mratsim
mratsim / bsr.cpp
Created March 25, 2018 23:28 — forked from primenumber/bsr.cpp
Parallel bit scan reverse
// author: prime ([email protected])
// License: MIT License
#include <iostream>
#include <vector>
#include <bitset>
#include <x86intrin.h>
#include <boost/timer/timer.hpp>
inline __m256i bsr_256_8_naive(__m256i x);
inline __m256i bsr_256_8_cvtfloat(__m256i x);
type
MpUint64 = object
lo, hi: uint32
# proc toMpUint64(u: uint64): MpUint64 {.noSideEffect, inline.}=
# cast[MpUint64](u)
# proc asMpUint64(u: uint64): MpUint64 {.noSideEffect, inline.}=
# (cast[ptr MpUint64](unsafeAddr u))[]
@mratsim
mratsim / for_expression.nim
Last active March 23, 2018 16:28
Fighting genSym "undeclared identifier"
import macros
proc getSubType(T: NimNode): NimNode =
# Get the subtype T of an input
result = getTypeInst(T)[1]
proc injectParam(param: NimNode): NimNode =
nnkPragmaExpr.newTree(
newIdentNode($param),
nnkPragma.newTree(ident("inject"))
@mratsim
mratsim / bitsholder.nim
Created March 20, 2018 12:56
static + macro workarounds
import typetraits
type
BitsHolder[bits: static[int]] = object
template `div`[bits: static[int]](bh: typedesc[BitsHolder[bits]], n: static[int]): typedesc =
BitsHolder[bits div n]
type
MpUintImpl*[bh] = object