From 9889fdb6ea5dfbf5cf7ff6270ed17b8551db7eef Mon Sep 17 00:00:00 2001 From: aoyama Date: Wed, 7 Jun 2023 12:56:22 +0000 Subject: [PATCH] Add portable version and m88k-specific version lb() function, because unfortunately gcc3 does not have __builtin_clz(). ok miod@ otto@ --- lib/libc/stdlib/malloc.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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 */ -- 2.20.1