C/C++: List all #define macros

I need to target a specific platform with some code I'm writing. You can list all of the #define macros on a given platform with these commands:

gcc -dM -E - < /dev/null

clang -dM -E - < /dev/null

In this big list of macros you should find something to help you target a given platform.

Leave A Reply

FFMPEG: Only output the encoding stats for quieter scripting

FFMPEG is a great tool, but it is very chatty. If you want to encode a file you'll get about 40 lines of headers and track information, most of which you probably will not read. For scripting purposes you probably only want to see the statistics. If you start FFMPEG, hide the banner, and set the loglevel low you can output only the stats you need

ffmpeg -y -hide_banner -loglevel quiet -stats ...
Leave A Reply

Perl: Using sha256 to hash strings into integers

I need 64bit integers to seed some PRNG work I'm doing. I found out that you can easily create four 64bit numbers from any string by using SHA256.

use Digest::SHA;

my $input = $ARGV[0] || int(rand() * 1000000);

# Get raw bytes and convert to an array of uint64_t
my $hash = Digest::SHA::sha256($input);
my @nums = unpack("Q*", $hash);

print "SHA256 Hashing '$input' to: " . join(", ", @nums) . "\n";

Note: We are using sha256 here, not sha256_hex because we need raw bytes.

Leave A Reply

Komihash is pretty amazing and simple hashing algorithm

I need a simple way to hash strings into 64bit integers so I started some research. Initially I was focusing on xxHash but it's slightly harder to implement than other newer hashes. I ended up landing on the amazing Komihash which bills itself as: "a very fast 64-bit hash function, mainly designed for hash-table, hash-map, and bloom-filter uses".

Komihash is available as a single Komihash.h file and is extremely easy to implement into existing code.

#include <stdio.h>
#include <stdlib.h>
#include "komihash.h"

int main(int argc, char *argv[]) {
    int seed        = 0;
    const char *buf = "Hello world";

    uint64_t hash_num = komihash(buf, strlen(buf), seed);

    printf("Komihash: %s = %llu\n", buf, hash_num); // 3745467240760726046
}

As a bonus it also comes with a PRNG which is equally simple to implement:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "komihash.h"

int main(int argc, char *argv[]) {
    uint64_t seed1 = time(NULL);
    uint64_t seed2 = (seed1 << 32);

    for (int i = 0; i < 5; i++) {
        uint64_t rand64 = komirand(&seed1, &seed2);
        printf("Komirand: %llu\n", rand64);
    }
}

In fact, I liked it so much I ported it to Perl in Crypt::Komihash.

Leave A Reply

Perl: Warning about non-portable numbers

If you get a Perl warning about non-portable numbers like this:

Hexadecimal number > 0xffffffff non-portable at /tmp/x.pl line 19.

It means that you are using a hex number greater than 2^32. I dunno why this is a warning when everything is 64bit these days, but it is. There have been a couple of times I needed this in my code so you can disable these warnings with:

no warnings 'portable';
Leave A Reply

Books of 2025

List of books I read in 2025. Also see the list of 2024. The date indicated denotes the date I started reading the book.

2025-01-06 - Mistborn by Brandon Sanderson - 643 pages
2025-01-17 - Ender's Game by Orson Scott Card - 324 pages
2025-01-22 - Thinner by Stephen King - 318 pages

Leave A Reply

C: Nanoseconds since Unix epoch

I needed a function to use as a simple seed for srand(). Unixtime in nanoseconds changes very frequently and serves as a semi-decent random seed.

#include <time.h> // for clock_gettime()

// Nanoseconds since Unix epoch
uint64_t nanos() {
    struct timespec ts;

    // int8_t ok = clock_gettime(CLOCK_MONOTONIC, &ts); // Uptime
    int8_t ok = clock_gettime(CLOCK_REALTIME, &ts);  // Since epoch

    if (ok != 0) {
        return 0; // Return 0 on failure (you can handle this differently)
    }

    // Calculate nanoseconds
    uint64_t ret = (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;

    return ret;
}

See also: Microseconds since epoch in Perl

Leave A Reply

Git: Show all changes to a given file

If you want to view the history of all the changes you've made to a specific file in Git you can use the log command in Git. This will show you all the commits that touched that file, and the associated commit message.

git log README.md

If you want to see a diff/patch for each commit you can run:

git log --follow --patch README

This can be very useful to track down when a specific piece of code went into (or was taken out of) a file. Be careful as this command only shows you the diff/patch for the file you request. If the commit contains changes to other files you will not see those unless you view the full commit.

Leave A Reply

Perl: Get microseconds since Unix epoch

I need to get system uptime with a higher resolution that just one second. This is the function I came up with to return uptime in microseconds.

sub micros {
    require Time::HiRes;

    my @p   = Time::HiRes::gettimeofday();
    my $ret = ($p[0] * 1000000) + $p[1];

    return $ret;
}
Leave A Reply

How big is 64bits

Older CPUs are 32bits and could address 4.2 gigabytes of memory. New CPUs are 64bits and can address 18.4 exabytes of memory.

32bits = 4,294,967,296
64bits = 18,446,744,073,709,551,616

Leave A Reply

PHP: Run a command and capture STDOUT and STDERR separately

I need to run a shell command in PHP and capture STDOUT and STDERR separately. This function lets you run a command and returns a hash with the various components and outputs.

function run_cmd($cmd) {
    $start   = hrtime(true);
    $cmd     = escapeshellcmd($cmd);
    $process = proc_open($cmd, [
        1 => ['pipe', 'w'], // STDOUT
        2 => ['pipe', 'w'], // STDERR
    ], $pipes);

    if (!is_resource($process)) { return []; }

    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);
    $exit   = proc_close($process);

    $ret = [
        'exit_code' => $exit,
        'stdout'    => trim($stdout),
        'stderr'    => trim($stderr),
        'cmd'       => $cmd,
        'exec_ms'   => (hrtime(true) - $start) / 1000000,
    ];

    return $ret;
}
Leave A Reply

Linux: Disable DNS cache in systemd

New Linux distributions are enabling local DNS caching. For most users this is a sane default, but as a system administrator this can sometimes get in the way. You can check if your local cache is running and see the statistics with:

resolvectl statistics

Which will tell you how active your cache is, what the hit/miss ratio is, and information about DNSSEC. If you need to clear your local cache you can run:

resolvectl flush-caches

Doing this repeatedly can get cumbersome if you are testing remote DNS frequently. If you want to disable local caching all together you can run:

systemctl disable systemd-resolved.service --now

Remember to update your /etc/resolv.conf to point at the correct DNS server after you're done.

Leave A Reply

Using Arduino interrupts to read RPM from a fan

I need to read the RPMs of some 12v PC fans so I wrote up a quick sketch to have an ESP32 monitor and log the RPM for me. You need to use an external interrupt that triggers every time the fan rotates (it triggers 2x per full rotation). I was having crashes before I added the ICACHE_RAM_ATTR parameter to the function. ICACHE_RAM_ATTR tell Arduino to store this function in RAM instead of on the flash. The logic here is that your interrupt function will probably be getting called a lot so it needs to be fast.

Connect the yellow RPM wire from your fan to pin 4, and the ground wire from the fan to the ground of the Arduino. Without the ground wire connected you will now have a shared ground between the Arduino and the fan and the interrupt will not fire.

const uint8_t tachoPin    = 4;
volatile uint32_t counter = 0;

ICACHE_RAM_ATTR void countPulses() {
    counter++;
}

void setup() {
    pinMode(tachoPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(tachoPin), countPulses, FALLING);
    Serial.begin(115200);
}

void loop() {
    delay(1000);
    Serial.printf("%u fan revolutions in the last second\n", counter / 2);

    counter = 0;
}

Note: This code will work on an Arduino Uno without the ICACHE_RAM_ATTR which can be confusing.

Note: Using the internal pull-up resistor on the ESP32 means you can connect the yellow fan wire directly to the MCU. On an Arduino Uno an external pull-up resistor is required.

Leave A Reply

Hashing intgers to get 64bit output

I'm working with a 32bit PRNG and I'd like to get a 64bit integer out of it. You can take two random 32bit integers and combine them into a 64bit one this way:

uint64_t high = rand32();
uint64_t low  = rand32();

uint64_t num64 = (high << 32) | low;

Someone on Reddit suggested using this cool hash to perform a similar task to "upgrade" a 32bit number into a 64bit one by hashing it:

// Borrowed from: https://elixir.bootlin.com/linux/v6.11.5/source/include/linux/hash.h
static inline uint64_t hash64(uint64_t val, unsigned bits) {
        return (val * 0x61c8864680b583ebull) >> (64 - bits);
}

I needed this functionality in Perl so got some help from Reddit and wrote it up:

sub hash64 {
    my ($val, $bits)   = @_;
    my $magic_constant = 7046029254386353131;

    use integer; # This forces integer math and overflow
    my $result = ($val * $magic_constant) >> (64 - $bits);

    # Convert from signed int to unsigned
    if ($result < 0) {
        no integer;
        $result += 18446744073709551615; # 2**64 - 1
        $result += 1;
    }

    return $result;
}
Leave A Reply

Perl: Several ways to generate Unicode

Once you find a Unicode code point you can put it into a Perl string in several ways:

my $thumbs_up = "";

$thumbs_up = "\x{1F44D}";
$thumbs_up = "\N{U+1F44D}";
$thumbs_up = chr(0x1F44D);
$thumbs_up = pack("U", 0x1F44D);

# Make sure STDOUT is set to accept UTF8
binmode(STDOUT, ":utf8");

print $thumbs_up x 2 . "\n";
Leave A Reply