MCPcopy Create free account
hub / github.com/documentdb/documentdb / AddInt64ToValue

Function AddInt64ToValue

pg_documentdb_core/src/io/pgbson_utils.c:817–863  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

815 * @overflowedFromInt64: Set if overflow from Int64 occurs, unset otherwise.
816 */
817static void
818AddInt64ToValue(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/*

Callers 3

AddNumberToBsonValueFunction · 0.85
AddInt32ToValueFunction · 0.85

Calls 4

AddDoubleToValueFunction · 0.85
AddDecimal128NumbersFunction · 0.85
BsonValueAsInt64Function · 0.85

Tested by

no test coverage detected