Coverage Report

Created: 2026-11-24 06:40

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.h"
28
#include "runtime/function_registry.h"
29
#include "runtime/runtime_options.h"
30
31
namespace cel {
32
namespace {
33
34
// Template functions providing arithmetic operations
35
template <class Type>
36
Value Add(Type v0, Type v1, const Function::InvokeContext& context);
37
38
template <>
39
Value Add<int64_t>(int64_t v0, int64_t v1,
40
80.9k
                   const Function::InvokeContext& context) {
41
80.9k
  auto sum = cel::internal::CheckedAdd(v0, v1);
42
80.9k
  if (!sum.ok()) {
43
4.56k
    return ErrorValue::From(sum.status(), context.arena());
44
4.56k
  }
45
76.4k
  return IntValue(*sum);
46
80.9k
}
47
48
template <>
49
Value Add<uint64_t>(uint64_t v0, uint64_t v1,
50
83.4k
                    const Function::InvokeContext& context) {
51
83.4k
  auto sum = cel::internal::CheckedAdd(v0, v1);
52
83.4k
  if (!sum.ok()) {
53
66.2k
    return ErrorValue::From(sum.status(), context.arena());
54
66.2k
  }
55
17.2k
  return UintValue(*sum);
56
83.4k
}
57
58
template <>
59
528
Value Add<double>(double v0, double v1, const Function::InvokeContext&) {
60
528
  return DoubleValue(v0 + v1);
61
528
}
62
63
template <class Type>
64
Value Sub(Type v0, Type v1, const Function::InvokeContext& context);
65
66
template <>
67
Value Sub<int64_t>(int64_t v0, int64_t v1,
68
183k
                   const Function::InvokeContext& context) {
69
183k
  auto diff = cel::internal::CheckedSub(v0, v1);
70
183k
  if (!diff.ok()) {
71
7.71k
    return ErrorValue::From(diff.status(), context.arena());
72
7.71k
  }
73
175k
  return IntValue(*diff);
74
183k
}
75
76
template <>
77
Value Sub<uint64_t>(uint64_t v0, uint64_t v1,
78
568k
                    const Function::InvokeContext& context) {
79
568k
  auto diff = cel::internal::CheckedSub(v0, v1);
80
568k
  if (!diff.ok()) {
81
437k
    return ErrorValue::From(diff.status(), context.arena());
82
437k
  }
83
130k
  return UintValue(*diff);
84
568k
}
85
86
template <>
87
9.53k
Value Sub<double>(double v0, double v1, const Function::InvokeContext&) {
88
9.53k
  return DoubleValue(v0 - v1);
89
9.53k
}
90
91
template <class Type>
92
Value Mul(Type v0, Type v1, const Function::InvokeContext& context);
93
94
template <>
95
Value Mul<int64_t>(int64_t v0, int64_t v1,
96
141k
                   const Function::InvokeContext& context) {
97
141k
  auto prod = cel::internal::CheckedMul(v0, v1);
98
141k
  if (!prod.ok()) {
99
72.5k
    return ErrorValue::From(prod.status(), context.arena());
100
72.5k
  }
101
68.5k
  return IntValue(*prod);
102
141k
}
103
104
template <>
105
Value Mul<uint64_t>(uint64_t v0, uint64_t v1,
106
159k
                    const Function::InvokeContext& context) {
107
159k
  auto prod = cel::internal::CheckedMul(v0, v1);
108
159k
  if (!prod.ok()) {
109
141k
    return ErrorValue::From(prod.status(), context.arena());
110
141k
  }
111
17.8k
  return UintValue(*prod);
112
159k
}
113
114
template <>
115
621
Value Mul<double>(double v0, double v1, const Function::InvokeContext&) {
116
621
  return DoubleValue(v0 * v1);
117
621
}
118
119
template <class Type>
120
Value Div(Type v0, Type v1, const Function::InvokeContext& context);
121
122
// Division operations for integer types should check for
123
// division by 0
124
template <>
125
Value Div<int64_t>(int64_t v0, int64_t v1,
126
820k
                   const Function::InvokeContext& context) {
127
820k
  auto quot = cel::internal::CheckedDiv(v0, v1);
128
820k
  if (!quot.ok()) {
129
304k
    return ErrorValue::From(quot.status(), context.arena());
130
304k
  }
131
515k
  return IntValue(*quot);
132
820k
}
133
134
// Division operations for integer types should check for
135
// division by 0
136
template <>
137
Value Div<uint64_t>(uint64_t v0, uint64_t v1,
138
3.72M
                    const Function::InvokeContext& context) {
139
3.72M
  auto quot = cel::internal::CheckedDiv(v0, v1);
140
3.72M
  if (!quot.ok()) {
141
1.38M
    return ErrorValue::From(quot.status(), context.arena());
142
1.38M
  }
143
2.33M
  return UintValue(*quot);
144
3.72M
}
145
146
template <>
147
36.7k
Value Div<double>(double v0, double v1, const Function::InvokeContext&) {
148
36.7k
  static_assert(std::numeric_limits<double>::is_iec559,
149
36.7k
                "Division by zero for doubles must be supported");
150
151
  // For double, division will result in +/- inf
152
36.7k
  return DoubleValue(v0 / v1);
153
36.7k
}
154
155
// Modulo operation
156
template <class Type>
157
Value Modulo(Type v0, Type v1, const Function::InvokeContext& context);
158
159
// Modulo operations for integer types should check for
160
// division by 0
161
template <>
162
Value Modulo<int64_t>(int64_t v0, int64_t v1,
163
7.21M
                      const Function::InvokeContext& context) {
164
7.21M
  auto mod = cel::internal::CheckedMod(v0, v1);
165
7.21M
  if (!mod.ok()) {
166
6.97M
    return ErrorValue::From(mod.status(), context.arena());
167
6.97M
  }
168
240k
  return IntValue(*mod);
169
7.21M
}
170
171
template <>
172
Value Modulo<uint64_t>(uint64_t v0, uint64_t v1,
173
127k
                       const Function::InvokeContext& context) {
174
127k
  auto mod = cel::internal::CheckedMod(v0, v1);
175
127k
  if (!mod.ok()) {
176
105k
    return ErrorValue::From(mod.status(), context.arena());
177
105k
  }
178
21.9k
  return UintValue(*mod);
179
127k
}
180
181
// Helper method
182
// Registers all arithmetic functions for template parameter type.
183
template <class Type>
184
75.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
75.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
75.6k
  CEL_RETURN_IF_ERROR(registry.Register(
187
75.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
75.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
75.6k
  CEL_RETURN_IF_ERROR(registry.Register(
191
75.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
75.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
75.6k
  CEL_RETURN_IF_ERROR(registry.Register(
195
75.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
75.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
75.6k
  return registry.Register(
199
75.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
75.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
75.6k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<long>(cel::FunctionRegistry&)
Line
Count
Source
184
25.2k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
25.2k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
187
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
25.2k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
191
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
25.2k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
195
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
25.2k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
25.2k
  return registry.Register(
199
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
25.2k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
25.2k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<unsigned long>(cel::FunctionRegistry&)
Line
Count
Source
184
25.2k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
25.2k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
187
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
25.2k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
191
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
25.2k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
195
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
25.2k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
25.2k
  return registry.Register(
199
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
25.2k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
25.2k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<double>(cel::FunctionRegistry&)
Line
Count
Source
184
25.2k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
25.2k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
187
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
25.2k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
191
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
25.2k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
195
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
25.2k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
25.2k
  return registry.Register(
199
25.2k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
25.2k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
25.2k
}
202
203
}  // namespace
204
205
absl::Status RegisterArithmeticFunctions(FunctionRegistry& registry,
206
25.2k
                                         const RuntimeOptions& options) {
207
25.2k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<int64_t>(registry));
208
25.2k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<uint64_t>(registry));
209
25.2k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<double>(registry));
210
211
  // Modulo
212
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
213
25.2k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::CreateDescriptor(
214
25.2k
          cel::builtin::kModulo, false),
215
25.2k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::WrapFunction(
216
25.2k
          &Modulo<int64_t>)));
217
218
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
219
25.2k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::CreateDescriptor(
220
25.2k
          cel::builtin::kModulo, false),
221
25.2k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::WrapFunction(
222
25.2k
          &Modulo<uint64_t>)));
223
224
  // Negation group
225
25.2k
  CEL_RETURN_IF_ERROR(registry.Register(
226
25.2k
      UnaryFunctionAdapter<Value, int64_t>::CreateDescriptor(cel::builtin::kNeg,
227
25.2k
                                                             false),
228
25.2k
      UnaryFunctionAdapter<Value, int64_t>::WrapFunction(
229
25.2k
          [](int64_t value, const Function::InvokeContext& context) -> Value {
230
25.2k
            auto inv = cel::internal::CheckedNegation(value);
231
25.2k
            if (!inv.ok()) {
232
25.2k
              return ErrorValue::From(inv.status(), context.arena());
233
25.2k
            }
234
25.2k
            return IntValue(*inv);
235
25.2k
          })));
236
237
25.2k
  return registry.Register(
238
25.2k
      UnaryFunctionAdapter<double, double>::CreateDescriptor(cel::builtin::kNeg,
239
25.2k
                                                             false),
240
25.2k
      UnaryFunctionAdapter<double, double>::WrapFunction(
241
25.2k
          [](double value) -> double { return -value; }));
242
25.2k
}
243
244
}  // namespace cel