1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.internal.foreign.abi;
  24 
  25 import java.util.ArrayList;
  26 
  27 class ShuffleRecipeOperationCollector {
  28     private static final int MAX_VECTOR_WIDTH_BITS = 256; // Look it up
  29     private static final int MAX_VECTOR_WIDTH_LONGS = MAX_VECTOR_WIDTH_BITS / 64;
  30 
  31     private final ArrayList<ShuffleRecipeOperation>[] ops;
  32     private int nPulls;
  33 
  34     @SuppressWarnings("unchecked")
  35     ShuffleRecipeOperationCollector() {
  36         ops = (ArrayList<ShuffleRecipeOperation>[])new ArrayList<?>[ShuffleRecipeClass.values().length];
  37         for (ShuffleRecipeClass c : ShuffleRecipeClass.values()) {
  38             ops[c.ordinal()] = new ArrayList<>();
  39         }
  40     }
  41 
  42     ArrayList<ShuffleRecipeOperation>[] getOps() {
  43         return ops;
  44     }
  45 
  46     int getNoofPulls() {
  47         return nPulls;
  48     }
  49 
  50     int getTotalNumberOfOps() {
  51         int n = 0;
  52         for (ArrayList<ShuffleRecipeOperation> oparr : ops) {
  53             n += oparr.size();
  54             n++; // account for implicit STOP at the end of each class
  55         }
  56         return n;
  57     }
  58 
  59     void add(ShuffleRecipeClass c, ShuffleRecipeOperation op) {
  60         if (op == ShuffleRecipeOperation.STOP) {
  61             throw new IllegalArgumentException("STOP is implicit and must not be added explicitly");
  62         }
  63         ops[c.ordinal()].add(op);
  64         if (op == ShuffleRecipeOperation.PULL) {
  65             nPulls++;
  66         }
  67     }
  68 
  69     void add(ShuffleRecipeClass c, ShuffleRecipeOperation op, long n) {
  70         for (long i = 0; i < n; i++) {
  71             add(c, op);
  72         }
  73     }
  74 
  75     void addPull(ShuffleRecipeClass c) {
  76         addPulls(c, 1);
  77     }
  78 
  79     void addPulls(ShuffleRecipeClass c, long n) {
  80         add(c, ShuffleRecipeOperation.PULL, n);
  81         if (c == ShuffleRecipeClass.VECTOR && n < MAX_VECTOR_WIDTH_LONGS) {
  82             add(c, ShuffleRecipeOperation.SKIP);
  83         }
  84     }
  85 
  86     public String asString() {
  87         StringBuilder sb = new StringBuilder();
  88         for(ShuffleRecipeClass cls : ShuffleRecipeClass.values()) {
  89             sb.append(cls.name()).append(": ").append(ops[cls.ordinal()]).append("\n");
  90         }
  91         return sb.toString();
  92     }
  93 }