Skip to content

Instantly share code, notes, and snippets.

@animetosho
animetosho / unrar-gcc.patch
Last active July 17, 2023 22:26
Quick'n'dirty patch to enable x86/ARM AES acceleration (plus Blake2s/RS16 x86 SIMD) in GCC/Clang for unrar 6.2.6
# apply with `patch -s -p1 < unrar-gcc.patch` in unrar 6.2.6 source directory
--- a/blake2s.cpp
+++ b/blake2s.cpp
@@ -18,7 +18,7 @@
0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL
};
-static const byte blake2s_sigma[10][16] =
+const byte blake2s_sigma[10][16] =
{
"use strict";
// arguments:
// dummy-nntp-server.js [-S] [-d=delay|0] [port/socket|119/563]
const DEBUG = false;
var delay = 0; // in miliseconds
var y;
var junkArticle, junkArticleCrc, junkFileCrc;
try {
@animetosho
animetosho / crc_manip.py
Created January 1, 2023 04:29
CRC32 utility functions in Python
# compute a*b
CRC32_POLYNOMIAL = 0xedb88320
def crc_multiply(a: int, b: int):
prod = 0
while b != 0:
prod ^= -(b>>31) & a
a = (a>>1) ^ (CRC32_POLYNOMIAL & -(a&1))
b = (b<<1) & 0xffffffff
return prod
@animetosho
animetosho / gf-affine-mul8-gen.c
Created November 28, 2022 12:07
Compute Affine table for GF(2^8) multiplication with 0x11d polynomial
#include <stdio.h>
#include <x86intrin.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
__m128i polymask1 = _mm_set1_epi64x(0xff00ffffff000000ULL); // polynomial 0x11d: 0x1d = 0b11101, reverse bits -> 10111000, then replace 1->ff, 0->00
for(int val=0; val<256; val++) {
@animetosho
animetosho / ffmpeg-hevc-version.diff
Created February 16, 2022 08:40
ffmpeg patch to fix up muxing HEVC streams with bad version, instead of erroring out
--- a/libavformat/hevc.c
+++ b/libavformat/hevc.c
@@ -1080,6 +1080,15 @@
return 0;
} else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
/* Not a valid Annex B start code prefix */
+ if (*data == 0) {
+ /* The version field is is ignored by the decoder, so any value should be allowed. As this code relies on magic identifiers, we'll be a bit more strict and only accept '0' as a candidate to be fixed */
+ /* (these instances are outputs likely sourced from older/buggy tools) */
+ /* it might be ideal to do extra checks on the subsequent field (profile/tier) but I can't find open documentation on how they work */
@animetosho
animetosho / gf2p8affineqb-articles.md
Last active February 2, 2024 11:53
A list of articles documenting uses of the GF2P8AFFINE instruction

Unexpected Uses for the Galois Field Affine Transformation Instruction

Intel added the Galois Field instruction set (GFNI) extensions to their Sunny Cove and Tremont cores. What’s particularly interesting is that GFNI is the only new SIMD extension that came with SSE and VEX/AVX encodings (in addition to EVEX/AVX512), to allow it to be supported on all future Intel cores, including those which don’t support AVX512 (such as the Atom line, as well as Celeron/Pentium branded “big” cores).

I suspect GFNI was aimed at accelerating SM4 encryption, however, one of the instructions can be used for many other purposes. The extension includes three instructions, but of particular interest here is the Affine Transformation (GF2P8AFFINEQB), aka bit-matrix multiply, instruction.

There have been various articles which discuss out-of-band

@animetosho
animetosho / galois-field-affine-uses.md
Last active February 6, 2024 00:42
A list of “out-of-band” uses for the GF2P8AFFINEQB instruction I haven’t seen documented elsewhere

Count Leading/Trailing Zero Bits (Byte-wise)

Counting the trailing zero bit count (TZCNT) can be done by isolating the lowest bit, then depositing this into the appropriate locations for the count. The leading zero bit count (LZCNT) can be done by reversing bits, then computing the TZCNT.

__m128i _mm_tzcnt_epi8(__m128i a) {
	// isolate lowest bit
	a = _mm_andnot_si128(_mm_add_epi8(a, _mm_set1_epi8(0xff)), a);
	// convert lowest bit to index