Address.cc revision 11025
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "mem/ruby/common/Address.hh"
30#include "mem/ruby/system/System.hh"
31
32Addr
33bitSelect(Addr addr, unsigned int small, unsigned int big)
34{
35    assert(big >= small);
36
37    if (big >= ADDRESS_WIDTH - 1) {
38        return (addr >> small);
39    } else {
40        Addr mask = ~((Addr)~0 << (big + 1));
41        // FIXME - this is slow to manipulate a 64-bit number using 32-bits
42        Addr partial = (addr & mask);
43        return (partial >> small);
44    }
45}
46
47Addr
48bitRemove(Addr addr, unsigned int small, unsigned int big)
49{
50    assert(big >= small);
51
52    if (small >= ADDRESS_WIDTH - 1) {
53        return addr;
54    } else if (big >= ADDRESS_WIDTH - 1) {
55        Addr mask = (Addr)~0 >> small;
56        return (addr & mask);
57    } else if (small == 0) {
58        Addr mask = (Addr)~0 << big;
59        return (addr & mask);
60    } else {
61        Addr mask = ~((Addr)~0 << small);
62        Addr lower_bits = addr & mask;
63        mask = (Addr)~0 << (big + 1);
64        Addr higher_bits = addr & mask;
65
66        // Shift the valid high bits over the removed section
67        higher_bits = higher_bits >> (big - small + 1);
68        return (higher_bits | lower_bits);
69    }
70}
71
72Addr
73maskLowOrderBits(Addr addr, unsigned int number)
74{
75  Addr mask;
76
77  if (number >= ADDRESS_WIDTH - 1) {
78      mask = ~0;
79  } else {
80      mask = (Addr)~0 << number;
81  }
82  return (addr & mask);
83}
84
85Addr
86maskHighOrderBits(Addr addr, unsigned int number)
87{
88    Addr mask;
89
90    if (number >= ADDRESS_WIDTH - 1) {
91        mask = ~0;
92    } else {
93        mask = (Addr)~0 >> number;
94    }
95    return (addr & mask);
96}
97
98Addr
99shiftLowOrderBits(Addr addr, unsigned int number)
100{
101    return (addr >> number);
102}
103
104Addr
105getOffset(Addr addr)
106{
107    return bitSelect(addr, 0, RubySystem::getBlockSizeBits() - 1);
108}
109
110Addr
111makeLineAddress(Addr addr)
112{
113    return maskLowOrderBits(addr, RubySystem::getBlockSizeBits());
114}
115
116// returns the next stride address based on line address
117Addr
118makeNextStrideAddress(Addr addr, int stride)
119{
120    return maskLowOrderBits(addr, RubySystem::getBlockSizeBits())
121        + RubySystem::getBlockSizeBytes() * stride;
122}
123