Beeg float library, a Rust port of Fabrice Bellard's libbf

serialx2 pts0 comments

GitHub - lifthrasiir/libbeef: Beeg float library, a Rust port of Fabrice Bellard's libbf ยท GitHub

/" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

lifthrasiir

libbeef

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

main

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>3 Commits<br>3 Commits

docs

docs

examples

examples

src

src

tests

tests

.gitignore

.gitignore

CLAUDE.md

CLAUDE.md

Cargo.toml

Cargo.toml

README.md

README.md

View all files

Repository files navigation

๐Ÿฅฉ libbeef

A Rust translation of Fabrice Bellard's libbf โ€”<br>a tiny arbitrary-precision floating-point library. The name stands for "Beeg<br>Float".

Full IEEE 754 semantics: signed zeros, NaN, infinities, configurable exponent<br>width, subnormals, all five rounding modes, all five status flags.

Transcendental functions: exp, log, pow, sin, cos, tan, atan, atan2, asin, acos.

Decimal floating point (BigDecimal) with independent base-10 arithmetic.

no_std compatible (requires alloc).

Pure Rust with zero dependencies.

Quick example

fn main() {<br>let a: Quad = "3.14159265358979323846".parse().unwrap();<br>let b: Quad = "2.71828182845904523536".parse().unwrap();<br>let result = (a * b).sin();<br>println!("{result}");<br>// 0.773942685266708278263054855332479932...<br>}">use libbeef::format::formats;<br>use libbeef::Float;

type Quad = Floatformats::Binary128>;

fn main() {<br>let a: Quad = "3.14159265358979323846".parse().unwrap();<br>let b: Quad = "2.71828182845904523536".parse().unwrap();<br>let result = (a * b).sin();<br>println!("{result}");<br>// 0.773942685266708278263054855332479932...

Float pairs a value with a compile-time format, so * and .sin() use<br>128-bit precision and round-to-nearest-even automatically โ€” no format argument<br>at every call site.

Performance

libbeef implements the same algorithms as the C libbf: NTT-based multiplication,<br>Newton iteration for division/sqrt, and AGM/binary-splitting for<br>transcendentals. The asymptotic complexity is optimal for each operation class:

Operation class<br>Complexity<br>Algorithm

add, sub, cmp<br>O(n)<br>Linear scan

mul<br>O(n log n)<br>Number-theoretic transform

div, sqrt<br>O(M(n))<br>Newton iteration over NTT mul

exp, log, sin, โ€ฆ<br>O(M(n) ยท log n)<br>AGM / binary splitting

Empirically (full data in docs/benchmark-report.md),<br>libbeef tracks the C libbf's throughput with constant-factor overhead from Rust's<br>bounds checking and allocation model:

op<br>256 bits<br>30 000 bits<br>300 000 bits<br>vs libbf<br>vs rug (GMP/MPFR)

mul<br>6.7<br>84<br>100 ns/limb<br>1.3โ€“2.0ร—<br>0.9โ€“1.3ร—

div<br>17.3<br>515<br>659 ns/limb<br>1.1โ€“1.4ร—<br>2.6โ€“3.5ร—

sqrt<br>38.3<br>384<br>597 ns/limb<br>1.2โ€“1.3ร—<br>3.2โ€“4.3ร—

sin<br>1051<br>โ€” ns/limb<br>0.7ร— (faster)<br>3.6ร—

The mul row is the most informative: a quadratic algorithm would show<br>~10ร— growth per decade of operand size (47 โ†’ 469 โ†’ 4688 limbs), but libbeef grows<br>4.2ร— then 1.2ร— โ€” the O(n log n) NTT envelope, the same shape as the C<br>original. At 300k bits libbeef is ~2ร— libbf and 1.3ร— GMP, while being 4ร— faster<br>than num-bigint's schoolbook/Toom multiplication.

For transcendentals , libbeef matches or beats the C libbf on sin/cos/tan/pow<br>and is within 15% on log/atan. The uniform 3โ€“5ร— gap to MPFR is algorithmic<br>(MPFR uses different, better algorithms for these functions; the C libbf shows<br>the same gap).

Division and sqrt show a larger constant-factor gap to GMP/MPFR (~3ร—). This is<br>an inherent property of libbf's Newton-reciprocal approach vs. GMP's tuned<br>divide-and-conquer โ€” the same ratio appears in the C original.

Why libbeef?

1. Pure Rust, no system dependencies. rug/GMP requires a C compiler, system<br>GMP/MPFR libraries, and a build script that probes the host. libbeef is a single<br>cargo add with no build.rs, -lm, nor pkg-config. It builds on any<br>target rustc supports โ€” including WASM, embedded, and cross-compilation โ€” with<br>zero configuration.

2. Small binary footprint. With GMP/MPFR statically linked, a trivial<br>program that multiplies two numbers and computes sin produces:

Library<br>Binary size (stripped)

libbeef<br>482 KiB

num-bigint (integers only, no trig)<br>448 KiB

malachite (integers only, no trig)<br>658 KiB

rug (GMP + MPFR...

libbeef libbf mpfr rust float search

Related Articles