- (inst rlwinm res x
- (mod (- 32 posn) 32) ; effectively rotate right
- (- 32 size n-fixnum-tag-bits)
- (- 31 n-fixnum-tag-bits))))
+ (let ((phantom-bits (- (+ size posn) 30)))
+ (cond
+ ((plusp phantom-bits)
+ ;; The byte to be loaded into RES includes sign bits which are not
+ ;; present in the input X physically. RLWINM as used below would
+ ;; mask these out with 0 even for negative inputs.
+ (inst srawi res x phantom-bits)
+ (inst rlwinm res x
+ (mod (- 32 posn (- phantom-bits)) 32)
+ (- 32 size n-fixnum-tag-bits)
+ (- 31 n-fixnum-tag-bits)))
+ (t
+ (inst rlwinm res x
+ (mod (- 32 posn) 32) ; effectively rotate right
+ (- 32 size n-fixnum-tag-bits)
+ (- 31 n-fixnum-tag-bits)))))))