| 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/core/quic_data_writer.h" | 5 #include "net/quic/core/quic_data_writer.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool QuicDataWriter::WriteUFloat16(uint64_t value) { | 47 bool QuicDataWriter::WriteUFloat16(uint64_t value) { |
| 48 uint16_t result; | 48 uint16_t result; |
| 49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
| 50 // Fast path: either the value is denormalized, or has exponent zero. | 50 // Fast path: either the value is denormalized, or has exponent zero. |
| 51 // Both cases are represented by the value itself. | 51 // Both cases are represented by the value itself. |
| 52 result = static_cast<uint16_t>(value); | 52 result = static_cast<uint16_t>(value); |
| 53 } else if (value >= kUFloat16MaxValue) { | 53 } else if (value >= kUFloat16MaxValue) { |
| 54 // Value is out of range; clamp it to the maximum representable. | 54 // Value is out of range; clamp it to the maximum representable. |
| 55 result = numeric_limits<uint16_t>::max(); | 55 result = std::numeric_limits<uint16_t>::max(); |
| 56 } else { | 56 } else { |
| 57 // The highest bit is between position 13 and 42 (zero-based), which | 57 // The highest bit is between position 13 and 42 (zero-based), which |
| 58 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | 58 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, |
| 59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | 59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 |
| 60 // and count the shifts. | 60 // and count the shifts. |
| 61 uint16_t exponent = 0; | 61 uint16_t exponent = 0; |
| 62 for (uint16_t offset = 16; offset > 0; offset /= 2) { | 62 for (uint16_t offset = 16; offset > 0; offset /= 2) { |
| 63 // Right-shift the value until the highest bit is in position 11. | 63 // Right-shift the value until the highest bit is in position 11. |
| 64 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | 64 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), |
| 65 // shift if the bit is at or above 11 + offset. | 65 // shift if the bit is at or above 11 + offset. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 // Hidden bit (position 11) is set. We should remove it and increment the | 77 // Hidden bit (position 11) is set. We should remove it and increment the |
| 78 // exponent. Equivalently, we just add it to the exponent. | 78 // exponent. Equivalently, we just add it to the exponent. |
| 79 // This hides the bit. | 79 // This hides the bit. |
| 80 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); | 80 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 return WriteBytes(&result, sizeof(result)); | 83 return WriteBytes(&result, sizeof(result)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { | 86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { |
| 87 if (val.size() > numeric_limits<uint16_t>::max()) { | 87 if (val.size() > std::numeric_limits<uint16_t>::max()) { |
| 88 return false; | 88 return false; |
| 89 } | 89 } |
| 90 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { | 90 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) { |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 return WriteBytes(val.data(), val.size()); | 93 return WriteBytes(val.data(), val.size()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 char* QuicDataWriter::BeginWrite(size_t length) { | 96 char* QuicDataWriter::BeginWrite(size_t length) { |
| 97 if (length_ > capacity_) { | 97 if (length_ > capacity_) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 void QuicDataWriter::WritePadding() { | 136 void QuicDataWriter::WritePadding() { |
| 137 DCHECK_LE(length_, capacity_); | 137 DCHECK_LE(length_, capacity_); |
| 138 if (length_ > capacity_) { | 138 if (length_ > capacity_) { |
| 139 return; | 139 return; |
| 140 } | 140 } |
| 141 memset(buffer_ + length_, 0x00, capacity_ - length_); | 141 memset(buffer_ + length_, 0x00, capacity_ - length_); |
| 142 length_ = capacity_; | 142 length_ = capacity_; |
| 143 } | 143 } |
| 144 | 144 |
| 145 } // namespace net | 145 } // namespace net |
| OLD | NEW |