2017-03-03 17:17:51 +00:00
|
|
|
/* count-leading-zeros.h -- counts the number of leading 0 bits in a word.
|
2024-01-02 01:47:10 +00:00
|
|
|
Copyright (C) 2012-2024 Free Software Foundation, Inc.
|
2017-03-03 17:17:51 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
This file is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
License, or (at your option) any later version.
|
2017-03-03 17:17:51 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
This file is distributed in the hope that it will be useful,
|
2017-03-03 17:17:51 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2017-03-03 17:17:51 +00:00
|
|
|
|
Update from Gnulib
Make the following changes by hand, and run 'admin/merge-gnulib'.
* .gitignore: Add lib/malloc/*.gl.h.
* admin/merge-gnulib: Copy lib/af_alg.h and lib/save-cwd.h
directly from Gnulib, without worrying about Gnulib modules,
as these files are special cases.
(AVOIDED_MODULES): Remove malloc-posix.
* lib/malloc.c, lib/realloc.c, m4/malloc.m4, m4/realloc.m4:
* m4/year2038.m4: New files, copied from Gnulib.
* lib/malloca.c, lib/malloca.h:
* m4/close-stream.m4, m4/glibc21.m4, m4/malloca.m4:
Remove. These are either no longer present in Gnulib, or are no
longer needed by modules that Emacs uses.
* oldXMenu/AddPane.c, oldXmenu/Addsel.c: Include XmenuInt.h first;
needed for new Gnulib.
* src/xmenu.c: Call emacs_abort, not abort.
2021-10-04 19:11:39 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2017-09-13 09:07:03 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2017-03-03 17:17:51 +00:00
|
|
|
|
|
|
|
/* Written by Eric Blake. */
|
|
|
|
|
|
|
|
#ifndef COUNT_LEADING_ZEROS_H
|
|
|
|
#define COUNT_LEADING_ZEROS_H 1
|
|
|
|
|
2023-05-15 01:51:22 +00:00
|
|
|
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE. */
|
|
|
|
#if !_GL_CONFIG_H_INCLUDED
|
|
|
|
#error "Please include config.h first."
|
|
|
|
#endif
|
|
|
|
|
2017-03-03 17:17:51 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
_GL_INLINE_HEADER_BEGIN
|
|
|
|
#ifndef COUNT_LEADING_ZEROS_INLINE
|
|
|
|
# define COUNT_LEADING_ZEROS_INLINE _GL_INLINE
|
|
|
|
#endif
|
|
|
|
|
2020-02-22 18:33:14 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-03-03 17:17:51 +00:00
|
|
|
/* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
|
|
|
|
expand to code that computes the number of leading zeros of the local
|
|
|
|
variable 'x' of type TYPE (an unsigned integer type) and return it
|
|
|
|
from the current function. */
|
Update from Gnulib
This incorporates:
2020-08-06 libgmp: add <gmp/gmp.h> support
2020-08-06 Consider that clang defines __OPTIMIZE__ like GCC does
2020-08-06 Use __builtin_expect with clang everywhere
2020-08-05 Use __builtin_clz{,l,ll} with clang, also on Windows
2020-08-05 Use __builtin_ctz{,l,ll} and __builtin_ffs{,l,ll} with clang
2020-07-31 _GL_CMP: Improve documentation
2020-07-30 alloca, largefile: sync with Autoconf master
* lib/c++defs.h, lib/cdefs.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, m4/alloca.m4, m4/gnulib-common.m4:
* m4/largefile.m4, m4/libgmp.m4:
Copy from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-08-06 22:24:47 +00:00
|
|
|
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) \
|
|
|
|
|| (__clang_major__ >= 4)
|
2017-03-03 17:17:51 +00:00
|
|
|
# define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
|
|
|
|
return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
|
|
|
|
#elif _MSC_VER
|
2022-09-28 07:29:09 +00:00
|
|
|
# pragma intrinsic (_BitScanReverse)
|
|
|
|
# if defined _M_X64
|
|
|
|
# pragma intrinsic (_BitScanReverse64)
|
|
|
|
# endif
|
2017-03-03 17:17:51 +00:00
|
|
|
# define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
unsigned long result; \
|
2022-09-28 07:29:09 +00:00
|
|
|
if (MSC_BUILTIN (&result, x)) \
|
|
|
|
return CHAR_BIT * sizeof x - 1 - result; \
|
|
|
|
return CHAR_BIT * sizeof x; \
|
2017-03-03 17:17:51 +00:00
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
#else
|
|
|
|
# define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
int count; \
|
|
|
|
unsigned int leading_32; \
|
|
|
|
if (! x) \
|
|
|
|
return CHAR_BIT * sizeof x; \
|
|
|
|
for (count = 0; \
|
|
|
|
(leading_32 = ((x >> (sizeof (TYPE) * CHAR_BIT - 32)) \
|
|
|
|
& 0xffffffffU), \
|
|
|
|
count < CHAR_BIT * sizeof x - 32 && !leading_32); \
|
|
|
|
count += 32) \
|
|
|
|
x = x << 31 << 1; \
|
|
|
|
return count + count_leading_zeros_32 (leading_32); \
|
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
|
|
|
|
/* Compute and return the number of leading zeros in X,
|
|
|
|
where 0 < X < 2**32. */
|
|
|
|
COUNT_LEADING_ZEROS_INLINE int
|
|
|
|
count_leading_zeros_32 (unsigned int x)
|
|
|
|
{
|
Merge from Gnulib
This incorporates:
2017-09-16 manywarnings: port to GCC on 64-bit MS-Windows
2017-09-13 all: Replace many more http URLs by https URLs
* build-aux/config.guess, build-aux/config.sub:
* build-aux/gitlog-to-changelog, doc/misc/texinfo.tex:
* lib/allocator.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, lib/dup2.c, lib/filevercmp.c:
* lib/fstatat.c, lib/fsync.c, lib/ftoastr.c, lib/ftoastr.h:
* lib/intprops.h, lib/signal.in.h, lib/stdio-impl.h, lib/stdio.in.h:
* lib/unistd.in.h, lib/utimens.c, m4/alloca.m4, m4/extern-inline.m4:
* m4/fstatat.m4, m4/gnulib-common.m4, m4/manywarnings.m4:
* m4/std-gnu11.m4, m4/sys_types_h.m4, m4/vararrays.m4:
Copy from Gnulib.
* lib/gnulib.mk.in: Regenerate.
2017-09-17 19:56:00 +00:00
|
|
|
/* <https://github.com/gibsjose/BitHacks>
|
Update from Gnulib
This incorporates:
2019-09-22 Update some URLs
2019-09-15 fcntl-h: fix compilation error of creat.c on MSVC
2019-09-15 creat: new module
2019-09-15 access: new module
2019-09-09 Add option to assume best, not worst, when cross-compiling.
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/careadlinkat.c, lib/careadlinkat.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, lib/diffseq.h, lib/fcntl.in.h:
* lib/ftoastr.c, lib/get-permissions.c:
* lib/ieee754.in.h, lib/inttypes.in.h, lib/mktime.c, lib/open.c:
* lib/pathmax.h, lib/pipe2.c, lib/stddef.in.h, lib/stdint.in.h:
* lib/stdlib.in.h, lib/str-two-way.h, lib/string.in.h, lib/time.in.h:
* lib/timegm.c, lib/unistd.in.h, m4/canonicalize.m4:
* m4/extern-inline.m4, m4/fcntl_h.m4, m4/fdopendir.m4:
* m4/getgroups.m4, m4/getopt.m4, m4/gettimeofday.m4:
* m4/gnulib-common.m4, m4/largefile.m4:
* m4/lstat.m4, m4/memmem.m4, m4/mktime.m4, m4/nocrash.m4, m4/open.m4:
* m4/pselect.m4, m4/putenv.m4, m4/readlink.m4, m4/regex.m4:
* m4/symlink.m4, m4/unistd_h.m4, m4/utimens.m4, m4/utimes.m4:
Copy from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
* m4/open-slash.m4: New file, copied from Gnulib.
2019-09-23 06:50:59 +00:00
|
|
|
<https://www.fit.vutbr.cz/~ibarina/pub/bithacks.pdf> */
|
2017-03-03 17:17:51 +00:00
|
|
|
static const char de_Bruijn_lookup[32] = {
|
|
|
|
31, 22, 30, 21, 18, 10, 29, 2, 20, 17, 15, 13, 9, 6, 28, 1,
|
|
|
|
23, 19, 11, 3, 16, 14, 7, 24, 12, 4, 8, 25, 5, 26, 27, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
return de_Bruijn_lookup[((x * 0x07c4acddU) & 0xffffffffU) >> 27];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Compute and return the number of leading zeros in X. */
|
|
|
|
COUNT_LEADING_ZEROS_INLINE int
|
|
|
|
count_leading_zeros (unsigned int x)
|
|
|
|
{
|
|
|
|
COUNT_LEADING_ZEROS (__builtin_clz, _BitScanReverse, unsigned int);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute and return the number of leading zeros in X. */
|
|
|
|
COUNT_LEADING_ZEROS_INLINE int
|
|
|
|
count_leading_zeros_l (unsigned long int x)
|
|
|
|
{
|
|
|
|
COUNT_LEADING_ZEROS (__builtin_clzl, _BitScanReverse, unsigned long int);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute and return the number of leading zeros in X. */
|
|
|
|
COUNT_LEADING_ZEROS_INLINE int
|
|
|
|
count_leading_zeros_ll (unsigned long long int x)
|
|
|
|
{
|
2022-09-28 07:29:09 +00:00
|
|
|
#if (defined _MSC_VER && !defined __clang__) && !defined _M_X64
|
|
|
|
/* 32-bit MSVC does not have _BitScanReverse64, only _BitScanReverse. */
|
|
|
|
unsigned long result;
|
|
|
|
if (_BitScanReverse (&result, (unsigned long) (x >> 32)))
|
|
|
|
return CHAR_BIT * sizeof x - 1 - 32 - result;
|
|
|
|
if (_BitScanReverse (&result, (unsigned long) x))
|
|
|
|
return CHAR_BIT * sizeof x - 1 - result;
|
|
|
|
return CHAR_BIT * sizeof x;
|
|
|
|
#else
|
2017-03-03 17:17:51 +00:00
|
|
|
COUNT_LEADING_ZEROS (__builtin_clzll, _BitScanReverse64,
|
|
|
|
unsigned long long int);
|
2022-09-28 07:29:09 +00:00
|
|
|
#endif
|
2017-03-03 17:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:33:14 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-03 17:17:51 +00:00
|
|
|
_GL_INLINE_HEADER_END
|
|
|
|
|
|
|
|
#endif /* COUNT_LEADING_ZEROS_H */
|