RTEMS 5.2
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nios2-count-zeros.h
1/*
2 * Author: Jeffrey O. Hill
3 *
4 * Copyright 2012. Los Alamos National Security, LLC.
5 * This material was produced under U.S. Government contract
6 * DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL),
7 * which is operated by Los Alamos National Security, LLC for
8 * the U.S. Department of Energy. The U.S. Government has rights
9 * to use, reproduce, and distribute this software. NEITHER THE
10 * GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY
11 * WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR
12 * THE USE OF THIS SOFTWARE.
13 *
14 * The license and distribution terms for this file may be
15 * found in the file LICENSE in this distribution or at
16 * http://www.rtems.org/license/LICENSE.
17 */
18
19#ifndef _NIOS2_COUNT_ZEROS_H
20#define _NIOS2_COUNT_ZEROS_H
21
22#include <stdint.h>
23
24#include <rtems/score/bitfield.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif /* __cplusplus */
29
30/*
31 * This implementation is currently much more efficient than
32 * the GCC provided __builtin_clz
33 */
34static inline unsigned _Nios2_Count_leading_zeros( uint32_t p )
35{
36 unsigned bitIdx;
37
38 if ( p <= 0xffffu ) {
39 if ( p < 0x100u ) {
40 bitIdx = _Bitfield_Leading_zeros[ p ] + 24u;
41 } else {
42 bitIdx = _Bitfield_Leading_zeros[ p >> 8u ] + 16u;
43 }
44 } else {
45 p >>= 16u;
46
47 if ( p < 0x100u ) {
48 bitIdx = _Bitfield_Leading_zeros[ p ] + 8u;
49 } else {
50 bitIdx = _Bitfield_Leading_zeros[ p >> 8u ];
51 }
52 }
53
54 return bitIdx;
55}
56
57/*
58 * This implementation is currently much more efficient than
59 * the GCC provided __builtin_ctz
60 */
61static inline unsigned _Nios2_Count_trailing_zeros( uint32_t p )
62{
63 return 31u - _Nios2_Count_leading_zeros( p & ( -p ) );
64}
65
66#ifdef __cplusplus
67}
68#endif /* __cplusplus */
69
70#endif /* _NIOS2_COUNT_ZEROS_H */
const unsigned char _Bitfield_Leading_zeros[256]
Definition: log2table.c:24
unsigned p
Definition: tte.h:17