Files
Basys3RiscV/lib/mathf.c
T
brad ArantandClaude Opus 5.5 5930b958b4 Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:18 -08:00

187 lines
5.1 KiB
C

/* Single-precision maths for BASIC: sinf cosf tanf atanf expf logf sqrtf
floorf. Range reduction by Cody and Waite, minimax polynomials as in the
Cephes library; good to an ulp or two, which BASIC's 6-7 printed digits
don't show. sqrtf and floorf are exact. (newlib's versions are larger and
bring in errno, stdio and malloc.) */
#include <math.h>
#include <stdint.h>
typedef union { float f; uint32_t i; } fbits;
static const float DP1 = 0.78515625f, DP2 = 2.4187564849853515625e-4f,
DP3 = 3.77489497744594108e-8f, FOPI = 1.27323954473516f;
/* x reduced to [-pi/4, pi/4]; returns the octant */
static float reduce(float x, int *oct)
{
int j = (int)(x * FOPI);
float y;
if (j & 1) j++;
y = (float)j;
*oct = j & 7;
return ((x - y * DP1) - y * DP2) - y * DP3;
}
static float sin_poly(float z)
{
float zz = z * z;
return ((-1.9515295891e-4f * zz + 8.3321608736e-3f) * zz - 1.6666654611e-1f) * zz * z + z;
}
static float cos_poly(float z)
{
float zz = z * z;
return ((2.443315711809948e-5f * zz - 1.388731625493765e-3f) * zz
+ 4.166664568298827e-2f) * zz * zz - 0.5f * zz + 1.0f;
}
float sinf(float x)
{
int neg = x < 0, j;
float z, y;
if (neg) x = -x;
z = reduce(x, &j);
if (j > 3) { neg = !neg; j -= 4; }
y = (j == 1 || j == 2) ? cos_poly(z) : sin_poly(z);
return neg ? -y : y;
}
float cosf(float x)
{
int neg = 0, j;
float z, y;
if (x < 0) x = -x;
z = reduce(x, &j);
if (j > 3) { neg = 1; j -= 4; }
if (j > 1) neg = !neg;
y = (j == 1 || j == 2) ? sin_poly(z) : cos_poly(z);
return neg ? -y : y;
}
float tanf(float x)
{
int neg = x < 0, j;
float z, zz, y;
if (neg) x = -x;
z = reduce(x, &j);
zz = z * z;
if (x > 1e-4f)
y = (((((9.38540185543e-3f * zz + 3.11992232697e-3f) * zz + 2.44301354525e-2f) * zz
+ 5.34112807005e-2f) * zz + 1.33387994085e-1f) * zz + 3.33331568548e-1f) * zz * z + z;
else
y = z;
if (j & 2) y = -1.0f / y;
return neg ? -y : y;
}
float atanf(float x)
{
int neg = x < 0;
float y, z;
if (neg) x = -x;
if (x > 2.414213562373095f) { y = 1.5707963267948966f; x = -1.0f / x; }
else if (x > 0.4142135623730950f) { y = 0.7853981633974483f; x = (x - 1.0f) / (x + 1.0f); }
else y = 0.0f;
z = x * x;
y += (((8.05374449538e-2f * z - 1.38776856032e-1f) * z + 1.99777106478e-1f) * z
- 3.33329491539e-1f) * z * x + x;
return neg ? -y : y;
}
/* x * 2^n */
static float scale2(float x, int n)
{
fbits u;
while (n > 127) { x *= 1.7014118e38f; n -= 127; }
while (n < -126) { x *= 1.1754944e-38f; n += 126; }
u.i = (uint32_t)(n + 127) << 23;
return x * u.f;
}
float expf(float x)
{
float z, y;
int n;
if (x > 88.72283905206835f) return HUGE_VALF;
if (x < -103.278929903431851f) return 0.0f;
z = floorf(1.44269504088896341f * x + 0.5f);
x -= z * 0.693359375f;
x -= z * -2.12194440e-4f;
n = (int)z;
z = x * x;
y = (((((1.9875691500e-4f * x + 1.3981999507e-3f) * x + 8.3334519073e-3f) * x
+ 4.1665795894e-2f) * x + 1.6666665459e-1f) * x + 5.0000001201e-1f) * z + x + 1.0f;
return scale2(y, n);
}
float logf(float x)
{
fbits u = { x };
int e;
float y, z;
if (x <= 0.0f) return x == 0.0f ? -HUGE_VALF : (x - x) / (x - x);
if (u.i >= 0x7F800000) return x;
e = 0;
if (u.i < 0x00800000) { u.f *= 16777216.0f; e = -24; } /* subnormal */
e += (int)(u.i >> 23) - 126;
u.i = (u.i & 0x007FFFFF) | 0x3F000000; /* [0.5, 1) */
x = u.f;
if (x < 0.707106781186547524f) { e--; x = x + x - 1.0f; }
else x = x - 1.0f;
z = x * x;
y = ((((((((7.0376836292e-2f * x - 1.1514610310e-1f) * x + 1.1676998740e-1f) * x
- 1.2420140846e-1f) * x + 1.4249322787e-1f) * x - 1.6668057665e-1f) * x
+ 2.0000714765e-1f) * x - 2.4999993993e-1f) * x + 3.3333331174e-1f) * x * z;
y += -2.12194440e-4f * (float)e;
y += -0.5f * z;
z = x + y;
return z + 0.693359375f * (float)e;
}
/* exact: one bit at a time (after fdlibm) */
float sqrtf(float x)
{
fbits u = { x };
int32_t ix = (int32_t)u.i, m, i;
uint32_t q, s, r, t;
if (ix <= 0) return (ix & 0x7FFFFFFF) == 0 ? x : (x - x) / (x - x);
if (ix >= 0x7F800000) return x;
m = ix >> 23;
if (m == 0) { /* subnormal */
for (i = 0; !(ix & 0x00800000); i++) ix <<= 1;
m -= i - 1;
}
m -= 127;
ix = (ix & 0x007FFFFF) | 0x00800000;
if (m & 1) ix += ix;
m >>= 1;
ix += ix;
q = s = 0;
r = 0x01000000;
while (r) {
t = s + r;
if (t <= (uint32_t)ix) { s = t + r; ix -= t; q += r; }
ix += ix;
r >>= 1;
}
if (ix) q += q & 1;
u.i = (q >> 1) + 0x3F000000 + ((uint32_t)m << 23);
return u.f;
}
float floorf(float x)
{
fbits u = { x };
int e = (int)(u.i >> 23 & 0xFF) - 0x7F;
uint32_t m;
if (e >= 23) return x;
if (e >= 0) {
m = 0x007FFFFF >> e;
if (!(u.i & m)) return x;
if (u.i >> 31) u.i += m;
u.i &= ~m;
} else if (!(u.i >> 31)) u.i = 0;
else if (u.i << 1) u.f = -1.0f;
return u.f;
}