| Marat Dukhan | 918a4a6 | 2019-10-27 19:49:49 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python | 
 | 2 | # Copyright 2019 Google LLC | 
 | 3 | # | 
 | 4 | # This source code is licensed under the BSD-style license found in the | 
 | 5 | # LICENSE file in the root directory of this source tree. | 
 | 6 |  | 
 | 7 |  | 
 | 8 | def _indent(text): | 
 | 9 |   return "\n".join(map(lambda t: "  " + t if t else t, text.splitlines())) | 
 | 10 |  | 
 | 11 |  | 
 | 12 | def _remove_duplicate_newlines(text): | 
 | 13 |   filtered_lines = list() | 
 | 14 |   last_newline = False | 
 | 15 |   for line in text.splitlines(): | 
 | 16 |     is_newline = len(line.strip()) == 0 | 
 | 17 |     if not is_newline or not last_newline: | 
 | 18 |       filtered_lines.append(line) | 
 | 19 |     last_newline = is_newline | 
 | 20 |   return "\n".join(filtered_lines) | 
 | 21 |  | 
 | 22 |  | 
 | 23 | _ARCH_TO_MACRO_MAP = { | 
 | 24 |   "aarch32": "XNN_ARCH_ARM", | 
 | 25 |   "aarch64": "XNN_ARCH_ARM64", | 
 | 26 |   "x86": "XNN_ARCH_X86", | 
 | 27 |   "x86-64": "XNN_ARCH_X86_64", | 
 | 28 | } | 
 | 29 |  | 
 | 30 | _ISA_TO_ARCH_MAP = { | 
 | 31 |   "neon": ["aarch32", "aarch64"], | 
 | 32 |   "neonfma": ["aarch32", "aarch64"], | 
 | 33 |   "neonfp16arith": ["aarch32", "aarch64"], | 
 | 34 |   "sse": ["x86", "x86-64"], | 
 | 35 |   "sse2": ["x86", "x86-64"], | 
 | 36 |   "avx": ["x86", "x86-64"], | 
 | 37 |   "avx512f": ["x86", "x86-64"], | 
 | 38 |   "psimd": [], | 
 | 39 | } | 
 | 40 |  | 
 | 41 | _ISA_TO_CHECK_MAP = { | 
 | 42 |   "neon": "TEST_REQUIRES_ARM_NEON", | 
 | 43 |   "neonfma": "TEST_REQUIRES_ARM_NEON_FMA", | 
 | 44 |   "neonfp16arith": "TEST_REQUIRES_ARM_NEON_FP16_ARITH", | 
 | 45 |   "sse": "TEST_REQUIRES_X86_SSE", | 
 | 46 |   "sse2": "TEST_REQUIRES_X86_SSE2", | 
 | 47 |   "avx": "TEST_REQUIRES_X86_AVX", | 
 | 48 |   "avx2": "TEST_REQUIRES_X86_AVX2", | 
 | 49 |   "avx512f": "TEST_REQUIRES_X86_AVX512F", | 
 | 50 |   "psimd": "TEST_REQUIRES_PSIMD", | 
 | 51 | } | 
 | 52 |  | 
 | 53 |  | 
 | 54 | def parse_target_name(target_name): | 
 | 55 |   arch = list() | 
 | 56 |   isa = None | 
 | 57 |   for target_part in target_name.split("_"): | 
 | 58 |     if target_part in _ARCH_TO_MACRO_MAP: | 
 | 59 |       arch = [target_part] | 
 | 60 |     elif target_part in _ISA_TO_ARCH_MAP: | 
 | 61 |       isa = target_part | 
 | 62 |   if isa and not arch: | 
 | 63 |     arch = _ISA_TO_ARCH_MAP[isa] | 
 | 64 |  | 
 | 65 |   return arch, isa | 
 | 66 |  | 
 | 67 |  | 
 | 68 | def generate_isa_check_macro(isa): | 
 | 69 |   return _ISA_TO_CHECK_MAP.get(isa, "") | 
 | 70 |  | 
 | 71 |  | 
 | 72 | def postprocess_test_case(test_case, arch, isa, assembly=False): | 
 | 73 |   test_case = _remove_duplicate_newlines(test_case) | 
 | 74 |   if arch: | 
 | 75 |     guard = " || ".join(map(_ARCH_TO_MACRO_MAP.get, arch)) | 
 | 76 |     if assembly: | 
 | 77 |       guard += " && XNN_ENABLE_ASSEMBLY" | 
 | 78 |     return "#if %s\n" % guard + \ | 
 | 79 |       _indent(test_case) + "\n" + \ | 
 | 80 |       "#endif  // %s\n" % guard | 
 | 81 |   elif isa == "psimd": | 
 | 82 |     guard = "!XNN_ARCH_ASMJS && !XNN_ARCH_WASM" | 
 | 83 |     return "#if %s\n" % guard + \ | 
 | 84 |       _indent(test_case) + "\n" + \ | 
 | 85 |       "#endif  // %s\n" % guard | 
 | 86 |   else: | 
 | 87 |     return test_case |