Skip to content

Instantly share code, notes, and snippets.

@fiveoutofnine
fiveoutofnine / 1_example-glyphs.txt
Last active January 13, 2024 03:34
Quick snippets/tutorial on how to condense a font by selecting a subset of characters. First, create a `.txt` file with the characters (as unicode chars) you want included.
U+0039
U+003A
U+002F
U+0023
U+0050
U+0075
U+007A
U+006C
U+0065
U+0041
@fiveoutofnine
fiveoutofnine / PrintIdenticon.s.sol
Last active February 17, 2023 02:24
A script to generate and print identicons.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/Test.sol";
import { LibString } from "solmate/utils/LibString.sol";
/// @title Script to generate and print an identicon.
/// @author fiveoutofnine
/// @dev To run the script, run the following commands:
@fiveoutofnine
fiveoutofnine / Log2.sol
Last active April 11, 2024 12:50
uint64 log_2 using De Bruijn sequences
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
contract Math {
uint256 private constant MAGIC = 0x3F79C6D30BACA89;
uint256 private constant LUT_ONE
= 0x3E040B2811171A2E20262C32341D3A363D0310161F2531393C02152438012300;
uint256 private constant LUT_DIFF
= 0x100FADEFAF10EDEF1E2EBF7E6F0F4DCE40717030E0601E2F90D090C03131422;
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;
library RadixSort {
uint256 internal constant ALPHABET_SIZE_LOG_2 = 7;
uint256 internal constant ALPHABET_SIZE = 1 << ALPHABET_SIZE_LOG_2;
uint256 internal constant MASK = ALPHABET_SIZE - 1;
function sort(uint256[] memory _list) internal pure {
uint256 iterations;
@fiveoutofnine
fiveoutofnine / HeapSort.sol
Created March 16, 2022 04:00
Solidity implementation of max-heapsort
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HeapSort {
function sort(uint256[] calldata _input) external pure returns (uint256[] memory) {
_buildMaxHeap(_input);
uint256 length = _input.length;
unchecked {
for (uint256 i = length - 1; i > 0; --i) {

fiveoutofnine simplified

Some of the bit twiddling in my Solidity implementation of an on-chain engine, fiveoutofnine.sol, can be quite esoteric, so I've recreated some of the logic below. Note that the code below is only logically equivalent (gives same results, but do different things). Functions have similar names, so it should be pretty easy to match up to the functions from Chess.sol and Engine.sol. The main abstraction in these files are using an array for the board (as opposed to 64 bitpacked uint4s) and struct for moves (as opposed to 2 bitpacked uint6s). To further simplify it, it's in a language anyone can read easily: Python.

Chess.py

class Move:
    def __init__(self, from, to):
        self.from = from
        self.to = to