Coverage Report

Created: 2026-09-03 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/runtime/standard/arithmetic_functions.cc
Line
Count
Source
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "runtime/standard/arithmetic_functions.h"
16
17
#include <cstdint>
18
#include <limits>
19
20
#include "absl/status/status.h"
21
#include "absl/strings/string_view.h"
22
#include "base/builtins.h"
23
#include "base/function_adapter.h"
24
#include "common/value.h"
25
#include "internal/overflow.h"
26
#include "internal/status_macros.h"
27
#include "runtime/function_registry.h"
28
#include "runtime/runtime_options.h"
29
30
namespace cel {
31
namespace {
32
33
// Template functions providing arithmetic operations
34
template <class Type>
35
Value Add(Type v0, Type v1);
36
37
template <>
38
23.7k
Value Add<int64_t>(int64_t v0, int64_t v1) {
39
23.7k
  auto sum = cel::internal::CheckedAdd(v0, v1);
40
23.7k
  if (!sum.ok()) {
41
9.14k
    return ErrorValue(sum.status());
42
9.14k
  }
43
14.5k
  return IntValue(*sum);
44
23.7k
}
45
46
template <>
47
4.73k
Value Add<uint64_t>(uint64_t v0, uint64_t v1) {
48
4.73k
  auto sum = cel::internal::CheckedAdd(v0, v1);
49
4.73k
  if (!sum.ok()) {
50
3.67k
    return ErrorValue(sum.status());
51
3.67k
  }
52
1.06k
  return UintValue(*sum);
53
4.73k
}
54
55
template <>
56
670
Value Add<double>(double v0, double v1) {
57
670
  return DoubleValue(v0 + v1);
58
670
}
59
60
template <class Type>
61
Value Sub(Type v0, Type v1);
62
63
template <>
64
220k
Value Sub<int64_t>(int64_t v0, int64_t v1) {
65
220k
  auto diff = cel::internal::CheckedSub(v0, v1);
66
220k
  if (!diff.ok()) {
67
333
    return ErrorValue(diff.status());
68
333
  }
69
220k
  return IntValue(*diff);
70
220k
}
71
72
template <>
73
6.89k
Value Sub<uint64_t>(uint64_t v0, uint64_t v1) {
74
6.89k
  auto diff = cel::internal::CheckedSub(v0, v1);
75
6.89k
  if (!diff.ok()) {
76
4.61k
    return ErrorValue(diff.status());
77
4.61k
  }
78
2.27k
  return UintValue(*diff);
79
6.89k
}
80
81
template <>
82
22.0k
Value Sub<double>(double v0, double v1) {
83
22.0k
  return DoubleValue(v0 - v1);
84
22.0k
}
85
86
template <class Type>
87
Value Mul(Type v0, Type v1);
88
89
template <>
90
31.8k
Value Mul<int64_t>(int64_t v0, int64_t v1) {
91
31.8k
  auto prod = cel::internal::CheckedMul(v0, v1);
92
31.8k
  if (!prod.ok()) {
93
19.5k
    return ErrorValue(prod.status());
94
19.5k
  }
95
12.2k
  return IntValue(*prod);
96
31.8k
}
97
98
template <>
99
675
Value Mul<uint64_t>(uint64_t v0, uint64_t v1) {
100
675
  auto prod = cel::internal::CheckedMul(v0, v1);
101
675
  if (!prod.ok()) {
102
227
    return ErrorValue(prod.status());
103
227
  }
104
448
  return UintValue(*prod);
105
675
}
106
107
template <>
108
355
Value Mul<double>(double v0, double v1) {
109
355
  return DoubleValue(v0 * v1);
110
355
}
111
112
template <class Type>
113
Value Div(Type v0, Type v1);
114
115
// Division operations for integer types should check for
116
// division by 0
117
template <>
118
205k
Value Div<int64_t>(int64_t v0, int64_t v1) {
119
205k
  auto quot = cel::internal::CheckedDiv(v0, v1);
120
205k
  if (!quot.ok()) {
121
19.7k
    return ErrorValue(quot.status());
122
19.7k
  }
123
185k
  return IntValue(*quot);
124
205k
}
125
126
// Division operations for integer types should check for
127
// division by 0
128
template <>
129
122k
Value Div<uint64_t>(uint64_t v0, uint64_t v1) {
130
122k
  auto quot = cel::internal::CheckedDiv(v0, v1);
131
122k
  if (!quot.ok()) {
132
36.3k
    return ErrorValue(quot.status());
133
36.3k
  }
134
86.3k
  return UintValue(*quot);
135
122k
}
136
137
template <>
138
73.5k
Value Div<double>(double v0, double v1) {
139
73.5k
  static_assert(std::numeric_limits<double>::is_iec559,
140
73.5k
                "Division by zero for doubles must be supported");
141
142
  // For double, division will result in +/- inf
143
73.5k
  return DoubleValue(v0 / v1);
144
73.5k
}
145
146
// Modulo operation
147
template <class Type>
148
Value Modulo(Type v0, Type v1);
149
150
// Modulo operations for integer types should check for
151
// division by 0
152
template <>
153
141k
Value Modulo<int64_t>(int64_t v0, int64_t v1) {
154
141k
  auto mod = cel::internal::CheckedMod(v0, v1);
155
141k
  if (!mod.ok()) {
156
1.89k
    return ErrorValue(mod.status());
157
1.89k
  }
158
139k
  return IntValue(*mod);
159
141k
}
160
161
template <>
162
9.89k
Value Modulo<uint64_t>(uint64_t v0, uint64_t v1) {
163
9.89k
  auto mod = cel::internal::CheckedMod(v0, v1);
164
9.89k
  if (!mod.ok()) {
165
9.46k
    return ErrorValue(mod.status());
166
9.46k
  }
167
432
  return UintValue(*mod);
168
9.89k
}
169
170
// Helper method
171
// Registers all arithmetic functions for template parameter type.
172
template <class Type>
173
67.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
67.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
67.6k
  CEL_RETURN_IF_ERROR(registry.Register(
176
67.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
67.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
67.6k
  CEL_RETURN_IF_ERROR(registry.Register(
180
67.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
67.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
67.6k
  CEL_RETURN_IF_ERROR(registry.Register(
184
67.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
67.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
67.6k
  return registry.Register(
188
67.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
67.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
67.6k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<long>(cel::FunctionRegistry&)
Line
Count
Source
173
22.5k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
22.5k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
176
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
22.5k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
180
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
22.5k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
184
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
22.5k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
22.5k
  return registry.Register(
188
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
22.5k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
22.5k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<unsigned long>(cel::FunctionRegistry&)
Line
Count
Source
173
22.5k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
22.5k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
176
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
22.5k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
180
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
22.5k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
184
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
22.5k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
22.5k
  return registry.Register(
188
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
22.5k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
22.5k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<double>(cel::FunctionRegistry&)
Line
Count
Source
173
22.5k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
22.5k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
176
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
22.5k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
180
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
22.5k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
184
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
22.5k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
22.5k
  return registry.Register(
188
22.5k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
22.5k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
22.5k
}
191
192
}  // namespace
193
194
absl::Status RegisterArithmeticFunctions(FunctionRegistry& registry,
195
22.5k
                                         const RuntimeOptions& options) {
196
22.5k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<int64_t>(registry));
197
22.5k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<uint64_t>(registry));
198
22.5k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<double>(registry));
199
200
  // Modulo
201
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
202
22.5k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::CreateDescriptor(
203
22.5k
          cel::builtin::kModulo, false),
204
22.5k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::WrapFunction(
205
22.5k
          &Modulo<int64_t>)));
206
207
22.5k
  CEL_RETURN_IF_ERROR(registry.Register(
208
22.5k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::CreateDescriptor(
209
22.5k
          cel::builtin::kModulo, false),
210
22.5k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::WrapFunction(
211
22.5k
          &Modulo<uint64_t>)));
212
213
  // Negation group
214
22.5k
  CEL_RETURN_IF_ERROR(
215
22.5k
      registry.Register(UnaryFunctionAdapter<Value, int64_t>::CreateDescriptor(
216
22.5k
                            cel::builtin::kNeg, false),
217
22.5k
                        UnaryFunctionAdapter<Value, int64_t>::WrapFunction(
218
22.5k
                            [](int64_t value) -> Value {
219
22.5k
                              auto inv = cel::internal::CheckedNegation(value);
220
22.5k
                              if (!inv.ok()) {
221
22.5k
                                return ErrorValue(inv.status());
222
22.5k
                              }
223
22.5k
                              return IntValue(*inv);
224
22.5k
                            })));
225
226
22.5k
  return registry.Register(
227
22.5k
      UnaryFunctionAdapter<double, double>::CreateDescriptor(cel::builtin::kNeg,
228
22.5k
                                                             false),
229
22.5k
      UnaryFunctionAdapter<double, double>::WrapFunction(
230
22.5k
          [](double value) -> double { return -value; }));
231
22.5k
}
232
233
}  // namespace cel