| 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 <stdint.h> |
| 8 |
| 7 #include <algorithm> | 9 #include <algorithm> |
| 8 #include <limits> | 10 #include <limits> |
| 9 | 11 |
| 10 using base::StringPiece; | 12 using base::StringPiece; |
| 11 using std::numeric_limits; | 13 using std::numeric_limits; |
| 12 | 14 |
| 13 namespace net { | 15 namespace net { |
| 14 | 16 |
| 15 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) | 17 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) |
| 16 : buffer_(buffer), capacity_(size), length_(0) { | 18 : buffer_(buffer), capacity_(size), length_(0) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 40 uint32 lo = static_cast<uint32>(value); | 42 uint32 lo = static_cast<uint32>(value); |
| 41 return WriteUInt32(lo) && WriteUInt16(hi); | 43 return WriteUInt32(lo) && WriteUInt16(hi); |
| 42 } | 44 } |
| 43 | 45 |
| 44 bool QuicDataWriter::WriteUInt64(uint64 value) { | 46 bool QuicDataWriter::WriteUInt64(uint64 value) { |
| 45 return WriteBytes(&value, sizeof(value)); | 47 return WriteBytes(&value, sizeof(value)); |
| 46 } | 48 } |
| 47 | 49 |
| 48 bool QuicDataWriter::WriteUFloat16(uint64 value) { | 50 bool QuicDataWriter::WriteUFloat16(uint64 value) { |
| 49 uint16 result; | 51 uint16 result; |
| 50 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 52 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
| 51 // Fast path: either the value is denormalized, or has exponent zero. | 53 // Fast path: either the value is denormalized, or has exponent zero. |
| 52 // Both cases are represented by the value itself. | 54 // Both cases are represented by the value itself. |
| 53 result = static_cast<uint16>(value); | 55 result = static_cast<uint16>(value); |
| 54 } else if (value >= kUFloat16MaxValue) { | 56 } else if (value >= kUFloat16MaxValue) { |
| 55 // Value is out of range; clamp it to the maximum representable. | 57 // Value is out of range; clamp it to the maximum representable. |
| 56 result = numeric_limits<uint16>::max(); | 58 result = numeric_limits<uint16>::max(); |
| 57 } else { | 59 } else { |
| 58 // The highest bit is between position 13 and 42 (zero-based), which | 60 // The highest bit is between position 13 and 42 (zero-based), which |
| 59 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | 61 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, |
| 60 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | 62 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 |
| 61 // and count the shifts. | 63 // and count the shifts. |
| 62 uint16 exponent = 0; | 64 uint16 exponent = 0; |
| 63 for (uint16 offset = 16; offset > 0; offset /= 2) { | 65 for (uint16 offset = 16; offset > 0; offset /= 2) { |
| 64 // Right-shift the value until the highest bit is in position 11. | 66 // Right-shift the value until the highest bit is in position 11. |
| 65 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | 67 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), |
| 66 // shift if the bit is at or above 11 + offset. | 68 // shift if the bit is at or above 11 + offset. |
| 67 if (value >= (GG_UINT64_C(1) << (kUFloat16MantissaBits + offset))) { | 69 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) { |
| 68 exponent += offset; | 70 exponent += offset; |
| 69 value >>= offset; | 71 value >>= offset; |
| 70 } | 72 } |
| 71 } | 73 } |
| 72 | 74 |
| 73 DCHECK_GE(exponent, 1); | 75 DCHECK_GE(exponent, 1); |
| 74 DCHECK_LE(exponent, kUFloat16MaxExponent); | 76 DCHECK_LE(exponent, kUFloat16MaxExponent); |
| 75 DCHECK_GE(value, GG_UINT64_C(1) << kUFloat16MantissaBits); | 77 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits); |
| 76 DCHECK_LT(value, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits); | 78 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits); |
| 77 | 79 |
| 78 // Hidden bit (position 11) is set. We should remove it and increment the | 80 // Hidden bit (position 11) is set. We should remove it and increment the |
| 79 // exponent. Equivalently, we just add it to the exponent. | 81 // exponent. Equivalently, we just add it to the exponent. |
| 80 // This hides the bit. | 82 // This hides the bit. |
| 81 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); | 83 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); |
| 82 } | 84 } |
| 83 | 85 |
| 84 return WriteBytes(&result, sizeof(result)); | 86 return WriteBytes(&result, sizeof(result)); |
| 85 } | 87 } |
| 86 | 88 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 DCHECK_LE(length_, capacity_); | 152 DCHECK_LE(length_, capacity_); |
| 151 if (length_ > capacity_) { | 153 if (length_ > capacity_) { |
| 152 return; | 154 return; |
| 153 } | 155 } |
| 154 memset(buffer_ + length_, 0x00, capacity_ - length_); | 156 memset(buffer_ + length_, 0x00, capacity_ - length_); |
| 155 length_ = capacity_; | 157 length_ = capacity_; |
| 156 } | 158 } |
| 157 | 159 |
| 158 | 160 |
| 159 } // namespace net | 161 } // namespace net |
| OLD | NEW |