diff options
author | aoyama <> | 2023-06-07 12:56:22 +0000 |
---|---|---|
committer | aoyama <> | 2023-06-07 12:56:22 +0000 |
commit | d323186428a08aba14848a7b6be0daadfe582ffd (patch) | |
tree | 84c1121857bdc246ca09c9143241a9e3ac2d26fe | |
parent | 3934c1776874150deec48a5faa9b913c03ac04a3 (diff) | |
download | openbsd-d323186428a08aba14848a7b6be0daadfe582ffd.tar.gz openbsd-d323186428a08aba14848a7b6be0daadfe582ffd.tar.bz2 openbsd-d323186428a08aba14848a7b6be0daadfe582ffd.zip |
Add portable version and m88k-specific version lb() function, because
unfortunately gcc3 does not have __builtin_clz().
ok miod@ otto@
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index c4196c74ed..04f211c4ef 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.285 2023/06/04 06:58:33 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.286 2023/06/07 12:56:22 aoyama Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -988,12 +988,32 @@ err: | |||
988 | return NULL; | 988 | return NULL; |
989 | } | 989 | } |
990 | 990 | ||
991 | #if defined(__GNUC__) && __GNUC__ < 4 | ||
992 | static inline unsigned int | ||
993 | lb(u_int x) | ||
994 | { | ||
995 | #if defined(__m88k__) | ||
996 | __asm__ __volatile__ ("ff1 %0, %0" : "=r" (x) : "0" (x)); | ||
997 | return x; | ||
998 | #else | ||
999 | /* portable version */ | ||
1000 | unsigned int count = 0; | ||
1001 | while ((x & (1U << (sizeof(int) * CHAR_BIT - 1))) == 0) { | ||
1002 | count++; | ||
1003 | x <<= 1; | ||
1004 | } | ||
1005 | return (sizeof(int) * CHAR_BIT - 1) - count; | ||
1006 | #endif | ||
1007 | } | ||
1008 | #else | ||
1009 | /* using built-in function version */ | ||
991 | static inline unsigned int | 1010 | static inline unsigned int |
992 | lb(u_int x) | 1011 | lb(u_int x) |
993 | { | 1012 | { |
994 | /* I need an extension just for integer-length (: */ | 1013 | /* I need an extension just for integer-length (: */ |
995 | return (sizeof(int) * CHAR_BIT - 1) - __builtin_clz(x); | 1014 | return (sizeof(int) * CHAR_BIT - 1) - __builtin_clz(x); |
996 | } | 1015 | } |
1016 | #endif | ||
997 | 1017 | ||
998 | /* https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/ | 1018 | /* https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/ |
999 | via Tony Finch */ | 1019 | via Tony Finch */ |