1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 /**
  28  * Operations on vectors that are not intrinsic candidates
  29  */
  30 @SuppressWarnings("cast")
  31 public final class IntVectorHelper {
  32 
  33     private IntVectorHelper() {}
  34 
  35     public interface BinaryOp {
  36         int apply(int i, int a, int b);
  37     }
  38 
  39     public interface UnaryOp {
  40         int apply(int i, int a);
  41     }
  42 
  43     public static 
  44     IntVector map(IntVector va, IntVector vb, BinaryOp op) {
  45         return va.bOp(vb, (i, a, b) -> op.apply(i, a, b));
  46     }
  47 
  48     public static 
  49     IntVector map(IntVector va, IntVector vb, Vector.Mask<Integer> m, BinaryOp op) {
  50         return va.bOp(vb, m, (i, a, b) -> op.apply(i, a, b));
  51     }
  52 
  53     public static 
  54     IntVector map(IntVector va, UnaryOp op) {
  55         return va.uOp((i, a) -> op.apply(i, a));
  56     }
  57 
  58     public static 
  59     IntVector map(IntVector va, Vector.Mask<Integer> m, UnaryOp op) {
  60         return va.uOp(m, (i, a) -> op.apply(i, a));
  61     }
  62 
  63     public static 
  64     IntVector div(IntVector va, IntVector vb) {
  65         return va.bOp(vb, (i, a, b) -> (int) (a / b));
  66     }
  67 
  68     public static 
  69     IntVector div(IntVector va, IntVector vb, Vector.Mask<Integer> m) {
  70         return va.bOp(vb, m, (i, a, b) -> (int) (a / b));
  71     }
  72 
  73     public static 
  74     IntVector mod(IntVector va, IntVector vb) {
  75         return va.bOp(vb, (i, a, b) -> (int) (a % b));
  76     }
  77 
  78     public static 
  79     IntVector mod(IntVector va, IntVector vb, Vector.Mask<Integer> m) {
  80         return va.bOp(vb, m, (i, a, b) -> (int) (a % b));
  81     }
  82 
  83     public static 
  84     IntVector addExact(IntVector va, IntVector vb) {
  85         return va.bOp(vb, (i, a, b) -> Math.addExact(a, b));
  86     }
  87 
  88     public static 
  89     IntVector addExact(IntVector va, IntVector vb, Vector.Mask<Integer> m) {
  90         return va.bOp(vb, m, (i, a, b) -> Math.addExact(a, b));
  91     }
  92 
  93     public static 
  94     IntVector decrementExact(IntVector va) {
  95         return va.uOp((i, a) -> Math.decrementExact(a));
  96     }
  97 
  98     public static 
  99     IntVector decrementExact(IntVector va, Vector.Mask<Integer> m) {
 100         return va.uOp(m, (i, a) -> Math.decrementExact(a));
 101     }
 102 
 103     public static 
 104     IntVector incrementExact(IntVector va) {
 105         return va.uOp((i, a) -> Math.incrementExact(a));
 106     }
 107 
 108     public static 
 109     IntVector incrementExact(IntVector va, Vector.Mask<Integer> m) {
 110         return va.uOp(m, (i, a) -> Math.incrementExact(a));
 111     }
 112 
 113     public static 
 114     IntVector multiplyExact(IntVector va, IntVector vb) {
 115         return va.bOp(vb, (i, a, b) -> Math.multiplyExact(a, b));
 116     }
 117 
 118     public static 
 119     IntVector multiplyExact(IntVector va, IntVector vb, Vector.Mask<Integer> m) {
 120         return va.bOp(vb, m, (i, a, b) -> Math.multiplyExact(a, b));
 121     }
 122 
 123     public static 
 124     IntVector negateExact(IntVector va) {
 125         return va.uOp((i, a) -> Math.negateExact(a));
 126     }
 127 
 128     public static 
 129     IntVector negateExact(IntVector va, Vector.Mask<Integer> m) {
 130         return va.uOp(m, (i, a) -> Math.negateExact(a));
 131     }
 132 
 133     public static 
 134     IntVector subtractExtract(IntVector va, IntVector vb) {
 135         return va.bOp(vb, (i, a, b) -> Math.subtractExact(a, b));
 136     }
 137 
 138     public static 
 139     IntVector subtractExtract(IntVector va, IntVector vb, Vector.Mask<Integer> m) {
 140         return va.bOp(vb, m, (i, a, b) -> Math.subtractExact(a, b));
 141     }
 142 
 143 
 144 }