[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / quic_data_writer.cc
blobd3a6699614437039540eb5e838eae492f4b98482
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_data_writer.h"
7 #include <stdint.h>
8 #include <algorithm>
9 #include <limits>
11 using base::StringPiece;
12 using std::numeric_limits;
14 namespace net {
16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer)
17 : buffer_(buffer), capacity_(size), length_(0) {
20 QuicDataWriter::~QuicDataWriter() {
23 char* QuicDataWriter::data() {
24 return buffer_;
27 bool QuicDataWriter::WriteUInt8(uint8 value) {
28 return WriteBytes(&value, sizeof(value));
31 bool QuicDataWriter::WriteUInt16(uint16 value) {
32 return WriteBytes(&value, sizeof(value));
35 bool QuicDataWriter::WriteUInt32(uint32 value) {
36 return WriteBytes(&value, sizeof(value));
39 bool QuicDataWriter::WriteUInt48(uint64 value) {
40 uint16 hi = static_cast<uint16>(value >> 32);
41 uint32 lo = static_cast<uint32>(value);
42 return WriteUInt32(lo) && WriteUInt16(hi);
45 bool QuicDataWriter::WriteUInt64(uint64 value) {
46 return WriteBytes(&value, sizeof(value));
49 bool QuicDataWriter::WriteUFloat16(uint64 value) {
50 uint16 result;
51 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) {
52 // Fast path: either the value is denormalized, or has exponent zero.
53 // Both cases are represented by the value itself.
54 result = static_cast<uint16>(value);
55 } else if (value >= kUFloat16MaxValue) {
56 // Value is out of range; clamp it to the maximum representable.
57 result = numeric_limits<uint16>::max();
58 } else {
59 // The highest bit is between position 13 and 42 (zero-based), which
60 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10,
61 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11
62 // and count the shifts.
63 uint16 exponent = 0;
64 for (uint16 offset = 16; offset > 0; offset /= 2) {
65 // Right-shift the value until the highest bit is in position 11.
66 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30),
67 // shift if the bit is at or above 11 + offset.
68 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) {
69 exponent += offset;
70 value >>= offset;
74 DCHECK_GE(exponent, 1);
75 DCHECK_LE(exponent, kUFloat16MaxExponent);
76 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits);
77 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits);
79 // Hidden bit (position 11) is set. We should remove it and increment the
80 // exponent. Equivalently, we just add it to the exponent.
81 // This hides the bit.
82 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits));
85 return WriteBytes(&result, sizeof(result));
88 bool QuicDataWriter::WriteStringPiece16(StringPiece val) {
89 if (val.size() > numeric_limits<uint16>::max()) {
90 return false;
92 if (!WriteUInt16(static_cast<uint16>(val.size()))) {
93 return false;
95 return WriteBytes(val.data(), val.size());
98 bool QuicDataWriter::WriteIOVector(const IOVector& data) {
99 char *dest = BeginWrite(data.TotalBufferSize());
100 if (!dest) {
101 return false;
103 for (size_t i = 0; i < data.Size(); ++i) {
104 WriteBytes(data.iovec()[i].iov_base, data.iovec()[i].iov_len);
107 return true;
110 char* QuicDataWriter::BeginWrite(size_t length) {
111 if (length_ > capacity_) {
112 return nullptr;
115 if (capacity_ - length_ < length) {
116 return nullptr;
119 #ifdef ARCH_CPU_64_BITS
120 DCHECK_LE(length, std::numeric_limits<uint32>::max());
121 #endif
123 return buffer_ + length_;
126 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) {
127 char* dest = BeginWrite(data_len);
128 if (!dest) {
129 return false;
132 memcpy(dest, data, data_len);
134 length_ += data_len;
135 return true;
138 bool QuicDataWriter::WriteRepeatedByte(uint8 byte, size_t count) {
139 char* dest = BeginWrite(count);
140 if (!dest) {
141 return false;
144 memset(dest, byte, count);
146 length_ += count;
147 return true;
150 void QuicDataWriter::WritePadding() {
151 DCHECK_LE(length_, capacity_);
152 if (length_ > capacity_) {
153 return;
155 memset(buffer_ + length_, 0x00, capacity_ - length_);
156 length_ = capacity_;
160 } // namespace net