Skip to content
Augustin LOPEZ edited this page Sep 18, 2019 · 17 revisions

Introduction

From The Open Group Base Specifications issue 7, 2018:

#include <string.h>

void *memset(void *s, int c, size_t n);
  • The memset() function shall copy c (converted to an unsigned char) into each of the first n bytes of the object pointed to by s.
  • The memset() function shall return s; no return value is reversed to indicate an error.
  • No error are defined.

Implementation

This implementation will not work on older and/or specialized hardware.

  • This memset() sets up to 8 bytes per iteration.
  • An alignment loop is performed when necessary, setting the first byte one by one (up to 7 bytes).
  • If the remaining bytes are less than 8, the last bytes are set one by one (up to 7 bytes).

Speed tests

  • Results are a percentage of the performance against a glibc memset [ldd (Ubuntu GLIBC 2.27-3ubuntu1) 2.27].
  • Compiler is gcc [(Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0].
  • for each size the test runtime is 5 seconds.

Basic implementation

Size\flag -O3 -fno-builtin -O2 -Os (none)
1 99% 104% 101% 100%
10 100% 101% 100% 101%
100 100% 97% 96% 87%
1K 105% 83% 81% 41%
10K 100% 35% 35% 8%
100K 104% 12% 13% 2%
1M 79% 10% 10% 2%

Current implementation

Size\flag -O3 -fno-builtin -O2 -Os (none)
1 102% 100% 100% 98%
10 101% 102% 99% 99%
100 98% 101% 101% 98%
1K 101% 95% 95% 84%
10K 98% 88% 78% 41%
100K 99% 74% 57% 17%
1M 80% 68% 53% 13%

Other implementations

Further reading

Clone this wiki locally