19edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes/* 29edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 39edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * 49edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 59edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * you may not use this file except in compliance with the License. 69edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * You may obtain a copy of the License at 79edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * 89edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 99edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * 109edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * Unless required by applicable law or agreed to in writing, software 119edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 129edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * See the License for the specific language governing permissions and 149edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes * limitations under the License. 159edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes */ 169edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 17a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu#include <fenv.h> 189edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes#include <math.h> 199edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 20df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris#include <benchmark/Benchmark.h> 21df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris 22df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris#define AT_COMMON_VALS \ 23df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris Arg(1234.0)->Arg(nan(""))->Arg(HUGE_VAL)->Arg(0.0) 24df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris 259edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes// Avoid optimization. 26055a59c3ed3ecd8f3cac4aa5496f3d21ab56a131Dan Albertvolatile double d; 27055a59c3ed3ecd8f3cac4aa5496f3d21ab56a131Dan Albertvolatile double v; 289edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 29df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_sqrt); 30df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_sqrt::Run(int iters) { 319edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 329edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 339edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 349edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 2.0; 359edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 369edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += sqrt(v); 379edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 389edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 399edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 409edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 419edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 42df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_log10); 43df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_log10::Run(int iters) { 449edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 459edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 469edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 479edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 1234.0; 489edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 499edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += log10(v); 509edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 519edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 529edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 539edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 549edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 55df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_logb); 56df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_logb::Run(int iters) { 579edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 589edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 599edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 609edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 1234.0; 619edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 629edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += logb(v); 639edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 649edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 659edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 669edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 6702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 68df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_WITH_ARG(BM_math_isinf, double)->AT_COMMON_VALS; 69df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_isinf::Run(int iters, double value) { 7002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 7102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 7202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 73df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris v = value; 7402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 7502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += (isinf)(v); 7602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 7702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 7802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 7902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 8002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 81df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_sin_fast); 82df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_sin_fast::Run(int iters) { 83a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StartBenchmarkTiming(); 8402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 85a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d = 1.0; 86a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu for (int i = 0; i < iters; ++i) { 87a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d += sin(d); 88a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu } 89a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu 90a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StopBenchmarkTiming(); 91a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu} 9202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 93df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_sin_feupdateenv); 94df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_sin_feupdateenv::Run(int iters) { 95a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StartBenchmarkTiming(); 9602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 97a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d = 1.0; 98a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu for (int i = 0; i < iters; ++i) { 99a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu fenv_t __libc_save_rm; 100a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu feholdexcept(&__libc_save_rm); 101a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu fesetround(FE_TONEAREST); 102a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d += sin(d); 103a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu feupdateenv(&__libc_save_rm); 104a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu } 10502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 106a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StopBenchmarkTiming(); 107a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu} 108a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu 109df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_NO_ARG(BM_math_sin_fesetenv); 110df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_sin_fesetenv::Run(int iters) { 111a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StartBenchmarkTiming(); 112a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu 113a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d = 1.0; 114a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu for (int i = 0; i < iters; ++i) { 115a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu fenv_t __libc_save_rm; 116a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu feholdexcept(&__libc_save_rm); 117a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu fesetround(FE_TONEAREST); 118a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu d += sin(d); 119a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu fesetenv(&__libc_save_rm); 120a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu } 121a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu 122a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu StopBenchmarkTiming(); 123a147a1da5c268e9d556c207be0d3da0a519b2d54Serban Constantinescu} 12402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 125df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher FerrisBENCHMARK_WITH_ARG(BM_math_fpclassify, double)->AT_COMMON_VALS; 126df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferrisvoid BM_math_fpclassify::Run(int iters, double value) { 12702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 12802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 12902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 130df4942c04a63ae6e4f5c78ece9f696d6b8b74d32Christopher Ferris v = value; 13102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 13202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += fpclassify(v); 13302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 13402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 13502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 13602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 137