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 8def _indent(text): 9 return "\n".join(map(lambda t: " " + t if t else t, text.splitlines())) 10 11 12def _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 "wasm": "XNN_ARCH_WASM", 29} 30 31_ISA_TO_ARCH_MAP = { 32 "neon": ["aarch32", "aarch64"], 33 "neonfma": ["aarch32", "aarch64"], 34 "neonfp16arith": ["aarch32", "aarch64"], 35 "sse": ["x86", "x86-64"], 36 "sse2": ["x86", "x86-64"], 37 "sse41": ["x86", "x86-64"], 38 "avx": ["x86", "x86-64"], 39 "fma3": ["x86", "x86-64"], 40 "avx2": ["x86", "x86-64"], 41 "avx512f": ["x86", "x86-64"], 42 "wasm": ["wasm"], 43 "psimd": [], 44} 45 46_ISA_TO_CHECK_MAP = { 47 "neon": "TEST_REQUIRES_ARM_NEON", 48 "neonfma": "TEST_REQUIRES_ARM_NEON_FMA", 49 "neonfp16arith": "TEST_REQUIRES_ARM_NEON_FP16_ARITH", 50 "sse": "TEST_REQUIRES_X86_SSE", 51 "sse2": "TEST_REQUIRES_X86_SSE2", 52 "sse41": "TEST_REQUIRES_X86_SSE41", 53 "avx": "TEST_REQUIRES_X86_AVX", 54 "avx2": "TEST_REQUIRES_X86_AVX2", 55 "fma3": "TEST_REQUIRES_X86_FMA3", 56 "avx512f": "TEST_REQUIRES_X86_AVX512F", 57 "psimd": "TEST_REQUIRES_PSIMD", 58} 59 60 61def parse_target_name(target_name): 62 arch = list() 63 isa = None 64 for target_part in target_name.split("_"): 65 if target_part in _ARCH_TO_MACRO_MAP: 66 arch = [target_part] 67 elif target_part in _ISA_TO_ARCH_MAP: 68 isa = target_part 69 if isa and not arch: 70 arch = _ISA_TO_ARCH_MAP[isa] 71 72 return arch, isa 73 74 75def generate_isa_check_macro(isa): 76 return _ISA_TO_CHECK_MAP.get(isa, "") 77 78 79def postprocess_test_case(test_case, arch, isa, assembly=False): 80 test_case = _remove_duplicate_newlines(test_case) 81 if arch: 82 guard = " || ".join(map(_ARCH_TO_MACRO_MAP.get, arch)) 83 if assembly: 84 guard += " && XNN_ENABLE_ASSEMBLY" 85 return "#if %s\n" % guard + \ 86 _indent(test_case) + "\n" + \ 87 "#endif // %s\n" % guard 88 elif isa == "psimd": 89 guard = "!XNN_ARCH_ASMJS && !XNN_ARCH_WASM" 90 return "#if %s\n" % guard + \ 91 _indent(test_case) + "\n" + \ 92 "#endif // %s\n" % guard 93 else: 94 return test_case 95