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
omega-mv / src / omega_numeric_reduce.c
| 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 | /* |
| 18 | * E1 WP-D: general reductions with a frozen order. Reference tier and CPU |
| 19 | * realization tier. Host only; opens no device. See omega_numeric_reduce.h |
| 20 | * and docs/numeric/E1_REDUCTION_CONTRACT.md. |
| 21 | */ |
| 22 | #include "omega_numeric_reduce.h" |
| 23 | #include "omega_numeric.h" |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #if !defined(__aarch64__) |
| 30 | #error "E1 reduction CPU tier uses AArch64 FP instructions (fadd, fmaxnm, fminnm, fdiv, ucvtf)" |
| 31 | #endif |
| 32 | |
| 33 | static const char *const RED_NAMES[OMEGA_RED_COUNT] = { "SUM", "MAX", "MIN", "MEAN" }; |
| 34 | |
| 35 | const char *omega_reduce_op_name(OmegaReduceOp op) { |
| 36 | return ((unsigned)op < OMEGA_RED_COUNT) ? RED_NAMES[op] : NULL; |
| 37 | } |
| 38 | |
| 39 | float omega_reduce_identity(OmegaReduceOp op) { |
| 40 | if (op == OMEGA_RED_MAX || op == OMEGA_RED_MIN) return omega_bits_to_float(OMEGA_REDUCE_MINMAX_PAD_BITS); |
| 41 | return omega_bits_to_float(OMEGA_REDUCE_SUM_PAD_BITS); |
| 42 | } |
| 43 | |
| 44 | unsigned omega_reduce_levels(size_t n) { |
| 45 | if (n == 0) return 0; |
| 46 | unsigned l = 1; |
| 47 | size_t cap = 32; |
| 48 | while (cap < n) { cap = (cap > ((size_t)-1) / 32) ? (size_t)-1 : cap * 32; l++; } |
| 49 | return l; |
| 50 | } |
| 51 | |
| 52 | static float empty_result(OmegaReduceOp op) { |
| 53 | return omega_bits_to_float(op == OMEGA_RED_SUM ? OMEGA_REDUCE_EMPTY_SUM_BITS : OMEGA_REDUCE_EMPTY_OTHER_BITS); |
| 54 | } |
| 55 | |
| 56 | static int common_checks(OmegaReduceOp op, const float *x, size_t n, const float *out) { |
| 57 | if ((unsigned)op >= OMEGA_RED_COUNT || !out || (n > 0 && !x)) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 58 | if (op == OMEGA_RED_MEAN && n > OMEGA_REDUCE_MEAN_MAX_N) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 59 | if (n > ((size_t)-1) / sizeof(float) - 64) return OMEGA_NUMERIC_ERR_OPERANDS; /* padding must not wrap */ |
| 60 | if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; |
| 61 | return OMEGA_NUMERIC_OK; |
| 62 | } |
| 63 | |
| 64 | /* ---- Reference tier ------------------------------------------------------ */ |
| 65 | |
| 66 | /* The lane tree of omega_warp_reduce_sum with the combine of op. SUM calls |
| 67 | * omega_warp_reduce_sum itself, so the tile order is the declared warp order. */ |
| 68 | static float ref_tile(OmegaReduceOp op, const float tile[32]) { |
| 69 | if (op == OMEGA_RED_SUM || op == OMEGA_RED_MEAN) return omega_warp_reduce_sum(tile); |
| 70 | float val[32]; |
| 71 | memcpy(val, tile, sizeof(val)); |
| 72 | static const int deltas[5] = { 16, 8, 4, 2, 1 }; |
| 73 | for (int s = 0; s < 5; s++) { |
| 74 | int d = deltas[s]; |
| 75 | for (int i = 0; i + d < 32; i++) |
| 76 | val[i] = (op == OMEGA_RED_MAX) ? omega_ref_fmax(val[i], val[i + d]) : omega_ref_fmin(val[i], val[i + d]); |
| 77 | } |
| 78 | return val[0]; |
| 79 | } |
| 80 | |
| 81 | static int ref_sum_like(OmegaReduceOp op, const float *x, size_t n, float *out) { |
| 82 | if (op == OMEGA_RED_MEAN) op = OMEGA_RED_SUM; /* MEAN reduces like SUM; the division is the caller's */ |
| 83 | if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } |
| 84 | const float pad = omega_reduce_identity(op); |
| 85 | size_t len = n; |
| 86 | const float *cur = x; |
| 87 | float *owned = NULL; |
| 88 | do { |
| 89 | size_t next_len = (len + 31) / 32; |
| 90 | float *next = malloc(next_len * sizeof(float)); |
| 91 | if (!next) { free(owned); return OMEGA_NUMERIC_ERR_BAD_ARGS; } |
| 92 | for (size_t j = 0; j < next_len; j++) { |
| 93 | float tile[32]; |
| 94 | for (size_t k = 0; k < 32; k++) { |
| 95 | size_t idx = j * 32 + k; |
| 96 | tile[k] = (idx < len) ? cur[idx] : pad; |
| 97 | } |
| 98 | next[j] = ref_tile(op, tile); |
| 99 | } |
| 100 | free(owned); |
| 101 | owned = next; |
| 102 | cur = next; |
| 103 | len = next_len; |
| 104 | } while (len > 1); |
| 105 | *out = cur[0]; |
| 106 | free(owned); |
| 107 | return OMEGA_NUMERIC_OK; |
| 108 | } |
| 109 | |
| 110 | int omega_reduce_reference(OmegaReduceOp op, const float *x, size_t n, float *out) { |
| 111 | int rc = common_checks(op, x, n, out); |
| 112 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 113 | if (op != OMEGA_RED_MEAN) return ref_sum_like(op, x, n, out); |
| 114 | if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } |
| 115 | float s; |
| 116 | rc = ref_sum_like(OMEGA_RED_SUM, x, n, &s); |
| 117 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 118 | *out = omega_math_div(s, omega_ref_u2f((uint32_t)n)); |
| 119 | return OMEGA_NUMERIC_OK; |
| 120 | } |
| 121 | |
| 122 | /* ---- CPU realization tier ------------------------------------------------- */ |
| 123 | |
| 124 | static inline float cpu_combine(OmegaReduceOp op, float a, float b) { |
| 125 | float r; |
| 126 | if (op == OMEGA_RED_SUM) { |
| 127 | __asm__ volatile("fadd %s0, %s1, %s2" : "=w"(r) : "w"(a), "w"(b)); |
| 128 | return r; |
| 129 | } |
| 130 | /* FMAXNM/FMINNM return NaN for a signaling NaN; Omega treats every NaN as |
| 131 | * missing data, so quiet signaling NaNs first (as the FMNMX CPU tier does). */ |
| 132 | float qa = omega_isnan(a) ? omega_bits_to_float(omega_float_to_bits(a) | 0x00400000U) : a; |
| 133 | float qb = omega_isnan(b) ? omega_bits_to_float(omega_float_to_bits(b) | 0x00400000U) : b; |
| 134 | if (op == OMEGA_RED_MAX) __asm__ volatile("fmaxnm %s0, %s1, %s2" : "=w"(r) : "w"(qa), "w"(qb)); |
| 135 | else __asm__ volatile("fminnm %s0, %s1, %s2" : "=w"(r) : "w"(qa), "w"(qb)); |
| 136 | return r; |
| 137 | } |
| 138 | |
| 139 | /* One buffer, compacted in place: tile j's result goes to buf[j] (j <= 32j). */ |
| 140 | static int cpu_sum_like(OmegaReduceOp op, const float *x, size_t n, float *out) { |
| 141 | if (op == OMEGA_RED_MEAN) op = OMEGA_RED_SUM; /* MEAN reduces like SUM; the division is the caller's */ |
| 142 | if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } |
| 143 | size_t cap = (n + 31) & ~(size_t)31; |
| 144 | float *buf = malloc(cap * sizeof(float)); |
| 145 | if (!buf) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 146 | memcpy(buf, x, n * sizeof(float)); |
| 147 | const uint32_t pad_bits = (op == OMEGA_RED_SUM) ? OMEGA_REDUCE_SUM_PAD_BITS : OMEGA_REDUCE_MINMAX_PAD_BITS; |
| 148 | size_t len = n; |
| 149 | for (;;) { |
| 150 | size_t padded = (len + 31) & ~(size_t)31; |
| 151 | for (size_t i = len; i < padded; i++) buf[i] = omega_bits_to_float(pad_bits); |
| 152 | size_t tiles = padded / 32; |
| 153 | for (size_t j = 0; j < tiles; j++) { |
| 154 | float *t = buf + j * 32; |
| 155 | for (unsigned d = 16; d >= 1; d >>= 1) |
| 156 | for (unsigned i = 0; i < d; i++) t[i] = cpu_combine(op, t[i], t[i + d]); |
| 157 | buf[j] = t[0]; |
| 158 | } |
| 159 | len = tiles; |
| 160 | if (len == 1) break; |
| 161 | } |
| 162 | *out = buf[0]; |
| 163 | free(buf); |
| 164 | return OMEGA_NUMERIC_OK; |
| 165 | } |
| 166 | |
| 167 | int omega_reduce_cpu(OmegaReduceOp op, const float *x, size_t n, float *out) { |
| 168 | int rc = common_checks(op, x, n, out); |
| 169 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 170 | if (op != OMEGA_RED_MEAN) return cpu_sum_like(op, x, n, out); |
| 171 | if (n == 0) { *out = empty_result(op); return OMEGA_NUMERIC_OK; } |
| 172 | float s, fn, r; |
| 173 | rc = cpu_sum_like(OMEGA_RED_SUM, x, n, &s); |
| 174 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 175 | uint32_t n32 = (uint32_t)n; |
| 176 | __asm__ volatile("ucvtf %s0, %w1" : "=w"(fn) : "r"(n32)); |
| 177 | __asm__ volatile("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(s), "w"(fn)); |
| 178 | *out = r; |
| 179 | return OMEGA_NUMERIC_OK; |
| 180 | } |
| 181 | |
| 182 | /* ---- Batched (last axis) -------------------------------------------------- */ |
| 183 | |
| 184 | static int rows_common(OmegaReduceOp op, const float *x, size_t rows, size_t n, size_t row_stride, |
| 185 | const float *out) { |
| 186 | if ((unsigned)op >= OMEGA_RED_COUNT || (rows > 0 && !out) || (rows > 0 && n > 0 && !x)) |
| 187 | return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 188 | if (rows > 1 && row_stride < n) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 189 | if (rows > 0 && n > 0) { |
| 190 | /* last element index (rows-1)*row_stride + n-1 and its byte offset must not wrap */ |
| 191 | size_t lim = SIZE_MAX / sizeof(float); |
| 192 | if (rows > 1 && row_stride > (lim - n) / (rows - 1)) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 193 | if (n > lim) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 194 | } |
| 195 | return OMEGA_NUMERIC_OK; |
| 196 | } |
| 197 | |
| 198 | int omega_reduce_rows_reference(OmegaReduceOp op, const float *x, size_t rows, size_t n, |
| 199 | size_t row_stride, float *out) { |
| 200 | int rc = rows_common(op, x, rows, n, row_stride, out); |
| 201 | for (size_t r = 0; rc == OMEGA_NUMERIC_OK && r < rows; r++) |
| 202 | rc = omega_reduce_reference(op, n ? x + r * row_stride : NULL, n, &out[r]); |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | int omega_reduce_rows_cpu(OmegaReduceOp op, const float *x, size_t rows, size_t n, |
| 207 | size_t row_stride, float *out) { |
| 208 | int rc = rows_common(op, x, rows, n, row_stride, out); |
| 209 | for (size_t r = 0; rc == OMEGA_NUMERIC_OK && r < rows; r++) |
| 210 | rc = omega_reduce_cpu(op, n ? x + r * row_stride : NULL, n, &out[r]); |
| 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | float omega_reduce_sequential_sum_not_contract(const float *x, size_t n) { |
| 215 | float s = omega_bits_to_float(OMEGA_REDUCE_SUM_PAD_BITS); |
| 216 | for (size_t i = 0; i < n; i++) s = s + x[i]; |
| 217 | return n ? s : 0.0f; |
| 218 | } |