< prev index next >

src/java.base/share/classes/jdk/internal/foreign/abi/ShuffleRecipeBuilder.java

Print this page


   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 jdk.internal.foreign.Util;
  26 import java.util.ArrayList;



  27 
  28 class ShuffleRecipeBuilder {
  29     private final ShuffleRecipeOperationCollector arguments = new ShuffleRecipeOperationCollector();
  30     private final ShuffleRecipeOperationCollector returns = new ShuffleRecipeOperationCollector();
  31 
  32     ShuffleRecipeBuilder() {






  33     }
  34 
  35     ShuffleRecipeOperationCollector getArgumentsCollector() {
  36         return arguments;
  37     }
  38 
  39     ShuffleRecipeOperationCollector getReturnsCollector() {
  40         return returns;







  41     }
  42 
  43     private int getTotalNumberOfOps() {
  44         return arguments.getTotalNumberOfOps() + returns.getTotalNumberOfOps();
  45     }
  46 
  47     private long[] allocArray() {
  48         long nOpBits = getTotalNumberOfOps() * ShuffleRecipeOperation.BITS_PER_OP;
  49         long nOpWords = nOpBits / 64;
  50 
  51         long nBits = nOpBits + nOpWords; // MSB in each word is reserved
  52 
  53         nBits = Util.alignUp(nBits, 64);
  54 
  55         long nWords = nBits / 64;
  56 
  57         return new long[(int)nWords];
  58     }
  59 
  60     private void encodeOp(long[] arr, int index, ShuffleRecipeOperation op) {
  61         int n = index * ShuffleRecipeOperation.BITS_PER_OP;
  62 
  63         int word = n / 64;
  64         n += word; // MSB bits are reserved
  65 
  66         int start_bit = n % 64;
  67         if (start_bit == 63) {
  68             word++;
  69             start_bit++;
  70         }
  71 
  72         arr[word] |= (long)op.ordinal() << start_bit;
  73     }
  74 
  75     private long[] recipeToLongArray() {
  76         long[] arr = allocArray();
  77 
  78         for (int i = 0; i < arr.length; i++) {
  79             arr[i] = (1L << 63);
  80         }
  81 
  82         int i = 0;
  83         for (ArrayList<ShuffleRecipeOperation> oparr : arguments.getOps()) {
  84             for (ShuffleRecipeOperation op : oparr) {
  85                 encodeOp(arr, i++, op);
  86             }
  87             encodeOp(arr, i++, ShuffleRecipeOperation.STOP);
  88         }
  89         for (ArrayList<ShuffleRecipeOperation> oparr : returns.getOps()) {
  90             for (ShuffleRecipeOperation op : oparr) {
  91                 encodeOp(arr, i++, op);
  92             }
  93             encodeOp(arr, i++, ShuffleRecipeOperation.STOP);
  94         }
  95 
  96         return arr;
  97     }
  98 
  99     ShuffleRecipe build() {
 100         return new ShuffleRecipe(recipeToLongArray(), getArgumentsCollector().getNoofPulls(), getReturnsCollector().getNoofPulls());
 101     }
 102 
 103     public String asString() {
 104         StringBuilder sb = new StringBuilder();
 105 
 106         sb.append("ShuffleRecipe: {\n");
 107         sb.append("  Arguments: {\n");
 108         sb.append(arguments.asString().indent(4));
 109         sb.append("  }\n");
 110         sb.append("  Returns: {\n");
 111         sb.append(returns.asString().indent(4));
 112         sb.append("  }\n");
 113         sb.append("}\n");
 114 
 115         return sb.toString();
 116     }
 117 }
   1 /*
   2  * Copyright (c) 2015, 2019, 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 jdk.internal.foreign.Util;
  26 import java.util.ArrayList;
  27 import java.util.HashMap;
  28 import java.util.List;
  29 import java.util.Map;
  30 
  31 class ShuffleRecipeBuilder {


  32 
  33     List<ShuffleRecipeOperation> ops = new ArrayList<>();
  34     Map<ArgumentBinding, Long> offsets = new HashMap<>();
  35     int nArgPulls;
  36     int nRetPulls;
  37 
  38     void addSkip() {
  39         ops.add(ShuffleRecipeOperation.SKIP);
  40     }
  41 
  42     void addStop() {
  43         ops.add(ShuffleRecipeOperation.STOP);
  44     }
  45 
  46     void addPulls(boolean isReturn, long n) {
  47         for (long i = 0; i < n; i++) {
  48             ops.add(ShuffleRecipeOperation.PULL);
  49             if (isReturn) {
  50                 nRetPulls++;
  51             } else {
  52                 nArgPulls++;
  53             }
  54         }
  55     }
  56 
  57     void addOffset(ArgumentBinding binding, long offset) {
  58         offsets.put(binding, offset);
  59     }
  60 
  61     private long[] allocArray() {
  62         long nOpBits = ops.size() * ShuffleRecipeOperation.BITS_PER_OP;
  63         long nOpWords = nOpBits / 64;
  64 
  65         long nBits = nOpBits + nOpWords; // MSB in each word is reserved
  66 
  67         nBits = Util.alignUp(nBits, 64);
  68 
  69         long nWords = nBits / 64;
  70 
  71         return new long[(int)nWords];
  72     }
  73 
  74     private void encodeOp(long[] arr, int index, ShuffleRecipeOperation op) {
  75         int n = index * ShuffleRecipeOperation.BITS_PER_OP;
  76 
  77         int word = n / 64;
  78         n += word; // MSB bits are reserved
  79 
  80         int start_bit = n % 64;
  81         if (start_bit == 63) {
  82             word++;
  83             start_bit++;
  84         }
  85 
  86         arr[word] |= (long)op.ordinal() << start_bit;
  87     }
  88 
  89     private long[] recipeToLongArray() {
  90         long[] arr = allocArray();
  91 
  92         for (int i = 0; i < arr.length; i++) {
  93             arr[i] = (1L << 63);
  94         }
  95 
  96         int i = 0;
  97 
  98         for (ShuffleRecipeOperation op : ops) {






  99             encodeOp(arr, i++, op);
 100         }


 101 
 102         return arr;
 103     }
 104 
 105     ShuffleRecipe build() {
 106         return new ShuffleRecipe(recipeToLongArray(), nArgPulls, nRetPulls, offsets);
 107     }
 108 
 109     public String asString() {
 110         StringBuilder sb = new StringBuilder();
 111 
 112         sb.append("ShuffleRecipe: {\n");
 113         sb.append("  Operations: {\n");
 114         sb.append(ops);



 115         sb.append("  }\n");
 116         sb.append("}\n");
 117 
 118         return sb.toString();
 119     }
 120 }
< prev index next >