Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: uint64.h | |
5 | * Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved | |
6 | * 4150 Network Circle, Santa Clara, California 95054, U.S.A. | |
7 | * | |
8 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; version 2 of the License. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | * | |
23 | * For the avoidance of doubt, and except that if any non-GPL license | |
24 | * choice is available it will apply instead, Sun elects to use only | |
25 | * the General Public License version 2 (GPLv2) at this time for any | |
26 | * software where a choice of GPL license versions is made | |
27 | * available with the language indicating that GPLv2 or any later version | |
28 | * may be used, or where a choice of which version of the GPL is applied is | |
29 | * otherwise unspecified. | |
30 | * | |
31 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
32 | * CA 95054 USA or visit www.sun.com if you need additional information or | |
33 | * have any questions. | |
34 | * | |
35 | * | |
36 | * ========== Copyright Header End ============================================ | |
37 | */ | |
38 | // Copyright 2001 ADVANCED MICRO DEVICES, INC. All Rights Reserved. | |
39 | // | |
40 | // This software and any related documentation (the "Materials") are the | |
41 | // confidential proprietary information of AMD. Unless otherwise provided | |
42 | // in an agreement specifically licensing the Materials, the Materials are | |
43 | // provided in confidence and may not to be used, distributed, modified, or | |
44 | // reproduced in whole or in part by any means. | |
45 | // | |
46 | // LIMITATION OF LIABILITY: THE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY | |
47 | // EXPRESS OR IMPLIED WARRANTY OF ANY KIND, INCLUDING BUT NOT LIMITED TO | |
48 | // WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT, TITLE, FITNESS FOR ANY | |
49 | // PARTICULAR PURPOSE, OR WARRANTIES ARISING FORM CONDUCT, COURSE OF | |
50 | // DEALING, OR USAGE OF TRADE. IN NO EVENT SHALL AMD OR ITS LICENSORS BE | |
51 | // LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, | |
52 | // DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF | |
53 | // INFORMATION) ARISING OUT OF THE USE OF OR INABILITY TO USE THE | |
54 | // MATERIALS, EVEN IF AMD HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH | |
55 | // DAMAGES. BECAUSE SOME JURISDICTIONS PROHIBIT THE EXCLUSION OR | |
56 | // LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE | |
57 | // ABOVE LIMITATION MAY NOT APPLY TO YOU. | |
58 | // | |
59 | // AMD does not assume any responsibility for any errors which may appear | |
60 | // in the Materials nor any responsibility to support or update the | |
61 | // Materials. AMD retains the right to modify the Materials at any time, | |
62 | // without notice, and is not obligated to provide such modified Materials | |
63 | // to you. | |
64 | // | |
65 | // NO SUPPORT OBLIGATION: AMD is not obligated to furnish, support, or make | |
66 | // any further information, software, technical information, know-how, or | |
67 | // show-how available to you. | |
68 | // | |
69 | // U.S. GOVERNMENT RESTRICTED RIGHTS: The Materials are provided with | |
70 | // "RESTRICTED RIGHTS." Use, duplication, or disclosure by the Government | |
71 | // is subject to the restrictions as set forth in FAR 52.227-14 and DFAR | |
72 | // 252.227-7013, et seq., or its successor. Use of the Materials by the | |
73 | // Government constitutes acknowledgement of AMD's proprietary rights in | |
74 | // them. | |
75 | // | |
76 | //////////////////////////////////////////////////////////////////////////////// | |
77 | ||
78 | /***************************************************************************** | |
79 | * | |
80 | * $RCSfile: uint64.h,v $ | |
81 | * | |
82 | * $Revision: 1.3 $ | |
83 | * $Date: 2008/02/21 09:51:26 $ | |
84 | * $Author: nhussain $ | |
85 | * $State: Exp $ | |
86 | * $Locker: $ | |
87 | * | |
88 | * Copyright (c) 1997 by Advanced Micro Devices, Inc. | |
89 | * | |
90 | * This file is protected by Federal Copyright Law, with all rights | |
91 | * reserved. No part of this file may be reproduced, stored in a | |
92 | * retrieval system, translated, transcribed, or transmitted, in any | |
93 | * form, or by any means manual, electric, electronic, mechanical, | |
94 | * electro-magnetic, chemical, optical, or otherwise, without prior | |
95 | * explicit written permission from Advanced Micro Devices, Inc. | |
96 | * | |
97 | ****************************************************************************/ | |
98 | #ifndef __UINT64_DEFINED__ | |
99 | #define __UINT64_DEFINED__ | |
100 | ||
101 | #include <stdlib.h> | |
102 | #include <stdio.h> | |
103 | #include <fstream> | |
104 | #include <iomanip> | |
105 | ||
106 | typedef unsigned int uint; | |
107 | typedef int BOOL; // bozo: stupid XLC compiler | |
108 | ||
109 | class uint64 { // 64-bit integer, use operator[] to directly access the individual 32-bit integers | |
110 | public: | |
111 | uint64() { hi = lo = 0; } | |
112 | uint64(uint first, uint second) { hi = first; lo = second; } | |
113 | uint64(uint Ad32) { hi = 0; lo = Ad32; } | |
114 | uint& operator[](uint i) { if (i>1) index_error(i); return i?hi:lo; } // can be used as an l-value | |
115 | ||
116 | private: | |
117 | uint hi, lo; | |
118 | ||
119 | void index_error(uint bad_index); | |
120 | }; | |
121 | ||
122 | inline uint uint_max(uint i, uint j) { return (i>j)?i:j; } | |
123 | ||
124 | inline BOOL operator==(uint64 i, uint64 j) { return (i[1] == j[1]) && (i[0] == j[0]); } | |
125 | inline BOOL operator<(uint64 i, uint64 j) { return (i[1] == j[1]) ? (i[0] < j[0]) : (i[1] < j[1]); } | |
126 | ||
127 | inline BOOL operator!=(uint64 i, uint64 j) { return !(i == j); } | |
128 | inline BOOL operator>=(uint64 i, uint64 j) { return !(i < j); } | |
129 | ||
130 | inline BOOL operator<=(uint64 i, uint64 j) { return ((i < j) || (i == j)); } | |
131 | inline BOOL operator>(uint64 i, uint64 j) { return !((i < j) || (i == j)); } | |
132 | ||
133 | inline uint64 operator|(uint64 i, uint64 j) { return uint64(i[1] | j[1], i[0] | j[0]); } | |
134 | inline uint64 operator&(uint64 i, uint64 j) { return uint64(i[1] & j[1], i[0] & j[0]); } | |
135 | inline uint64 operator^(uint64 i, uint64 j) { return uint64(i[1] ^ j[1], i[0] ^ j[0]); } | |
136 | inline uint64 operator~(uint64 i) { return uint64(~i[1],~i[0]); } | |
137 | uint64 operator<<(uint64 i, uint shift); | |
138 | uint64 operator>>(uint64 i, uint shift); | |
139 | inline uint64 operator+(uint64 i, uint64 j) { return uint64( i[1]+j[1] + ((i[0]+j[0]<uint_max(i[0],j[0]))?1:0), i[0]+j[0] ); } | |
140 | inline uint64 operator-(uint64 i, uint64 j) { return uint64( i[1]-j[1] - ((i[0]<j[0])?1:0), i[0]-j[0] ); } | |
141 | ||
142 | inline uint64 operator|=(uint64 &i, uint64 j) { i = i | j; return i; } | |
143 | inline uint64 operator&=(uint64 &i, uint64 j) { i = i & j; return i; } | |
144 | inline uint64 operator^=(uint64 &i, uint64 j) { i = i ^ j; return i; } | |
145 | inline uint64 operator+=(uint64 &i, uint64 j) { i = i + j; return i; } | |
146 | inline uint64 operator-=(uint64 &i, uint64 j) { i = i - j; return i; } | |
147 | inline uint64 operator<<=(uint64 &i, uint shift) { i = i << shift; return i; } | |
148 | inline uint64 operator>>=(uint64 &i, uint shift) { i = i >> shift; return i; } | |
149 | ||
150 | std::ostream& operator<<(std::ostream& os, uint64 Ad); | |
151 | ||
152 | // just a little routine demonstrating how to use the uint64 class | |
153 | void test_uint64(); | |
154 | ||
155 | ||
156 | // convert "uint64" to "unsigned long long" | |
157 | inline unsigned long long Uint64ToLonglong(uint64 i) | |
158 | { | |
159 | return( (((unsigned long long)i[1]) << 32) | i[0] ); | |
160 | } | |
161 | ||
162 | // convert "unsigned long long" to "uint64" | |
163 | inline uint64 LonglongToUint64(unsigned long long j) | |
164 | { | |
165 | return (uint64( (uint (j>>32)), (uint (0x00000000ffffffffll & j)) ) ); | |
166 | } | |
167 | ||
168 | // return the high order 32 bits of an unsigned long long | |
169 | inline uint LonglongToIntHi(unsigned long long j) { return (uint (j>>32)); }; | |
170 | ||
171 | // return the low order 32 bits of an unsigned long long | |
172 | inline uint LonglongToIntLo(unsigned long long j) | |
173 | { | |
174 | return (uint (0x00000000ffffffffll & j)); | |
175 | }; | |
176 | ||
177 | inline unsigned long long IntToLonglong(unsigned int hi, unsigned int lo) | |
178 | { | |
179 | return( (((unsigned long long) hi) << 32) | lo ); | |
180 | } | |
181 | ||
182 | ||
183 | // return a mask to zero out the upper bits above "hi" | |
184 | inline unsigned long long ZeroUpper(unsigned int hi, unsigned int lo) | |
185 | { | |
186 | return( ~(0xffffffffffffffffLL << (hi - lo + 1)) ); | |
187 | } | |
188 | ||
189 | ||
190 | // this function takes two integers (hi_half and lo_half), | |
191 | // extracts the field [hi:lo] | |
192 | // base specifies the address value of bit 0 | |
193 | ||
194 | inline unsigned long long ExtractField(unsigned int hi_half, unsigned int lo_half, unsigned int hi, unsigned int lo, unsigned int base=0) | |
195 | { | |
196 | unsigned long long data = IntToLonglong(hi_half, lo_half); | |
197 | unsigned long long mask = ZeroUpper(hi, lo); | |
198 | ||
199 | data = data >> (lo - base); // shift the field to bit 0 | |
200 | return ( data & mask ); // mask out the upper bits | |
201 | } | |
202 | ||
203 | #endif | |
204 | ||
205 | ||
206 | ||
207 | ||
208 |