Skip to content

Commit

Permalink
Merge pull request #260 from ngtcp2/ispow2
Browse files Browse the repository at this point in the history
ispow2: Better fallback implementation
  • Loading branch information
tatsuhiro-t authored Sep 19, 2024
2 parents 3e14e6f + 8ff5d34 commit ccbc5dd
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lib/nghttp3_ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,23 @@

#include "nghttp3_macro.h"

static int ispow2(size_t n) {
#if defined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_ARM) || (defined(_M_ARM64) && _MSC_VER < 1941))
unsigned int __popcnt(unsigned int x) {
unsigned int c = 0;
for (; x; ++c) {
x &= x - 1;
}
return c;
return n && !(n & (n - 1));
#elif defined(WIN32)
return 1 == __popcnt((unsigned int)n);
#else /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \
(defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */
return 1 == __builtin_popcount((unsigned int)n);
#endif /* !((defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \
(defined(_M_ARM64) && _MSC_VER < 1941))) || defined(WIN32)) */
}
#endif /* defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM) || \
(defined(_M_ARM64) && _MSC_VER < 1941)) */

int nghttp3_ringbuf_init(nghttp3_ringbuf *rb, size_t nmemb, size_t size,
const nghttp3_mem *mem) {
if (nmemb) {
#ifdef WIN32
assert(1 == __popcnt((unsigned int)nmemb));
#else /* !defined(WIN32) */
assert(1 == __builtin_popcount((unsigned int)nmemb));
#endif /* !defined(WIN32) */
assert(ispow2(nmemb));

rb->buf = nghttp3_mem_malloc(mem, nmemb * size);
if (rb->buf == NULL) {
Expand Down Expand Up @@ -128,11 +125,7 @@ int nghttp3_ringbuf_reserve(nghttp3_ringbuf *rb, size_t nmemb) {
return 0;
}

#ifdef WIN32
assert(1 == __popcnt((unsigned int)nmemb));
#else /* !defined(WIN32) */
assert(1 == __builtin_popcount((unsigned int)nmemb));
#endif /* !defined(WIN32) */
assert(ispow2(nmemb));

buf = nghttp3_mem_malloc(rb->mem, nmemb * rb->size);
if (buf == NULL) {
Expand Down

0 comments on commit ccbc5dd

Please sign in to comment.