This file contains hidden or 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
// Selection algorithm | |
// Partial sorting | |
// http://www.unicode.org/Public/UCD/latest/ucd/CaseFolding.txt | |
switch(rune) { | |
case 0x0041: return 0x0061; // LATIN CAPITAL LETTER A | |
case 0x0042: return 0x0062; // LATIN CAPITAL LETTER B | |
case 0x0043: return 0x0063; // LATIN CAPITAL LETTER C | |
case 0x0044: return 0x0064; // LATIN CAPITAL LETTER D | |
case 0x0045: return 0x0065; // LATIN CAPITAL LETTER E |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#include <time.h> | |
#define szof(a) ((int)sizeof(a)) | |
#define cntof(a) ((int)(sizeof(a) / sizeof((a)[0]))) | |
static void |
This file contains hidden or 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
#include <stdio.h> | |
#include <limits.h> | |
#define byte4_dup(c) (((c)<<24u)|((c)<<16u)|((c)<<8u)|(c)) | |
#define byte4_cmp_lt(x,n) (((x)-~0UL/255u*(n))&~(x)&~0UL/255u*128u) // x>=0; 0<=n<=128 | |
#define byte4_cmp_gt(x,n) (((x)+~0UL/255*(127-(n))|(x))&~0UL/255*128) // x>=0; 0<=n<=127 | |
#ifdef _MSC_VER | |
static int bit_ffs32(unsigned int u) {_BitScanForward(&u, u); return (int)(u);} | |
static int bit_fls32(unsigned int u) {_BitScanBackward(&u, u); return 31-(int)(u);} |
This file contains hidden or 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
/* This algorithm uses as a predicate equality of the first and the last characters from the substring. These two characters are populated in two registers | |
* F and L respectively. Then in each iteration two chunks of strings are loaded. The first chunk (A) is read from offset i (where i is the current offset) | |
* and the second chunk (B) is read from offset i + k - 1, where k is sub string's length. Then we compute a vector expression F == A and B == L. | |
* This step yields a byte vector (or a bit mask), where "true" values denote position of potential substring occurrences. | |
* Finally, just at these positions an exact comparisons of sub strings are performed. | |
* | |
* Example: Let's assume 8-byte registers. We're searching for word "cat", thus: | |
* | |
* F = [ c | c | c | c | c | c | c | c ] | |
* L = [ t | t | t | t | t | t | t | t ] |
This file contains hidden or 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
#define xglue(x, y) x##y | |
#define glue(x, y) xglue(x, y) | |
#define uniqid(name) glue(name, __LINE__) | |
#ifdef _MSC_VER | |
#define swap(a,b) do { decltype((a) + 0) _t = (a); (a) = (b); (b) = _t; } while(0) | |
#else | |
#define swap(a,b) do {__typeof__((a) + 0) _t = (a); (a) = (b); (b) = _t; } while(0) | |
#endif |
This file contains hidden or 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
/** | |
* Advent of Code 2022 | |
* Day 2: Rock Paper Scissors | |
* https://adventofcode.com/2022/day/2 | |
* | |
* http://codercorner.com/Modulo3.htm | |
* (Index+1)%3 = (1 << Index1) & 3 | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> |
This file contains hidden or 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
static void | |
qdiag(float *restrict qres, const float *restrict A) { | |
/* Diagonalizer: http://melax.github.io/diag.html | |
'A' must be a symmetric matrix. | |
Returns quaternion q such that corresponding matrix Q | |
can be used to Diagonalize 'A' | |
Diagonal matrix D = Q * A * Transpose(Q); and A = QT*D*Q | |
The rows of q are the eigenvectors D's diagonal is the eigenvalues | |
As per 'row' convention if float3x3 Q = q.getmatrix(); then v*Q = q*v*conj(q) | |
*/ |
This file contains hidden or 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
/* | |
Enable FMA support: | |
clang -O2 -Wall -mfma | |
*/ | |
static inline float | |
rsqrta(float x) { | |
/* Exact bits: 13.71 */ | |
/* δ+max = 7.459289×10^−5 */ | |
/* δ−max = −7.450387×10^−5 */ | |
union { float f; int i; } v = {x}; |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <limits.h> | |
#include <float.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <stdint.h> | |
#include <stdarg.h> | |
#include <math.h> |
This file contains hidden or 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
static inline int | |
bin_search(const void *vals, int cnt, int siz, void *val, | |
int(*cmp_less)(const void *a, const void *b)) { | |
int half, nleft = cnt; | |
const unsigned char *base = vals; | |
while ((half = (nleft >> 1)) > 0) { | |
const unsigned char *mid = base + half * siz; | |
base = cmp_less(mid, val) ? mid : base; | |
nleft -= half; | |
} |