Address.cc revision 11118
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 31#include "mem/ruby/system/RubySystem.hh" 32 33Addr 34bitSelect(Addr addr, unsigned int small, unsigned int big) 35{ 36 assert(big >= small); 37 38 if (big >= ADDRESS_WIDTH - 1) { 39 return (addr >> small); 40 } else { 41 Addr mask = ~((Addr)~0 << (big + 1)); 42 // FIXME - this is slow to manipulate a 64-bit number using 32-bits 43 Addr partial = (addr & mask); 44 return (partial >> small); 45 } 46} 47 48Addr 49bitRemove(Addr addr, unsigned int small, unsigned int big) 50{ 51 assert(big >= small); 52 53 if (small >= ADDRESS_WIDTH - 1) { 54 return addr; 55 } else if (big >= ADDRESS_WIDTH - 1) { 56 Addr mask = (Addr)~0 >> small; 57 return (addr & mask); 58 } else if (small == 0) { 59 Addr mask = (Addr)~0 << big; 60 return (addr & mask); 61 } else { 62 Addr mask = ~((Addr)~0 << small); 63 Addr lower_bits = addr & mask; 64 mask = (Addr)~0 << (big + 1); 65 Addr higher_bits = addr & mask; 66 67 // Shift the valid high bits over the removed section 68 higher_bits = higher_bits >> (big - small + 1); 69 return (higher_bits | lower_bits); 70 } 71} 72 73Addr 74maskLowOrderBits(Addr addr, unsigned int number) 75{ 76 Addr mask; 77 78 if (number >= ADDRESS_WIDTH - 1) { 79 mask = ~0; 80 } else { 81 mask = (Addr)~0 << number; 82 } 83 return (addr & mask); 84} 85 86Addr 87maskHighOrderBits(Addr addr, unsigned int number) 88{ 89 Addr mask; 90 91 if (number >= ADDRESS_WIDTH - 1) { 92 mask = ~0; 93 } else { 94 mask = (Addr)~0 >> number; 95 } 96 return (addr & mask); 97} 98 99Addr 100shiftLowOrderBits(Addr addr, unsigned int number) 101{ 102 return (addr >> number); 103} 104 105Addr 106getOffset(Addr addr) 107{ 108 return bitSelect(addr, 0, RubySystem::getBlockSizeBits() - 1); 109} 110 111Addr 112makeLineAddress(Addr addr) 113{ 114 return maskLowOrderBits(addr, RubySystem::getBlockSizeBits()); 115} 116 117// returns the next stride address based on line address 118Addr 119makeNextStrideAddress(Addr addr, int stride) 120{ 121 return maskLowOrderBits(addr, RubySystem::getBlockSizeBits()) 122 + RubySystem::getBlockSizeBytes() * stride; 123} 124 125std::string 126printAddress(Addr addr) 127{ 128 std::stringstream out; 129 out << "[" << std::hex << "0x" << addr << "," << " line 0x" 130 << maskLowOrderBits(addr, RubySystem::getBlockSizeBits()) 131 << std::dec << "]"; 132 return out.str(); 133} 134