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_hostmath.c

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