xref: /aosp_15_r20/external/perfetto/src/trace_processor/perfetto_sql/intrinsics/functions/math.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 // Copyright (C) 2023 The Android Open Source Project
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 //      http://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 "src/trace_processor/perfetto_sql/intrinsics/functions/math.h"
16 
17 #include <cmath>
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/base/status.h"
21 #include "perfetto/trace_processor/basic_types.h"
22 #include "src/trace_processor/perfetto_sql/engine/perfetto_sql_engine.h"
23 #include "src/trace_processor/perfetto_sql/intrinsics/functions/sql_function.h"
24 #include "src/trace_processor/sqlite/sqlite_utils.h"
25 #include "src/trace_processor/util/status_macros.h"
26 
27 namespace perfetto::trace_processor {
28 
29 namespace {
30 
31 struct Ln : public SqlFunction {
Runperfetto::trace_processor::__anond3a085950111::Ln32   static base::Status Run(Context*,
33                           size_t argc,
34                           sqlite3_value** argv,
35                           SqlValue& out,
36                           Destructors&) {
37     PERFETTO_CHECK(argc == 1);
38     switch (sqlite3_value_numeric_type(argv[0])) {
39       case SQLITE_INTEGER:
40       case SQLITE_FLOAT: {
41         double value = sqlite3_value_double(argv[0]);
42         if (value > 0.0) {
43           out = SqlValue::Double(std::log(value));
44         }
45         break;
46       }
47       case SqlValue::kNull:
48       case SqlValue::kString:
49       case SqlValue::kBytes:
50         break;
51     }
52 
53     return base::OkStatus();
54   }
55 };
56 
57 struct Exp : public SqlFunction {
Runperfetto::trace_processor::__anond3a085950111::Exp58   static base::Status Run(Context*,
59                           size_t argc,
60                           sqlite3_value** argv,
61                           SqlValue& out,
62                           Destructors&) {
63     PERFETTO_CHECK(argc == 1);
64     switch (sqlite3_value_numeric_type(argv[0])) {
65       case SQLITE_INTEGER:
66       case SQLITE_FLOAT:
67         out = SqlValue::Double(std::exp(sqlite3_value_double(argv[0])));
68         break;
69       case SqlValue::kNull:
70       case SqlValue::kString:
71       case SqlValue::kBytes:
72         break;
73     }
74 
75     return base::OkStatus();
76   }
77 };
78 
79 struct Sqrt : public SqlFunction {
Runperfetto::trace_processor::__anond3a085950111::Sqrt80   static base::Status Run(Context*,
81                           size_t argc,
82                           sqlite3_value** argv,
83                           SqlValue& out,
84                           Destructors&) {
85     PERFETTO_CHECK(argc == 1);
86     switch (sqlite3_value_numeric_type(argv[0])) {
87       case SQLITE_INTEGER:
88       case SQLITE_FLOAT:
89         out = SqlValue::Double(std::sqrt(sqlite3_value_double(argv[0])));
90         break;
91       case SqlValue::kNull:
92       case SqlValue::kString:
93       case SqlValue::kBytes:
94         break;
95     }
96 
97     return base::OkStatus();
98   }
99 };
100 
101 }  // namespace
102 
RegisterMathFunctions(PerfettoSqlEngine & engine)103 base::Status RegisterMathFunctions(PerfettoSqlEngine& engine) {
104   RETURN_IF_ERROR(engine.RegisterStaticFunction<Ln>("ln", 1, nullptr, true));
105   RETURN_IF_ERROR(engine.RegisterStaticFunction<Exp>("exp", 1, nullptr, true));
106   return engine.RegisterStaticFunction<Sqrt>("sqrt", 1, nullptr, true);
107 }
108 
109 }  // namespace perfetto::trace_processor
110