1 // Copyright 2022 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "System/CPUID.hpp"
16 #include "System/Math.hpp"
17
18 #include "benchmark/benchmark.h"
19
20 #include <limits>
21
22 using namespace sw;
23
24 // Global variable the C++ compiler can't eliminate.
25 volatile float y;
26
Subnormals(benchmark::State & state,bool FTZ,bool DAZ)27 static void Subnormals(benchmark::State &state, bool FTZ, bool DAZ)
28 {
29 CPUID::setFlushToZero(FTZ);
30 CPUID::setDenormalsAreZero(DAZ);
31
32 for(auto _ : state)
33 {
34 // 2^-64 is a normalized value which when squared produces 2^-128, a denormalized value.
35 volatile float x = exp2(-64);
36
37 for(int i = 0; i < state.range(); i++)
38 {
39 y = x * x;
40 }
41 }
42
43 CPUID::setFlushToZero(false);
44 CPUID::setDenormalsAreZero(false);
45 }
46
47 BENCHMARK_CAPTURE(Subnormals, Default, false, false)->Arg(100000)->Unit(benchmark::kMillisecond);
48 BENCHMARK_CAPTURE(Subnormals, FlushToZero, true, false)->Arg(100000)->Unit(benchmark::kMillisecond);
49 BENCHMARK_CAPTURE(Subnormals, DenormalsAreZero, false, true)->Arg(100000)->Unit(benchmark::kMillisecond);
50 BENCHMARK_CAPTURE(Subnormals, FTZ_and_DAZ, true, true)->Arg(100000)->Unit(benchmark::kMillisecond);