• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-32": "XNN_ARCH_X86",
27  "x86-64": "XNN_ARCH_X86_64",
28  "wasm": "XNN_ARCH_WASM",
29  "wasmsimd": "XNN_ARCH_WASMSIMD",
30}
31
32_ISA_TO_ARCH_MAP = {
33  "neon": ["aarch32", "aarch64"],
34  "neonfma": ["aarch32", "aarch64"],
35  "neonv8": ["aarch32", "aarch64"],
36  "neonfp16arith": ["aarch32", "aarch64"],
37  "neondot": ["aarch32", "aarch64"],
38  "sse": ["x86-32", "x86-64"],
39  "sse2": ["x86-32", "x86-64"],
40  "ssse3": ["x86-32", "x86-64"],
41  "sse41": ["x86-32", "x86-64"],
42  "avx": ["x86-32", "x86-64"],
43  "xop": ["x86-32", "x86-64"],
44  "fma3": ["x86-32", "x86-64"],
45  "avx2": ["x86-32", "x86-64"],
46  "avx512f": ["x86-32", "x86-64"],
47  "avx512skx": ["x86-32", "x86-64"],
48  "wasm32": ["wasm", "wasmsimd"],
49  "wasm": ["wasm", "wasmsimd"],
50  "wasmsimd": ["wasmsimd"],
51}
52
53_ISA_TO_CHECK_MAP = {
54  "neon": "TEST_REQUIRES_ARM_NEON",
55  "neonfma": "TEST_REQUIRES_ARM_NEON_FMA",
56  "neonv8": "TEST_REQUIRES_ARM_NEON_V8",
57  "neonfp16arith": "TEST_REQUIRES_ARM_NEON_FP16_ARITH",
58  "neondot": "TEST_REQUIRES_ARM_NEON_DOT",
59  "sse": "TEST_REQUIRES_X86_SSE",
60  "sse2": "TEST_REQUIRES_X86_SSE2",
61  "ssse3": "TEST_REQUIRES_X86_SSSE3",
62  "sse41": "TEST_REQUIRES_X86_SSE41",
63  "avx": "TEST_REQUIRES_X86_AVX",
64  "xop": "TEST_REQUIRES_X86_XOP",
65  "avx2": "TEST_REQUIRES_X86_AVX2",
66  "fma3": "TEST_REQUIRES_X86_FMA3",
67  "avx512f": "TEST_REQUIRES_X86_AVX512F",
68  "avx512skx": "TEST_REQUIRES_X86_AVX512SKX",
69}
70
71
72def parse_target_name(target_name):
73  arch = list()
74  isa = None
75  for target_part in target_name.split("_"):
76    if target_part in _ARCH_TO_MACRO_MAP:
77      if target_part in _ISA_TO_ARCH_MAP:
78        arch = _ISA_TO_ARCH_MAP[target_part]
79        isa = target_part
80      else:
81        arch = [target_part]
82    elif target_part in _ISA_TO_ARCH_MAP:
83      isa = target_part
84  if isa and not arch:
85    arch = _ISA_TO_ARCH_MAP[isa]
86
87  return arch, isa
88
89
90def generate_isa_check_macro(isa):
91  return _ISA_TO_CHECK_MAP.get(isa, "")
92
93
94def postprocess_test_case(test_case, arch, isa, assembly=False):
95  test_case = _remove_duplicate_newlines(test_case)
96  if arch:
97    guard = " || ".join(map(_ARCH_TO_MACRO_MAP.get, arch))
98    if assembly:
99      guard += " && XNN_ENABLE_ASSEMBLY"
100    return "#if %s\n" % guard + _indent(test_case) + "\n" + \
101      "#endif  // %s\n" % guard
102  else:
103    return test_case
104