summaryrefslogtreecommitdiffstats
path: root/testing/gtest/benchmark/BlackBox.h
blob: 568f3837352b2cf1fb9aa52eb1c20c1e840c7c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GTEST_BLACKBOX_H
#define GTEST_BLACKBOX_H

#include "mozilla/Attributes.h"
#if defined(_MSC_VER)
#  include <intrin.h>
#endif  // _MSC_VER

namespace mozilla {

#if defined(_MSC_VER)

char volatile* UseCharPointer(char volatile*);

MOZ_ALWAYS_INLINE_EVEN_DEBUG void* BlackBoxVoidPtr(void* aPtr) {
  aPtr = const_cast<char*>(UseCharPointer(reinterpret_cast<char*>(aPtr)));
  _ReadWriteBarrier();
  return aPtr;
}

#else

// See: https://youtu.be/nXaxk27zwlk?t=2441
MOZ_ALWAYS_INLINE_EVEN_DEBUG void* BlackBoxVoidPtr(void* aPtr) {
  // "g" is what we want here, but the comment in the Google
  // benchmark code suggests that GCC likes "i,r,m" better.
  // However, on Mozilla try server i,r,m breaks GCC but g
  // works in GCC, so using g for both clang and GCC.
  // godbolt.org indicates that g works already in GCC 4.9,
  // which is the oldest GCC we support at the time of this
  // code landing. godbolt.org suggests that this clearly
  // works is LLVM 5, but it's unclear if this inhibits
  // all relevant optimizations properly on earlier LLVM.
  asm volatile("" : "+g"(aPtr) : "g"(aPtr) : "memory");
  return aPtr;
}

#endif  // _MSC_VER

template <class T>
MOZ_ALWAYS_INLINE_EVEN_DEBUG T* BlackBox(T* aPtr) {
  return static_cast<T*>(BlackBoxVoidPtr(aPtr));
}

}  // namespace mozilla

#endif  // GTEST_BLACKBOX_H