Skip to content

Commit e237ecd

Browse files
committed
Use clz to calculate the nearest power of 2
This approach is more efficient, as it removes the loop and uses the built-in function clz to perform the calculation in constant time.
1 parent 32e5251 commit e237ecd

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/path.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ void twin_path_arc(twin_path_t *path,
210210
if (sides > 1024)
211211
sides = 1024;
212212

213-
int32_t n = 2;
214-
while ((1 << n) < sides)
215-
n++;
213+
/* Calculate the nearest power of 2 that is >= sides. */
214+
int32_t n = (sides > 1) ? (31 - twin_clz(sides - 1) + 1) : 2;
216215

217216
twin_angle_t step = TWIN_ANGLE_360 >> n;
218217
twin_angle_t inc = step;

0 commit comments

Comments
 (0)