* Adds an Int64 to the current value. If the current value is an double * applies the sum as an double. If the value overflows, applies * the sum as a double * @overflowedFromInt64: Set if overflow from Int64 occurs, unset otherwise. */
| 815 | * @overflowedFromInt64: Set if overflow from Int64 occurs, unset otherwise. |
| 816 | */ |
| 817 | static void |
| 818 | AddInt64ToValue(bson_value_t *current, int64_t value, bool *overflowedFromInt64) |
| 819 | { |
| 820 | if (current->value_type == BSON_TYPE_DOUBLE) |
| 821 | { |
| 822 | /* current is already double - just do double add. */ |
| 823 | AddDoubleToValue(current, (double) value); |
| 824 | |
| 825 | *overflowedFromInt64 = false; |
| 826 | return; |
| 827 | } |
| 828 | else if (current->value_type == BSON_TYPE_DECIMAL128) |
| 829 | { |
| 830 | bson_value_t valueToAdd; |
| 831 | valueToAdd.value.v_int64 = value; |
| 832 | valueToAdd.value_type = BSON_TYPE_INT64; |
| 833 | |
| 834 | valueToAdd.value.v_decimal128 = GetBsonValueAsDecimal128Quantized(&valueToAdd); |
| 835 | valueToAdd.value_type = BSON_TYPE_DECIMAL128; |
| 836 | AddDecimal128Numbers(current, &valueToAdd, current); |
| 837 | |
| 838 | *overflowedFromInt64 = false; |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | /* current is int64 or int32. */ |
| 843 | int64_t currentSum = BsonValueAsInt64(current); |
| 844 | |
| 845 | /* check for overflow: */ |
| 846 | /* if current + value > INT64_MAX -> current > INT64_MAX - value */ |
| 847 | /* if current + (-value) < INT64_MIN -> current < INT64_MIN - (-value) */ |
| 848 | if ((value > 0 && currentSum > INT64_MAX - value) || |
| 849 | (value < 0 && currentSum < INT64_MIN - value)) |
| 850 | { |
| 851 | *overflowedFromInt64 = true; |
| 852 | |
| 853 | /* coerce to double. */ |
| 854 | AddDoubleToValue(current, (double) value); |
| 855 | } |
| 856 | else |
| 857 | { |
| 858 | *overflowedFromInt64 = false; |
| 859 | |
| 860 | current->value.v_int64 = currentSum + value; |
| 861 | current->value_type = BSON_TYPE_INT64; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | |
| 866 | /* |
no test coverage detected