OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_data_writer.h" | 5 #include "net/quic/quic_data_writer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 |
| 14 using base::StringPiece; |
| 15 using std::numeric_limits; |
| 16 |
14 namespace net { | 17 namespace net { |
15 | 18 |
16 QuicDataWriter::QuicDataWriter(size_t size) | 19 QuicDataWriter::QuicDataWriter(size_t size) |
17 : buffer_(new char[size]), | 20 : buffer_(new char[size]), |
18 capacity_(size), | 21 capacity_(size), |
19 length_(0) { | 22 length_(0) { |
20 } | 23 } |
21 | 24 |
22 QuicDataWriter::~QuicDataWriter() { | 25 QuicDataWriter::~QuicDataWriter() { |
23 delete[] buffer_; | 26 delete[] buffer_; |
(...skipping 30 matching lines...) Expand all Loading... |
54 } | 57 } |
55 | 58 |
56 bool QuicDataWriter::WriteUFloat16(uint64 value) { | 59 bool QuicDataWriter::WriteUFloat16(uint64 value) { |
57 uint16 result; | 60 uint16 result; |
58 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 61 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
59 // Fast path: either the value is denormalized, or has exponent zero. | 62 // Fast path: either the value is denormalized, or has exponent zero. |
60 // Both cases are represented by the value itself. | 63 // Both cases are represented by the value itself. |
61 result = static_cast<uint16>(value); | 64 result = static_cast<uint16>(value); |
62 } else if (value >= kUFloat16MaxValue) { | 65 } else if (value >= kUFloat16MaxValue) { |
63 // Value is out of range; clamp it to the maximum representable. | 66 // Value is out of range; clamp it to the maximum representable. |
64 result = std::numeric_limits<uint16>::max(); | 67 result = numeric_limits<uint16>::max(); |
65 } else { | 68 } else { |
66 // The highest bit is between position 13 and 42 (zero-based), which | 69 // The highest bit is between position 13 and 42 (zero-based), which |
67 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | 70 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, |
68 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | 71 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 |
69 // and count the shifts. | 72 // and count the shifts. |
70 uint16 exponent = 0; | 73 uint16 exponent = 0; |
71 for (uint16 offset = 16; offset > 0; offset /= 2) { | 74 for (uint16 offset = 16; offset > 0; offset /= 2) { |
72 // Right-shift the value until the highest bit is in position 11. | 75 // Right-shift the value until the highest bit is in position 11. |
73 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | 76 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), |
74 // shift if the bit is at or above 11 + offset. | 77 // shift if the bit is at or above 11 + offset. |
(...skipping 10 matching lines...) Expand all Loading... |
85 | 88 |
86 // Hidden bit (position 11) is set. We should remove it and increment the | 89 // Hidden bit (position 11) is set. We should remove it and increment the |
87 // exponent. Equivalently, we just add it to the exponent. | 90 // exponent. Equivalently, we just add it to the exponent. |
88 // This hides the bit. | 91 // This hides the bit. |
89 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); | 92 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); |
90 } | 93 } |
91 | 94 |
92 return WriteBytes(&result, sizeof(result)); | 95 return WriteBytes(&result, sizeof(result)); |
93 } | 96 } |
94 | 97 |
95 bool QuicDataWriter::WriteStringPiece16(base::StringPiece val) { | 98 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { |
96 if (val.size() > std::numeric_limits<uint16>::max()) { | 99 if (val.size() > numeric_limits<uint16>::max()) { |
97 return false; | 100 return false; |
98 } | 101 } |
99 if (!WriteUInt16(static_cast<uint16>(val.size()))) { | 102 if (!WriteUInt16(static_cast<uint16>(val.size()))) { |
100 return false; | 103 return false; |
101 } | 104 } |
102 return WriteBytes(val.data(), val.size()); | 105 return WriteBytes(val.data(), val.size()); |
103 } | 106 } |
104 | 107 |
105 bool QuicDataWriter::WriteIOVector(const IOVector& data) { | 108 bool QuicDataWriter::WriteIOVector(const IOVector& data) { |
106 char *dest = BeginWrite(data.TotalBufferSize()); | 109 char *dest = BeginWrite(data.TotalBufferSize()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 DCHECK_LT(offset, capacity_); | 193 DCHECK_LT(offset, capacity_); |
191 size_t latched_length = length_; | 194 size_t latched_length = length_; |
192 length_ = offset; | 195 length_ = offset; |
193 bool success = WriteUInt48(value); | 196 bool success = WriteUInt48(value); |
194 DCHECK_LE(length_, latched_length); | 197 DCHECK_LE(length_, latched_length); |
195 length_ = latched_length; | 198 length_ = latched_length; |
196 return success; | 199 return success; |
197 } | 200 } |
198 | 201 |
199 } // namespace net | 202 } // namespace net |
OLD | NEW |