Coverage Report

Created: 2026-07-11 06:47

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