Fills up a complete array in memory setting bytes 2-by-2, in chuncks of 8 bytes. Size of the array must be multiple of 8.
void cpct_memset_f8 (void* array, u16 value, u16 size);
(2B HL) array | Pointer to the first byte of the array to be filled up (starting point in memory) |
(2B DE) value | 16-bit value to be set (Pair of bytes) |
(2B BC) size | Number of bytes to be set (>= 8, multiple of 8) |
call cpct_memset_f8_asm
Sets all pairs of bytes of an array in memory to the same given value. This is the same operation as std memset from standard C library, but with the added advantage of being faster and letting the user define the contents 16-bits-by-16-bits instead of 8. The technique used by this function is as follows:
1 | It saves the value of SP to recover it at the end of the function |
2 | It places SP at the last 2-bytes of the array |
3 | It uses PUSH instructions to set bytes 2-by-2, in chuncks of 8 bytes, until the entire array is set |
This function works for array sizes from 8 to 65528. However, it is recommended that you use it for values much greater than 8. Depending on your code, using <cpct_memset_f> for values in the range [8-16] could underperform simple variable assignments.
AF, BC, DE, HL
C-binding | 46 bytes |
ASM-binding | 41 bytes |
Case | microSecs (us) | CPU Cycles | ---------------------------------------------------------- Any | 53 + 20CH + 3CHHH | 212 + 80*CH + 12(CHHH) | ---------------------------------------------------------- Asm saving | -16 | -64 | ----------------------------------------------------------
BC = array size (Number of total bytes to set) CH = BC \ 8 (number of chuncks, 1 chunck = 8 bytes) CHHH = CH \ 256 - 1 \ = integer division