Coverage Report

Created: 2026-11-21 06:12

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