types.hh revision 14297:b4519e586f5e
11689SN/A/* 21689SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 37897Shestness@cs.utexas.edu * All rights reserved. 41689SN/A * 51689SN/A * Redistribution and use in source and binary forms, with or without 61689SN/A * modification, are permitted provided that the following conditions are 71689SN/A * met: redistributions of source code must retain the above copyright 81689SN/A * notice, this list of conditions and the following disclaimer; 91689SN/A * redistributions in binary form must reproduce the above copyright 101689SN/A * notice, this list of conditions and the following disclaimer in the 111689SN/A * documentation and/or other materials provided with the distribution; 121689SN/A * neither the name of the copyright holders nor the names of its 131689SN/A * contributors may be used to endorse or promote products derived from 141689SN/A * this software without specific prior written permission. 151689SN/A * 161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271689SN/A * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu */ 302756Sksewell@umich.edu 317897Shestness@cs.utexas.edu/** 321689SN/A * @file 331689SN/A * Defines global host-dependent types: 342325SN/A * Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t. 352325SN/A */ 361060SN/A 371060SN/A#ifndef __BASE_TYPES_HH__ 381060SN/A#define __BASE_TYPES_HH__ 392292SN/A 402292SN/A#include <inttypes.h> 411681SN/A 421060SN/A#include <cassert> 432980Sgblack@eecs.umich.edu#include <memory> 441060SN/A#include <ostream> 457813Ssteve.reinhardt@amd.com#include <stdexcept> 461858SN/A 476658Snate@binkert.org#include "base/refcnt.hh" 484598Sbinkertn@umich.edu 492325SN/A/** uint64_t constant */ 501717SN/A#define ULL(N) ((uint64_t)N##ULL) 512683Sktlim@umich.edu/** int64_t constant */ 521717SN/A#define LL(N) ((int64_t)N##LL) 531717SN/A 542292SN/A/** Statistics counter type. Not much excuse for not using a 64-bit 552292SN/A * integer here, but if you're desperate and only run short 562817Sksewell@umich.edu * simulations you could make this 32 bits. 571060SN/A */ 581060SN/Atypedef int64_t Counter; 595529Snate@binkert.org 605529Snate@binkert.org/** 612316SN/A * Tick count type. 622316SN/A */ 632680Sktlim@umich.edutypedef uint64_t Tick; 642817Sksewell@umich.edu 652817Sksewell@umich.educonst Tick MaxTick = ULL(0xffffffffffffffff); 662843Sktlim@umich.edu 672843Sktlim@umich.edu/** 682669Sktlim@umich.edu * Cycles is a wrapper class for representing cycle counts, i.e. a 691060SN/A * relative difference between two points in time, expressed in a 701060SN/A * number of clock cycles. 715529Snate@binkert.org * 725529Snate@binkert.org * The Cycles wrapper class is a type-safe alternative to a 732733Sktlim@umich.edu * typedef, aiming to avoid unintentional mixing of cycles and ticks 741060SN/A * in the code base. 751060SN/A * 761060SN/A * Note that there is no overloading of the bool operator as the 775529Snate@binkert.org * compiler is allowed to turn booleans into integers and this causes 782292SN/A * a whole range of issues in a handful locations. The solution to 792292SN/A * this problem would be to use the safe bool idiom, but for now we 801060SN/A * make do without the test and use the more elaborate comparison > 811060SN/A * Cycles(0). 822348SN/A */ 832348SN/Aclass Cycles 842348SN/A{ 852348SN/A 862348SN/A private: 871060SN/A 882733Sktlim@umich.edu /** Member holding the actual value. */ 891060SN/A uint64_t c; 901060SN/A 912325SN/A public: 921060SN/A 931061SN/A /** Explicit constructor assigning a value. */ 944329Sktlim@umich.edu explicit constexpr Cycles(uint64_t _c) : c(_c) { } 951060SN/A 965595Sgblack@eecs.umich.edu /** Default constructor for parameter classes. */ 972292SN/A Cycles() : c(0) { } 982292SN/A 992292SN/A /** Converting back to the value type. */ 1002292SN/A constexpr operator uint64_t() const { return c; } 1012817Sksewell@umich.edu 1022829Sksewell@umich.edu /** Prefix increment operator. */ 1031060SN/A Cycles& operator++() 1041060SN/A { ++c; return *this; } 1051060SN/A 1061060SN/A /** Prefix decrement operator. Is only temporarily used in the O3 CPU. */ 1071060SN/A Cycles& operator--() 1082307SN/A { assert(c != 0); --c; return *this; } 1092307SN/A 1101060SN/A /** In-place addition of cycles. */ 1111060SN/A Cycles& operator+=(const Cycles& cc) 1126022Sgblack@eecs.umich.edu { c += cc.c; return *this; } 1136022Sgblack@eecs.umich.edu 1143781Sgblack@eecs.umich.edu /** Greater than comparison used for > Cycles(0). */ 1152292SN/A constexpr bool operator>(const Cycles& cc) const 1161060SN/A { return c > cc.c; } 1171060SN/A 1182829Sksewell@umich.edu constexpr Cycles operator +(const Cycles& b) const 1192829Sksewell@umich.edu { return Cycles(c + b.c); } 1202829Sksewell@umich.edu 1211060SN/A constexpr Cycles operator -(const Cycles& b) const 1221060SN/A { 1231060SN/A return c >= b.c ? Cycles(c - b.c) : 1241060SN/A throw std::invalid_argument("RHS cycle value larger than LHS"); 1252292SN/A } 1261755SN/A 1271060SN/A constexpr Cycles operator <<(const int32_t shift) const 1281060SN/A { return Cycles(c << shift); } 1292292SN/A 1301755SN/A constexpr Cycles operator >>(const int32_t shift) const 1312292SN/A { return Cycles(c >> shift); } 1322292SN/A 1331060SN/A friend std::ostream& operator<<(std::ostream &out, const Cycles & cycles); 1342292SN/A}; 1355336Shines@cs.fsu.edu 1361060SN/A/** 1371060SN/A * Address type 1382292SN/A * This will probably be moved somewhere else in the near future. 1391060SN/A * This should be at least as big as the biggest address width in use 1401060SN/A * in the system, which will probably be 64 bits. 1412292SN/A */ 1421060SN/Atypedef uint64_t Addr; 1431060SN/A 1441060SN/Atypedef uint16_t MicroPC; 1457823Ssteve.reinhardt@amd.com 1461060SN/Astatic const MicroPC MicroPCRomBit = 1 << (sizeof(MicroPC) * 8 - 1); 1477823Ssteve.reinhardt@amd.com 1481060SN/Astatic inline MicroPC 1491060SN/AromMicroPC(MicroPC upc) 1502292SN/A{ 1511060SN/A return upc | MicroPCRomBit; 1521060SN/A} 1531060SN/A 1541060SN/Astatic inline MicroPC 1551060SN/AnormalMicroPC(MicroPC upc) 1561060SN/A{ 1572829Sksewell@umich.edu return upc & ~MicroPCRomBit; 1582829Sksewell@umich.edu} 1592829Sksewell@umich.edu 1602829Sksewell@umich.edustatic inline bool 1616221Snate@binkert.orgisRomMicroPC(MicroPC upc) 1622829Sksewell@umich.edu{ 1632829Sksewell@umich.edu return MicroPCRomBit & upc; 1642829Sksewell@umich.edu} 1652829Sksewell@umich.edu 1662829Sksewell@umich.educonst Addr MaxAddr = (Addr)-1; 1672829Sksewell@umich.edu 1682829Sksewell@umich.edutypedef uint64_t RegVal; 1692829Sksewell@umich.edu 1702829Sksewell@umich.edustatic inline uint32_t 1712829Sksewell@umich.edufloatToBits32(float val) 1722829Sksewell@umich.edu{ 1732829Sksewell@umich.edu union 1742829Sksewell@umich.edu { 1752829Sksewell@umich.edu float f; 1762829Sksewell@umich.edu uint32_t i; 1775336Shines@cs.fsu.edu } u; 1782829Sksewell@umich.edu u.f = val; 1792829Sksewell@umich.edu return u.i; 1802829Sksewell@umich.edu} 1816221Snate@binkert.org 1826221Snate@binkert.orgstatic inline uint64_t 1832829Sksewell@umich.edufloatToBits64(double val) 1842829Sksewell@umich.edu{ 1852829Sksewell@umich.edu union 1865606Snate@binkert.org { 1877823Ssteve.reinhardt@amd.com double f; 1882829Sksewell@umich.edu uint64_t i; 1895606Snate@binkert.org } u; 1907823Ssteve.reinhardt@amd.com u.f = val; 1912829Sksewell@umich.edu return u.i; 1922829Sksewell@umich.edu} 1932829Sksewell@umich.edu 1946221Snate@binkert.orgstatic inline uint64_t floatToBits(double val) { return floatToBits64(val); } 1956221Snate@binkert.orgstatic inline uint32_t floatToBits(float val) { return floatToBits32(val); } 1962829Sksewell@umich.edu 1972829Sksewell@umich.edustatic inline float 1982829Sksewell@umich.edubitsToFloat32(uint32_t val) 1992829Sksewell@umich.edu{ 2002829Sksewell@umich.edu union 2012829Sksewell@umich.edu { 2022829Sksewell@umich.edu float f; 2032829Sksewell@umich.edu uint32_t i; 2042875Sksewell@umich.edu } u; 2052875Sksewell@umich.edu u.i = val; 2062875Sksewell@umich.edu return u.f; 2073221Sktlim@umich.edu} 2086221Snate@binkert.org 2092875Sksewell@umich.edustatic inline double 2103221Sktlim@umich.edubitsToFloat64(uint64_t val) 2113221Sktlim@umich.edu{ 2123221Sktlim@umich.edu union 2132875Sksewell@umich.edu { 2142875Sksewell@umich.edu double f; 2152875Sksewell@umich.edu uint64_t i; 2162875Sksewell@umich.edu } u; 2172875Sksewell@umich.edu u.i = val; 2182875Sksewell@umich.edu return u.f; 2192875Sksewell@umich.edu} 2202875Sksewell@umich.edu 2212875Sksewell@umich.edustatic inline double bitsToFloat(uint64_t val) { return bitsToFloat64(val); } 2222875Sksewell@umich.edustatic inline float bitsToFloat(uint32_t val) { return bitsToFloat32(val); } 2232875Sksewell@umich.edu 2242875Sksewell@umich.edu/** 2252875Sksewell@umich.edu * Thread index/ID type 2263221Sktlim@umich.edu */ 2273221Sktlim@umich.edutypedef int16_t ThreadID; 2283221Sktlim@umich.educonst ThreadID InvalidThreadID = (ThreadID)-1; 2292875Sksewell@umich.edu 2305336Shines@cs.fsu.edu/** Globally unique thread context ID */ 2312875Sksewell@umich.edutypedef int ContextID; 2322875Sksewell@umich.educonst ContextID InvalidContextID = (ContextID)-1; 2332875Sksewell@umich.edu 2346221Snate@binkert.org/** 2356221Snate@binkert.org * Port index/ID type, and a symbolic name for an invalid port id. 2362875Sksewell@umich.edu */ 2372875Sksewell@umich.edutypedef int16_t PortID; 2382875Sksewell@umich.educonst PortID InvalidPortID = (PortID)-1; 2395606Snate@binkert.org 2407823Ssteve.reinhardt@amd.comclass FaultBase; 2412875Sksewell@umich.edutypedef std::shared_ptr<FaultBase> Fault; 2425606Snate@binkert.org 2437823Ssteve.reinhardt@amd.com// Rather than creating a shared_ptr instance and assigning it nullptr, 2442875Sksewell@umich.edu// we just create an alias. 2452875Sksewell@umich.educonstexpr decltype(nullptr) NoFault = nullptr; 2462875Sksewell@umich.edu 2476221Snate@binkert.orgstruct AtomicOpFunctor 2486221Snate@binkert.org{ 2492875Sksewell@umich.edu virtual void operator()(uint8_t *p) = 0; 2502875Sksewell@umich.edu virtual AtomicOpFunctor* clone() = 0; 2512875Sksewell@umich.edu virtual ~AtomicOpFunctor() {} 2522875Sksewell@umich.edu}; 2532875Sksewell@umich.edu 2542875Sksewell@umich.edutemplate <class T> 2552875Sksewell@umich.edustruct TypedAtomicOpFunctor : public AtomicOpFunctor 2562875Sksewell@umich.edu{ 2571060SN/A void operator()(uint8_t *p) { execute((T *)p); } 2582292SN/A virtual AtomicOpFunctor* clone() = 0; 2595595Sgblack@eecs.umich.edu virtual void execute(T * p) = 0; 2602292SN/A}; 2611755SN/A 2621060SN/Atypedef std::unique_ptr<AtomicOpFunctor> AtomicOpFunctorPtr; 2632292SN/A 2645595Sgblack@eecs.umich.eduenum ByteOrder { 2651684SN/A BigEndianByteOrder, 2665358Sgblack@eecs.umich.edu LittleEndianByteOrder 2675358Sgblack@eecs.umich.edu}; 2685358Sgblack@eecs.umich.edu 2695358Sgblack@eecs.umich.edu#endif // __BASE_TYPES_HH__ 2705358Sgblack@eecs.umich.edu