Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <windows.h>
- #include <xmmintrin.h>
- #define N 12345678
- double sec(){
- LARGE_INTEGER t, frequency;
- QueryPerformanceFrequency(&frequency);
- QueryPerformanceCounter(&t);
- return t.QuadPart / (double)frequency.QuadPart;
- }
- float slow_imprecise_sqrt(float number){
- long i;
- float x2, y;
- const float threehalfs = 1.5F;
- x2 = number * 0.5F;
- y = number;
- i = * ( long * ) &y; // evil floating point bit level hacking
- i = 0x5f3759df - ( i >> 1 ); // what the fuck?
- y = * ( float * ) &i;
- y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
- //y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
- return y;
- }
- // printed at the end so the compiler does not optimize the loop away
- float anti_optimizer = 0.0f;
- double naive_benchmark(float checksum){
- double t = sec();
- size_t i;
- for (i = 0; i < N; i++){
- checksum = 1.0f/sqrtf(checksum);
- }
- anti_optimizer += checksum;
- return sec() - t;
- }
- double sse_benchmark(float checksum){
- double t = sec();
- __m128 x = _mm_set1_ps(checksum);
- size_t i;
- for (i = 0; i < N; i++){
- x = _mm_rsqrt_ps(x);
- }
- float *f = (float*)&x;
- anti_optimizer += f[0] + f[1] + f[2] + f[3];
- return sec() - t;
- }
- double quake_benchmark(float checksum){
- double t = sec();
- size_t i;
- for (i = 0; i < N; i++){
- checksum = slow_imprecise_sqrt(checksum);
- }
- anti_optimizer += checksum;
- return sec() - t;
- }
- void run_benchmark(double (*benchmark)(float), const char *name){
- double fastest_benchmark = 1e20;
- // always 1 but the compiler does not know so it can't optimize
- float checksum = rand() >= 0;
- // run a few times and print the fastest time
- size_t i;
- for (i = 0; i < 10; i++){
- double dt = benchmark(checksum);
- if (dt < fastest_benchmark) fastest_benchmark = dt;
- }
- printf("%f milliseconds for %s benchmark\n", 1000.0*fastest_benchmark, name);
- }
- int main(){
- run_benchmark(naive_benchmark, "naive");
- run_benchmark(sse_benchmark, "sse");
- run_benchmark(quake_benchmark, "quake");
- printf("ignore: %f\n", anti_optimizer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement