gpu-compute,mem-ruby: Refactor GPU coalescer
[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 static inline bool
79 findCarry(int width, uint64_t dest, uint64_t src1, uint64_t src2)
80 {
81 int shift = width - 1;
82 return ((~(dest >> shift) & 1) +
83 ((src1 >> shift) & 1) +
84 ((src2 >> shift) & 1)) & 0x2;
85 }
86
87 /**
88 * Calculate the overflow flag from an addition.
89 */
90 static inline bool
91 findOverflow(int width, uint64_t dest, uint64_t src1, uint64_t src2)
92 {
93 int shift = width - 1;
94 return ((src1 ^ ~src2) & (src1 ^ dest)) & (1ULL << shift);
95 }
96
97 /**
98 * Calculate the parity of a value. 1 is for odd parity and 0 is for even.
99 *
100 * Parameters:
101 * dest: a value to be tested.
102 *
103 * Rationale:
104 * findParity simply performs bitwise XOR operations on each "pair" of bits
105 * in the dest parameter; the procedure being that a pair of ones will be
106 * XOR'ed out of the intermediate value.
107 *
108 * This process is repeated until one last pair of bits are XOR'ed together.
109 * If the intermediate is still one, then there is exactly one high bit
110 * which does not have a corresponding high bit. Therefore, the value must
111 * have odd parity, and we return 1 accordingly. Otherwise we return 0.
112 */
113 static inline bool
114 findParity(int width, uint64_t dest)
115 {
116 dest &= mask(width);
117 dest ^= (dest >> 32);
118 dest ^= (dest >> 16);
119 dest ^= (dest >> 8);
120 dest ^= (dest >> 4);
121 dest ^= (dest >> 2);
122 dest ^= (dest >> 1);
123 return dest & 1;
124 }
125
126 /**
127 * Calculate the negative flag.
128 */
129 static inline bool
130 findNegative(int width, uint64_t dest)
131 {
132 return bits(dest, width - 1);
133 }
134
135 /**
136 * Calculate the zero flag.
137 */
138 static inline bool
139 findZero(int width, uint64_t dest)
140 {
141 return !(dest & mask(width));
142 }
143
144 #endif // __BASE_CONDCODE_HH__