| 1050 | } |
| 1051 | |
| 1052 | Status InferenceContext::Add(DimensionHandle first, DimensionOrConstant second, |
| 1053 | DimensionHandle* out) { |
| 1054 | const int64 first_value = Value(first); |
| 1055 | const int64 second_value = Value(second); |
| 1056 | // Special cases. |
| 1057 | if (first_value == 0) { |
| 1058 | *out = MakeDim(second); |
| 1059 | } else if (second_value == 0) { |
| 1060 | *out = first; |
| 1061 | } else if (first_value == kUnknownDim || second_value == kUnknownDim) { |
| 1062 | *out = UnknownDim(); |
| 1063 | } else { |
| 1064 | // Invariant: Both values are known and positive. Still in run-time we can |
| 1065 | // get pair of values which cannot be store in output. Check below will |
| 1066 | // report error. We still need to avoid undefined behavior of signed |
| 1067 | // overflow and use unsigned addition. |
| 1068 | const int64 sum = static_cast<uint64>(first_value) + second_value; |
| 1069 | if (sum < 0) { |
| 1070 | return errors::InvalidArgument("Dimension size overflow from adding ", |
| 1071 | first_value, " and ", second_value); |
| 1072 | } |
| 1073 | *out = MakeDim(sum); |
| 1074 | } |
| 1075 | return Status::OK(); |
| 1076 | } |
| 1077 | |
| 1078 | Status InferenceContext::Subtract(DimensionHandle first, |
| 1079 | DimensionOrConstant second, |