Skip to content

Instantly share code, notes, and snippets.

@nicky-zs
Last active June 30, 2018 03:06
Show Gist options
  • Save nicky-zs/bd1d984629f4ca5f3c84e0f10b7975fe to your computer and use it in GitHub Desktop.
Save nicky-zs/bd1d984629f4ca5f3c84e0f10b7975fe to your computer and use it in GitHub Desktop.
benchmark of SIMD
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
typedef int v4si __attribute__ ((vector_size (16)));
// /8 /10 /12 /16
const v4si mask = {0x000000FF, 0x0000C0FF, 0x0000F0FF, 0x0000FFFF};
// 10.0.0.0 100.64.0.0 172.16.0.0 192.168.0.0
const v4si netw = {0x0000000A, 0x00004064, 0x000010AC, 0x0000A8C0};
#ifdef NO_SSE
__attribute__ ((__target__("no-sse")))
#endif
static inline int priv(int ip) {
v4si r = ((ip & mask) == netw);
return !!(r[0] | r[1] | r[2] | r[3]);
}
static const unsigned long N = 1000000;
static void usage(char *cmd) {
printf("usage: %s <ip> [loop_count=%lu]\n", cmd, N);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
usage(argv[0]);
return -1;
}
size_t n = argc > 2 ? atol(argv[2]) : N;
int ip = inet_addr(argv[1]);
clock_t start = clock();
for (unsigned long i = 0; i < n; ++i) {
priv(ip);
__asm__("");
}
clock_t end = clock();
unsigned long cost = end - start;
printf("inner ip: %u, cost %.5f seconds, %.5f nano seconds per each\n", priv(ip),
((double) cost) / CLOCKS_PER_SEC,
((double) cost) * 1000000000 / CLOCKS_PER_SEC / n);
return 0;
}
#!/bin/bash
ARCH=(
native
nocona
core2
nehalem
westmere
bonnell
silvermont
knl
sandybridge
ivybridge
haswell
broadwell
)
gcc -O0 -DNO_SSE inner_ip_benchmark.c -o inner_ip_benchmark
echo "./inner_ip_benchmark ${1:-0.0.0.0} ${2:-100000000}"
./inner_ip_benchmark ${1:-0.0.0.0} ${2:-100000000}
echo
for arch in ${ARCH[@]}; do
gcc -O3 -march=$arch inner_ip_benchmark.c -o inner_ip_benchmark.$arch
done
for arch in ${ARCH[@]}; do
echo "./inner_ip_benchmark.$arch ${1:-0.0.0.0} ${2:-100000000}":
./inner_ip_benchmark.$arch ${1:-0.0.0.0} ${2:-100000000}
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment