Q_QADDI(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

πŸ’… Q_QADDI(3) β€” bestie's guide to the qmath integer-math squad πŸŽ€

ok so real talk: this is from the Library Functions Manual, and it's about the fixed-point math functions that apply plain integers to a Q number. yes, plural, there's a whole little friend group of them and we're meeting all six. sit down, hydrate, let's go ✨


🌸 NAME 🌸

Q_QADDI, Q_QDIVI, Q_QMULI, Q_QSUBI, Q_QFRACI, Q_QCPYVALI β€” the fixed-point math functions which apply integers to a Q number. that's the whole vibe. six functions, one mission, zero cap πŸ’–


✨ SYNOPSIS ✨

ok here's the lineup, and yes you include the header first, always:

#include <sys/qmath.h>

int
Q_QADDI(QTYPE *a, ITYPE b);

int
Q_QDIVI(QTYPE *a, ITYPE b);

int
Q_QMULI(QTYPE *a, ITYPE b);

int
Q_QSUBI(QTYPE *a, ITYPE b);

int
Q_QFRACI(QTYPE *q, ITYPE n, ITYPE d);

int
Q_QCPYVALI(QTYPE *q, ITYPE i);

every single one of them returns an int, and the args are exactly as shown β€” QTYPE pointer first, ITYPE after. don't go rearranging them, they're not that kind of function 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

ok so the first four are the classic arithmetic girlies. Q_QADDI(), Q_QDIVI(), Q_QMULI() and Q_QSUBI() will add, divide, multiply or subtract b to/by/from a respectively β€” and the result gets stored right back in a. in place. main character energy, it's all about a πŸ’…

(quick read of that: Q_QADDI() does a + b, Q_QDIVI() does a / b, Q_QMULI() does a * b, Q_QSUBI() does a - b. all landing back in a. iconic.)

then there's Q_QFRACI(), our fraction specialist πŸ“ β€” it computes the fraction n divided by d and stores the fixed-point result in q. so like, numerator over denominator, but the answer's fixed-point and it lives in q. periodt.

and finally Q_QCPYVALI(), the glow-up function 🌈 β€” it overwrites q's integer and fractional bits with the Q representation of the integer value i. both sets of bits. gone, replaced, reborn. not me being dramatic about a copy operation 😭

now, the types. all of those functions operate on the following data types, and honestly this is the part you screenshot for later:

  • s8q_t
  • u8q_t
  • s16q_t
  • u16q_t
  • s32q_t
  • u32q_t
  • s64q_t
  • u64q_t

which are all referred to generically as QTYPE. and the ITYPE? that one refers to the stdint(7) integer types. yes there's a whole other manpage for those, iykyk.

for more details (and there ARE more details, bestie), go see qmath(3). 🧚


πŸ”₯ RETURN VALUES πŸ”₯

ok the vibe check on what comes back:

  • Q_QADDI(), Q_QDIVI(), Q_QMULI(), Q_QSUBI(), Q_QFRACI() and Q_QCPYVALI() all return 0 on success ✨
  • or an errno on failure. yeah. not vibes.
  • EINVAL gets returned for divide-by-zero β€” like, obviously, you cannot do that, and the function WILL call you out πŸ’€
  • EOVERFLOW comes back for overflow
  • ERANGE comes back for underflow

so 0 = slay, everything else = read the errno and figure out your life πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

go touch these for more lore:


⭐ HISTORY ⭐

the qmath(3) functions first appeared in FreeBSD 13.0. a whole era. we stan a documented origin story πŸ’œ


πŸŽ€ AUTHORS πŸŽ€

the qmath(3) functions and this manual page were written by Lawrence Stewart <lstewart@FreeBSD.org> and sponsored by Netflix, Inc. β€” so yes, your fixed-point math is brought to you by the same people who brought you the "are you still watching?" jump scare 😭 sponsorship slays though, fr fr 🫢


and that's the page! this lil drop was from the Debian edition, dated July 8, 2018 β€” shoutout to the footer, we remembered you πŸ’–βœ¨ no cap, now go do some fixed-point math responsibly. πŸŒΈπŸ¦‹πŸ’…

πŸ“¦ where this page comes from