|
| 1 | +function SetBoundsReturn#(CapFat, CapAddrW) setBoundsFat(CapFat cap, Address lengthFull); |
| 2 | + CapFat ret = cap; |
| 3 | + // Find new exponent by finding the index of the most significant bit of the |
| 4 | + // length, or counting leading zeros in the high bits of the length, and |
| 5 | + // substracting them to the CapAddr width (taking away the bottom MW-1 bits: |
| 6 | + // trim (MW-1) bits from the bottom of length since any length with a |
| 7 | + // significance that small will yield an exponent of zero). |
| 8 | + CapAddr length = truncate(lengthFull); |
| 9 | + Bit#(TSub#(CapAddrW,TSub#(MW,1))) lengthMSBs = truncateLSB(length); |
| 10 | + Exp zeros = zeroExtend(countZerosMSB(lengthMSBs)); |
| 11 | + // Adjust resetExp by one since it's scale reaches 1-bit greater than a |
| 12 | + // 64-bit length can express. |
| 13 | + Bool maxZero = (zeros==(resetExp-1)); |
| 14 | + Bool intExp = !(maxZero && length[fromInteger(valueOf(TSub#(MW,2)))]==1'b0); |
| 15 | + // Do this without subtraction |
| 16 | + //fromInteger(valueof(TSub#(SizeOf#(Address),TSub#(MW,1)))) - zeros; |
| 17 | + Exp e = (resetExp-1) - zeros; |
| 18 | + // Derive new base bits by extracting MW bits from the capability address |
| 19 | + // starting at the new exponent's position. |
| 20 | + CapAddrPlus2 base = {2'b0, cap.address}; |
| 21 | + Bit#(TAdd#(MW,1)) newBaseBits = truncate(base>>e); |
| 22 | + |
| 23 | + // Derive new top bits by extracting MW bits from the capability address + |
| 24 | + // requested length, starting at the new exponent's position, and rounding up |
| 25 | + // if significant bits are lost in the process. |
| 26 | + CapAddrPlus2 len = {2'b0, length}; |
| 27 | + CapAddrPlus2 top = base + len; |
| 28 | + |
| 29 | + // Create a mask with all bits set below the MSB of length and then masking |
| 30 | + // all bits below the mantissa bits. |
| 31 | + CapAddrPlus2 lmask = smearMSBRight(len); |
| 32 | + // The shift amount required to put the most significant set bit of the len |
| 33 | + // just above the bottom HalfExpW bits that are taken by the exp. |
| 34 | + Integer shiftAmount = valueOf(TSub#(TSub#(MW,2),HalfExpW)); |
| 35 | + |
| 36 | + // Calculate all values associated with E=e (e not rounding up) |
| 37 | + // Round up considering the stolen HalfExpW exponent bits if required |
| 38 | + Bit#(TAdd#(MW,1)) newTopBits = truncate(top>>e); |
| 39 | + // Check if non-zero bits were lost in the low bits of top, either in the 'e' |
| 40 | + // shifted out bits or in the HalfExpW bits stolen for the exponent |
| 41 | + // Shift by MW-1 to move MSB of mask just below the mantissa, then up |
| 42 | + // HalfExpW more to take in the bits that will be lost for the exponent when |
| 43 | + // it is non-zero. |
| 44 | + CapAddrPlus2 lmaskLor = lmask>>fromInteger(shiftAmount+1); |
| 45 | + CapAddrPlus2 lmaskLo = lmask>>fromInteger(shiftAmount); |
| 46 | + // For the len, we're not actually losing significance since we're not |
| 47 | + // storing it, we just want to know if any low bits are non-zero so that we |
| 48 | + // will know if it will cause the total length to round up. |
| 49 | + Bool lostSignificantLen = (len&lmaskLor)!=0 && intExp; |
| 50 | + Bool lostSignificantTop = (top&lmaskLor)!=0 && intExp; |
| 51 | + // Check if non-zero bits were lost in the low bits of base, either in the |
| 52 | + // 'e' shifted out bits or in the HalfExpW bits stolen for the exponent |
| 53 | + Bool lostSignificantBase = (base&lmaskLor)!=0 && intExp; |
| 54 | + |
| 55 | + // Calculate all values associated with E=e+1 (e rounding up due to msb of L |
| 56 | + // increasing by 1) This value is just to avoid adding later. |
| 57 | + Bit#(MW) newTopBitsHigher = truncateLSB(newTopBits); |
| 58 | + // Check if non-zero bits were lost in the low bits of top, either in the 'e' |
| 59 | + // shifted out bits or in the HalfExpW bits stolen for the exponent Shift by |
| 60 | + // MW-1 to move MSB of mask just below the mantissa, then up HalfExpW more to |
| 61 | + // take in the bits that will be lost for the exponent when it is non-zero. |
| 62 | + Bool lostSignificantTopHigher = (top&lmaskLo)!=0 && intExp; |
| 63 | + // Check if non-zero bits were lost in the low bits of base, either in the |
| 64 | + // 'e' shifted out bits or in the HalfExpW bits stolen for the exponent |
| 65 | + Bool lostSignificantBaseHigher = (base&lmaskLo)!=0 && intExp; |
| 66 | + // If either base or top lost significant bits and we wanted an exact |
| 67 | + // setBounds, void the return capability |
| 68 | + |
| 69 | + // We need to round up Exp if the msb of length will increase. |
| 70 | + // We can check how much the length will increase without looking at the |
| 71 | + // result of adding the length to the base. We do this by adding the lower |
| 72 | + // bits of the length to the base and then comparing both halves (above and |
| 73 | + // below the mask) to zero. Either side that is non-zero indicates an extra |
| 74 | + // "1" that will be added to the "mantissa" bits of the length, potentially |
| 75 | + // causing overflow. Finally check how close the requested length is to |
| 76 | + // overflow, and test in relation to how much the length will increase. |
| 77 | + CapAddrPlus2 topLo = (lmaskLor & len) + (lmaskLor & base); |
| 78 | + CapAddrPlus2 mwLsbMask = lmaskLor ^ lmaskLo; |
| 79 | + // If the first bit of the mantissa of the top is not the sum of the |
| 80 | + // corrosponding bits of base and length, there was a carry in. |
| 81 | + Bool lengthCarryIn = (mwLsbMask & top) != ((mwLsbMask & base)^(mwLsbMask & len)); |
| 82 | + Bool lengthRoundUp = lostSignificantTop; |
| 83 | + Bool lengthIsMax = (len & (~lmaskLor)) == (lmask ^ lmaskLor); |
| 84 | + Bool lengthIsMaxLessOne = (len & (~lmaskLor)) == (lmask ^ lmaskLo); |
| 85 | + |
| 86 | + Bool lengthOverflow = False; |
| 87 | + if (lengthIsMax && (lengthCarryIn || lengthRoundUp)) lengthOverflow = True; |
| 88 | + if (lengthIsMaxLessOne && lengthCarryIn && lengthRoundUp) lengthOverflow = True; |
| 89 | + |
| 90 | + if(lengthOverflow && intExp) begin |
| 91 | + e = e+1; |
| 92 | + ret.bounds.topBits = lostSignificantTopHigher ? newTopBitsHigher + 'b1000 |
| 93 | + : newTopBitsHigher; |
| 94 | + ret.bounds.baseBits = truncateLSB(newBaseBits); |
| 95 | + end else begin |
| 96 | + ret.bounds.topBits = lostSignificantTop ? truncate(newTopBits + 'b1000) |
| 97 | + : truncate(newTopBits); |
| 98 | + ret.bounds.baseBits = truncate(newBaseBits); |
| 99 | + end |
| 100 | + Bool exact = !(lostSignificantBase || lostSignificantTop); |
| 101 | + |
| 102 | + ret.bounds.exp = e; |
| 103 | + // Update the addrBits fields |
| 104 | + ret.addrBits = ret.bounds.baseBits; |
| 105 | + // Derive new format from newly computed exponent value, and round top up if |
| 106 | + // necessary |
| 107 | + if (!intExp) begin // If we have an Exp of 0 and no implied MSB of L. |
| 108 | + ret.format = Exp0; |
| 109 | + end else begin |
| 110 | + ret.format = EmbeddedExp; |
| 111 | + Bit#(HalfExpW) botZeroes = 0; |
| 112 | + ret.bounds.baseBits = {truncateLSB(ret.bounds.baseBits), botZeroes}; |
| 113 | + ret.bounds.topBits = {truncateLSB(ret.bounds.topBits), botZeroes}; |
| 114 | + end |
| 115 | + |
| 116 | + // Begin calculate newLength in case this is a request just for a |
| 117 | + // representable length: |
| 118 | + CapAddrPlus2 newLength = {2'b0, length}; |
| 119 | + CapAddrPlus2 baseMask = -1; // Override the result from the previous line if |
| 120 | + // we represent everything. |
| 121 | + if (intExp) begin |
| 122 | + CapAddrPlus2 oneInLsb = (lmask ^ (lmask>>1)) >> shiftAmount; |
| 123 | + CapAddrPlus2 newLengthRounded = newLength + oneInLsb; |
| 124 | + newLength = (newLength & (~lmaskLor)); |
| 125 | + newLengthRounded = (newLengthRounded & (~lmaskLor)); |
| 126 | + if (lostSignificantLen) newLength = newLengthRounded; |
| 127 | + baseMask = (lengthIsMax && lostSignificantTop) ? ~lmaskLo : ~lmaskLor; |
| 128 | + end |
| 129 | + |
| 130 | + // Return derived capability |
| 131 | + return SetBoundsReturn { cap: ret |
| 132 | + , exact: exact |
| 133 | + , length: truncate(newLength) |
| 134 | + , mask: truncate(baseMask) }; |
| 135 | +endfunction |
0 commit comments