From 44aa595269516d44f6642890a9211bcdde8b29fd Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 28 Apr 2022 15:52:10 -0700 Subject: libs/din: premultiply din->width * din->height dotgradient() is very hot and needs this result when indexing din->grid[]. Since it doesn't change for a given din instance, just cache the result @ din_new(). --- src/libs/din/din.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/libs') diff --git a/src/libs/din/din.c b/src/libs/din/din.c index ad87642..44c6ec8 100644 --- a/src/libs/din/din.c +++ b/src/libs/din/din.c @@ -9,6 +9,7 @@ typedef struct din_t { int width, height, depth; + int W_x_H; v3f_t grid[]; } din_t; @@ -33,7 +34,7 @@ void din_randomize(din_t *din) r.y = randf(); r.z = randf(); - din->grid[z * din->width * din->height + y * din->width + x] = v3f_normalize(&r); + din->grid[z * din->W_x_H + y * din->width + x] = v3f_normalize(&r); } } } @@ -56,6 +57,9 @@ din_t * din_new(int width, int height, int depth) din->height = height; din->depth = depth; + /* premultiply this since we do it a lot in addressing din->grid[] */ + din->W_x_H = width * height; + din_randomize(din); return din; @@ -76,7 +80,7 @@ static inline float dotgradient(const din_t *din, int x, int y, int z, const v3f assert(y < din->height); assert(z < din->depth); - return v3f_dot(&din->grid[z * din->width * din->height + y * din->width + x], &distance); + return v3f_dot(&din->grid[z * din->W_x_H + y * din->width + x], &distance); } -- cgit v1.2.1