8 #ifndef V8_BASE_SAFE_CONVERSIONS_IMPL_H_ 9 #define V8_BASE_SAFE_CONVERSIONS_IMPL_H_ 13 #include "src/base/logging.h" 14 #include "src/base/macros.h" 23 template <
typename NumericType>
25 static const int value = std::numeric_limits<NumericType>::is_iec559
26 ? std::numeric_limits<NumericType>::max_exponent
27 : (
sizeof(NumericType) * 8 + 1 -
28 std::numeric_limits<NumericType>::is_signed);
31 enum IntegerRepresentation {
32 INTEGER_REPRESENTATION_UNSIGNED,
33 INTEGER_REPRESENTATION_SIGNED
41 enum NumericRangeRepresentation {
42 NUMERIC_RANGE_NOT_CONTAINED,
43 NUMERIC_RANGE_CONTAINED
52 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed
53 ? INTEGER_REPRESENTATION_SIGNED
54 : INTEGER_REPRESENTATION_UNSIGNED,
55 IntegerRepresentation SrcSign =
56 std::numeric_limits<Src>::is_signed
57 ? INTEGER_REPRESENTATION_SIGNED
58 : INTEGER_REPRESENTATION_UNSIGNED >
63 template <
typename Dst,
typename Src, IntegerRepresentation Sign>
65 static const NumericRangeRepresentation value =
67 ? NUMERIC_RANGE_CONTAINED
68 : NUMERIC_RANGE_NOT_CONTAINED;
73 template <
typename Dst,
typename Src>
76 INTEGER_REPRESENTATION_SIGNED,
77 INTEGER_REPRESENTATION_UNSIGNED> {
78 static const NumericRangeRepresentation value =
80 ? NUMERIC_RANGE_CONTAINED
81 : NUMERIC_RANGE_NOT_CONTAINED;
85 template <
typename Dst,
typename Src>
88 INTEGER_REPRESENTATION_UNSIGNED,
89 INTEGER_REPRESENTATION_SIGNED> {
90 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED;
93 enum RangeConstraint {
95 RANGE_UNDERFLOW = 0x1,
97 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW
101 inline RangeConstraint GetRangeConstraint(
int integer_range_constraint) {
102 DCHECK(integer_range_constraint >= RANGE_VALID &&
103 integer_range_constraint <= RANGE_INVALID);
104 return static_cast<RangeConstraint
>(integer_range_constraint);
110 inline RangeConstraint GetRangeConstraint(
bool is_in_upper_bound,
111 bool is_in_lower_bound) {
112 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) |
113 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW));
119 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed
120 ? INTEGER_REPRESENTATION_SIGNED
121 : INTEGER_REPRESENTATION_UNSIGNED,
122 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed
123 ? INTEGER_REPRESENTATION_SIGNED
124 : INTEGER_REPRESENTATION_UNSIGNED,
125 NumericRangeRepresentation DstRange =
126 StaticDstRangeRelationToSrcRange<Dst, Src>::value >
134 template <
typename Dst,
136 IntegerRepresentation DstSign,
137 IntegerRepresentation SrcSign>
142 NUMERIC_RANGE_CONTAINED> {
143 static RangeConstraint Check(Src value) {
return RANGE_VALID; }
148 template <
typename Dst,
typename Src>
151 INTEGER_REPRESENTATION_SIGNED,
152 INTEGER_REPRESENTATION_SIGNED,
153 NUMERIC_RANGE_NOT_CONTAINED> {
154 static RangeConstraint Check(Src value) {
155 return std::numeric_limits<Dst>::is_iec559
156 ? GetRangeConstraint(value <= std::numeric_limits<Dst>::max(),
157 value >= -std::numeric_limits<Dst>::max())
158 : GetRangeConstraint(value <= std::numeric_limits<Dst>::max(),
159 value >= std::numeric_limits<Dst>::min());
164 template <
typename Dst,
typename Src>
167 INTEGER_REPRESENTATION_UNSIGNED,
168 INTEGER_REPRESENTATION_UNSIGNED,
169 NUMERIC_RANGE_NOT_CONTAINED> {
170 static RangeConstraint Check(Src value) {
171 return GetRangeConstraint(value <= std::numeric_limits<Dst>::max(),
true);
176 template <
typename Dst,
typename Src>
179 INTEGER_REPRESENTATION_SIGNED,
180 INTEGER_REPRESENTATION_UNSIGNED,
181 NUMERIC_RANGE_NOT_CONTAINED> {
182 static RangeConstraint Check(Src value) {
183 return sizeof(Dst) >
sizeof(Src)
185 : GetRangeConstraint(
186 value <= static_cast<Src>(std::numeric_limits<Dst>::max()),
193 template <
typename Dst,
typename Src>
196 INTEGER_REPRESENTATION_UNSIGNED,
197 INTEGER_REPRESENTATION_SIGNED,
198 NUMERIC_RANGE_NOT_CONTAINED> {
199 static RangeConstraint Check(Src value) {
201 ? GetRangeConstraint(
true, value >= static_cast<Src>(0))
202 : GetRangeConstraint(
203 value <= static_cast<Src>(std::numeric_limits<Dst>::max()),
204 value >= static_cast<Src>(0));
208 template <
typename Dst,
typename Src>
209 inline RangeConstraint DstRangeRelationToSrcRange(Src value) {
211 STATIC_ASSERT(std::numeric_limits<Src>::is_specialized);
212 STATIC_ASSERT(std::numeric_limits<Dst>::is_specialized);
220 #endif // V8_BASE_SAFE_CONVERSIONS_IMPL_H_