summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/time_clamper.cc
blob: 9e120ff6cc4da5352fb2658dbf34888ee269ee85 (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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/time_clamper.h"

#include "base/bit_cast.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/cryptographically_random_number.h"

#include <cmath>

namespace blink {

TimeClamper::TimeClamper() {
  CryptographicallyRandomValues(&secret_, sizeof(secret_));
}

double TimeClamper::ClampTimeResolution(double time_seconds) const {
  DCHECK_GE(time_seconds, 0);
  double interval = floor(time_seconds / kResolutionSeconds);
  double clamped_time = interval * kResolutionSeconds;
  double tick_threshold = ThresholdFor(clamped_time);

  if (time_seconds >= tick_threshold)
    return (interval + 1) * kResolutionSeconds;
  return clamped_time;
}

inline double TimeClamper::ThresholdFor(double clamped_time) const {
  uint64_t time_hash = MurmurHash3(bit_cast<int64_t>(clamped_time) ^ secret_);
  return clamped_time + kResolutionSeconds * ToDouble(time_hash);
}

// static
inline double TimeClamper::ToDouble(uint64_t value) {
  // Exponent for double values for [1.0 .. 2.0]
  static const uint64_t kExponentBits = uint64_t{0x3FF0000000000000};
  static const uint64_t kMantissaMask = uint64_t{0x000FFFFFFFFFFFFF};
  uint64_t random = (value & kMantissaMask) | kExponentBits;
  return bit_cast<double>(random) - 1;
}

// static
inline uint64_t TimeClamper::MurmurHash3(uint64_t value) {
  value ^= value >> 33;
  value *= uint64_t{0xFF51AFD7ED558CCD};
  value ^= value >> 33;
  value *= uint64_t{0xC4CEB9FE1A85EC53};
  value ^= value >> 33;
  return value;
}

}  // namespace blink