From 4e7b5673fd4b1f05213e9f91972aab15730f0dd8 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:52:31 +0100 Subject: [PATCH] Math's powers, logarithms, rounding and inverses; Sys.space Report 2026-10.15. Math, after fdlibm as before: pow (e_pow, log2 in two parts through log2Parts), log2 and log10 on the same parts, tan (k_tan), asin and acos (e_asin, e_acos), atan public, ceil and round. Against glibc on 36,000 random arguments each is within an ulp; Math's test checks C's values and the edges. Sys.space: statfs, or C's statvfs, for the bytes free and in all; C gains statvfs. --- diff --git a/CLAUDE.md b/CLAUDE.md index d2acdc0..5bb5d2e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -86,10 +86,12 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes on the list in `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, -by the owner's word: report 2026-10.12; the compiler follows 2026-10.14, +by the owner's word: report 2026-10.12; the compiler follows 2026-10.15, whose `Sys` writes without waiting, for rat's pastes (10.13), and whose blank, `_ := f(x)`, drops a result that is otherwise warned of -(10.14), also by the owner's word. Its parser +(10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, +rounding and the inverse functions, and `Sys` `space` (10.15), also +by the owner's word. Its parser reaches the machine only through `gen.pas`; its steps 5 to 10 await the owner's review, and git has them. Keep `doc/compiler.md`'s table of lines and its routine names true when the diff --git a/lib/C.raz b/lib/C.raz index 3466099..56a02ab 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -50,6 +50,7 @@ extern proc chdir*(path: ^U8): I32; extern proc stat*(path, st: ^U8): I32; extern proc lstat*(path, st: ^U8): I32; +extern proc statvfs*(path, st: ^U8): I32; extern proc symlink*(target, path: ^U8): I32; extern proc posix_openpt*(flags: I32): I32; extern proc grantpt*(fd: I32): I32; diff --git a/lib/Math.raz b/lib/Math.raz index 7744d84..357c657 100644 --- a/lib/Math.raz +++ b/lib/Math.raz @@ -2,10 +2,11 @@ last bit or so. They follow fdlibm, Sun's library that most C ones come from: each brings its argument into a small range, where a polynomial is accurate, and puts the range back on the result. The - polynomials' coefficients are fdlibm's. + polynomials' coefficients are fdlibm's. Each is within an ulp, a + unit in the result's last place. - Left out: tan, the inverse sines and powers, until a program needs - them. *) + Left out: the hyperbolic functions, fmod and hypot, until a program + needs them. *) module Math; import Rt; @@ -53,6 +54,24 @@ return t; end; +(* The smallest whole number not below x. *) +proc ceil*(x: F64): F64; +begin + return -floor(-x); +end; + +(* The whole number nearest x, a half away from 0: the distance to + floor(|x|) is exact, where x + 0.5 may round up. *) +proc round*(x: F64): F64; +var a, t: F64; +begin + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); + t := floor(a); + if a - t >= 0.5 then t := t + 1.0; end; + if negative(x) then return -t; end; + return t; +end; + (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, in two steps where k is past what an F64's exponent holds. *) proc scale(x: F64; k: INT): F64; @@ -108,8 +127,238 @@ + z * (6.666666666666735130E-01 + w * (2.857142874366239149E-01 + w * (1.818357216161805012E-01 + w * 1.479819860511658591E-01))); return s * (hfsq + r) + F64(k) * ln2lo - hfsq + f + F64(k) * ln2hi; +end; + +(* pow and the logarithms after fdlibm's pow, which works on the two + 32-bit halves of an F64: the high one signed, with the sign and + exponent, and the low one. *) +proc high(x: F64): INT; +var h: INT; +begin + h := INT(bits(x) shr 32); + if h >= 80000000H then DEC(h, 100000000H); end; + return h; +end; + +proc low(x: F64): INT; +begin + return INT(bits(x) & 0FFFFFFFFH); +end; + +(* x with its low half 0: at most 21 bits, so that the product of two + such is exact. *) +proc noLow(x: F64): F64; +begin + return fromBits(bits(x) & 0FFFFFFFF00000000H); +end; + +proc withHigh(x: F64; h: INT): F64; +begin + return fromBits(U64(h & 0FFFFFFFFH) shl 32 | bits(x) & 0FFFFFFFFH); end; +(* log2 x, for a finite x above 0, as hi + lo to about 2^-64 of it, + hi with its low half 0. x = 2^n m, with m from 1 to sqrt(3); ln m + comes from s = (m - b)/(m + b), with b 1 or 1.5 as m is nearest, and + the series in s, made exact to its first terms in two parts. *) +proc log2Parts(x: F64): (F64, F64); +var + ix, j, k, n: INT; + u, v, ss, sh, sl, th, tl, s2, r, ph, pl, zh, zl, t, hi, b, dh, dl: F64; +begin + n := 0; + ix := high(x); + if ix < 00100000H then (* subnormal: made normal *) + x := x * 9007199254740992.0; (* 2^53 *) + n := -53; + ix := high(x); + end; + INC(n, (ix shr 20) - 3FFH); + j := ix & 000FFFFFH; + ix := j | 3FF00000H; + if j <= 3988EH then k := 0; (* m below sqrt(3/2) *) + elsif j < 0BB67AH then k := 1; (* below sqrt(3) *) + else + k := 0; + INC(n); + DEC(ix, 00100000H); + end; + x := withHigh(x, ix); + if k = 0 then b := 1.0; dh := 0.0; dl := 0.0; + else b := 1.5; dh := 5.84962487220764160156E-01; dl := 1.35003920212974897128E-08; (* log2 1.5 *) + end; + u := x - b; + v := 1.0 / (x + b); + ss := u * v; + sh := noLow(ss); + th := withHigh(0.0, ((ix shr 1) | 20000000H) + 00080000H + k shl 18); (* x + b, its high half *) + tl := x - (th - b); + sl := v * ((u - sh * th) - sh * tl); + s2 := ss * ss; + r := s2 * s2 * (5.99999999999994648725E-01 + s2 * (4.28571428578550184252E-01 + s2 * (3.33333329818377432918E-01 + + s2 * (2.72728123808534006489E-01 + s2 * (2.30660745775561754067E-01 + s2 * 2.06975017800338417784E-01))))); + r := r + sl * (sh + ss); + s2 := sh * sh; + th := noLow(3.0 + s2 + r); + tl := r - ((th - 3.0) - s2); + u := sh * th; + v := sl * th + tl * ss; + ph := noLow(u + v); + pl := v - (ph - u); + (* 2/(3 ln 2), in two parts, times p *) + zh := 9.61796700954437255859E-01 * ph; + zl := pl * 9.61796693925975554329E-01 - 7.02846165095275826516E-09 * ph + dl; + t := F64(n); + hi := noLow(((zh + zl) + dh) + t); + return hi, zl - (((hi - t) - dh) - zh); +end; + +(* The logarithm to base 2 of x; exact for a power of 2. *) +proc log2*(x: F64): F64; +var hi, lo: F64; +begin + if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x) * 1.44269504088896338700; end; + hi, lo := log2Parts(x); + return hi + lo; +end; + +(* The logarithm to base 10 of x: log2 x times log10 2, each in two + parts, the high parts' product exact. *) +proc log10*(x: F64): F64; +var hi, lo: F64; +begin + if (x # x) or (x <= 0.0) or (x > 1.7976931348623157E308) then return ln(x); end; + hi, lo := log2Parts(x); + return hi * 0.30102992057800293 + (hi * 7.508597826552624E-08 + lo * 0.3010299956639812); +end; + +(* x to the power y, as fdlibm's pow: within an ulp, and exact for a + whole result an F64 holds. log2 |x| in two parts, times y in two + parts, is z, and the result 2^z: 2^n times 2^r, for r at most 1/2 + across, from a series. *) +proc pow*(x, y: F64): F64; +var + hx, hy, ix, iy, lx, ly, i, j, k, n, yisint: INT; + ax, z, ph, pl, y1, t1, t2, r, s, t, u, v, w: F64; +begin + hx := high(x); lx := low(x); hy := high(y); ly := low(y); + ix := hx & 7FFFFFFFH; + iy := hy & 7FFFFFFFH; + if iy | ly = 0 then return 1.0; end; (* x^0, even for a NaN *) + if (hx = 3FF00000H) and (lx = 0) then return 1.0; end; (* 1^y, even for a NaN *) + if (x # x) or (y # y) then return x + y; end; + (* whether y is whole, as x < 0 needs to know: 0 not, 1 odd, 2 even *) + yisint := 0; + if hx < 0 then + if iy >= 43400000H then yisint := 2; (* 2^53 or more *) + elsif iy >= 3FF00000H then + k := (iy shr 20) - 3FFH; + if k > 20 then + j := ly shr (52 - k); + if j shl (52 - k) = ly then yisint := 2 - (j & 1); end; + elsif ly = 0 then + j := iy shr (20 - k); + if j shl (20 - k) = iy then yisint := 2 - (j & 1); end; + end; + end; + end; + if ly = 0 then + if iy = 7FF00000H then (* y an infinity *) + if (ix - 3FF00000H) | lx = 0 then return 1.0; end; (* (-1)^inf *) + if ix >= 3FF00000H then + if hy >= 0 then return y; end; + return 0.0; + end; + if hy < 0 then return -y; end; + return 0.0; + end; + if iy = 3FF00000H then + if hy < 0 then return 1.0 / x; end; + return x; + end; + if hy = 40000000H then return x * x; end; + if (hy = 3FE00000H) and (hx >= 0) then return sqrt(x); end; + end; + ax := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); + if (lx = 0) and ((ix = 7FF00000H) or (ix = 0) or (ix = 3FF00000H)) then (* x 0, 1 or an infinity *) + z := ax; + if hy < 0 then z := 1.0 / z; end; + if hx < 0 then + if (ix - 3FF00000H) | yisint = 0 then z := (z - z) / (z - z); (* (-1)^y, y not whole *) + elsif yisint = 1 then z := -z; + end; + end; + return z; + end; + if (hx < 0) and (yisint = 0) then return (x - x) / (x - x); end; (* NaN *) + s := 1.0; + if (hx < 0) and (yisint = 1) then s := -1.0; end; + if iy > 41E00000H then (* |y| above 2^31 *) + if iy > 43F00000H then (* above 2^64: x^y over- or underflows *) + if (ix <= 3FEFFFFFH) = (hy < 0) then return 1.0E300 * 1.0E300; end; + return 1.0E-300 * 1.0E-300; + end; + if ix < 3FEFFFFFH then + if hy < 0 then return s * 1.0E300 * 1.0E300; end; + return s * 1.0E-300 * 1.0E-300; + end; + if ix > 3FF00000H then + if hy > 0 then return s * 1.0E300 * 1.0E300; end; + return s * 1.0E-300 * 1.0E-300; + end; + (* |x - 1| is at most 2^-20: log2 x from the series of ln(1 + t) *) + t := ax - 1.0; + w := t * t * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); + u := 1.44269502162933349609E+00 * t; + v := t * 1.92596299112661746887E-08 - w * 1.44269504088896338700; + t1 := noLow(u + v); + t2 := v - (t1 - u); + else + t1, t2 := log2Parts(ax); + end; + (* z = y log2 x, as ph + pl *) + y1 := noLow(y); + pl := (y - y1) * t1 + y * t2; + ph := y1 * t1; + z := pl + ph; + j := high(z); + i := low(z); + if j >= 40900000H then (* z at least 1024 *) + if ((j - 40900000H) | i # 0) or (pl + 8.0085662595372944372E-17 > z - ph) then + return s * 1.0E300 * 1.0E300; + end; + elsif j & 7FFFFFFFH >= 4090CC00H then (* z at most -1075 *) + if (j # -3F6F3400H) or (i # 0) or (pl <= z - ph) then return s * 1.0E-300 * 1.0E-300; end; + end; + (* 2^z: n the nearest whole number, taken from ph *) + i := j & 7FFFFFFFH; + k := (i shr 20) - 3FFH; + n := 0; + if i > 3FE00000H then + n := j + 00100000H shr (k + 1); + k := ((n & 7FFFFFFFH) shr 20) - 3FFH; + t := withHigh(0.0, n - (n & (000FFFFFH shr k))); + n := ((n & 000FFFFFH) | 00100000H) shr (20 - k); + if j < 0 then n := -n; end; + ph := ph - t; + end; + t := noLow(pl + ph); + u := t * 6.93147182464599609375E-01; + v := (pl - (t - ph)) * 6.93147180559945286227E-01 - t * 1.90465429995776804525E-09; + z := u + v; + w := v - (z - u); + t := z * z; + t1 := z - t * (1.66666666666666019037E-01 + t * (-2.77777777770155933842E-03 + + t * (6.61375632143793436117E-05 + t * (-1.65339022054652515390E-06 + t * 4.13813679705723846039E-08)))); + r := (z * t1) / (t1 - 2.0) - (w + z * w); + z := 1.0 - (r - z); + j := high(z) + n shl 20; + if j < 00100000H then z := scale(z, n); (* a subnormal result *) + else z := withHigh(z, j); + end; + return s * z; +end; + (* a times b: the low 64 bits of the product, and the high. *) proc mul(a, b: U64): (U64, U64); code @@ -258,12 +507,73 @@ elsif n = 2 then return -kernelCos(y, yy); end; return kernelSin(y, yy); +end; + +(* tan(x + y), for x + y at most pi/4 across and y below an ulp of x, + or, for k -1, -1/tan(x + y): fdlibm's. Past 0.6744, tan(pi/4 - x) + is found and turned back; -1/w is made exact to its first part. *) +proc kernelTan(x, y: F64; k: INT): F64; +var hx, ix: INT; z, r, v, w, s, a, t: F64; +begin + hx := high(x); + ix := hx & 7FFFFFFFH; + if ix < 3E300000H then (* below 2^-28 *) + if k = 1 then return x; end; + if x = 0.0 then return 1.0 / fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); end; + w := x + y; + z := noLow(w); + v := y - (z - x); + a := -1.0 / w; + t := noLow(a); + s := 1.0 + t * z; + return t + a * (s + t * v); + end; + if ix >= 3FE59428H then + if hx < 0 then x := -x; y := -y; end; + z := 7.85398163397448278999E-01 - x; (* pi/4, in two parts *) + w := 3.06161699786838301793E-17 - y; + x := z + w; + y := 0.0; + end; + z := x * x; + w := z * z; + r := 1.33333333333201242699E-01 + w * (2.18694882948595424599E-02 + w * (3.59207910759131235356E-03 + + w * (5.88041240820264096874E-04 + w * (7.81794442939557092300E-05 - w * 1.85586374855275456654E-05)))); + v := z * (5.39682539762260521377E-02 + w * (8.86323982359930005737E-03 + w * (1.45620945432529025516E-03 + + w * (2.46463134818469906812E-04 + w * (7.14072491382608190305E-05 + w * 2.59073051863633712884E-05))))); + s := z * x; + r := y + z * (s * (r + v) + y); + r := r + 3.33333333333334091986E-01 * s; + w := x + r; + if ix >= 3FE59428H then + v := F64(k); + w := v - 2.0 * (x - (w * w / (w + v) - r)); + if hx < 0 then return -w; end; + return w; + end; + if k = 1 then return w; end; + z := noLow(w); + v := r - (z - x); + a := -1.0 / w; + t := noLow(a); + s := 1.0 + t * z; + return t + a * (s + t * v); +end; + +(* The tangent of x, in radians; NaN for an infinity or NaN. *) +proc tan*(x: F64): F64; +var n: INT; y, yy: F64; +begin + if (x = 0.0) or (x # x) then return x; end; + if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) + n, y, yy := reduce(x); + return kernelTan(y, yy, 1 - (n & 1) * 2); end; (* The arc tangent of x, at least 0: atan(x) = atan(c) + atan(t), t small, for c one of 1/2, 1, 3/2 and infinity, as x is nearest; the atan(c) in two parts as pi is. *) -proc atan(x: F64): F64; +proc atanAbove(x: F64): F64; var hi, lo, z, w, s1, s2: F64; begin if x < 0.4375 then @@ -303,15 +613,96 @@ else a := y / x; if a < 0.0 then a := -a; end; - a := atan(a); + a := atanAbove(a); end; (* pi less a, with what pi lacks of its true value *) if negative(x) then a := pi - (a - 1.2246467991473531772E-16); end; end; if negative(y) then return -a; end; return a; +end; + +(* The arc tangent of x, from -pi/2 to pi/2. *) +proc atan*(x: F64): F64; +begin + if x # x then return x; end; + if negative(x) then return -atanAbove(-x); end; + return atanAbove(x); +end; + +(* asin(sqrt t)/sqrt t - 1, near enough, for t up to 1/4: fdlibm's + rational function, for asin and acos. *) +proc arcSeries(t: F64): F64; +begin + return t * (1.66666666666666657415E-01 + t * (-3.25565818622400915405E-01 + t * (2.01212532134862925881E-01 + + t * (-4.00555345006794114027E-02 + t * (7.91534994289814532176E-04 + t * 3.47933107596021167570E-05))))) + / (1.0 + t * (-2.40339491173441421878E+00 + t * (2.02094576023350569471E+00 + + t * (-6.88283971605453293030E-01 + t * 7.70381505559019352791E-02)))); +end; + +(* The arc sine of x, from -pi/2 to pi/2; NaN past -1 and 1. Past + 1/2, asin x = pi/2 - 2 asin(sqrt((1 - |x|)/2)), the root in two + parts. *) +proc asin*(x: F64): F64; +var ix: INT; a, t, s, w, c, p, q: F64; +begin + ix := high(x) & 7FFFFFFFH; + if ix >= 3FF00000H then + if (ix = 3FF00000H) and (low(x) = 0) then return x * 1.57079632679489655800 + x * 6.12323399573676603587E-17; end; + return (x - x) / (x - x); (* NaN *) + end; + if ix < 3FE00000H then (* below 1/2 *) + if ix < 3E400000H then return x; end; (* below 2^-27 *) + return x + x * arcSeries(x * x); + end; + a := fromBits(bits(x) & 7FFFFFFFFFFFFFFFH); + t := (1.0 - a) * 0.5; + s := sqrt(t); + if ix >= 3FEF3333H then (* past 0.975 *) + t := 1.57079632679489655800 - (2.0 * (s + s * arcSeries(t)) - 6.12323399573676603587E-17); + else + w := noLow(s); + c := (t - w * w) / (s + w); + p := 2.0 * s * arcSeries(t) - (6.12323399573676603587E-17 - 2.0 * c); + q := 7.85398163397448278999E-01 - 2.0 * w; + t := 7.85398163397448278999E-01 - (p - q); + end; + if negative(x) then return -t; end; + return t; end; +(* The arc cosine of x, from 0 to pi; NaN past -1 and 1: pi/2 - asin x + near 0, and 2 asin(sqrt((1 -+ x)/2)) past 1/2 either way. *) +proc acos*(x: F64): F64; +var hx, ix: INT; z, s, w, c, df: F64; +begin + hx := high(x); + ix := hx & 7FFFFFFFH; + if ix >= 3FF00000H then + if (ix = 3FF00000H) and (low(x) = 0) then + if hx > 0 then return 0.0; end; + return pi + 2.0 * 6.12323399573676603587E-17; + end; + return (x - x) / (x - x); (* NaN *) + end; + if ix < 3FE00000H then (* below 1/2 *) + if ix <= 3C600000H then return 1.57079632679489655800 + 6.12323399573676603587E-17; end; + return 1.57079632679489655800 - (x - (6.12323399573676603587E-17 - x * arcSeries(x * x))); + end; + if hx < 0 then + z := (1.0 + x) * 0.5; + s := sqrt(z); + w := arcSeries(z) * s - 6.12323399573676603587E-17; + return pi - 2.0 * (s + w); + end; + z := (1.0 - x) * 0.5; + s := sqrt(z); + df := noLow(s); + c := (z - df * df) / (s + df); + w := arcSeries(z) * s + c; + return 2.0 * (df + w); +end; + (* The module's test: values C's library gives, to within two units in the last place, and the edges. *) proc near(got, want: F64): BOOL; @@ -351,6 +742,26 @@ or not near(atan2(5.0, 0.1), 1.550798992821746) or not near(atan2(1.0, -1.0E-20), 1.5707963267948966) or (atan2(0.0, -1.0) # pi) or not negative(atan2(-0.0, 1.0)) or (atan2(-inf, inf) # -pi / 4.0) or not near(atan2(inf, -inf), 2.356194490192345) or (atan2(1.0, 0.0) # pi / 2.0) then Rt.stop("Math.atan2"); end; + if (ceil(2.5) # 3.0) or (ceil(-2.5) # -2.0) or not negative(ceil(-0.5)) or (ceil(1.0E300) # 1.0E300) + or (round(2.5) # 3.0) or (round(-2.5) # -3.0) or (round(0.49999999999999994) # 0.0) + or not negative(round(-0.3)) then Rt.stop("Math.ceil, round"); end; + if not near(pow(2.5, 3.7), 29.67413253642086) or not near(pow(0.3, -12.5), 3435455.4096325925) + or not near(pow(1.0000001, 1.0E9), 2.6881038582144647E43) or (pow(-2.0, -3.0) # -0.125) + or (pow(10.0, 22.0) # 1.0E22) or (pow(2.0, -1074.0) # scale(1.0, -1074)) or (pow(2.0, 1024.0) # inf) + or (pow(nan, 0.0) # 1.0) or (pow(1.0, nan) # 1.0) or (pow(-1.0, inf) # 1.0) or (pow(0.0, -1.0) # inf) + or (pow(-0.0, -1.0) # -inf) or (pow(-2.0, 0.5) = pow(-2.0, 0.5)) then Rt.stop("Math.pow"); end; + if (log2(8.0) # 3.0) or (log2(0.5) # -1.0) or not near(log2(10.0), 3.321928094887362) + or not near(log2(3.0E-310), -1028.212746914361) or (log10(1000.0) # 3.0) or (log10(1.0E22) # 22.0) + or not near(log10(2.0), 0.3010299956639812) or not near(log10(7.0E-300), -299.15490195998575) + or (log2(0.0) # -inf) or (log10(-1.0) = log10(-1.0)) then Rt.stop("Math.log2, log10"); end; + if not near(tan(1.0), 1.5574077246549023) or not near(tan(-1.5707963267948966), -1.633123935319537E16) + or not near(tan(1.0E22), -1.6287782256068988) or not negative(tan(-0.0)) or (tan(inf) = tan(inf)) + then Rt.stop("Math.tan"); end; + if not near(asin(0.3), 0.3046926540153975) or not near(asin(-0.99), -1.4292568534704693) + or (asin(1.0) # pi / 2.0) or (asin(1.5) = asin(1.5)) or not near(acos(0.7), 0.7953988301841436) + or not near(acos(-0.2), 1.7721542475852274) or (acos(1.0) # 0.0) or (acos(-1.0) # pi) + or not near(atan(0.5), 0.4636476090008061) or not near(atan(-1.0E10), -1.5707963266948965) + or (atan(inf) # pi / 2.0) or not negative(atan(-0.0)) then Rt.stop("Math.asin, acos, atan"); end; end; end Math. diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 07c2865..932353a 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -263,6 +263,19 @@ return i, 0; end; +(* The bytes free to an ordinary user on path's file system, and the + bytes in all. *) +proc space*(path: []U8): (I64, I64, INT); +var a: [4096]U8; st: [15]INT; r, unit: INT; (* struct statfs: 120 bytes *) +begin + if not cstr(path, a) then return 0, 0, -36; end; + r := SYSCALL(137, ADR(a), ADR(st)); (* statfs *) + if r # 0 then return 0, 0, r; end; + unit := st[9]; (* f_frsize, the unit blocks are counted in *) + if unit = 0 then unit := st[1]; end; (* f_bsize *) + return st[4] * unit, st[2] * unit, 0; (* f_bavail, f_blocks *) +end; + (* Opens the directory at path, for nextEntry. *) proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; @@ -1046,7 +1059,7 @@ directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -1076,6 +1089,10 @@ if (err # 0) or not i.dir or i.file then exit(2); end; i, err := info("/dev/null"); if (err # 0) or i.dir or i.file then exit(2); end; + free, total, err := space("/tmp"); + if (err # 0) or (total <= 0) or (free < 0) or (free > total) then exit(2); end; + free, total, err := space("/no/such/dir"); + if err # -2 then exit(2); end; _ := remove("/tmp/raz-sys-link"); if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-link"); diff --git a/lib/Sys.raz b/lib/Sys.raz index f1366be..a144a53 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -292,6 +292,16 @@ return i, 0; end; +(* The bytes free to an ordinary user on path's file system, and the + bytes in all. *) +proc space*(path: []U8): (I64, I64, INT); +var a: [4096]U8; st: [14]INT; (* glibc's struct statvfs: 112 bytes *) +begin + if not cstr(path, a) then return 0, 0, -36; end; + if C.statvfs(ADR(a[0]), ^U8(ADR(st))) # 0 then return 0, 0, failed(); end; + return st[4] * st[1], st[2] * st[1], 0; (* f_bavail, f_blocks, in f_frsize *) +end; + (* Opens the directory at path, for nextEntry. *) proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; @@ -1012,7 +1022,7 @@ and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var free, total: I64; fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; @@ -1042,6 +1052,10 @@ if (err # 0) or not i.dir or i.file then exit(2); end; i, err := info("/dev/null"); if (err # 0) or i.dir or i.file then exit(2); end; + free, total, err := space("/tmp"); + if (err # 0) or (total <= 0) or (free < 0) or (free > total) then exit(2); end; + free, total, err := space("/no/such/dir"); + if err # -2 then exit(2); end; _ := remove("/tmp/raz-sys-link"); if symlink("/tmp/raz-sys-dir", "/tmp/raz-sys-link") # 0 then exit(2); end; i, err := info("/tmp/raz-sys-link"); diff --git a/src/raz.pas b/src/raz.pas index d89610d..ef331e9 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -34,7 +34,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.14'; { the version of doc/raz.md it follows } + Report = '2026-10.15'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first }