[vector] optimize rotate left/right

Paul Sandoz paul.sandoz at oracle.com
Fri May 18 21:49:06 UTC 2018



> On May 18, 2018, at 2:05 PM, Lupusoru, Razvan A <razvan.a.lupusoru at intel.com> wrote:
> 
> Hi Paul,
> 
> I am not quite sure that taking negative of shift count is correct. I believe that for int case, it should be 32 - shiftcount. 
> 
> public final IntVector<S> rotateL(int s) {
>  return shiftL(s).or(shiftR(Integer.SIZE-s));
> }
> 
> Does that sound right to you?
> 

It assumes, for integers, the low 5 bits are taken, which is the same behavior as the >>> operation and the iushr byte code. I believe from a prior email you sent that for vector shifts by a scalar truncation is performed but we may need to revisit the intrinsic implementation of variable vector shifts to support truncation rather than saturation. Did i get that correct or am i confused?

The vector implementation was translated from the scalar equivalent on Integer (and the vector specification leverages the scalar equivalent):

/**
 * Returns the value obtained by rotating the two's complement binary
 * representation of the specified {@code int} value left by the
 * specified number of bits.  (Bits shifted out of the left hand, or
 * high-order, side reenter on the right, or low-order.)
 *
 * <p>Note that left rotation with a negative distance is equivalent to
 * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
 * distance)}.  Note also that rotation by any multiple of 32 is a
 * no-op, so all but the last five bits of the rotation distance can be
 * ignored, even if the distance is negative: {@code rotateLeft(val,
 * distance) == rotateLeft(val, distance & 0x1F)}.
 *
 * @param i the value whose bits are to be rotated left
 * @param distance the number of bit positions to rotate left
 * @return the value obtained by rotating the two's complement binary
 *     representation of the specified {@code int} value left by the
 *     specified number of bits.
 * @since 1.5
 */
public static int rotateLeft(int i, int distance) {
    return (i << distance) | (i >>> -distance);
}

Paul.

 
> Thanks,
> Razvan
> 
> -----Original Message-----
> From: panama-dev [mailto:panama-dev-bounces at openjdk.java.net] On Behalf Of Paul Sandoz
> Sent: Thursday, May 17, 2018 5:51 PM
> To: panama-dev at openjdk.java.net
> Subject: [vector] optimize rotate left/right
> 
> Hi,
> 
> We can implement the rotate left/right by composing with shift left/right and or:
> 
>  http://cr.openjdk.java.net/~psandoz/panama/bit-rotate-optimize/webrev/ <http://cr.openjdk.java.net/~psandoz/panama/bit-rotate-optimize/webrev/>
> 
> (Tests are needed for shift and rotate)
> 
> I made the methods final to indicate that they are considered suitable optimized (thus the test reporting on method overriding can skip these methods).
> 
> Paul.



More information about the panama-dev mailing list