math_benchmark.cpp revision 02c78a386739a8a2b3007efeb00a9ca04132100a
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 179edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes#include "benchmark.h" 189edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 199edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes#include <math.h> 209edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 219edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes// Avoid optimization. 229edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughesdouble d; 239edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughesdouble v; 249edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 259edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughesstatic void BM_math_sqrt(int iters) { 269edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 279edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 289edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 299edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 2.0; 309edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 319edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += sqrt(v); 329edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 339edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 349edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 359edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 369edb3e004b487e08cbbb54f2af18b15241550513Elliott HughesBENCHMARK(BM_math_sqrt); 379edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 389edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughesstatic void BM_math_log10(int iters) { 399edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 409edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 419edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 429edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 1234.0; 439edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 449edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += log10(v); 459edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 469edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 479edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 489edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 499edb3e004b487e08cbbb54f2af18b15241550513Elliott HughesBENCHMARK(BM_math_log10); 509edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 519edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughesstatic void BM_math_logb(int iters) { 529edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StartBenchmarkTiming(); 539edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 549edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d = 0.0; 559edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes v = 1234.0; 569edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes for (int i = 0; i < iters; ++i) { 579edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes d += logb(v); 589edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes } 599edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes 609edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes StopBenchmarkTiming(); 619edb3e004b487e08cbbb54f2af18b15241550513Elliott Hughes} 629edb3e004b487e08cbbb54f2af18b15241550513Elliott HughesBENCHMARK(BM_math_logb); 6302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 6402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_isinf_NORMAL(int iters) { 6502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 6602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 6702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 6802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = 1234.0; // FP_NORMAL 6902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 7002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += (isinf)(v); 7102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 7202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 7302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 7402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 7502c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_isinf_NORMAL); 7602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 7702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_isinf_NAN(int iters) { 7802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 7902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 8002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 8102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = nan(""); // FP_NAN 8202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 8302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += (isinf)(v); 8402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 8502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 8602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 8702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 8802c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_isinf_NAN); 8902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 9002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_isinf_INFINITE(int iters) { 9102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 9202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 9302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 9402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = HUGE_VAL; // FP_INFINITE 9502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 9602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += (isinf)(v); 9702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 9802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 9902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 10002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 10102c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_isinf_INFINITE); 10202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 10302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_isinf_ZERO(int iters) { 10402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 10502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 10602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 10702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = 0.0; // FP_ZERO 10802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 10902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += (isinf)(v); 11002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 11102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 11202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 11302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 11402c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_isinf_ZERO); 11502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 11602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 11702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 11802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 11902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 12002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 12102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_fpclassify_NORMAL(int iters) { 12202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 12302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 12402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 12502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = 1234.0; // FP_NORMAL 12602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 12702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += fpclassify(v); 12802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 12902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 13002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 13102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 13202c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_fpclassify_NORMAL); 13302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 13402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_fpclassify_NAN(int iters) { 13502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 13602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 13702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 13802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = nan(""); // FP_NAN 13902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 14002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += fpclassify(v); 14102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 14202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 14302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 14402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 14502c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_fpclassify_NAN); 14602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 14702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_fpclassify_INFINITE(int iters) { 14802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 14902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 15002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 15102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = HUGE_VAL; // FP_INFINITE 15202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 15302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += fpclassify(v); 15402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 15502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 15602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 15702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 15802c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_fpclassify_INFINITE); 15902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 16002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughesstatic void BM_math_fpclassify_ZERO(int iters) { 16102c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StartBenchmarkTiming(); 16202c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 16302c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d = 0.0; 16402c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes v = 0.0; // FP_ZERO 16502c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes for (int i = 0; i < iters; ++i) { 16602c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes d += fpclassify(v); 16702c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes } 16802c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes 16902c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes StopBenchmarkTiming(); 17002c78a386739a8a2b3007efeb00a9ca04132100aElliott Hughes} 17102c78a386739a8a2b3007efeb00a9ca04132100aElliott HughesBENCHMARK(BM_math_fpclassify_ZERO); 172