math_benchmark.cpp revision 9edb3e004b487e08cbbb54f2af18b15241550513
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);
63