Skip to content

Instantly share code, notes, and snippets.

@Marc-B-Reynolds
Marc-B-Reynolds / tanpi.c
Created May 10, 2020 07:24
hacky scalar branch-free tanpi (not intended for use)
// Public Domain under http://unlicense.org, see link for details.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "xmmintrin.h"
#include "wmmintrin.h"
{% if page.mathjax3 %}
<script>
window.MathJax = {
options: {
menuOptions: {
settings: {
zoom: "None",
zscale: "150%"
}
@Marc-B-Reynolds
Marc-B-Reynolds / approx.sollya
Last active January 27, 2021 22:26
sollya script and output for sincospi in both single and double precision
/* -*- mode: c; -*- */
// create primary range approximations of sin(pi x) and cos(pi x) for x on [-1/4,1/4]
// both for single and double precision
R = [0;1/4];
T = floating;
E = relative;
B = [|24...|];
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include "gdef.h"
#include "swrite.h"
#include "bbattery.h"
#include "scomp.h"
#include "swalk.h"
#include "svaria.h"
@Marc-B-Reynolds
Marc-B-Reynolds / foo.c
Created June 28, 2019 12:55
RU(sqrt(x)) & RD(sqrt(x)) in round-to-nearest
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
inline uint32_t f32_to_bits(float x)
{
uint32_t u; memcpy(&u, &x, 4); return u;
}
@Marc-B-Reynolds
Marc-B-Reynolds / gist:948b550fbcba3726da8bdb680b50e440
Created April 13, 2019 22:37
factor Pi into two singles A*B
(* Mathematica source
same method as the previous gist for double. This validates that the constants giving here are optimal:
https://stackoverflow.com/questions/26692859/best-machine-optimized-polynomial-minimax-approximation-to-arctangent-on-1-1
*)
(* round(Pi,48,RN) *)
s = 221069929750889
(* prime factors of 's' and nearby integers *)
f0 = FactorInteger[s];
@Marc-B-Reynolds
Marc-B-Reynolds / foo.txt
Created April 11, 2019 09:28
factor PI into two doubles A*B
(*
Pi correctly rounded to 106 digits via sollya:
> display=dyadic!;
> round(Pi,106,RN);
63719069007931157819013617823235b-104
*)
s = 63719069007931157819013617823235
(* prime factors of 's' and nearby integers *)
@Marc-B-Reynolds
Marc-B-Reynolds / foo.c
Created March 19, 2019 17:13
tweet dep break-up details
// SEE: https://lemire.me/blog/2019/03/19/the-fastest-conventional-random-number-generator-that-can-pass-big-crush/
__uint128_t g_lehmer64_state;
uint64_t lehmer64() {
uint64_t r = (g_lehmer64_state >> 64);
g_lehmer64_state *= 0xda942042e4dd58b5;
return r;
}
@Marc-B-Reynolds
Marc-B-Reynolds / averot.c
Last active June 27, 2019 12:26
empirical test of average rotation angle of uniform rand rotations
// Public Domain under http://unlicense.org, see link for details.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "xmmintrin.h"
@Marc-B-Reynolds
Marc-B-Reynolds / gist:80ceaf0b4724ad428d86d7c025d0d9ad
Last active December 7, 2016 10:40
2x32-bit xorshift/weyl sequence
// 1 dep-chain (assuming state words in reg) to result.
// DON'T USE ME UNLESS YOUR PARINOID AND
// USING SOMETHING MORE EXPENSIVE (actually inferior
// to some other formulations...period is only 2^32-1)
// Zero is illegal for both state words.
typedef struct { uint32_t s[2]; } rng_state_t;
// smallcrush: passes
// crush: 1 systematic failure = 72 LinearComp, r = 29