From 21ff9c8ca7ac39afd4f5f33c08cc865773d7dae0 Mon Sep 17 00:00:00 2001 From: Logan Gunthorpe Date: Wed, 11 Mar 2015 08:38:30 -0700 Subject: [PATCH] Fix for next highest power of 2 function. --- src/readthrd.c | 11 +++++++++-- src/writethrd.c | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/readthrd.c b/src/readthrd.c index 13d64e1..d167701 100644 --- a/src/readthrd.c +++ b/src/readthrd.c @@ -180,10 +180,17 @@ static ssize_t check_file(const char *f) return ret; } - static int next_power_of_2(int x) { - return (x & -x) + x; + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + + return x; } struct readthrd *readthrd_start(const char *fpath, diff --git a/src/writethrd.c b/src/writethrd.c index 2f5c147..ab7b6c2 100644 --- a/src/writethrd.c +++ b/src/writethrd.c @@ -200,7 +200,15 @@ static int check_file(const char *f, int truncate) static int next_power_of_2(int x) { - return (x & -x) + x; + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + x++; + + return x; } struct writethrd *writethrd_start(const char *fpath, const char *swap_phrase,