interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_platform_linux_cpolicy.h

97 lines · 3259 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* C oracle for om/platform-linux/plat_policy.om. Compiled into the platform only when
3 * CUDAPP_MIGRATION_PENDING_C_PLATFORM_LINUX is set. The differential test always includes it. */
4#ifndef OMEGA_GPU_PLATFORM_LINUX_CPOLICY_H
5#define OMEGA_GPU_PLATFORM_LINUX_CPOLICY_H
6#include <stdint.h>
7#include <string.h>
8
9static uint64_t plat_c_byte(const uint64_t m[16], uint64_t idx) {
10 return (m[2 + idx / 8] >> ((idx % 8) * 8)) & 255u;
11}
12
13static int plat_c_sp(uint64_t c) {
14 return c == 32 || c == 9 || c == 10 || c == 11 || c == 12 || c == 13;
15}
16
17static uint64_t plat_c(const uint64_t m[16], uint64_t q[16]) {
18 if (q) memset(q, 0, 16 * sizeof q[0]);
19 if (m[0] == 1) {
20 q[0] = (2ull << 28) | (4ull << 16) | (0x0028ull >> 2);
21 q[4] = 0x11ull << 27;
22 return 5;
23 }
24 if (m[0] == 2) {
25 if (m[1] != 1 || m[2] < '2' || m[2] > '4' || m[3] != 0) return 1;
26 return m[2] - '0';
27 }
28 if (m[0] == 3) {
29 if (m[3] == 0 || m[2] > m[1] || m[3] > m[1] - m[2]) return 7;
30 return 0;
31 }
32 if (m[0] == 4) {
33 if (m[5] != 0) { q[0] = m[5]; return 1; }
34 uint64_t local = m[1] && m[2] != 4 && (m[3] & 32u) == 0;
35 uint64_t cached = m[4];
36 if (local && cached) { q[0] = 10; return 1; }
37 if (cached && (m[2] != 2 || (m[3] & 33u))) { q[0] = 10; return 1; }
38 q[1] = local;
39 q[2] = cached;
40 q[3] = local ? 1 : cached ? 2 : 3;
41 return 1;
42 }
43 if (m[0] == 5) {
44 if (m[1] == 0 || m[1] > (UINT64_MAX - 0xfffull)) return 0;
45 q[0] = (m[1] + 0xfffull) & ~0xfffull;
46 return 1;
47 }
48 if (m[0] == 7) {
49 uint64_t n = m[1];
50 if (n == 0 || n > 32 || plat_c_byte(m, 0) == '-') return 0;
51 uint64_t i = 0;
52 while (i < n && plat_c_sp(plat_c_byte(m, i))) i++;
53 int sign = 0;
54 if (i < n) {
55 uint64_t c = plat_c_byte(m, i);
56 if (c == '+') i++;
57 else if (c == '-') { sign = 1; i++; }
58 }
59 if (i == n) return 0;
60 uint64_t acc = 0;
61 for (; i < n; i++) {
62 uint64_t d = plat_c_byte(m, i);
63 if (d < '0' || d > '9') return 0;
64 uint64_t dig = d - '0';
65 if (acc > 1844674407370955161ull) return 0;
66 acc *= 10;
67 if (acc > UINT64_MAX - dig) return 0;
68 acc += dig;
69 }
70 q[0] = sign ? 0ull - acc : acc;
71 return 1;
72 }
73 if (m[0] == 8) return m[1] >= m[2] ? 12 : 0;
74 if (m[0] == 9) {
75 if (m[1] == 0 || m[1] > UINT64_MAX - m[2]) return 7;
76 uint64_t words = m[1] + m[2];
77 if (words > (UINT64_MAX / 4) || words * 4 > m[3]) return 7;
78 return 0;
79 }
80 if (m[0] == 10) {
81 if ((m[1] & 32u) == 0) return 8;
82 if (m[2] & 3u) return 1;
83 return 0;
84 }
85 if (m[0] == 12) {
86 if (m[1] < 2) return 0;
87 for (uint64_t i = 0; i < 3 && i + 1 < m[1]; i++) {
88 uint64_t gen = m[5 + i * 2];
89 if (gen && m[4 + i * 2] == m[2] && gen == m[3]) return i + 1;
90 }
91 return 0;
92 }
93 if (m[0] == 13) return m[1] && m[2] ? 1 : 0;
94 if (m[0] == 14) return m[1] == 0 ? 0 : (m[2] ? m[2] : 121);
95 return 0;
96}
97#endif