RTEMS 6.1-rc5
Loading...
Searching...
No Matches
nios2-count-zeros.h
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Author: Jeffrey O. Hill
5 *
6 * Copyright 2012. Los Alamos National Security, LLC.
7 * This material was produced under U.S. Government contract
8 * DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL),
9 * which is operated by Los Alamos National Security, LLC for
10 * the U.S. Department of Energy. The U.S. Government has rights
11 * to use, reproduce, and distribute this software. NEITHER THE
12 * GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY
13 * WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR
14 * THE USE OF THIS SOFTWARE.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef _NIOS2_COUNT_ZEROS_H
39#define _NIOS2_COUNT_ZEROS_H
40
41#include <stdint.h>
42
43#include <rtems/score/bitfield.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif /* __cplusplus */
48
49/*
50 * This implementation is currently much more efficient than
51 * the GCC provided __builtin_clz
52 */
53static inline unsigned _Nios2_Count_leading_zeros( uint32_t p )
54{
55 unsigned bitIdx;
56
57 if ( p <= 0xffffu ) {
58 if ( p < 0x100u ) {
59 bitIdx = _Bitfield_Leading_zeros[ p ] + 24u;
60 } else {
61 bitIdx = _Bitfield_Leading_zeros[ p >> 8u ] + 16u;
62 }
63 } else {
64 p >>= 16u;
65
66 if ( p < 0x100u ) {
67 bitIdx = _Bitfield_Leading_zeros[ p ] + 8u;
68 } else {
69 bitIdx = _Bitfield_Leading_zeros[ p >> 8u ];
70 }
71 }
72
73 return bitIdx;
74}
75
76/*
77 * This implementation is currently much more efficient than
78 * the GCC provided __builtin_ctz
79 */
80static inline unsigned _Nios2_Count_trailing_zeros( uint32_t p )
81{
82 return 31u - _Nios2_Count_leading_zeros( p & ( -p ) );
83}
84
85#ifdef __cplusplus
86}
87#endif /* __cplusplus */
88
89#endif /* _NIOS2_COUNT_ZEROS_H */
const unsigned char _Bitfield_Leading_zeros[256]
Definition: log2table.c:44
unsigned p
Definition: tte.h:17