interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_gpu_hostmath.c

107 lines · 4776 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/* omega_gpu_hostmath: libm-free double-precision log / exp / pow / sincos (see the header). */
18#include "omega_gpu_hostmath.h"
19#include <stdint.h>
20#include <string.h>
21
22static double bits_d(uint64_t u) { double d; memcpy(&d, &u, 8); return d; }
23static uint64_t d_bits(double d) { uint64_t u; memcpy(&u, &d, 8); return u; }
24
25#define HM_LN2_HI 6.93147180369123816490e-01 /* fdlibm: ln2 split so that k * LN2_HI is exact for |k| < 2^20 */
26#define HM_LN2_LO 1.90821492927058770002e-10
27#define HM_INV_LN2 1.44269504088896338700e+00
28#define HM_SQRT2 1.41421356237309504880e+00
29
30/* 2^k as a double for -1074 <= k <= 1023 (two steps below the normal range) */
31static double pow2i(int k) {
32 if (k > 1023) return bits_d(0x7ff0000000000000ull);
33 if (k < -1074) return 0.0;
34 if (k >= -1022) return bits_d((uint64_t)(k + 1023) << 52);
35 return bits_d((uint64_t)1 << 52) * bits_d((uint64_t)(k + 1022 + 1023) << 52); /* 2^-1022 * 2^(k+1022) */
36}
37
38double omega_hostmath_log(double x) {
39 if (!(x > 0.0) || x > 1.7976931348623157e308) return bits_d(0x7ff8000000000000ull);
40 int e = 0;
41 if (x < 2.2250738585072014e-308) { x *= 18014398509481984.0; e = -54; } /* subnormal: scale by 2^54 */
42 uint64_t u = d_bits(x);
43 e += (int)((u >> 52) & 0x7ff) - 1023;
44 double m = bits_d((u & 0x000fffffffffffffull) | 0x3ff0000000000000ull); /* [1, 2) */
45 if (m > HM_SQRT2) { m *= 0.5; e += 1; } /* [sqrt(1/2), sqrt(2)) */
46 double t = (m - 1.0) / (m + 1.0), t2 = t * t; /* |t| < 0.1716 */
47 double s = 0.0;
48 for (int k = 25; k >= 1; k -= 2) s = s * t2 + 1.0 / (double)k; /* sum t2^j / (2j+1), j = 0..12 */
49 return (double)e * HM_LN2_HI + (2.0 * t * s + (double)e * HM_LN2_LO);
50}
51
52double omega_hostmath_exp(double y) {
53 if (y != y) return y;
54 if (y > 709.78) return bits_d(0x7ff0000000000000ull);
55 if (y < -745.2) return 0.0;
56 double fk = y * HM_INV_LN2;
57 long long k = (long long)(fk + (fk >= 0.0 ? 0.5 : -0.5));
58 double kd = (double)k;
59 double r = (y - kd * HM_LN2_HI) - kd * HM_LN2_LO; /* |r| <= 0.3466 */
60 double p = 1.0;
61 for (int n = 17; n >= 1; n--) p = 1.0 + r * p / (double)n; /* 1 + r(1 + r/2(1 + r/3(...))) */
62 if (k < -1020) return p * pow2i(-1020) * pow2i((int)k + 1020);
63 return p * pow2i((int)k);
64}
65
66double omega_hostmath_pow(double base, double e) {
67 if (base == 1.0 || e == 0.0) return 1.0;
68 return omega_hostmath_exp(e * omega_hostmath_log(base));
69}
70
71/* fdlibm e_rem_pio2 pieces of pi/2: the first three have 33 significant bits, so n * piece is exact for n < 2^20 */
72#define HM_PIO2_1 1.57079632673412561417e+00
73#define HM_PIO2_2 6.07710050630396597660e-11
74#define HM_PIO2_3 2.02226624871116645580e-21
75#define HM_PIO2_3T 8.47842766036889956997e-32
76#define HM_2_OVER_PI 6.36619772367581382433e-01
77
78void omega_hostmath_sincos(double a, double *s, double *c) {
79 double fn = a * HM_2_OVER_PI;
80 long long n = (long long)(fn + (fn >= 0.0 ? 0.5 : -0.5));
81 double nd = (double)n;
82 double r = a - nd * HM_PIO2_1;
83 r -= nd * HM_PIO2_2;
84 r -= nd * HM_PIO2_3;
85 r -= nd * HM_PIO2_3T; /* |r| <= pi/4 + tiny */
86 double w = r * r;
87 double ps = 1.0;
88 for (int k = 15; k >= 3; k -= 2) ps = 1.0 - w * ps / (double)(k * (k - 1)); /* sin r = r * ps, terms to r^15 */
89 double pc = 1.0;
90 for (int k = 16; k >= 2; k -= 2) pc = 1.0 - w * pc / (double)(k * (k - 1)); /* cos r = pc, terms to r^16 */
91 double sr = r * ps, cr = pc;
92 switch ((int)(n & 3)) {
93 case 0: *s = sr; *c = cr; break;
94 case 1: *s = cr; *c = -sr; break;
95 case 2: *s = -sr; *c = -cr; break;
96 default: *s = -cr; *c = sr; break;
97 }
98}
99
100float omega_hostmath_pow2f(int e) {
101 uint32_t u;
102 if (e > 127) u = 0x7f800000u; /* +inf */
103 else if (e >= -126) u = (uint32_t)(e + 127) << 23; /* normal: biased exponent, zero mantissa */
104 else if (e >= -149) u = 1u << (e + 149); /* subnormal: one mantissa bit */
105 else u = 0;
106 float f; memcpy(&f, &u, 4); return f;
107}