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