Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
- Remove printf() call on release builds. This is more deterministic
  than printf() modifiers. We only use it printing u64, f32 variables.
- Add unit tests.
  • Loading branch information
e2dk4r committed Oct 26, 2024
1 parent 78ad948 commit 7bd14ad
Show file tree
Hide file tree
Showing 12 changed files with 1,211 additions and 387 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: install packages
run: sudo apt-get install -y --no-install-recommends --no-install-suggests
meson ninja-build
libevdev-dev liburing-dev
liburing-dev
libwayland-dev libwayland-client0 wayland-protocols

- name: configure
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: install packages
run: sudo apt-get install -y --no-install-recommends --no-install-suggests
meson ninja-build
liburing-dev
libwayland-dev libwayland-client0 wayland-protocols

- name: configure
run: meson setup build --buildtype release -Dtest=true

- name: test
run: meson test -C build
15 changes: 15 additions & 0 deletions include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#if GAMEPAD_IDLE_INHIBIT_DEBUG
#define debug_assert(x) \
if (!(x)) { \
__builtin_debugtrap(); \
}
#else
#define debug_assert(x)
#endif

#define runtime_assert(x) \
if (!(x)) { \
__builtin_trap(); \
}
57 changes: 57 additions & 0 deletions include/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "type.h"

/*
* Returns binary logarithm of π‘₯.
*
* ctz(π‘₯) 31^clz(π‘₯) clz(π‘₯)
* uint32 π‘₯ bsf(π‘₯) tzcnt(π‘₯) ffs(π‘₯) bsr(π‘₯) lzcnt(π‘₯)
* 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0
* 0x80000000 31 31 32 31 0
* 0x00000010 4 4 5 4 27
* 0x08000010 4 4 5 27 4
* 0x08000000 27 27 28 27 4
* 0xffffffff 0 0 1 31 0
*
* @param x is a 64-bit integer
* @return number in range 0..63 or undefined if π‘₯ is 0
*
* @note Adapted from https://github.com/jart/cosmopolitan/blob/master/libc/intrin/bsrl.c
* @copyright
* ╒══════════════════════════════════════════════════════════════════════════════╕
* β”‚ Copyright 2023 Justine Alexandra Roberts Tunney β”‚
* β”‚ β”‚
* β”‚ Permission to use, copy, modify, and/or distribute this software for β”‚
* β”‚ any purpose with or without fee is hereby granted, provided that the β”‚
* β”‚ above copyright notice and this permission notice appear in all copies. β”‚
* β”‚ β”‚
* β”‚ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL β”‚
* β”‚ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED β”‚
* β”‚ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE β”‚
* β”‚ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL β”‚
* β”‚ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR β”‚
* β”‚ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER β”‚
* β”‚ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR β”‚
* β”‚ PERFORMANCE OF THIS SOFTWARE. β”‚
* β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/
static inline u8
bsrl(s64 x)
{
static const u8 kDebruijn[64] = {
0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61, 54, 58, 35, 52, 50, 42,
21, 44, 38, 32, 29, 23, 17, 11, 4, 62, 46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43,
31, 22, 10, 45, 25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63,
};

x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return kDebruijn[(x * 0x03f79d71b4cb0a89ull) >> 58];
}
Loading

0 comments on commit 7bd14ad

Please sign in to comment.