misc: Standardize the way create() constructs SimObjects.
[gem5.git] / src / base / condcodes.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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 #ifndef __BASE_CONDCODE_HH__
30 #define __BASE_CONDCODE_HH__
31
32 #include "base/bitfield.hh"
33
34 /**
35 * Calculate the carry flag from an addition. This should work even when
36 * a carry value is also added in.
37 *
38 * Parameters:
39 * dest: The result value of the addition.
40 * src1: One of the addends that was added.
41 * src2: The other addend that was added in.
42 *
43 * Rationale:
44 * This code analyzes the most sig. bits of the source addends and result,
45 * and deduces the carry out flag from them without needing the carry in bit.
46 *
47 * Observe that we have four cases after an addition regarding the carry
48 * in and carry out bits:
49 *
50 * If we have no carry in but a carry out:
51 * src1 and src2 must both be 1, with the result bit being 0. Hence,
52 * ~0 + 1 + 1 => 11, which has a high second bit. We return true.
53 *
54 * If we have a carry in and a carry out:
55 * src1 and src2 can either be 1 and 0, or vice versa. In this case,
56 * the addition with the carry in gives a result bit of 0 but a carry out.
57 * Hence,
58 * ~0 + 1 + 0 => 10, or ~0 + 0 + 1 => 10. We return true.
59 *
60 * Or, src1 and src2 can both be one. Along with the carry, this gives
61 * a result of 1 and a carry out of 1. Hence,
62 * ~1 + 1 + 1 => 10. We return true.
63 *
64 * If we have no carry in and no carry out:
65 * src1 and src2 can either be 1 and 0, 0 and 1, or 0 and 0.
66 * In the first two cases the result bit is 1, which when negated does not
67 * contribute to the sum algorithm at all. In the last case the result bit
68 * is zero, but neither src1 nor src2 contribute to the sum either. Hence,
69 * ~1 + 1 + 0 => 1,
70 * ~1 + 0 + 1 => 1,
71 * ~0 + 0 + 0 => 1.
72 * So we return false for all of these cases.
73 *
74 * If we have a carry in, but no carry out:
75 * src1 and src2 can neither be 1. So the overall result bit is 1. Hence:
76 * ~1 + 0 + 0 => 0. We return false.
77 *
78 * @ingroup api_base_utils
79 */
80 static inline bool
81 findCarry(int width, uint64_t dest, uint64_t src1, uint64_t src2)
82 {
83 int shift = width - 1;
84 return ((~(dest >> shift) & 1) +
85 ((src1 >> shift) & 1) +
86 ((src2 >> shift) & 1)) & 0x2;
87 }
88
89 /**
90 * Calculate the overflow flag from an addition.
91 *
92 * @ingroup api_base_utils
93 */
94 static inline bool
95 findOverflow(int width, uint64_t dest, uint64_t src1, uint64_t src2)
96 {
97 int shift = width - 1;
98 return ((src1 ^ ~src2) & (src1 ^ dest)) & (1ULL << shift);
99 }
100
101 /**
102 * Calculate the parity of a value. 1 is for odd parity and 0 is for even.
103 *
104 * Parameters:
105 * dest: a value to be tested.
106 *
107 * Rationale:
108 * findParity simply performs bitwise XOR operations on each "pair" of bits
109 * in the dest parameter; the procedure being that a pair of ones will be
110 * XOR'ed out of the intermediate value.
111 *
112 * This process is repeated until one last pair of bits are XOR'ed together.
113 * If the intermediate is still one, then there is exactly one high bit
114 * which does not have a corresponding high bit. Therefore, the value must
115 * have odd parity, and we return 1 accordingly. Otherwise we return 0.
116 *
117 * @ingroup api_base_utils
118 */
119 static inline bool
120 findParity(int width, uint64_t dest)
121 {
122 dest &= mask(width);
123 dest ^= (dest >> 32);
124 dest ^= (dest >> 16);
125 dest ^= (dest >> 8);
126 dest ^= (dest >> 4);
127 dest ^= (dest >> 2);
128 dest ^= (dest >> 1);
129 return dest & 1;
130 }
131
132 /**
133 * Calculate the negative flag.
134 *
135 * @ingroup api_base_utils
136 */
137 static inline bool
138 findNegative(int width, uint64_t dest)
139 {
140 return bits(dest, width - 1);
141 }
142
143 /**
144 * Calculate the zero flag.
145 *
146 * @ingroup api_base_utils
147 */
148 static inline bool
149 findZero(int width, uint64_t dest)
150 {
151 return !(dest & mask(width));
152 }
153
154 #endif // __BASE_CONDCODE_HH__