[vector] optimize rotate left/right

Lupusoru, Razvan A razvan.a.lupusoru at intel.com
Fri May 18 21:51:36 UTC 2018


Gotcha - I understand now. It was not immediately obvious since the masking of shift count happens implicitly so semantically it did not make sense to me. It seems correct now. Thanks for explanation.

--Razvan

-----Original Message-----
From: Paul Sandoz [mailto:paul.sandoz at oracle.com] 
Sent: Friday, May 18, 2018 2:49 PM
To: Lupusoru, Razvan A <razvan.a.lupusoru at intel.com>
Cc: panama-dev at openjdk.java.net
Subject: Re: [vector] optimize rotate left/right



> 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