Skip to content

Commit

Permalink
array module support: byteswap()
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-sans-paille committed Aug 7, 2024
1 parent 10a833b commit ce5ab11
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
46 changes: 46 additions & 0 deletions pythran/pythonic/array/array/byteswap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef PYTHONIC_ARRAY_ARRAY_BYTESWAP_HPP
#define PYTHONIC_ARRAY_ARRAY_BYTESWAP_HPP

#include "pythonic/include/array/array/byteswap.hpp"
#include "pythonic/utils/functor.hpp"

#include <byteswap.h>

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{
void byteswap(char *buffer, size_t n, std::integral_constant<unsigned, 2>)
{
auto *ibuffer = reinterpret_cast<uint16_t *>(buffer);
for (size_t i = 0; i < n; i++)
ibuffer[i] = bswap_16(ibuffer[i]);
}
void byteswap(char *buffer, size_t n, std::integral_constant<unsigned, 4>)
{
auto *ibuffer = reinterpret_cast<uint32_t *>(buffer);
for (size_t i = 0; i < n; i++)
ibuffer[i] = bswap_32(ibuffer[i]);
}
void byteswap(char *buffer, size_t n, std::integral_constant<unsigned, 8>)
{
auto *ibuffer = reinterpret_cast<uint64_t *>(buffer);
for (size_t i = 0; i < n; i++)
ibuffer[i] = bswap_64(ibuffer[i]);
}

template <class T>
types::none_type byteswap(types::array<T> &seq)
{
byteswap(reinterpret_cast<char *>(seq.data()), seq.size(),
std::integral_constant<unsigned, sizeof(T)>{});
return {};
}

} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
28 changes: 28 additions & 0 deletions pythran/pythonic/include/array/array/byteswap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef PYTHONIC_INCLUDE_ARRAY_ARRAY_BYTESWAP_HPP
#define PYTHONIC_INCLUDE_ARRAY_ARRAY_BYTESWAP_HPP

#include "pythonic/include/types/array.hpp"
#include "pythonic/include/utils/functor.hpp"

PYTHONIC_NS_BEGIN

namespace array
{

namespace array
{

template <class T>
types::none_type byteswap(types::array<T> &seq);

template <class T>
types::none_type byteswap(types::array<T> &&seq)
{
return {};
}

DEFINE_FUNCTOR(pythonic::array::array, byteswap);
} // namespace array
} // namespace array
PYTHONIC_NS_END
#endif
3 changes: 2 additions & 1 deletion pythran/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def update_effects(self, node):
"array": {
# array have fixed type, no need for signature
"append": MethodIntr(),
"buffer_info": MethodIntr(),
"buffer_info": ConstMethodIntr(),
"byteswap": MethodIntr(),
},
"list": {
"append": MethodIntr(signature=Fun[[List[T0], T0], None]),
Expand Down
4 changes: 4 additions & 0 deletions pythran/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ def test_array_append(self):
def test_array_buffer_info(self):
self.run_test("def array_buffer_info_(n): import array; x = array.array('h', [n]); return x.buffer_info()[1]",
2, array_buffer_info_=[int])

def test_array_byteswap(self):
self.run_test("def array_byteswap_(n): import array; x = array.array('H', [n]); x.byteswap(); return x.tolist()",
2, array_byteswap_=[int])

0 comments on commit ce5ab11

Please sign in to comment.