source: rtems/cpukit/score/cpu/nios2/rtems/score/nios2-count-zeros.h @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 1.7 KB
Line 
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 = __log2table[ p ] + 24u;
41    } else {
42      bitIdx = __log2table[ p >> 8u ] + 16u;
43    }
44  } else {
45    p >>= 16u;
46
47    if ( p < 0x100u ) {
48      bitIdx = __log2table[ p ] + 8u;
49    } else {
50      bitIdx = __log2table[ 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 */
Note: See TracBrowser for help on using the repository browser.