f_sizeof#
Return the payload storage of a scalar or an array in C bytes, as
integer(f_size_t), without accessing any element.
use futile ! or:
use f_utils, only: f_sizeof ! or:
use f_sizeof_module, only: f_sizeof
f_sizeof_module (flib/f_sizeof.f90) defines the interface; f_utils
and futile re-export it unchanged. f_precisions no longer provides
it: code importing f_sizeof only from f_precisions should import
f_sizeof_module instead.
Signature#
Generated from the FYPP documentation stub doc/generated/f_size_doc.f90
(emitted by fypp/size-doc.fypp from the same loops as flib/f_sizeof.f90,
which fypp/generate-sizeof.py generates from fypp/sizeof.fypp). In the
signature, any_intrinsic is a documentation placeholder: the argument may
be of any intrinsic type, of any Futile ABI kind.
The signature line merges both calling forms; their exact argument order is
given in the description below and under Typical calls.
- interface f_sizeof_module/f_sizeof(x[, ln])#
Calling forms:
f_sizeof(x), for any ABI type/kind and rank 0-7, andf_sizeof(ln, x), for character arrays (rank 1-7) only, withln=len(x)as the first argument. The result isinteger(f_size_t). Payload bytes, excluding descriptors, stride gaps and allocator overhead. All inquiries are pure; positive sizes must fit integer(f_size_t).- Parameters:
x [any_intrinsic, character(len=ln), in,rank 0-7] :: Scalar or array whose payload bytes are counted: any intrinsic type, of any Futile ABI kind; rank 0–7. No element is read, also from pure procedures.
- Options:
ln [integer, in] :: Character-array form
f_sizeof(ln, x)only: the element lengthlen(x), passed as the first argument;f_sizeof(x)gives the same result.
Typical calls#
bytes = f_sizeof(array) ! payload bytes, any ABI type/kind, rank 0-7
bytes = f_sizeof(value) ! scalar byte width
bytes = f_sizeof('abc') ! 3: one byte per character
bytes = f_sizeof(len(names), names) ! character array, element length first
bytes = f_sizeof(names) ! the one-argument array form also works
Returns#
The payload size in C bytes as integer(f_size_t), a kind compatible with
C size_t; Fortran still treats the integer as signed. A zero-length string
and an array with any zero extent have zero bytes. Noncontiguous sections
count their payload only, excluding stride gaps, descriptors and allocation
overhead. For character(len=3) :: a(2), f_sizeof(a) is six bytes on the
supported C-character platforms, while f_size counts two elements.
Supported kinds and ranks#
Scalar and array overloads cover the concrete f_precisions
ABI kinds and ranks zero through seven; semantic
kind aliases do not add duplicate overloads. The character(len=ln) row is
the character-array form f_sizeof(ln, x).
111 specific procedures implement f_sizeof (generated by FYPP; table emitted by the documentation stub f_size_doc.f90 from the same loops).
Type (kind) |
ranks of |
ranks of |
specifics |
|---|---|---|---|
|
0-7 |
– |
8 each |
|
1-7 |
0 |
7 |
Notes#
Byte widths
Byte widths use storage inquiries rather than assuming that a kind number
denotes a byte width. Scalar widths are storage_size divided by the bit
width of one C character, without hard-coding eight. Array sizes are the
product of f_size and the matching scalar width. No array element is
read, and the inquiry can be called from pure procedures. The generated
size_inquiries regression checks the results against f_loc address
differences and calls every kind/rank inquiry from a pure function.
Representable sizes
Let H = huge(0_f_size_t) and B be the bytes per element. A byte size is
representable when the element count is at most H / B (integer division);
representability of every positive intermediate and final product is a
precondition. Nonpositive factors yield zero before any multiplication, and
empty arrays never overflow a temporary product. f_sizeof performs no
overflow termination or error reporting; no result, saturation or wrapping
behaviour is promised outside that range. Regression tests cover the largest
representable arithmetic values and empty/negative extents without allocating
giant buffers.
For signed 64-bit f_size_t, H is 9,223,372,036,854,775,807. The first
overflowing element counts for ordinary storage widths are:
Bytes per element |
First overflow, 64-bit |
First overflow, 32-bit |
|---|---|---|
1 |
9,223,372,036,854,775,808 |
2,147,483,648 |
4 |
2,305,843,009,213,693,952 |
536,870,912 |
8 |
1,152,921,504,606,846,976 |
268,435,456 |
16 |
576,460,752,303,423,488 |
134,217,728 |
Thus an eight-byte real array first overflows at 2**60 elements on a 64-bit
kind: 8 EiB (2**63 bytes), for example a hypothetical cubic array with
1,048,576 elements along each axis. With a 32-bit kind, the first overflow is
already 2 GiB of payload. Real address-space, memory, compiler descriptor and
allocator limits generally prevent such allocations much earlier on 64-bit
systems: these are arithmetic limits, not supported allocation sizes. C’s
unsigned size_t may represent larger values than the corresponding signed
Fortran integer; the upper unsigned half is outside this API contract.
Migration
The scalar result kind changed from default integer to f_size_t:
dependent Fortran code must be recompiled against the updated module files.
Examples#
All examples are compiled and run by CI.
From tests/flib/size_contracts.F90:
subroutine example_size_contracts()
real(f_double) :: values(2,3)
character(len=3) :: names(2)
integer(f_size_t) :: bytes, elements
values=0.0_f_double
names='abc'
elements=f_size(values) ! 6 elements, independent of their type.
bytes=f_sizeof(values) ! 48 bytes on an eight-byte double ABI.
call expect_size(elements,6_f_size_t,'element count')
call expect_size(bytes,6_f_size_t*storage_size(0.0_f_double,kind=f_size_t)/8_f_size_t,'byte count')
call expect_size(f_size(len(names),names),2_f_size_t,'two string elements')
call expect_size(f_sizeof(len(names),names),6_f_size_t,'six character bytes')
call expect_size(f_size('abc'),1_f_size_t,'one scalar string')
call expect_size(f_sizeof('abc'),3_f_size_t,'three scalar character bytes')
end subroutine example_size_contracts
From tests/flib/f_precisions_examples.f90:
subroutine example_f_sizeof()
real(f_double) :: x
integer(f_integer) :: i
call require(f_sizeof(x) > 0, 'f_sizeof failed for real(f_double)')
call require(f_sizeof(i) > 0, 'f_sizeof failed for integer(f_integer)')
call require(kind(f_sizeof(x)) == f_size_t, 'incorrect scalar byte-count kind')
call require(f_sizeof(x) == storage_size(x,kind=f_size_t)/8_f_size_t, &
'real size differs from storage_size')
call require(f_sizeof(i) == storage_size(i,kind=f_size_t)/8_f_size_t, &
'integer size differs from storage_size')
end subroutine example_f_sizeof
See also#
f_size – element counts, with the same kinds and ranks.
f_memcpy_bytes – copies whose
nbytesusually comes fromf_sizeof.f_memcpy – element-count copies; capacities are checked in bytes.