         }
 
         func normalize(pointerData: inout DataWithPointer) {
-            if self.range < Constants.topValue {
+            if self.range < UInt32(Constants.topValue) {
                 self.range <<= 8
-                self.code = (self.code << 8) | pointerData.alignedByte().toInt()
+                self.code = (self.code << 8) | UInt32(pointerData.alignedByte())
             }
         }
 
         func decode(directBits: Int, pointerData: inout DataWithPointer) -> Int {
-            var res = 0
+            var res: UInt32 = 0
             var count = directBits
             repeat {
                 self.range >>= 1
-                self.code -= self.range
-                let t = 0 - (self.code >> 31)
-                self.code += self.range & t
+                self.code = UInt32.subtractWithOverflow(self.code, self.range).0
+                let t = UInt32.subtractWithOverflow(0, self.code >> 31).0
+                self.code = UInt32.addWithOverflow(self.code, self.range & t).0
 
                 if self.code == self.range {
                     self.isCorrupted = true

                        dist += bitTreeReverseDecode(probs: &posDecoders, startIndex: dist - posSlot,
                                                     bits: numDirectBits, rangeDecoder: &rangeDecoder, pointerData: &pointerData)
                    } else {
                        dist += rangeDecoder.decode(directBits: (numDirectBits - Constants.numAlignBits) << Constants.numAlignBits,
                                                    pointerData: &pointerData)
                        dist += alignDecoder.reverseDecode(with: &rangeDecoder, pointerData: &pointerData)
                    }
                    rep0 = dist

        var isRep0Long = Array(repeating: Constants.probInitValue,
                               count: Constants.numStates << Constants.numPosBitsMax)

        var posSlotDecoder: [BitTreeDecoder] = []
        for _ in 0..<Constants.numLenToPosStates {
            posSlotDecoder.append(BitTreeDecoder(numBits: 6))
        }
        var alignDecoder = BitTreeDecoder(numBits: Constants.numAlignBits)
        var posDecoders = Array(repeating: Constants.probInitValue, count: 1 + Constants.numFullDistances - Constants.endPosModelIndex)

