1 #!/bin/bash
   2 #
   3 # Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 #
   6 # This code is free software; you can redistribute it and/or modify it
   7 # under the terms of the GNU General Public License version 2 only, as
   8 # published by the Free Software Foundation.  Oracle designates this
   9 # particular file as subject to the "Classpath" exception as provided
  10 # by Oracle in the LICENSE file that accompanied this code.
  11 #
  12 # This code is distributed in the hope that it will be useful, but WITHOUT
  13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15 # version 2 for more details (a copy is included in the LICENSE file that
  16 # accompanied this code).
  17 #
  18 # You should have received a copy of the GNU General Public License version
  19 # 2 along with this work; if not, write to the Free Software Foundation,
  20 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21 #
  22 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23 # or visit www.oracle.com if you need additional information or have
  24 # questions.
  25 #
  26 
  27 TEMPLATE_FOLDER="templates/"
  28 generate_perf_tests=true
  29 
  30 unary="Unary-op"
  31 unary_masked="Unary-Masked-op"
  32 unary_scalar="Unary-Scalar-op"
  33 ternary="Ternary-op"
  34 ternary_masked="Ternary-Masked-op"
  35 ternary_scalar="Ternary-Scalar-op"
  36 binary="Binary-op"
  37 binary_masked="Binary-Masked-op"
  38 binary_scalar="Binary-Scalar-op"
  39 blend="Blend-op"
  40 compare_template="Compare"
  41 reduction_scalar="Reduction-Scalar-op"
  42 reduction_scalar_min="Reduction-Scalar-Min-op"
  43 reduction_scalar_max="Reduction-Scalar-Max-op"
  44 reduction_op="Reduction-op"
  45 reduction_op_min="Reduction-Min-op"
  46 reduction_op_max="Reduction-Max-op"
  47 unary_math_template="Unary-op-math"
  48 binary_math_template="Binary-op-math"
  49 bool_reduction_scalar="BoolReduction-Scalar-op"
  50 bool_reduction_template="BoolReduction-op"
  51 with_op_template="With-Op"
  52 shift_template="Shift-op"
  53 shift_masked_template="Shift-Masked-op"
  54 gather_template="Gather-op"
  55 scatter_template="Scatter-op"
  56 get_template="Get-op"
  57 rearrange_template="Rearrange"
  58 
  59 function replace_variables {
  60   local filename=$1
  61   local output=$2
  62   local kernel=$3
  63   local test=$4
  64   local op=$5
  65   local init=$6
  66   local guard=$7
  67   local masked=$8
  68   local op_name=$9
  69 
  70   if [ "x${kernel}" != "x" ]; then
  71     local kernel_escaped=$(echo -e "$kernel" | tr '\n' '|')
  72     sed "s/\[\[KERNEL\]\]/${kernel_escaped}/g" $filename > ${filename}.current1
  73     cat ${filename}.current1 | tr '|' "\n" > ${filename}.current
  74     rm -f "${filename}.current1"
  75   else
  76     cp $filename ${filename}.current
  77   fi
  78 
  79   sed -i -e "s/\[\[TEST\]\]/${test}/g" ${filename}.current
  80   sed -i -e "s/\[\[TEST_TYPE\]\]/${masked}/g" ${filename}.current
  81   sed -i -e "s/\[\[TEST_OP\]\]/${op}/g" ${filename}.current
  82   sed -i -e "s/\[\[TEST_INIT\]\]/${init}/g" ${filename}.current
  83   sed -i -e "s/\[\[OP_NAME\]\]/${op_name}/g" ${filename}.current
  84 
  85   # Guard the test if necessary
  86   if [ "$guard" != "" ]; then
  87     echo -e "#if[${guard}]\n" >> $output
  88   fi
  89   cat "${filename}.current" >> $output
  90   if [ "$guard" != "" ]; then
  91     echo -e "#end[${guard}]\n" >> $output
  92   fi
  93 
  94   rm -f ${filename}.current
  95 }
  96 
  97 function gen_op_tmpl {
  98   local template=$1
  99   local test=$2
 100   local op=$3
 101   local unit_output=$4
 102   local perf_output=$5
 103   local perf_scalar_output=$6
 104   local guard=""
 105   local init=""
 106   if [ $# -gt 6 ]; then
 107     guard=$7
 108   fi
 109   if [ $# == 8 ]; then
 110     init=$8
 111   fi
 112 
 113   local masked=""
 114   if [[ $template == *"Masked"* ]]; then
 115     masked="Masked"
 116   fi
 117 
 118   local op_name=""
 119   if [[ $template == *"Shift"* ]]; then
 120     op_name="Shift"
 121   elif [[ $template == *"Get"* ]]; then
 122     op_name="extract"
 123   fi
 124 
 125   local unit_filename="${TEMPLATE_FOLDER}/Unit-${template}.template"
 126   local kernel_filename="${TEMPLATE_FOLDER}/Kernel-${template}.template"
 127   local perf_wrapper_filename="${TEMPLATE_FOLDER}/Perf-wrapper.template"
 128   local perf_vector_filename="${TEMPLATE_FOLDER}/Perf-${template}.template"
 129   local perf_scalar_filename="${TEMPLATE_FOLDER}/Perf-Scalar-${template}.template"
 130 
 131   local kernel=""
 132   if [ -f $kernel_filename ]; then
 133     kernel="$(cat $kernel_filename)"
 134   fi
 135 
 136   # Replace template variables in both unit and performance test files (if any)
 137   replace_variables $unit_filename $unit_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 138 
 139   if [ -f $perf_vector_filename ]; then
 140     replace_variables $perf_vector_filename  $perf_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 141   elif [ -f $kernel_filename ]; then
 142     replace_variables $perf_wrapper_filename $perf_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 143   elif [[ $template != *"-Scalar-"* ]] && [[ $template != "Get-op" ]] && [[ $template != "With-Op" ]]; then
 144     echo "Warning: missing perf: $@"
 145   fi
 146 
 147   if [ -f $perf_scalar_filename ]; then
 148     replace_variables $perf_scalar_filename $perf_scalar_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 149   elif [[ $template != *"-Scalar-"* ]] && [[ $template != "Get-op" ]] && [[ $template != "With-Op" ]]; then
 150     echo "Warning: Missing PERF SCALAR: $perf_scalar_filename"
 151   fi
 152 }
 153 
 154 function gen_binary_alu_op {
 155   echo "Generating binary op $1 ($2)..."
 156   gen_op_tmpl $binary "$@"
 157   gen_op_tmpl $binary_masked "$@"
 158 }
 159 
 160 function gen_shift_cst_op {
 161   echo "Generating Shift constant op $1 ($2)..."
 162   gen_op_tmpl $shift_template "$@"
 163   gen_op_tmpl $shift_masked_template "$@"
 164 }
 165 
 166 function gen_unary_alu_op {
 167   echo "Generating unary op $1 ($2)..."
 168   gen_op_tmpl $unary_scalar "$@"
 169   gen_op_tmpl $unary "$@"
 170   gen_op_tmpl $unary_masked "$@"
 171 }
 172 
 173 function gen_ternary_alu_op {
 174   echo "Generating ternary op $1 ($2)..."
 175   gen_op_tmpl $ternary_scalar "$@"
 176   gen_op_tmpl $ternary "$@"
 177   gen_op_tmpl $ternary_masked "$@"
 178 }
 179 
 180 function gen_binary_op {
 181   echo "Generating binary op $1 ($2)..."
 182 #  gen_op_tmpl $binary_scalar "$@"
 183   gen_op_tmpl $binary "$@"
 184 }
 185 
 186 function gen_reduction_op {
 187   echo "Generating reduction op $1 ($2)..."
 188   gen_op_tmpl $reduction_scalar "$@"
 189   gen_op_tmpl $reduction_op "$@"
 190 }
 191 
 192 function gen_reduction_op_min {
 193   echo "Generating reduction op $1 ($2)..."
 194   gen_op_tmpl $reduction_scalar_min "$@"
 195   gen_op_tmpl $reduction_op_min "$@"
 196 }
 197 
 198 function gen_reduction_op_max {
 199   echo "Generating reduction op $1 ($2)..."
 200   gen_op_tmpl $reduction_scalar_max "$@"
 201   gen_op_tmpl $reduction_op_max "$@"
 202 }
 203 
 204 function gen_bool_reduction_op {
 205   echo "Generating boolean reduction op $1 ($2)..."
 206   gen_op_tmpl $bool_reduction_scalar "$@"
 207   gen_op_tmpl $bool_reduction_template "$@"
 208 }
 209 
 210 function gen_with_op {
 211   echo "Generating with op $1 ($2)..."
 212   gen_op_tmpl $with_op_template "$@"
 213 }
 214 
 215 function gen_get_op {
 216   echo "Generating get op $1 ($2)..."
 217   gen_op_tmpl $get_template "$@"
 218 }
 219 
 220 function gen_unit_header {
 221   cat $TEMPLATE_FOLDER/Unit-header.template > $1
 222 }
 223 
 224 function gen_unit_footer {
 225   cat $TEMPLATE_FOLDER/Unit-footer.template >> $1
 226 }
 227 
 228 function gen_perf_header {
 229   cat $TEMPLATE_FOLDER/Perf-header.template > $1
 230 }
 231 
 232 function gen_perf_footer {
 233   cat $TEMPLATE_FOLDER/Perf-footer.template >> $1
 234 }
 235 
 236 function gen_perf_scalar_header {
 237   cat $TEMPLATE_FOLDER/Perf-Scalar-header.template > $1
 238 }
 239 
 240 function gen_perf_scalar_footer {
 241   cat $TEMPLATE_FOLDER/Perf-Scalar-footer.template >> $1
 242 }
 243 unit_output="unit_tests.template"
 244 perf_output="perf_tests.template"
 245 perf_scalar_output="perf_scalar_tests.template"
 246 gen_unit_header $unit_output
 247 gen_perf_header $perf_output
 248 gen_perf_scalar_header $perf_scalar_output
 249 
 250 # ALU binary ops.
 251 gen_binary_alu_op "add" "a + b" $unit_output $perf_output $perf_scalar_output
 252 gen_binary_alu_op "sub" "a - b" $unit_output $perf_output $perf_scalar_output
 253 gen_binary_alu_op "div" "a \/ b" $unit_output $perf_output $perf_scalar_output "FP"
 254 gen_binary_alu_op "mul" "a \* b" $unit_output $perf_output $perf_scalar_output
 255 gen_binary_alu_op "and" "a \& b" $unit_output $perf_output $perf_scalar_output "BITWISE"
 256 gen_binary_alu_op "or" "a | b" $unit_output $perf_output $perf_scalar_output "BITWISE"
 257 gen_binary_alu_op "xor" "a ^ b" $unit_output $perf_output $perf_scalar_output "BITWISE"
 258 
 259 # Shifts
 260 gen_binary_alu_op "shiftR" "(a >>> b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 261 gen_binary_alu_op "shiftL" "(a << b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 262 gen_binary_alu_op "aShiftR" "(a >> b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 263 gen_shift_cst_op "aShiftR" "(a >> b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 264 gen_shift_cst_op "shiftR" "(a >>> b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 265 gen_shift_cst_op "shiftL" "(a << b)" $unit_output $perf_output $perf_scalar_output "intOrLong"
 266 gen_shift_cst_op "aShiftR" "(a >> (b \& 7))" $unit_output $perf_output $perf_scalar_output "byte"
 267 gen_shift_cst_op "shiftL" "(a << (b \& 7))" $unit_output $perf_output $perf_scalar_output "byte"
 268 gen_shift_cst_op "shiftR" "((a \& 0xFF) >>> (b \& 7))" $unit_output $perf_output $perf_scalar_output "byte"
 269 gen_shift_cst_op "aShiftR" "(a >> (b \& 15))" $unit_output $perf_output $perf_scalar_output "short"
 270 gen_shift_cst_op "shiftL" "(a << (b \& 15))" $unit_output $perf_output $perf_scalar_output "short"
 271 gen_shift_cst_op "shiftR" "((a \& 0xFFFF) >>> (b \& 15))" $unit_output $perf_output $perf_scalar_output "short"
 272 
 273 # Masked reductions.
 274 gen_binary_op "max" "Math.max(a, b)" $unit_output $perf_output $perf_scalar_output
 275 gen_binary_op "min" "Math.min(a, b)" $unit_output $perf_output $perf_scalar_output
 276 
 277 # Reductions.
 278 gen_reduction_op "andAll" "\&" $unit_output $perf_output $perf_scalar_output "BITWISE" "-1"
 279 gen_reduction_op "orAll" "|" $unit_output $perf_output $perf_scalar_output "BITWISE" "0"
 280 gen_reduction_op "xorAll" "^" $unit_output $perf_output $perf_scalar_output "BITWISE" "0"
 281 gen_reduction_op "addAll" "+" $unit_output $perf_output $perf_scalar_output "" "0"
 282 gen_reduction_op "mulAll" "*" $unit_output $perf_output $perf_scalar_output "" "1"
 283 gen_reduction_op_min "minAll" "" $unit_output $perf_output $perf_scalar_output "" "\$Wideboxtype\$.\$MaxValue\$"
 284 gen_reduction_op_max "maxAll" "" $unit_output $perf_output $perf_scalar_output "" "\$Wideboxtype\$.\$MinValue\$"
 285 
 286 
 287 # Boolean reductions.
 288 gen_bool_reduction_op "anyTrue" "|" $unit_output $perf_output $perf_scalar_output "BITWISE" "false"
 289 gen_bool_reduction_op "allTrue" "\&" $unit_output $perf_output $perf_scalar_output "BITWISE" "true"
 290 
 291 #Insert
 292 gen_with_op "with" "" $unit_output $perf_output $perf_scalar_output "" ""
 293 
 294 # Compares
 295 gen_op_tmpl $compare_template "lessThan" "<" $unit_output $perf_output $perf_scalar_output
 296 gen_op_tmpl $compare_template "greaterThan" ">" $unit_output $perf_output $perf_scalar_output
 297 gen_op_tmpl $compare_template "equal" "==" $unit_output $perf_output $perf_scalar_output
 298 gen_op_tmpl $compare_template "notEqual" "!=" $unit_output $perf_output $perf_scalar_output
 299 gen_op_tmpl $compare_template "lessThanEq" "<=" $unit_output $perf_output $perf_scalar_output
 300 gen_op_tmpl $compare_template "greaterThanEq" ">=" $unit_output $perf_output $perf_scalar_output
 301 
 302 # Blend.
 303 gen_op_tmpl $blend "blend" "" $unit_output $perf_output $perf_scalar_output
 304 
 305 # Rearrange
 306 gen_op_tmpl $rearrange_template "rearrange" "" $unit_output $perf_output $perf_scalar_output
 307 
 308 # Get
 309 gen_get_op "" "" $unit_output $perf_output $perf_scalar_output
 310 
 311 # Math
 312 gen_op_tmpl $unary_math_template "sin" "Math.sin((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 313 gen_op_tmpl $unary_math_template "exp" "Math.exp((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 314 gen_op_tmpl $unary_math_template "log1p" "Math.log1p((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 315 gen_op_tmpl $unary_math_template "log" "Math.log((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 316 gen_op_tmpl $unary_math_template "log10" "Math.log10((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 317 gen_op_tmpl $unary_math_template "expm1" "Math.expm1((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 318 gen_op_tmpl $unary_math_template "cos" "Math.cos((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 319 gen_op_tmpl $unary_math_template "tan" "Math.tan((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 320 gen_op_tmpl $unary_math_template "sinh" "Math.sinh((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 321 gen_op_tmpl $unary_math_template "cosh" "Math.cosh((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 322 gen_op_tmpl $unary_math_template "tanh" "Math.tanh((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 323 gen_op_tmpl $unary_math_template "asin" "Math.asin((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 324 gen_op_tmpl $unary_math_template "acos" "Math.acos((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 325 gen_op_tmpl $unary_math_template "atan" "Math.atan((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 326 gen_op_tmpl $unary_math_template "cbrt" "Math.cbrt((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 327 gen_op_tmpl $binary_math_template "hypot" "Math.hypot((double)a, (double)b)" $unit_output $perf_output $perf_scalar_output "FP"
 328 gen_op_tmpl $binary_math_template "pow" "Math.pow((double)a, (double)b)" $unit_output $perf_output $perf_scalar_output "FP"
 329 gen_op_tmpl $binary_math_template "atan2" "Math.atan2((double)a, (double)b)" $unit_output $perf_output $perf_scalar_output "FP"
 330 
 331 # Ternary operations.
 332 gen_ternary_alu_op "fma" "Math.fma(a, b, c)" $unit_output $perf_output $perf_scalar_output "FP"
 333 
 334 # Unary operations.
 335 gen_unary_alu_op "neg" "-((\$type\$)a)" $unit_output $perf_output $perf_scalar_output
 336 gen_unary_alu_op "abs" "Math.abs((\$type\$)a)" $unit_output $perf_output $perf_scalar_output
 337 gen_unary_alu_op "not" "~((\$type\$)a)" $unit_output $perf_output $perf_scalar_output "BITWISE"
 338 gen_unary_alu_op "sqrt" "Math.sqrt((double)a)" $unit_output $perf_output $perf_scalar_output "FP"
 339 
 340 # Gather Scatter operations.
 341 gen_op_tmpl $gather_template "gather" "" $unit_output $perf_output $perf_scalar_output "!byteOrShort"
 342 gen_op_tmpl $scatter_template "scatter" "" $unit_output $perf_output $perf_scalar_output "!byteOrShort"
 343 
 344 gen_unit_footer $unit_output
 345 gen_perf_footer $perf_output
 346 gen_perf_scalar_footer $perf_scalar_output
 347 
 348 rm -f templates/*.current*