1 #!/bin/bash
   2 #
   3 # Copyright (c) 2018, 2020, 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 any
  24 # questions.
  25 #
  26 
  27 generate_perf_tests=$1
  28 
  29 TEMPLATE_FOLDER="templates/"
  30 
  31 unit_output="unit_tests.template"
  32 perf_output="perf_tests.template"
  33 perf_scalar_output="perf_scalar_tests.template"
  34 
  35 unary="Unary-op"
  36 unary_masked="Unary-Masked-op"
  37 unary_scalar="Unary-Scalar-op"
  38 ternary="Ternary-op"
  39 ternary_masked="Ternary-Masked-op"
  40 ternary_scalar="Ternary-Scalar-op"
  41 binary="Binary-op"
  42 binary_masked="Binary-Masked-op"
  43 binary_scalar="Binary-Scalar-op"
  44 blend="Blend-op"
  45 test_template="Test"
  46 compare_template="Compare"
  47 reduction_scalar="Reduction-Scalar-op"
  48 reduction_scalar_min="Reduction-Scalar-Min-op"
  49 reduction_scalar_max="Reduction-Scalar-Max-op"
  50 reduction_scalar_masked="Reduction-Scalar-Masked-op"
  51 reduction_scalar_min_masked="Reduction-Scalar-Masked-Min-op"
  52 reduction_scalar_max_masked="Reduction-Scalar-Masked-Max-op"
  53 reduction_op="Reduction-op"
  54 reduction_op_min="Reduction-Min-op"
  55 reduction_op_max="Reduction-Max-op"
  56 reduction_op_masked="Reduction-Masked-op"
  57 reduction_op_min_masked="Reduction-Masked-Min-op"
  58 reduction_op_max_masked="Reduction-Masked-Max-op"
  59 unary_math_template="Unary-op-math"
  60 binary_math_template="Binary-op-math"
  61 bool_reduction_scalar="BoolReduction-Scalar-op"
  62 bool_reduction_template="BoolReduction-op"
  63 with_op_template="With-Op"
  64 shift_template="Shift-op"
  65 shift_masked_template="Shift-Masked-op"
  66 gather_template="Gather-op"
  67 gather_masked_template="Gather-Masked-op"
  68 scatter_template="Scatter-op"
  69 scatter_masked_template="Scatter-Masked-op"
  70 get_template="Get-op"
  71 rearrange_template="Rearrange"
  72 broadcast_template="Broadcast"
  73 zero_template="Zero"
  74 slice_template="Slice-op"
  75 slice1_template="Slice-bop"
  76 slice1_masked_template="Slice-Masked-bop"
  77 unslice_template="Unslice-op"
  78 unslice1_template="Unslice-bop"
  79 unslice1_masked_template="Unslice-Masked-bop"
  80 
  81 function replace_variables {
  82   local filename=$1
  83   local output=$2
  84   local kernel=$3
  85   local test=$4
  86   local op=$5
  87   local init=$6
  88   local guard=$7
  89   local masked=$8
  90   local op_name=$9
  91 
  92   if [ "x${kernel}" != "x" ]; then
  93     local kernel_escaped=$(echo -e "$kernel" | tr '\n' '|')
  94     sed "s/\[\[KERNEL\]\]/${kernel_escaped}/g" $filename > ${filename}.current1
  95     cat ${filename}.current1 | tr '|' "\n" > ${filename}.current
  96     rm -f "${filename}.current1"
  97   else
  98     cp $filename ${filename}.current
  99   fi
 100 
 101   # Check if we need to do multiple replacements
 102   # If you want to emit for an operation using lanewise(VectorOperator.**, ..) and also using dedicated instruction (e.g. add(..)), then
 103   # pass the 'test' argument as "OPERATOR_NAME+func_Name" (e.g. "ADD+add")
 104   # if there is a masked version available for the operation add "withMask" to 'test' argument (e.g. "ADD+add+withMask")
 105   local test_func=""
 106   local withMask=""
 107   local tests=($(awk -F+ '{$1=$1} 1' <<< $test))
 108   if [ "${tests[1]}" != "" ]; then
 109     test=${tests[0]}
 110     test_func=${tests[1]}
 111     withMask=${tests[2]}
 112   fi
 113 
 114   sed_prog="
 115     s/\<OPTIONAL\>\(.*\)\<\\OPTIONAL\>/\1/g
 116     s/\[\[TEST_TYPE\]\]/${masked}/g
 117     s/\[\[TEST_OP\]\]/${op}/g
 118     s/\[\[TEST_INIT\]\]/${init}/g
 119     s/\[\[OP_NAME\]\]/${op_name}/g
 120   "
 121   sed_prog_2="$sed_prog
 122     s/\[\[TEST\]\]/${test_func}/g
 123     s/[.][^(]*(VectorOperators.$test_func, /.$test_func(/g
 124     s/[.][^(]*(VectorOperators.$test_func,/.$test_func(/g
 125     s/[.][^(]*(VectorOperators.$test_func/.$test_func(/g
 126   "
 127   sed_prog="
 128     $sed_prog
 129     s/\[\[TEST\]\]/${test}/g
 130   "
 131 
 132   # Guard the test if necessary
 133   if [ "$guard" != "" ]; then
 134     echo -e "#if[${guard}]\n" >> $output
 135   fi
 136   sed -e "$sed_prog" < ${filename}.current >> $output
 137   # If we also have a dedicated function for the operation then use 2nd sed expression
 138   if [[ "$filename" == *"Unit"* ]] && [ "$test_func" != "" ]; then
 139     if [ "$masked" == "" ] || [ "$withMask" != "" ]; then 
 140       sed -e "$sed_prog_2" < ${filename}.current >> $output
 141     fi
 142   fi
 143   if [ "$guard" != "" ]; then
 144     echo -e "#end[${guard}]\n" >> $output
 145   fi
 146 
 147   rm -f ${filename}.current
 148 }
 149 
 150 function gen_op_tmpl {
 151   local template=$1
 152   local test=$2
 153   local op=$3
 154   local guard=""
 155   local init=""
 156   if [ $# -gt 3 ]; then
 157     guard=$4
 158   fi
 159   if [ $# == 5 ]; then
 160     init=$5
 161   fi
 162 
 163   local masked=""
 164   if [[ $template == *"Masked"* ]]; then
 165     masked="Masked"
 166   fi
 167 
 168   local op_name=""
 169   if [[ $template == *"Shift"* ]]; then
 170     op_name="Shift"
 171   elif [[ $template == *"Get"* ]]; then
 172     op_name="extract"
 173   fi
 174 
 175   local kernel_filename="${TEMPLATE_FOLDER}/Kernel-${template}.template"
 176   local unit_filename="${TEMPLATE_FOLDER}/Unit-${template}.template"
 177   if [ ! -f $unit_filename ]; then
 178     # Leverage general unit code snippet if no specialization exists
 179     unit_filename="${TEMPLATE_FOLDER}/Unit-${template%_*}.template"
 180     echo $unit_filename
 181   fi
 182 
 183   local kernel=""
 184   if [ -f $kernel_filename ]; then
 185     kernel="$(cat $kernel_filename)"
 186   fi
 187 
 188   # Replace template variables in unit test files (if any)
 189   replace_variables $unit_filename $unit_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 190 
 191   if [ $generate_perf_tests == true ]; then
 192     # Replace template variables in performance test files (if any)
 193     local perf_wrapper_filename="${TEMPLATE_FOLDER}/Perf-wrapper.template"
 194     local perf_vector_filename="${TEMPLATE_FOLDER}/Perf-${template}.template"
 195     local perf_scalar_filename="${TEMPLATE_FOLDER}/Perf-Scalar-${template}.template"
 196 
 197     if [ -f $perf_vector_filename ]; then
 198       replace_variables $perf_vector_filename  $perf_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 199     elif [ -f $kernel_filename ]; then
 200       replace_variables $perf_wrapper_filename $perf_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 201     elif [[ $template != *"-Scalar-"* ]] && [[ $template != "Get-op" ]] && [[ $template != "With-Op" ]]; then
 202       echo "Warning: missing perf: $@"
 203     fi
 204 
 205     if [ -f $perf_scalar_filename ]; then
 206       replace_variables $perf_scalar_filename $perf_scalar_output "$kernel" "$test" "$op" "$init" "$guard" "$masked" "$op_name"
 207     elif [[ $template != *"-Scalar-"* ]] && [[ $template != "Get-op" ]] && [[ $template != "With-Op" ]]; then
 208       echo "Warning: Missing PERF SCALAR: $perf_scalar_filename"
 209     fi
 210   fi
 211 }
 212 
 213 function gen_binary_alu_op {
 214   echo "Generating binary op $1 ($2)..."
 215   gen_op_tmpl $binary "$@"
 216   gen_op_tmpl $binary_masked "$@"
 217 }
 218 
 219 function gen_shift_cst_op {
 220   echo "Generating Shift constant op $1 ($2)..."
 221   gen_op_tmpl $shift_template "$@"
 222   gen_op_tmpl $shift_masked_template "$@"
 223 }
 224 
 225 function gen_unary_alu_op {
 226   echo "Generating unary op $1 ($2)..."
 227   gen_op_tmpl $unary_scalar "$@"
 228   gen_op_tmpl $unary "$@"
 229   gen_op_tmpl $unary_masked "$@"
 230 }
 231 
 232 function gen_ternary_alu_op {
 233   echo "Generating ternary op $1 ($2)..."
 234   gen_op_tmpl $ternary_scalar "$@"
 235   gen_op_tmpl $ternary "$@"
 236   gen_op_tmpl $ternary_masked "$@"
 237 }
 238 
 239 function gen_binary_op {
 240   echo "Generating binary op $1 ($2)..."
 241 #  gen_op_tmpl $binary_scalar "$@"
 242   gen_op_tmpl $binary "$@"
 243   gen_op_tmpl $binary_masked "$@"
 244 }
 245 
 246 function gen_binary_op_no_masked {
 247   echo "Generating binary op $1 ($2)..."
 248 #  gen_op_tmpl $binary_scalar "$@"
 249   gen_op_tmpl $binary "$@"
 250 }
 251 
 252 function gen_reduction_op {
 253   echo "Generating reduction op $1 ($2)..."
 254   gen_op_tmpl $reduction_scalar "$@"
 255   gen_op_tmpl $reduction_op "$@"
 256   gen_op_tmpl $reduction_scalar_masked "$@"
 257   gen_op_tmpl $reduction_op_masked "$@"
 258 }
 259 
 260 function gen_reduction_op_min {
 261   echo "Generating reduction op $1 ($2)..."
 262   gen_op_tmpl $reduction_scalar_min "$@"
 263   gen_op_tmpl $reduction_op_min "$@"
 264   gen_op_tmpl $reduction_scalar_min_masked "$@"
 265   gen_op_tmpl $reduction_op_min_masked "$@"
 266 }
 267 
 268 function gen_reduction_op_max {
 269   echo "Generating reduction op $1 ($2)..."
 270   gen_op_tmpl $reduction_scalar_max "$@"
 271   gen_op_tmpl $reduction_op_max "$@"
 272   gen_op_tmpl $reduction_scalar_max_masked "$@"
 273   gen_op_tmpl $reduction_op_max_masked "$@"
 274 }
 275 
 276 function gen_bool_reduction_op {
 277   echo "Generating boolean reduction op $1 ($2)..."
 278   gen_op_tmpl $bool_reduction_scalar "$@"
 279   gen_op_tmpl $bool_reduction_template "$@"
 280 }
 281 
 282 function gen_with_op {
 283   echo "Generating with op $1 ($2)..."
 284   gen_op_tmpl $with_op_template "$@"
 285 }
 286 
 287 function gen_get_op {
 288   echo "Generating get op $1 ($2)..."
 289   gen_op_tmpl $get_template "$@"
 290 }
 291 
 292 function gen_unit_header {
 293   cat $TEMPLATE_FOLDER/Unit-header.template > $1
 294 }
 295 
 296 function gen_unit_footer {
 297   cat $TEMPLATE_FOLDER/Unit-footer.template >> $1
 298 }
 299 
 300 function gen_perf_header {
 301   cat $TEMPLATE_FOLDER/Perf-header.template > $1
 302 }
 303 
 304 function gen_perf_footer {
 305   cat $TEMPLATE_FOLDER/Perf-footer.template >> $1
 306 }
 307 
 308 function gen_perf_scalar_header {
 309   cat $TEMPLATE_FOLDER/Perf-Scalar-header.template > $1
 310 }
 311 
 312 function gen_perf_scalar_footer {
 313   cat $TEMPLATE_FOLDER/Perf-Scalar-footer.template >> $1
 314 }
 315 
 316 gen_unit_header $unit_output
 317 
 318 if [ $generate_perf_tests == true ]; then
 319   gen_perf_header $perf_output
 320   gen_perf_scalar_header $perf_scalar_output
 321 fi
 322 
 323 # ALU binary ops.
 324 # Here "ADD+add+withMask" says VectorOperator name is "ADD", and we have a dedicate method too named 'add', and add() is also available with mask variant.
 325 gen_binary_alu_op "ADD+add+withMask" "a + b" 
 326 gen_binary_alu_op "SUB+sub+withMask" "a - b" 
 327 gen_binary_alu_op "MUL+mul+withMask" "a \* b"
 328 gen_binary_alu_op "DIV+div+withMask" "a \/ b" "FP"
 329 gen_op_tmpl "Binary-op_bitwise-div" "DIV+div+withMask" "a \/ b" "BITWISE"
 330 gen_op_tmpl "Binary-Masked-op_bitwise-div" "DIV+div+withMask" "a \/ b" "BITWISE"
 331 gen_binary_alu_op "FIRST_NONZERO" "{#if[FP]?Double.doubleToLongBits}(a)!=0?a:b"
 332 gen_binary_alu_op "AND+and"   "a \& b"  "BITWISE"
 333 gen_binary_alu_op "AND_NOT" "a \& ~b" "BITWISE"
 334 gen_binary_alu_op "OR"    "a | b"   "BITWISE"
 335 # Missing:        "OR_UNCHECKED"
 336 gen_binary_alu_op "XOR"   "a ^ b"   "BITWISE"
 337 
 338 # Shifts
 339 gen_binary_alu_op "LSHL" "(a << b)" "intOrLong"
 340 gen_binary_alu_op "LSHL" "(a << (b \& 0x7))" "byte"
 341 gen_binary_alu_op "LSHL" "(a << (b \& 0xF))" "short"
 342 gen_binary_alu_op "ASHR" "(a >> b)" "intOrLong"
 343 gen_binary_alu_op "ASHR" "(a >> (b \& 0x7))" "byte"
 344 gen_binary_alu_op "ASHR" "(a >> (b \& 0xF))" "short"
 345 gen_binary_alu_op "LSHR" "(a >>> b)" "intOrLong"
 346 gen_binary_alu_op "LSHR" "((a \& 0xFF) >>> (b \& 0x7))" "byte"
 347 gen_binary_alu_op "LSHR" "((a \& 0xFFFF) >>> (b \& 0xF))" "short"
 348 gen_shift_cst_op  "LSHL" "(a << b)" "intOrLong"
 349 gen_shift_cst_op  "LSHL" "(a << (b \& 7))" "byte"
 350 gen_shift_cst_op  "LSHL" "(a << (b \& 15))" "short"
 351 gen_shift_cst_op  "LSHR" "(a >>> b)" "intOrLong"
 352 gen_shift_cst_op  "LSHR" "((a \& 0xFF) >>> (b \& 7))" "byte"
 353 gen_shift_cst_op  "LSHR" "((a \& 0xFFFF) >>> (b \& 15))" "short"
 354 gen_shift_cst_op  "ASHR" "(a >> b)" "intOrLong"
 355 gen_shift_cst_op  "ASHR" "(a >> (b \& 7))" "byte"
 356 gen_shift_cst_op  "ASHR" "(a >> (b \& 15))" "short"
 357 
 358 # Masked reductions.
 359 gen_binary_op_no_masked "MIN+min" "Math.min(a, b)"
 360 gen_binary_op_no_masked "MAX+max" "Math.max(a, b)"
 361 
 362 # Reductions.
 363 gen_reduction_op "AND" "\&" "BITWISE" "-1"
 364 gen_reduction_op "OR" "|" "BITWISE" "0"
 365 gen_reduction_op "XOR" "^" "BITWISE" "0"
 366 gen_reduction_op "ADD" "+" "" "0"
 367 gen_reduction_op "MUL" "*" "" "1"
 368 gen_reduction_op_min "MIN" "" "" "\$Wideboxtype\$.\$MaxValue\$"
 369 gen_reduction_op_max "MAX" "" "" "\$Wideboxtype\$.\$MinValue\$"
 370 #gen_reduction_op "reduce_FIRST_NONZERO" "lanewise_FIRST_NONZERO" "{#if[FP]?Double.doubleToLongBits}(a)=0?a:b" "" "1"
 371 
 372 # Boolean reductions.
 373 gen_bool_reduction_op "anyTrue" "|" "BITWISE" "false"
 374 gen_bool_reduction_op "allTrue" "\&" "BITWISE" "true"
 375 
 376 #Insert
 377 gen_with_op "withLane" "" "" ""
 378 
 379 # Tests
 380 gen_op_tmpl $test_template "IS_DEFAULT" "bits(a)==0"
 381 gen_op_tmpl $test_template "IS_NEGATIVE" "bits(a)<0"
 382 gen_op_tmpl $test_template "IS_FINITE" "\$Boxtype\$.isFinite(a)" "FP"
 383 gen_op_tmpl $test_template "IS_NAN" "\$Boxtype\$.isNaN(a)" "FP"
 384 gen_op_tmpl $test_template "IS_INFINITE" "\$Boxtype\$.isInfinite(a)" "FP"
 385 
 386 # Compares
 387 gen_op_tmpl $compare_template "LT+lt" "<"
 388 gen_op_tmpl $compare_template "GT" ">"
 389 gen_op_tmpl $compare_template "EQ+eq" "=="
 390 gen_op_tmpl $compare_template "NE" "!="
 391 gen_op_tmpl $compare_template "LE" "<="
 392 gen_op_tmpl $compare_template "GE" ">="
 393 
 394 # Blend.
 395 gen_op_tmpl $blend "blend" ""
 396 
 397 # Rearrange
 398 gen_op_tmpl $rearrange_template "rearrange" ""
 399 
 400 # Get
 401 gen_get_op "" ""
 402 
 403 # Broadcast
 404 gen_op_tmpl $broadcast_template "broadcast" ""
 405 
 406 # Zero
 407 gen_op_tmpl $zero_template "zero" ""
 408 
 409 # Slice
 410 gen_op_tmpl $slice_template "sliceUnary" ""
 411 gen_op_tmpl $slice1_template "sliceBinary" ""
 412 gen_op_tmpl $slice1_masked_template "slice" ""
 413 
 414 # Unslice
 415 gen_op_tmpl $unslice_template "unsliceUnary" ""
 416 gen_op_tmpl $unslice1_template "unsliceBinary" ""
 417 gen_op_tmpl $unslice1_masked_template "unslice" ""
 418 
 419 # Math
 420 gen_op_tmpl $unary_math_template "SIN" "Math.sin((double)a)" "FP"
 421 gen_op_tmpl $unary_math_template "EXP" "Math.exp((double)a)" "FP"
 422 gen_op_tmpl $unary_math_template "LOG1P" "Math.log1p((double)a)" "FP"
 423 gen_op_tmpl $unary_math_template "LOG" "Math.log((double)a)" "FP"
 424 gen_op_tmpl $unary_math_template "LOG10" "Math.log10((double)a)" "FP"
 425 gen_op_tmpl $unary_math_template "EXPM1" "Math.expm1((double)a)" "FP"
 426 gen_op_tmpl $unary_math_template "COS" "Math.cos((double)a)" "FP"
 427 gen_op_tmpl $unary_math_template "TAN" "Math.tan((double)a)" "FP"
 428 gen_op_tmpl $unary_math_template "SINH" "Math.sinh((double)a)" "FP"
 429 gen_op_tmpl $unary_math_template "COSH" "Math.cosh((double)a)" "FP"
 430 gen_op_tmpl $unary_math_template "TANH" "Math.tanh((double)a)" "FP"
 431 gen_op_tmpl $unary_math_template "ASIN" "Math.asin((double)a)" "FP"
 432 gen_op_tmpl $unary_math_template "ACOS" "Math.acos((double)a)" "FP"
 433 gen_op_tmpl $unary_math_template "ATAN" "Math.atan((double)a)" "FP"
 434 gen_op_tmpl $unary_math_template "CBRT" "Math.cbrt((double)a)" "FP"
 435 gen_op_tmpl $binary_math_template "HYPOT" "Math.hypot((double)a, (double)b)" "FP"
 436 gen_op_tmpl $binary_math_template "POW" "Math.pow((double)a, (double)b)" "FP"
 437 gen_op_tmpl $binary_math_template "ATAN2" "Math.atan2((double)a, (double)b)" "FP"
 438 
 439 # Ternary operations.
 440 gen_ternary_alu_op "FMA" "Math.fma(a, b, c)" "FP"
 441 gen_ternary_alu_op "BITWISE_BLEND" "(a\&~(c))|(b\&c)" "BITWISE"
 442 
 443 # Unary operations.
 444 gen_unary_alu_op "NEG" "-((\$type\$)a)"
 445 gen_unary_alu_op "ABS+abs" "Math.abs((\$type\$)a)"
 446 gen_unary_alu_op "NOT" "~((\$type\$)a)" "BITWISE"
 447 gen_unary_alu_op "ZOMO" "(a==0?0:-1)" "BITWISE"
 448 gen_unary_alu_op "SQRT" "Math.sqrt((double)a)" "FP"
 449 
 450 # Gather Scatter operations.
 451 gen_op_tmpl $gather_template "gather" ""
 452 gen_op_tmpl $gather_masked_template "gather" ""
 453 gen_op_tmpl $scatter_template "scatter" ""
 454 gen_op_tmpl $scatter_masked_template "scatter" ""
 455 
 456 gen_unit_footer $unit_output
 457 
 458 if [ $generate_perf_tests == true ]; then
 459   gen_perf_footer $perf_output
 460   gen_perf_scalar_footer $perf_scalar_output
 461 fi
 462 
 463 rm -f templates/*.current*