Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/tensor/omega_tensor_cpu.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / tensor / omega_tensor_cpu.c` 111 lines ยท 5756 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* CPU realization of M20 OMEGA_TENSOR: the E1 CPU tier (explicit AArch64 18 * instructions in omega_numeric_cpu_realize) for elementwise ops, the frozen 19 * E1 transcendental sequences (omega_numeric_transc.c, bounded contract) for 20 * EXP2 / LOG2 / SIGMOID / TANH / SIN / COS, the E1 polynomials omega_math_exp / 21 * omega_math_log (omega_numeric.c) for EXP / LOG, the E1 FCMP conditions for the 22 * CR-3 mask compare, and the reduction seam for reductions. No arithmetic outside 23 * those. RELU is a bit select in omega_tensor.c. */ 24 #include "omega_tensor.h" 25 #include "omega_tensor_reduce_seam.h" 26 #include "omega_numeric_transc.h" 27 28 static int cpu_elementwise(OmegaNumericOp op, const float *a, const float *b, const float *c, 29 float *out, size_t n) { 30 return omega_numeric_cpu_realize(op, a, b, c, out, n); 31 } 32 33 /* One E1 scalar call per element; nothing else touches the value. */ 34 static float (*const TRANSC_FN[OMEGA_TU_COUNT])(float) = { 35 [OMEGA_TU_EXP2] = omega_math_exp2, [OMEGA_TU_LOG2] = omega_math_log2, /* MUT:TRANSC_EXP2_LOG2_SWAP */ 36 [OMEGA_TU_SIGMOID] = omega_math_sigmoid, 37 [OMEGA_TU_TANH] = omega_math_tanh, 38 /* E1 Omega-defined polynomials, src/omega_numeric.c:458 and :501 (cut ops) */ 39 [OMEGA_TU_EXP] = omega_math_exp, [OMEGA_TU_LOG] = omega_math_log, /* MUT:TRANSC_EXP_LOG_SWAP */ 40 [OMEGA_TU_RSQRT] = omega_math_rsqrt, [OMEGA_TU_ERF] = omega_math_erf, /* MUT:TRANSC_RSQRT_ERF_SWAP */ 41 [OMEGA_TU_GELU] = omega_math_gelu, 42 [OMEGA_TU_SIN] = omega_math_sin, [OMEGA_TU_COS] = omega_math_cos, /* MUT:TRIG_SIN_COS_SWAP */ 43 }; 44 45 static int cpu_transc(OmegaTensorUnaryOp op, const float *a, float *out, size_t n) { 46 if ((unsigned)op >= OMEGA_TU_COUNT || !TRANSC_FN[op] || !a || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; 47 if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; 48 float (*f)(float) = TRANSC_FN[op]; 49 for (size_t i = 0; i < n; i++) out[i] = f(a[i]); /* MUT:TRIG_DOMAIN_NUMBER */ 50 return OMEGA_NUMERIC_OK; 51 } 52 53 54 /* 55 * CR-3 mask compare. Same FCMP + condition per predicate as the E1 CPU 56 * compare-select ops (src/omega_numeric.c, CPU_SEL table after the 57 * "E1 compare-and-select" comment), but FCSEL picks between the constants 58 * +1.0 and +0.0 instead of between a and b. FCMP flags: less 1000, equal 59 * 0110, greater 0010, unordered 0011 (NZCV), so every ordered predicate is 60 * false on NaN and -0 == +0. NE and EQU chain two FCSELs, as in E1. 61 * FCSEL moves bits only: no rounding, no arithmetic. 62 */ 63 static int cpu_compare(OmegaNumericOp op, const float *a, const float *b, float *out, size_t n) { 64 if (!a || !b || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; 65 if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; 66 const float one = 1.0f; 67 const float zero = 0.0f; /* MUT:CMP_FALSE_NEG_ZERO */ 68 for (size_t i = 0; i < n; i++) { 69 float x = a[i], y = b[i], r, t; 70 #define CPU_CMP(cond_) __asm__ volatile("fcmp %s1, %s2\n\tfcsel %s0, %s3, %s4, " cond_ \ 71 : "=&w"(r) : "w"(x), "w"(y), "w"(one), "w"(zero) : "cc") 72 switch (op) { 73 case OMEGA_NOP_FSETP_SEL: CPU_CMP("ge"); break; /* GE: N==V */ 74 case OMEGA_NOP_FSETP_LT_SEL: CPU_CMP("mi"); break; /* N */ 75 case OMEGA_NOP_FSETP_LE_SEL: CPU_CMP("ls"); break; /* C==0 or Z */ 76 case OMEGA_NOP_FSETP_GT_SEL: CPU_CMP("gt"); break; /* !Z, N==V */ 77 case OMEGA_NOP_FSETP_EQ_SEL: CPU_CMP("eq"); break; 78 case OMEGA_NOP_FSETP_NUM_SEL: CPU_CMP("vc"); break; /* ordered */ 79 case OMEGA_NOP_FSETP_NAN_SEL: CPU_CMP("vs"); break; /* unordered */ 80 case OMEGA_NOP_FSETP_LTU_SEL: CPU_CMP("lt"); break; /* N!=V */ 81 case OMEGA_NOP_FSETP_LEU_SEL: CPU_CMP("le"); break; /* Z or N!=V */ 82 case OMEGA_NOP_FSETP_GTU_SEL: CPU_CMP("hi"); break; /* C and !Z */ 83 case OMEGA_NOP_FSETP_GEU_SEL: CPU_CMP("pl"); break; /* N==0 */ 84 case OMEGA_NOP_FSETP_NEU_SEL: CPU_CMP("ne"); break; /* !Z */ 85 case OMEGA_NOP_FSETP_NE_SEL: /* less or greater */ 86 __asm__ volatile("fcmp %s2, %s3\n\tfcsel %s1, %s4, %s5, gt\n\tfcsel %s0, %s4, %s1, mi" 87 : "=&w"(r), "=&w"(t) : "w"(x), "w"(y), "w"(one), "w"(zero) : "cc"); 88 break; 89 case OMEGA_NOP_FSETP_EQU_SEL: /* equal or unordered */ 90 __asm__ volatile("fcmp %s2, %s3\n\tfcsel %s1, %s4, %s5, vs\n\tfcsel %s0, %s4, %s1, eq" 91 : "=&w"(r), "=&w"(t) : "w"(x), "w"(y), "w"(one), "w"(zero) : "cc"); 92 break; 93 default: 94 return OMEGA_NUMERIC_ERR_BAD_ARGS; 95 } 96 #undef CPU_CMP 97 out[i] = r; 98 } 99 return OMEGA_NUMERIC_OK; 100 } 101 102 static const OmegaTensorRealization CPU_REALIZATION = { 103 .name = "CPU_E1_TIER", 104 .reduce_order = OMEGA_TENSOR_REDUCE_DECLARED_ORDER, 105 .elementwise = cpu_elementwise, 106 .reduce = omega_tensor_seam_reduce_cpu, 107 .transc = cpu_transc, 108 .compare = cpu_compare, 109 }; 110 111 const OmegaTensorRealization *omega_tensor_cpu_realization(void) { return &CPU_REALIZATION; }