Signed saturation.
If
This function returns a (possibly negative) integer. For consistency with
signed-byte-p, size must be strictly greater than 0. In contrast, the
related bitops::bitops/saturate functions always return
Function:
(defun logsat (size i) (declare (type unsigned-byte size)) (declare (xargs :guard (and (and (integerp size) (< 0 size)) (integerp i)))) (declare (xargs :split-types t)) (let ((__function__ 'logsat)) (declare (ignorable __function__)) (let* ((i (mbe :logic (ifix i) :exec i)) (val (expt2 (the unsigned-byte (1- size)))) (maxval (the unsigned-byte (1- (the unsigned-byte val)))) (minval (- val))) (declare (type unsigned-byte val maxval) (type integer i minval)) (if (>= i maxval) maxval (if (<= i minval) minval i)))))
Theorem:
(defthm logsat-type (b* ((int (logsat size i))) (integerp int)) :rule-classes :type-prescription)