# f_memcpy

Copy raw storage from `src` to `dest`, with `n` counting source
elements, without numerical or character-code conversion.

```fortran
use futile                                  ! or:
use f_memcpy_module, only: f_memcpy, f_memcpy_bytes
```

## Signature

Generated from the FYPP documentation stub `doc/generated/f_memcpy_doc.f90` (emitted by
`fypp/memcpy-doc.fypp` from the same loops as `flib/f_memcpy.f90`). In the
signature, `any_intrinsic` is a documentation placeholder: the argument may
be of any intrinsic type, of any Futile [ABI kind](../../f_prec.md#futile-abi-kinds).
The supported combinations are listed under [Supported kinds and ranks]().

### *interface*  f_memcpy_module/f_memcpy(dest, src, [n])

Copy raw storage, with n counting source elements, not destination elements.
Same-kind arrays support equal ranks, scalar/array and rank-one/array pairs,
through rank seven. Scalar starts support all ABI kind pairs and require n.
Counts accept f_integer or f_size_t. An omitted array-source count copies
the full source; scalar-to-array inference fills its byte capacity exactly.
Known capacities, contiguity and count overflow are checked. Invalid copies
raise ERR_INVALID_COPY. Zero-byte copies obtain no address; overlap is safe.
Scalar starts require caller-guaranteed backing capacity. No conversion,
allocation or ownership transfer occurs. Borrowed f_subptr views can adapt
other array ranks; their writes modify the original destination storage.

* **Parameters:**
  * **dest** *[**any_intrinsic* *,* *target,rank 0-7* *]* :: Definable contiguous destination storage: any intrinsic type, of any Futile [ABI kind](../../f_prec.md#futile-abi-kinds); rank 0–7. A scalar is the start of a larger contiguous buffer whose capacity the caller guarantees.
  * **src** *[**any_intrinsic* *,* *target,rank 0-7* *]* :: Contiguous source storage, rank 0–7; an array source has the same type and kind as `dest`. A scalar character string is one source element of `len(src)` characters.
* **Options:**
  **n** *[**integer(f_size_t|f_integer)* *,* *in* *]* :: Number of **source elements** (not bytes). Kinds: `f_size_t`, `f_integer`. Mandatory when both arguments are scalars; otherwise inferred from the source extent or the destination byte capacity.

### Count inference when the count is omitted

* Array source: copies the entire source payload.
* Scalar source with array destination: fills the destination’s byte capacity,
  which must be an exact multiple of the source element width.
* Two scalar buffers: `n` is mandatory.

## Supported kinds and ranks

The typed generic covers the concrete `f_precisions` ABI kinds (including
complex and character) and ranks zero through seven: same type/kind arrays with
equal ranks, scalar/array pairs, and rank-one/other-array pairs. Scalar/scalar
starts accept all type/kind pairs. Other rank changes go through borrowed
[f_subptr](f_subptr.md) views; unsupported combinations are rejected at compile time
(no `class(*)` dispatch).

**1196** specific procedures implement `f_memcpy` (generated by FYPP; table emitted by the documentation stub `f_memcpy_doc.f90` from the same loops).

| Type (kind)                                                                                                                                                                                    | ranks of `dest`   | ranks of `src`   | rank pairings                                    | count kinds             | specifics   |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|------------------|--------------------------------------------------|-------------------------|-------------|
| `integer` (f_int8, f_short, f_integer, f_long); `real` (f_simple, f_double, f_long_double); `complex` (f_simple, f_double, f_long_double); `logical` (f_byte, f_logical); `character` (len=\*) | 0-7               | 0-7              | equal, scalar/array, rank-1/array, scalar starts | `f_size_t`, `f_integer` | 92 each     |

Scalar buffer starts (both arguments rank 0) accept every source type/kind with any of the 13 destination type/kinds.

## Notes

### Capacity and bounds checks

Known array capacities are checked in bytes before writing. Negative/excessive
counts, an unrepresentable byte count, fractional inferred source elements and
noncontiguous array sections raise `ERR_INVALID_COPY` without changing the
destination. Zero-byte copies access neither data address; empty arrays copy
zero elements. A zero-width scalar source cannot infer a count for a
destination with nonzero byte capacity.

### Overlap, contiguity and temporaries

Partial overlap is supported through C `memmove`. `TARGET` on the internal
dummies permits `C_LOC`; it does not guarantee that an arbitrary actual
argument is passed without a temporary. Use contiguous variables, and actual
array elements for scalar buffer starts. There is deliberately no
`CONTIGUOUS` dummy attribute that would silently pack a strided section.

### Ownership

No allocation or ownership transfer occurs. When `dest` is a borrowed
[f_subptr](f_subptr.md) view, writes modify the owner’s storage.

### Character data

`n` counts complete source elements including each string’s full length.
Copying `['ABC','DEF']` from length-three elements into three length-two
elements gives `['AB','CD','EF']`: no padding or truncation, and a partial copy
can end inside a destination element. For character codes use
`f_atoi`/`f_itoa` or `IACHAR`/`ACHAR` explicitly; the former mixed
character/integer overloads were removed.

### Integer addresses

An integer address value is itself an integer buffer, not an instruction to
dereference that address.

## Examples

All examples are compiled and run by CI (`tests/flib/memcpy_check.f90`).

### Into a borrowed view

```fortran
owner=-99.0_f_double
view=>f_subptr(owner(:,:,2),from=3_f_integer,n=6_f_size_t,lbound=-2_f_integer)
call require(associated(view),'destination view associated before copy')
call require(lbound(view,1)==-2,'destination view lower bound')
call f_memcpy(src=matrix,dest=view)
nullify(view)
expected=-99.0_f_double
expected(15:20)=payload
call require(all(reshape(owner,[24])==expected),'subptr destination modifies owner and preserves canaries')
```

### Complex to real pairs

One complex value into two reals (the storage transfer used by the
complex DIIS path):

```fortran
  ! One complex SOURCE element occupies two real destination slots.
  ! Scalar destinations denote buffer starts; the caller owns their capacity.
  z=cmplx(3.0_f_double,-7.0_f_double,kind=f_double)
  pair=-99.0_f_double
  call f_memcpy(src=z,dest=pair(2),n=1)
  call require(all(pair(2:3) == [real(z,f_double),aimag(z)]),'complex pair storage')
  call require(pair(1) == -99.0_f_double .and. pair(4) == -99.0_f_double,'complex pair canaries')
```

## See also

* [f_memcpy_bytes](f_memcpy_bytes.md) – copy between scalar buffer starts with an explicit **byte** count.
* [f_subptr](f_subptr.md) – borrowed rank-one views to adapt array ranks before copying.
* [f_subptr_str](f_subptr_str.md) – character-unit views when copying into or out of strings.
* [f_size](f_size.md) / [f_sizeof](f_sizeof.md) – element counts and byte sizes for `n`.
