From ab3c03231376fe459744d852893996a78f7f37c6 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sat, 25 Jan 2025 02:28:40 +0300 Subject: [PATCH] /apps/examples/circle-raster: acc variant --- apps/examples/circle-raster/game.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/apps/examples/circle-raster/game.c b/apps/examples/circle-raster/game.c index 32e3935..2f2d6c7 100644 --- a/apps/examples/circle-raster/game.c +++ b/apps/examples/circle-raster/game.c @@ -56,6 +56,26 @@ static void benchmark(struct state *state) { profile_end("int32_t"); + profile_start("int32_t acc"); + + for (int i = 0; i < 1000; ++i) { + int32_t const rsi = (int32_t)state->r * (int32_t)state->r; + int32_t acc = 1; + for (int32_t iy = (int32_t)state->r - 1; iy >= 0; --iy) { + while (acc * acc < rsi - iy * iy) acc++; + for (int32_t ix = -acc; ix < acc; ++ix) { + /* lower portion */ + x = (float)ix; + y = (float)iy; + /* upper portion */ + x = (float)ix; + y = (float)-iy - 1; + } + } + } + + profile_end("int32_t acc"); + (void)x; (void)y; } @@ -82,10 +102,14 @@ void game_tick(void) { state->r -= 1; int32_t const rsi = (int32_t)state->r * (int32_t)state->r; - for (int32_t iy = -(int32_t)state->r; iy <= (int32_t)state->r - 1; ++iy) { - int32_t const dx = ceil_sqrt(rsi - (iy + (iy <= 0)) * (iy + (iy <= 0))); - for (int32_t ix = -dx; ix < dx; ++ix) { + int32_t acc = 1; + for (int32_t iy = (int32_t)state->r - 1; iy >= 0; --iy) { + while (acc * acc < rsi - iy * iy) acc++; + for (int32_t ix = -acc; ix < acc; ++ix) { + /* lower portion */ draw_box((Rect){mouse_snap.x + (float)ix * 8, mouse_snap.y + (float)iy * 8, 8, 8}, 1, (Color){125, 125, 0, 255}); + /* upper portion */ + draw_box((Rect){mouse_snap.x + (float)ix * 8, mouse_snap.y + (float)(-iy - 1) * 8, 8, 8}, 1, (Color){125, 125, 0, 255}); } }