aboutsummaryrefslogtreecommitdiffstats
path: root/mempsetelem.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-08-18 09:58:23 +0200
committerMattias Andrée <maandree@kth.se>2024-08-18 09:58:23 +0200
commita69f0f613687edf6c1f1ee83b462f77e8ea3c9a9 (patch)
treed976683461a0f427d2f1ef79a8732a048dd0c67b /mempsetelem.c
parentMerge tag '1.3' into since (diff)
parentUpdate VERSION_MINOR (diff)
downloadlibsimple-a69f0f613687edf6c1f1ee83b462f77e8ea3c9a9.tar.gz
libsimple-a69f0f613687edf6c1f1ee83b462f77e8ea3c9a9.tar.bz2
libsimple-a69f0f613687edf6c1f1ee83b462f77e8ea3c9a9.tar.xz
Merge tag '1.4' into since
Version 1.4
Diffstat (limited to 'mempsetelem.c')
-rw-r--r--mempsetelem.c48
1 files changed, 31 insertions, 17 deletions
diff --git a/mempsetelem.c b/mempsetelem.c
index 6a3bd4f..61b202b 100644
--- a/mempsetelem.c
+++ b/mempsetelem.c
@@ -1,5 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include "libsimple.h"
+#include "common.h"
#ifndef TEST
@@ -10,32 +10,42 @@ libsimple_mempsetelem(void *buf_, const void *item, size_t width, size_t n)
case 0:
return buf_;
case 1:
- return &((char *)memset(buf_, *(char *)item, n))[n];
+ return &((char *)memset(buf_, *(const char *)item, n))[n];
case 2:
+ if ((uintptr_t)buf_ % _Alignof(uint16_t) || (uintptr_t)item % _Alignof(uint16_t))
+ goto misaligned;
{
- uint16_t *buf = buf_, e = *(uint16_t *)item;
+ uint16_t *buf = buf_;
uint16_t *end = &buf[n];
+ uint16_t e = *(const uint16_t *)item;
for (; buf != end; buf++)
*buf = e;
return buf;
}
case 4:
+ if ((uintptr_t)buf_ % _Alignof(uint32_t) || (uintptr_t)item % _Alignof(uint32_t))
+ goto misaligned;
{
- uint32_t *buf = buf_, e = *(uint32_t *)item;
+ uint32_t *buf = buf_;
uint32_t *end = &buf[n];
+ uint32_t e = *(const uint32_t *)item;
for (; buf != end; buf++)
*buf = e;
return buf;
}
case 8:
+ if ((uintptr_t)buf_ % _Alignof(uint64_t) || (uintptr_t)item % _Alignof(uint64_t))
+ goto misaligned;
{
- uint64_t *buf = buf_, e = *(uint64_t *)item;
+ uint64_t *buf = buf_;
uint64_t *end = &buf[n];
+ uint64_t e = *(const uint64_t *)item;
for (; buf != end; buf++)
*buf = e;
return buf;
}
default:
+ misaligned:
{
char *buf = buf_;
size_t i;
@@ -54,27 +64,31 @@ libsimple_mempsetelem(void *buf_, const void *item, size_t width, size_t n)
int
main(void)
{
- char buf_[4096];
- char *buf = buf_;
+ _Alignas(8) char buf_[4096];
+ _Alignas(8) char *buf = buf_;
size_t i;
memset(buf, 0, sizeof(buf_));
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 0, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 0, 10) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 1, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 2, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 4, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 8, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 16, 0) == &buf[0]);
- assert(libsimple_mempsetelem(buf, &(uint64_t){~0}, 3, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 0, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 0, 10) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 1, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 2, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 4, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 8, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 16, 0) == &buf[0]);
+ assert(libsimple_mempsetelem(buf, &(uint64_t){~(uint64_t)0}, 3, 0) == &buf[0]);
assert(libsimple_mempsetelem(buf, &(uint8_t){0x09}, 1, 3000) == &buf[3000]);
assert(libsimple_mempsetelem(buf, &(uint16_t){0x0807}, 2, 1000) == &buf[2000]);
assert(libsimple_mempsetelem(buf, &(uint32_t){0x10203040UL}, 4, 300) == &buf[1200]);
assert(libsimple_mempsetelem(buf, &(uint64_t){0x0102030450607080ULL}, 8, 100) == &buf[800]);
- assert(libsimple_mempsetelem(buf, (char []){0xA0, 0xB0, 0xC0}, 3, 16) == &buf[48]);
+ assert(libsimple_mempsetelem(buf, (char []){(char)0xA0, (char)0xB0, (char)0xC0}, 3, 16) == &buf[48]);
+
+#if defined(__clang__)
+# pragma clang diagnostic ignored "-Wcast-align"
+#endif
for (i = 0; i < 48; i++)
- assert(buf[i] == ((char []){0xA0, 0xB0, 0xC0})[i % 3]);
+ assert(buf[i] == ((char []){(char)0xA0, (char)0xB0, (char)0xC0})[i % 3]);
for (; i < 800; i += 8)
assert(*(uint64_t *)&buf[i] == 0x0102030450607080ULL);
for (; i < 1200; i += 4)