Discussion:
addon functions
(too old to reply)
Hul Tytus
2023-08-01 21:21:44 UTC
Permalink
Lewin Edwards in his dosfs file system uses the ldiv() functions shown below:
sector = ldiv(offset, SECTOR_SIZE).quot and
offset = ldiv(offset, SECTOR_SIZE).rem
Anybody know how this is declared or, maybe, defined?

Hul
Hans-Bernhard Bröker
2023-08-01 21:44:35 UTC
Permalink
Post by Hul Tytus
sector = ldiv(offset, SECTOR_SIZE).quot and
offset = ldiv(offset, SECTOR_SIZE).rem
Anybody know how this is declared or, maybe, defined?
Seriously? Does every C compiler you use come without a manual?

How else could you possibly find it hard to discover the declaration and
semantics of a 33+ years old C standard library function like that?
Grant Edwards
2023-08-01 22:26:58 UTC
Permalink
Post by Hul Tytus
sector = ldiv(offset, SECTOR_SIZE).quot and
offset = ldiv(offset, SECTOR_SIZE).rem
Anybody know how this is declared or, maybe, defined?
$ man ldiv

------------------------------------------------------------------------------
div(3) Library Functions Manual div(3)

NAME
div, ldiv, lldiv, imaxdiv - compute quotient and remainder of an inte‐
ger division

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS
#include <stdlib.h>

div_t div(int numerator, int denominator);
ldiv_t ldiv(long numerator, long denominator);
lldiv_t lldiv(long long numerator, long long denominator);

#include <inttypes.h>

imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator);


[...]
Hul Tytus
2023-08-03 16:52:09 UTC
Permalink
Thanks Grant. I'd never seen those before and assumed they were Lewin's creation.

Hul
Post by Grant Edwards
Post by Hul Tytus
sector = ldiv(offset, SECTOR_SIZE).quot and
offset = ldiv(offset, SECTOR_SIZE).rem
Anybody know how this is declared or, maybe, defined?
$ man ldiv
------------------------------------------------------------------------------
div(3) Library Functions Manual div(3)
NAME
div, ldiv, lldiv, imaxdiv - compute quotient and remainder of an inte???
ger division
LIBRARY
Standard C library (libc, -lc)
SYNOPSIS
#include <stdlib.h>
div_t div(int numerator, int denominator);
ldiv_t ldiv(long numerator, long denominator);
lldiv_t lldiv(long long numerator, long long denominator);
#include <inttypes.h>
imaxdiv_t imaxdiv(intmax_t numerator, intmax_t denominator);
[...]
David Brown
2023-08-02 16:38:08 UTC
Permalink
Post by Hul Tytus
sector = ldiv(offset, SECTOR_SIZE).quot and
offset = ldiv(offset, SECTOR_SIZE).rem
Anybody know how this is declared or, maybe, defined?
Hul
It's a standard C library function. You can see its definition in the C
standards (draft versions of all modern C standards are freely available
online). A good reference for C is the "cppreference" site, with the
exact page here being:

<https://en.cppreference.com/w/c/numeric/math/div>

The "div" functions are almost never used these days - any decent
compiler will do as good or better when the code is expressed simply as:

sector = offset / SECTOR_SIZE;
offset = offset % SECTOR_SIZE;

But the "div" functions made sense with older and poorer compilers.
Loading...