From 0f04938c25f9e19c8b2d761cc5eb9bd57eacddd6 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Sat, 22 Mar 2014 23:01:06 -0400 Subject: [PATCH] faster way to get next power-of-2 --- include/2d.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/2d.h b/include/2d.h index 6c76ba3..6ccea95 100644 --- a/include/2d.h +++ b/include/2d.h @@ -728,5 +728,21 @@ void gr_bitmap(int x, int y); // Moreover, it is _really_ intended for use with 45 degree angles. void gr_pline_special(vector **pts, int num_pts, int thickness); +// return next power-of-2 +inline int next_pow2(int p) +{ + if (p < 0) + return 0; + + --p; + p |= p >> 1; + p |= p >> 2; + p |= p >> 4; + p |= p >> 8; + p |= p >> 16; + + return p+1; +} + #endif -- 2.39.2