Coverage Report

Created: 2026-07-16 06:29

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
48.7k
Value Add<int64_t>(int64_t v0, int64_t v1) {
39
48.7k
  auto sum = cel::internal::CheckedAdd(v0, v1);
40
48.7k
  if (!sum.ok()) {
41
18.8k
    return ErrorValue(sum.status());
42
18.8k
  }
43
29.9k
  return IntValue(*sum);
44
48.7k
}
45
46
template <>
47
3.78k
Value Add<uint64_t>(uint64_t v0, uint64_t v1) {
48
3.78k
  auto sum = cel::internal::CheckedAdd(v0, v1);
49
3.78k
  if (!sum.ok()) {
50
2.80k
    return ErrorValue(sum.status());
51
2.80k
  }
52
974
  return UintValue(*sum);
53
3.78k
}
54
55
template <>
56
7.53k
Value Add<double>(double v0, double v1) {
57
7.53k
  return DoubleValue(v0 + v1);
58
7.53k
}
59
60
template <class Type>
61
Value Sub(Type v0, Type v1);
62
63
template <>
64
183k
Value Sub<int64_t>(int64_t v0, int64_t v1) {
65
183k
  auto diff = cel::internal::CheckedSub(v0, v1);
66
183k
  if (!diff.ok()) {
67
202
    return ErrorValue(diff.status());
68
202
  }
69
183k
  return IntValue(*diff);
70
183k
}
71
72
template <>
73
3.57k
Value Sub<uint64_t>(uint64_t v0, uint64_t v1) {
74
3.57k
  auto diff = cel::internal::CheckedSub(v0, v1);
75
3.57k
  if (!diff.ok()) {
76
643
    return ErrorValue(diff.status());
77
643
  }
78
2.93k
  return UintValue(*diff);
79
3.57k
}
80
81
template <>
82
11.8k
Value Sub<double>(double v0, double v1) {
83
11.8k
  return DoubleValue(v0 - v1);
84
11.8k
}
85
86
template <class Type>
87
Value Mul(Type v0, Type v1);
88
89
template <>
90
10.2k
Value Mul<int64_t>(int64_t v0, int64_t v1) {
91
10.2k
  auto prod = cel::internal::CheckedMul(v0, v1);
92
10.2k
  if (!prod.ok()) {
93
4.14k
    return ErrorValue(prod.status());
94
4.14k
  }
95
6.11k
  return IntValue(*prod);
96
10.2k
}
97
98
template <>
99
1.03k
Value Mul<uint64_t>(uint64_t v0, uint64_t v1) {
100
1.03k
  auto prod = cel::internal::CheckedMul(v0, v1);
101
1.03k
  if (!prod.ok()) {
102
540
    return ErrorValue(prod.status());
103
540
  }
104
492
  return UintValue(*prod);
105
1.03k
}
106
107
template <>
108
413
Value Mul<double>(double v0, double v1) {
109
413
  return DoubleValue(v0 * v1);
110
413
}
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
357k
Value Div<int64_t>(int64_t v0, int64_t v1) {
119
357k
  auto quot = cel::internal::CheckedDiv(v0, v1);
120
357k
  if (!quot.ok()) {
121
16.5k
    return ErrorValue(quot.status());
122
16.5k
  }
123
340k
  return IntValue(*quot);
124
357k
}
125
126
// Division operations for integer types should check for
127
// division by 0
128
template <>
129
29.8k
Value Div<uint64_t>(uint64_t v0, uint64_t v1) {
130
29.8k
  auto quot = cel::internal::CheckedDiv(v0, v1);
131
29.8k
  if (!quot.ok()) {
132
16.3k
    return ErrorValue(quot.status());
133
16.3k
  }
134
13.4k
  return UintValue(*quot);
135
29.8k
}
136
137
template <>
138
146k
Value Div<double>(double v0, double v1) {
139
146k
  static_assert(std::numeric_limits<double>::is_iec559,
140
146k
                "Division by zero for doubles must be supported");
141
142
  // For double, division will result in +/- inf
143
146k
  return DoubleValue(v0 / v1);
144
146k
}
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
277k
Value Modulo<int64_t>(int64_t v0, int64_t v1) {
154
277k
  auto mod = cel::internal::CheckedMod(v0, v1);
155
277k
  if (!mod.ok()) {
156
206k
    return ErrorValue(mod.status());
157
206k
  }
158
71.6k
  return IntValue(*mod);
159
277k
}
160
161
template <>
162
941
Value Modulo<uint64_t>(uint64_t v0, uint64_t v1) {
163
941
  auto mod = cel::internal::CheckedMod(v0, v1);
164
941
  if (!mod.ok()) {
165
419
    return ErrorValue(mod.status());
166
419
  }
167
522
  return UintValue(*mod);
168
941
}
169
170
// Helper method
171
// Registers all arithmetic functions for template parameter type.
172
template <class Type>
173
65.0k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
65.0k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
65.0k
  CEL_RETURN_IF_ERROR(registry.Register(
176
65.0k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
65.0k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
65.0k
  CEL_RETURN_IF_ERROR(registry.Register(
180
65.0k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
65.0k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
65.0k
  CEL_RETURN_IF_ERROR(registry.Register(
184
65.0k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
65.0k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
65.0k
  return registry.Register(
188
65.0k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
65.0k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
65.0k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<long>(cel::FunctionRegistry&)
Line
Count
Source
173
21.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
21.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
176
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
21.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
180
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
21.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
184
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
21.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
21.6k
  return registry.Register(
188
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
21.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
21.6k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<unsigned long>(cel::FunctionRegistry&)
Line
Count
Source
173
21.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
21.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
176
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
21.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
180
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
21.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
184
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
21.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
21.6k
  return registry.Register(
188
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
21.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
21.6k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<double>(cel::FunctionRegistry&)
Line
Count
Source
173
21.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
174
21.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
175
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
176
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
177
21.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
178
179
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
180
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
181
21.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
182
183
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
184
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
185
21.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
186
187
21.6k
  return registry.Register(
188
21.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
189
21.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
190
21.6k
}
191
192
}  // namespace
193
194
absl::Status RegisterArithmeticFunctions(FunctionRegistry& registry,
195
21.6k
                                         const RuntimeOptions& options) {
196
21.6k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<int64_t>(registry));
197
21.6k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<uint64_t>(registry));
198
21.6k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<double>(registry));
199
200
  // Modulo
201
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
202
21.6k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::CreateDescriptor(
203
21.6k
          cel::builtin::kModulo, false),
204
21.6k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::WrapFunction(
205
21.6k
          &Modulo<int64_t>)));
206
207
21.6k
  CEL_RETURN_IF_ERROR(registry.Register(
208
21.6k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::CreateDescriptor(
209
21.6k
          cel::builtin::kModulo, false),
210
21.6k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::WrapFunction(
211
21.6k
          &Modulo<uint64_t>)));
212
213
  // Negation group
214
21.6k
  CEL_RETURN_IF_ERROR(
215
21.6k
      registry.Register(UnaryFunctionAdapter<Value, int64_t>::CreateDescriptor(
216
21.6k
                            cel::builtin::kNeg, false),
217
21.6k
                        UnaryFunctionAdapter<Value, int64_t>::WrapFunction(
218
21.6k
                            [](int64_t value) -> Value {
219
21.6k
                              auto inv = cel::internal::CheckedNegation(value);
220
21.6k
                              if (!inv.ok()) {
221
21.6k
                                return ErrorValue(inv.status());
222
21.6k
                              }
223
21.6k
                              return IntValue(*inv);
224
21.6k
                            })));
225
226
21.6k
  return registry.Register(
227
21.6k
      UnaryFunctionAdapter<double, double>::CreateDescriptor(cel::builtin::kNeg,
228
21.6k
                                                             false),
229
21.6k
      UnaryFunctionAdapter<double, double>::WrapFunction(
230
21.6k
          [](double value) -> double { return -value; }));
231
21.6k
}
232
233
}  // namespace cel