From: aoyama Date: Wed, 7 Jun 2023 12:56:22 +0000 (+0000) Subject: Add portable version and m88k-specific version lb() function, because X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=9889fdb6ea5dfbf5cf7ff6270ed17b8551db7eef;p=openbsd Add portable version and m88k-specific version lb() function, because unfortunately gcc3 does not have __builtin_clz(). ok miod@ otto@ --- diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index c4196c74ed0..04f211c4eff 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.c,v 1.285 2023/06/04 06:58:33 otto Exp $ */ +/* $OpenBSD: malloc.c,v 1.286 2023/06/07 12:56:22 aoyama Exp $ */ /* * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek * Copyright (c) 2012 Matthew Dempsky @@ -988,12 +988,32 @@ err: return NULL; } +#if defined(__GNUC__) && __GNUC__ < 4 +static inline unsigned int +lb(u_int x) +{ +#if defined(__m88k__) + __asm__ __volatile__ ("ff1 %0, %0" : "=r" (x) : "0" (x)); + return x; +#else + /* portable version */ + unsigned int count = 0; + while ((x & (1U << (sizeof(int) * CHAR_BIT - 1))) == 0) { + count++; + x <<= 1; + } + return (sizeof(int) * CHAR_BIT - 1) - count; +#endif +} +#else +/* using built-in function version */ static inline unsigned int lb(u_int x) { /* I need an extension just for integer-length (: */ return (sizeof(int) * CHAR_BIT - 1) - __builtin_clz(x); } +#endif /* https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/ via Tony Finch */