check_SockAddr_is_zeroes.c
text/x-csrc
Filename: check_SockAddr_is_zeroes.c
Type: text/x-csrc
Part: 0
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <sys/socket.h>
#define LOOPS 1000
typedef struct
{
struct sockaddr_storage addr;
socklen_t salen;
} SockAddr;
static inline bool
pg_memory_is_all_zeros(const void *ptr, size_t len)
{
const unsigned char *p = (const unsigned char *) ptr;
const unsigned char *end = &p[len];
const unsigned char *aligned_end = (const unsigned char *)
((uintptr_t) end & (~(sizeof(size_t) - 1)));
if (len < sizeof(size_t))
{
while (p < end)
{
if (*p++ != 0)
return false;
}
return true;
}
/* "len" in the [sizeof(size_t), sizeof(size_t) * 8 - 1] range */
if (len < sizeof(size_t) * 8)
{
/* Compare bytes until the pointer "p" is aligned */
while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)
{
if (p == end)
return true;
if (*p++ != 0)
return false;
}
/*
* Compare remaining size_t-aligned chunks.
*
* There is no risk to read beyond the memory area, as "aligned_end"
* cannot be higher than "end".
*/
for (; p < aligned_end; p += sizeof(size_t))
{
if (*(size_t *) p != 0)
return false;
}
/* Compare remaining bytes until the end */
while (p < end)
{
if (*p++ != 0)
return false;
}
return true;
}
/* "len" in the [sizeof(size_t) * 8, inf) range */
/* Compare bytes until the pointer "p" is aligned */
while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)
{
if (p == end)
return true;
if (*p++ != 0)
return false;
}
/*
* Compare 8 * sizeof(size_t) chunks at once.
*
* For performance reasons, we manually unroll this loop and purposefully
* use bitwise-ORs to combine each comparison. This prevents boolean
* short-circuiting and lets the compiler know that it's safe to access
* all 8 elements regardless of the result of the other comparisons. This
* seems to be enough to coax a few compilers into using SIMD
* instructions.
*/
for (; p < aligned_end - (sizeof(size_t) * 7); p += sizeof(size_t) * 8)
{
if ((((size_t *) p)[0] != 0) | (((size_t *) p)[1] != 0) |
(((size_t *) p)[2] != 0) | (((size_t *) p)[3] != 0) |
(((size_t *) p)[4] != 0) | (((size_t *) p)[5] != 0) |
(((size_t *) p)[6] != 0) | (((size_t *) p)[7] != 0))
return false;
}
/*
* Compare remaining size_t-aligned chunks.
*
* There is no risk to read beyond the memory area, as "aligned_end"
* cannot be higher than "end".
*/
for (; p < aligned_end; p += sizeof(size_t))
{
if (*(size_t *) p != 0)
return false;
}
/* Compare remaining bytes until the end */
while (p < end)
{
if (*p++ != 0)
return false;
}
return true;
}
#define NANOSEC_PER_SEC 1000000000
// Returns difference in nanoseconds
int64_t
get_clock_diff(struct timespec *t1, struct timespec *t2)
{
int64_t nanosec = (t1->tv_sec - t2->tv_sec) * NANOSEC_PER_SEC;
nanosec += (t1->tv_nsec - t2->tv_nsec);
return nanosec;
}
int main()
{
volatile bool result;
struct timespec start,end;
int64_t memcmp_time, func_time;
SockAddr zero_clientaddr;
SockAddr Fakeclientaddr;
memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
memset(&Fakeclientaddr, 0, sizeof(Fakeclientaddr));
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (int i = 0; i < LOOPS; i++)
{
result = (memcmp(&Fakeclientaddr, &zero_clientaddr, sizeof(Fakeclientaddr)) == 0);
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
memcmp_time = get_clock_diff(&end, &start);
printf("memcmp: done in %ld nanoseconds\n", memcmp_time);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (int i = 0; i < LOOPS; i++)
{
result = pg_memory_is_all_zeros(&Fakeclientaddr, sizeof(Fakeclientaddr));
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
func_time = get_clock_diff(&end, &start);
printf("pg_memory_is_all_zeros: done in %ld nanoseconds (%g times faster than memcmp)\n", func_time, (double) memcmp_time / func_time);
return 0;
}