Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / bl / lib / utl / src / BL_Utils.h
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: BL_Utils.h
5* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
7*
8* The above named program is free software; you can redistribute it and/or
9* modify it under the terms of the GNU General Public
10* License version 2 as published by the Free Software Foundation.
11*
12* The above named program is distributed in the hope that it will be
13* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public
18* License along with this work; if not, write to the Free Software
19* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*
21* ========== Copyright Header End ============================================
22*/
23
24#ifndef __BL_Utils_h__
25#define __BL_Utils_h__
26
27#include "BL_Types.h"
28
29//Extract a range of bits from a 64-bit word.
30inline uint64_t bit_selection(uint64_t v,uint8_t e,uint8_t s)
31{
32 assert((e-s) <= 63);
33 if((e-s) == 63)
34 return v;
35 else
36 return ((v) & (((1ULL << ((e)-(s)+1))-1)<<(s)));
37}
38
39//Extract and downshift a bitfield indexed by (s)tarting
40//offset and (len)gth from a (v)alue
41
42inline uint64_t bit_shift(uint64_t v,uint8_t s,uint8_t len)
43{
44 return (bit_selection(v,((s)+(len-1)),s)>>(s));
45}
46
47// get_lsb() isolated the least significat bit in v. This
48// works through the carry generated by twos complement
49// negate operation.
50
51inline uint64_t get_lsb( uint64_t v )
52{
53 return v & -v;
54}
55
56// clr_lsb() clears the least significant of v. Note this
57// works through the borrow logic generated by - 1
58
59inline uint64_t clr_lsb( uint64_t v )
60{
61 return v & (v - 1);
62}
63
64// is_power_two() returns true when value v is a power of two.
65// The routines fails for v == 0 ... you'll have to add that
66// zero test explicitely.
67
68inline bool is_power_of_two( uint64_t v )
69{
70 return clr_lsb(v) == 0;
71}
72
73// bit2idx() returns the index of the set bit in v. It uses a
74// De Bruijn sequence, which is a cyclic sequence of n bits in
75// which no sequence of n bits appears more then once.
76// http://en.wikipedia.org/wiki/De_Bruijn_sequence. The magic
77// sequence in the constant multiplier in the function below.
78// The value v has to be a power of two.
79
80static inline uint_t bit2idx( uint32_t v )
81{
82 assert(is_power_of_two(v));
83
84 static uint_t bit2tbl[32] =
85 {
86 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
87 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
88 };
89
90 // Note that we ignore bit 32 and above of the multiply result
91 // as the type is uint32_t.
92
93 return bit2tbl[(v * 0x077CB531) >> 27];
94}
95
96// rounds a value down to a power_of_two.
97inline uint64_t round_down_to_power_of_two(uint64_t value, uint64_t power_of_two)
98{
99 assert(is_power_of_two(power_of_two));
100
101 return value & ~(power_of_two - 1);
102}
103
104
105#endif
106