Aussie AI

Fast Memory Block Operations

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Fast Memory Block Operations

The slow way to do things in arrays is one element at a time. The faster way is to use the standard memory block functions on the whole array. There are a number of standard functions that operate on array data or memory blocks and they are very fast. Memory block operations in the standard C++ libraries are implemented using fast assembly language behind the scenes. The main functions in the standard C++ library that operate on binary bytes in a memory block are:

  • memset — set bytes to a value, or clear bytes to zero.
  • memcpy — copy bytes in a block (non-overlapping).
  • memmove — copy bytes, allowing overlapping blocks.
  • memcmp — compare two blocks of bytes.
  • memchr — search for a byte in a block.

Note that unlike the standard string functions (such as strlen), these functions do not assume a block is null-terminated by a zero byte. Zero is simply a binary value, and these functions don't stop at a zero byte. All of these functions operate on a block of memory with a fixed byte length.

Each compiler environment typically offers some extra non-standard byte-wise functions that are also fast. Some of the less standardized C++ intrinsics that operate on memory blocks include:

  • _memccpy — copy bytes up to a specified sentinel byte.
  • memicmp / _memicmp — compare bytes ignoring letter case.
  • bcopy — copy a block of bytes.
  • bzero — clear a block of bytes to zero.
  • bcmp — compare bytes in two blocks.
  • _byteswap_uint64 — swap the bytes of an integer (Microsoft).
  • __builtin_bswap16 — swap the bytes in an integer (GCC). There are versions for 32-bit and 64-bit.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++