Coverage Report

Created: 2026-11-12 06:42

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
33.5k
                   const Function::InvokeContext& context) {
41
33.5k
  auto sum = cel::internal::CheckedAdd(v0, v1);
42
33.5k
  if (!sum.ok()) {
43
18.7k
    return ErrorValue::From(sum.status(), context.arena());
44
18.7k
  }
45
14.8k
  return IntValue(*sum);
46
33.5k
}
47
48
template <>
49
Value Add<uint64_t>(uint64_t v0, uint64_t v1,
50
24.9k
                    const Function::InvokeContext& context) {
51
24.9k
  auto sum = cel::internal::CheckedAdd(v0, v1);
52
24.9k
  if (!sum.ok()) {
53
24.7k
    return ErrorValue::From(sum.status(), context.arena());
54
24.7k
  }
55
218
  return UintValue(*sum);
56
24.9k
}
57
58
template <>
59
400
Value Add<double>(double v0, double v1, const Function::InvokeContext&) {
60
400
  return DoubleValue(v0 + v1);
61
400
}
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
193k
                   const Function::InvokeContext& context) {
69
193k
  auto diff = cel::internal::CheckedSub(v0, v1);
70
193k
  if (!diff.ok()) {
71
16.8k
    return ErrorValue::From(diff.status(), context.arena());
72
16.8k
  }
73
176k
  return IntValue(*diff);
74
193k
}
75
76
template <>
77
Value Sub<uint64_t>(uint64_t v0, uint64_t v1,
78
365k
                    const Function::InvokeContext& context) {
79
365k
  auto diff = cel::internal::CheckedSub(v0, v1);
80
365k
  if (!diff.ok()) {
81
285k
    return ErrorValue::From(diff.status(), context.arena());
82
285k
  }
83
79.9k
  return UintValue(*diff);
84
365k
}
85
86
template <>
87
10.2k
Value Sub<double>(double v0, double v1, const Function::InvokeContext&) {
88
10.2k
  return DoubleValue(v0 - v1);
89
10.2k
}
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
92.5k
                   const Function::InvokeContext& context) {
97
92.5k
  auto prod = cel::internal::CheckedMul(v0, v1);
98
92.5k
  if (!prod.ok()) {
99
46.3k
    return ErrorValue::From(prod.status(), context.arena());
100
46.3k
  }
101
46.2k
  return IntValue(*prod);
102
92.5k
}
103
104
template <>
105
Value Mul<uint64_t>(uint64_t v0, uint64_t v1,
106
94.1k
                    const Function::InvokeContext& context) {
107
94.1k
  auto prod = cel::internal::CheckedMul(v0, v1);
108
94.1k
  if (!prod.ok()) {
109
93.4k
    return ErrorValue::From(prod.status(), context.arena());
110
93.4k
  }
111
720
  return UintValue(*prod);
112
94.1k
}
113
114
template <>
115
1.64k
Value Mul<double>(double v0, double v1, const Function::InvokeContext&) {
116
1.64k
  return DoubleValue(v0 * v1);
117
1.64k
}
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
462k
                   const Function::InvokeContext& context) {
127
462k
  auto quot = cel::internal::CheckedDiv(v0, v1);
128
462k
  if (!quot.ok()) {
129
179k
    return ErrorValue::From(quot.status(), context.arena());
130
179k
  }
131
282k
  return IntValue(*quot);
132
462k
}
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
2.73M
                    const Function::InvokeContext& context) {
139
2.73M
  auto quot = cel::internal::CheckedDiv(v0, v1);
140
2.73M
  if (!quot.ok()) {
141
1.02M
    return ErrorValue::From(quot.status(), context.arena());
142
1.02M
  }
143
1.71M
  return UintValue(*quot);
144
2.73M
}
145
146
template <>
147
24.7k
Value Div<double>(double v0, double v1, const Function::InvokeContext&) {
148
24.7k
  static_assert(std::numeric_limits<double>::is_iec559,
149
24.7k
                "Division by zero for doubles must be supported");
150
151
  // For double, division will result in +/- inf
152
24.7k
  return DoubleValue(v0 / v1);
153
24.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
5.23M
                      const Function::InvokeContext& context) {
164
5.23M
  auto mod = cel::internal::CheckedMod(v0, v1);
165
5.23M
  if (!mod.ok()) {
166
5.04M
    return ErrorValue::From(mod.status(), context.arena());
167
5.04M
  }
168
188k
  return IntValue(*mod);
169
5.23M
}
170
171
template <>
172
Value Modulo<uint64_t>(uint64_t v0, uint64_t v1,
173
82.0k
                       const Function::InvokeContext& context) {
174
82.0k
  auto mod = cel::internal::CheckedMod(v0, v1);
175
82.0k
  if (!mod.ok()) {
176
63.5k
    return ErrorValue::From(mod.status(), context.arena());
177
63.5k
  }
178
18.4k
  return UintValue(*mod);
179
82.0k
}
180
181
// Helper method
182
// Registers all arithmetic functions for template parameter type.
183
template <class Type>
184
74.6k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
74.6k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
74.6k
  CEL_RETURN_IF_ERROR(registry.Register(
187
74.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
74.6k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
74.6k
  CEL_RETURN_IF_ERROR(registry.Register(
191
74.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
74.6k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
74.6k
  CEL_RETURN_IF_ERROR(registry.Register(
195
74.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
74.6k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
74.6k
  return registry.Register(
199
74.6k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
74.6k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
74.6k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<long>(cel::FunctionRegistry&)
Line
Count
Source
184
24.8k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
24.8k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
187
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
24.8k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
191
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
24.8k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
195
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
24.8k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
24.8k
  return registry.Register(
199
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
24.8k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
24.8k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<unsigned long>(cel::FunctionRegistry&)
Line
Count
Source
184
24.8k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
24.8k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
187
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
24.8k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
191
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
24.8k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
195
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
24.8k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
24.8k
  return registry.Register(
199
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
24.8k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
24.8k
}
arithmetic_functions.cc:absl::lts_20260526::Status cel::(anonymous namespace)::RegisterArithmeticFunctionsForType<double>(cel::FunctionRegistry&)
Line
Count
Source
184
24.8k
absl::Status RegisterArithmeticFunctionsForType(FunctionRegistry& registry) {
185
24.8k
  using FunctionAdapter = cel::BinaryFunctionAdapter<Value, Type, Type>;
186
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
187
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kAdd, false),
188
24.8k
      FunctionAdapter::WrapFunction(&Add<Type>)));
189
190
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
191
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kSubtract, false),
192
24.8k
      FunctionAdapter::WrapFunction(&Sub<Type>)));
193
194
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
195
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kMultiply, false),
196
24.8k
      FunctionAdapter::WrapFunction(&Mul<Type>)));
197
198
24.8k
  return registry.Register(
199
24.8k
      FunctionAdapter::CreateDescriptor(cel::builtin::kDivide, false),
200
24.8k
      FunctionAdapter::WrapFunction(&Div<Type>));
201
24.8k
}
202
203
}  // namespace
204
205
absl::Status RegisterArithmeticFunctions(FunctionRegistry& registry,
206
24.8k
                                         const RuntimeOptions& options) {
207
24.8k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<int64_t>(registry));
208
24.8k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<uint64_t>(registry));
209
24.8k
  CEL_RETURN_IF_ERROR(RegisterArithmeticFunctionsForType<double>(registry));
210
211
  // Modulo
212
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
213
24.8k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::CreateDescriptor(
214
24.8k
          cel::builtin::kModulo, false),
215
24.8k
      BinaryFunctionAdapter<Value, int64_t, int64_t>::WrapFunction(
216
24.8k
          &Modulo<int64_t>)));
217
218
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
219
24.8k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::CreateDescriptor(
220
24.8k
          cel::builtin::kModulo, false),
221
24.8k
      BinaryFunctionAdapter<Value, uint64_t, uint64_t>::WrapFunction(
222
24.8k
          &Modulo<uint64_t>)));
223
224
  // Negation group
225
24.8k
  CEL_RETURN_IF_ERROR(registry.Register(
226
24.8k
      UnaryFunctionAdapter<Value, int64_t>::CreateDescriptor(cel::builtin::kNeg,
227
24.8k
                                                             false),
228
24.8k
      UnaryFunctionAdapter<Value, int64_t>::WrapFunction(
229
24.8k
          [](int64_t value, const Function::InvokeContext& context) -> Value {
230
24.8k
            auto inv = cel::internal::CheckedNegation(value);
231
24.8k
            if (!inv.ok()) {
232
24.8k
              return ErrorValue::From(inv.status(), context.arena());
233
24.8k
            }
234
24.8k
            return IntValue(*inv);
235
24.8k
          })));
236
237
24.8k
  return registry.Register(
238
24.8k
      UnaryFunctionAdapter<double, double>::CreateDescriptor(cel::builtin::kNeg,
239
24.8k
                                                             false),
240
24.8k
      UnaryFunctionAdapter<double, double>::WrapFunction(
241
24.8k
          [](double value) -> double { return -value; }));
242
24.8k
}
243
244
}  // namespace cel