Skip to content

Commit 72c31d9

Browse files
authored
move f$vk_dot_product to runtime-common (#1422)
1 parent 74d4514 commit 72c31d9

5 files changed

Lines changed: 54 additions & 58 deletions

File tree

builtin-functions/kphp-light/stdlib/array-functions.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,5 @@ function array_combine ($keys ::: array, $values ::: array) ::: ^2;
183183

184184
function array_column ($a ::: array, $column_key, $index_key = null) ::: array< ^1[*][*] > | false;
185185

186-
// ===== UNSUPPORTED =====
187-
188186
/** @kphp-extern-func-info stub cpp_template_call */
189187
function vk_dot_product ($a ::: array, $b ::: array) ::: ^1[*] | ^2[*];

runtime-common/stdlib/array/array-functions.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
#include <cstdint>
88
#include <limits>
9+
#include <numeric>
910
#include <tuple>
1011

1112
#include "common/type_traits/function_traits.h"
1213
#include "common/type_traits/list_of_types.h"
14+
#include "common/vector-product.h"
1315
#include "runtime-common/core/runtime-core.h"
1416

1517
inline constexpr int64_t SORT_REGULAR = 0;
@@ -1167,3 +1169,51 @@ auto f$array_column(const Optional<T>& a, const mixed& column_key,
11671169

11681170
return f$array_column(a.val(), column_key, index_key);
11691171
}
1172+
1173+
template<class T>
1174+
T vk_dot_product_sparse(const array<T>& a, const array<T>& b) noexcept {
1175+
T result = T();
1176+
for (const auto& it : a) {
1177+
const auto* b_val = b.find_value(it);
1178+
if (b_val && !f$is_null(*b_val)) {
1179+
result += it.get_value() * (*b_val);
1180+
}
1181+
}
1182+
return result;
1183+
}
1184+
1185+
template<class T>
1186+
T vk_dot_product_dense(const array<T>& a, const array<T>& b) noexcept {
1187+
static_assert(!std::is_same<T, int>{}, "int is forbidden");
1188+
1189+
T result = T();
1190+
int64_t size = min(a.count(), b.count());
1191+
for (int64_t i = 0; i < size; i++) {
1192+
result += a.get_value(i) * b.get_value(i);
1193+
}
1194+
return result;
1195+
}
1196+
1197+
template<>
1198+
inline int64_t vk_dot_product_dense<int64_t>(const array<int64_t>& a, const array<int64_t>& b) noexcept {
1199+
const int64_t size = min(a.count(), b.count());
1200+
const int64_t* ap = a.get_const_vector_pointer();
1201+
const int64_t* bp = b.get_const_vector_pointer();
1202+
return std::inner_product(ap, ap + size, bp, 0L);
1203+
}
1204+
1205+
template<>
1206+
inline double vk_dot_product_dense<double>(const array<double>& a, const array<double>& b) noexcept {
1207+
int64_t size = min(a.count(), b.count());
1208+
const double* ap = a.get_const_vector_pointer();
1209+
const double* bp = b.get_const_vector_pointer();
1210+
return __dot_product(ap, bp, static_cast<int>(size));
1211+
}
1212+
1213+
template<class T>
1214+
T f$vk_dot_product(const array<T>& a, const array<T>& b) noexcept {
1215+
if (a.is_vector() && b.is_vector()) {
1216+
return vk_dot_product_dense<T>(a, b);
1217+
}
1218+
return vk_dot_product_sparse<T>(a, b);
1219+
}

runtime-light/stdlib/array/array-functions.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,3 @@ kphp::coro::task<> f$uksort(array<T>& a, Comparator compare) {
402402
co_return a.ksort(std::move(compare));
403403
}
404404
}
405-
406-
template<class T>
407-
T f$vk_dot_product(const array<T>& /*unused*/, const array<T>& /*unused*/) {
408-
kphp::log::error("call to unsupported function");
409-
}

runtime/array_functions.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
#pragma once
66

7-
#include <numeric>
8-
97
#include "common/type_traits/function_traits.h"
10-
#include "common/vector-product.h"
118
#include "runtime-common/core/runtime-core.h"
129
#include "runtime-common/stdlib/array/array-functions.h"
1310
#include "runtime/math_functions.h"
@@ -178,54 +175,6 @@ void f$shuffle(array<T>& a) {
178175
a = std::move(result);
179176
}
180177

181-
template<class T>
182-
T vk_dot_product_sparse(const array<T>& a, const array<T>& b) {
183-
T result = T();
184-
for (const auto& it : a) {
185-
const auto* b_val = b.find_value(it);
186-
if (b_val && !f$is_null(*b_val)) {
187-
result += it.get_value() * (*b_val);
188-
}
189-
}
190-
return result;
191-
}
192-
193-
template<class T>
194-
T vk_dot_product_dense(const array<T>& a, const array<T>& b) {
195-
static_assert(!std::is_same<T, int>{}, "int is forbidden");
196-
197-
T result = T();
198-
int64_t size = min(a.count(), b.count());
199-
for (int64_t i = 0; i < size; i++) {
200-
result += a.get_value(i) * b.get_value(i);
201-
}
202-
return result;
203-
}
204-
205-
template<>
206-
inline int64_t vk_dot_product_dense<int64_t>(const array<int64_t>& a, const array<int64_t>& b) {
207-
const int64_t size = min(a.count(), b.count());
208-
const int64_t* ap = a.get_const_vector_pointer();
209-
const int64_t* bp = b.get_const_vector_pointer();
210-
return std::inner_product(ap, ap + size, bp, 0L);
211-
}
212-
213-
template<>
214-
inline double vk_dot_product_dense<double>(const array<double>& a, const array<double>& b) {
215-
int64_t size = min(a.count(), b.count());
216-
const double* ap = a.get_const_vector_pointer();
217-
const double* bp = b.get_const_vector_pointer();
218-
return __dot_product(ap, bp, static_cast<int>(size));
219-
}
220-
221-
template<class T>
222-
T f$vk_dot_product(const array<T>& a, const array<T>& b) {
223-
if (a.is_vector() && b.is_vector()) {
224-
return vk_dot_product_dense<T>(a, b);
225-
}
226-
return vk_dot_product_sparse<T>(a, b);
227-
}
228-
229178
template<typename Result, typename U, typename Comparator>
230179
Result array_functions_impl_::async_sort([[maybe_unused]] array<U>& arr, [[maybe_unused]] Comparator comparator, [[maybe_unused]] bool renumber) noexcept {
231180
struct async_sort_stub_class {};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ok
2+
<php?
3+
4+
var_dump(vk_dot_product([1, 2, 3], [4, 5, 6]));

0 commit comments

Comments
 (0)