File: time_clamper.cc

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (58 lines) | stat: -rw-r--r-- 1,820 bytes parent folder | download | duplicates (9)
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
// 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/core/timing/time_clamper.h"

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

#include <cmath>

namespace blink {

TimeClamper::TimeClamper() : secret_(base::RandUint64()) {}

double TimeClamper::ClampTimeResolution(double time_seconds) const {
  bool was_negative = false;
  if (time_seconds < 0) {
    was_negative = true;
    time_seconds = -time_seconds;
  }
  double interval = floor(time_seconds / kResolutionSeconds);
  double clamped_time = interval * kResolutionSeconds;
  double tick_threshold = ThresholdFor(clamped_time);

  if (time_seconds >= tick_threshold)
    clamped_time = (interval + 1) * kResolutionSeconds;
  if (was_negative)
    clamped_time = -clamped_time;
  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