This file contains 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
//To run Q.js examples: | |
// 1. Open a new browser tab in Chrome and turn on developer toolbar. | |
// 2. Copy/Paste this gist in the console and hit enter to run all the snippets. | |
// Based on the inspiration from samples @ https://github.com/kriskowal/q | |
//////////////////////////////////////////////////////////////////// | |
//////////////////////////////////////////////////////////////////// |
This file contains 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 | |
import unicode | |
const | |
Base = 36 | |
TMin = 1 | |
TMax = 26 | |
Skew = 38 | |
Damp = 700 | |
InitialBias = 72 |
This file contains 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
iterator factorials(k: int): int = | |
## Yields from 0! to k! as an iterator | |
var (n, f) = (0, 1) | |
yield 1 # for 0! | |
while n < k: | |
yield f | |
n = n + 1; f = f * (n + 1) | |
iterator fibonacci(cap: int): int = |
This file contains 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 benchmark as bm | |
proc popcnt(i: int): int = | |
# this doesn't check if the instruction exists! | |
asm """ | |
popcnt %%rbx, %%rax | |
:"=a"(`result`) | |
:"b"(`i`) | |
""" |
This file contains 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
{.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 = |
This file contains 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
## NOTICE: I have no idea how this works on most systems. it will eat your family | |
# these probably don't work right | |
{.emit: """#ifndef __float128 | |
#define __float128 long double | |
#define __int128 long long | |
#endif""".} | |
type float128* {.importc: "__float128", nodecl.} = clongdouble | |
type int128* {.importc: "__int128", nodecl.} = clonglong |
This file contains 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 windows | |
when defined(windows): | |
type HCRYPTPROV* = ptr int # ULONG_PTR typedef | |
const PROV_RSA_FULL* = DWORD(1) | |
const CRYPT_VERIFYCONTEXT* = DWORD(0xf0000000) | |
proc CryptAcquireContextA(phProv: ptr HCRYPTPROV, pszContainer, pszProvider: LPCTSTR, | |
dwProvType, dwFlags: DWORD): WINBOOL {.stdcall, dynlib: "AdvApi32.dll", importc: "CryptAcquireContextA".} | |
proc CryptAcquireContextW(phProv: ptr HCRYPTPROV, pszContainer, pszProvider: LPCTSTR, |