Skip to content

Instantly share code, notes, and snippets.

@rygorous
rygorous / cheb.cpp
Created December 1, 2010 07:38
Chebyshev
__forceinline double chebyshev_ryg(int n, double x)
{
if (n == 0) return 1.0;
if (n == 1) return x;
double t = x, t1 = 1, t2;
// if you don't use a for here, the vc++ loop optimizer doesn't get it!
for (int i=1; i<n; i++) {
t2 = t1;
t1 = t;
@rygorous
rygorous / cheb2.cpp
Created December 1, 2010 07:48
Chebyshev v2
__forceinline double chebyshev_ryg(int n, double x)
{
if (n == 0) return 1.0;
if (n == 1) return x;
double t = x, t1 = 1, t2;
x += x;
for (int i=1; i<n; i++) {
t2 = t1;
t1 = t;
@rygorous
rygorous / icbrt.c
Created December 4, 2010 19:56
Integer Cube Root (64-bit safe version)
ulong icbrt64(ulong x) {
int s;
ulong y, b, bs;
s = 30;
y = 0;
while (s >= 0) {
y += y;
b = 3*y*(y * 1) + 1;
bs = b << s;
@rygorous
rygorous / main.cpp
Created September 18, 2011 05:42
Minitris
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
// ---- platform specific part
enum {
CURSOR_LEFT = 256,
@rygorous
rygorous / quad_align.cpp
Created September 25, 2011 02:27
Does your GPU have pixel alignment requirements for quads?
#include <windows.h>
#include <D3D10.h>
#include <D3DX10.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <algorithm>
// "Does your GPU have pixel alignment requirements for quads?" tester
// Win32 only, D3D10+ only because it's a quick hack.
@rygorous
rygorous / gist:1383867
Created November 21, 2011 20:43
Read implementation
ErrorCode Read(BufferedStream *s, void *buffer, size_t numBytes)
{
U8 *dest = (U8 *)buffer;
while (numBytes) {
if (s->cursor == s->end)
s->Refill(s);
size_t count = min(numBytes, (size_t)(s->end - s->cursor));
memcpy(dest, s->cursor, count);
dest += count;
s->cursor += count;
0000: 0 = a xor a
0001: a and b
0010: a and (not b) = a andc b
0011: b = b and b
0100: (not a) and b = b andc a
0101: a = a and a
0110: a xor b
0111: a or b
1000: (not a) and (not b) = not (a or b) = a nor b
1001: (not a) xor b = a xnor b
@rygorous
rygorous / gist:1440600
Created December 6, 2011 23:36
PPC rlwinm/rlwimi equivalent C code
static inline U32 ppcmask(U32 mb, U32 me)
{
U32 maskmb = ~0u >> mb;
U32 maskme = ~0u << (31 - me);
return (mb <= me) ? maskmb & maskme : maskmb | maskme;
}
static inline U32 rotl32(U32 x, U32 amount)
{
return (x << amount) | (x >> ((32 - amount) & 31));
@rygorous
rygorous / gist:1748043
Created February 5, 2012 21:34
Binary search variants
bool contains(const T& value)
{
#if 0 // variant 0
return std::binary_search(values.begin(), values.end(), value);
#elif 0 // variant 1
size_t l = 0, r = values.size();
while (l < r) {
size_t x = (l + r) >> 1;
if (values[x] < value)
l = x + 1;
This program estimates a rotation under constraints that are naturally stated in angles.
The original version works directly on Euler angles: first an initial solution is
determined using an iterative algorithm, then positions of other local optima are
determined (this is the part that works in angles), then the candidate solutions are
evaluated and passed through the iterative algorithm again to polish the results. Here's
the result of solving a few instances of the problem. Note that the initial solution is
always found as one of the valid solutions (as it should be), but the constrained
minimization part sometimes produces significant amount of error that requires a lot of
extra iterations to correct: