Lua: Byte order swapping

Not that with memory2.* one needs that very much...
This commit is contained in:
Ilari Liusvaara 2014-01-20 19:50:28 +02:00
parent 156a23782a
commit 1da06a7675
3 changed files with 64 additions and 0 deletions

40
lua.lyx
View file

@ -803,6 +803,46 @@ Like bit.flagdecode, but outputs the string in the opposite order (most signific
nt bit first).
\end_layout
\begin_layout Subsection
bit.swap{,s}{,h,d,q}word: Swap word endian
\end_layout
\begin_layout Itemize
Syntax: number bit.swapword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swaphword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapdword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapqword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapsword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapshword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapsdword(number n)
\end_layout
\begin_layout Itemize
Syntax: number bit.swapsqword(number n)
\end_layout
\begin_layout Standard
Swap endianess of (un)signed integer <n>.
\end_layout
\begin_layout Standard
\begin_inset Newpage pagebreak
\end_inset

BIN
lua.pdf

Binary file not shown.

View file

@ -1,5 +1,7 @@
#include "lua/internal.hpp"
#include "library/minmax.hpp"
#include "library/int24.hpp"
#include "library/serialization.hpp"
#define BITWISE_BITS 48
#define BITWISE_MASK ((1ULL << (BITWISE_BITS)) - 1)
@ -103,6 +105,20 @@ namespace
}
};
template<typename T>
class lua_bswap : public lua::function
{
public:
lua_bswap(const std::string& s) : lua::function(lua_func_bit, s) {};
int invoke(lua::state& L)
{
T val = L.get_numeric_argument<T>(1, fname.c_str());
serialization::swap_endian(val);
L.pushnumber(val);
return 1;
}
};
lua::fnptr lua_bextract(lua_func_bit, "bit.extract", [](lua::state& L, const std::string& fname)
-> int {
uint64_t num = L.get_numeric_argument<uint64_t>(1, fname.c_str());
@ -260,4 +276,12 @@ namespace
lua_shifter<shift_lshift> bit_lshift("bit.lshift");
lua_shifter<shift_arshift> bit_arshift("bit.arshift");
lua_shifter<shift_lrshift> bit_lrshift("bit.lrshift");
lua_bswap<uint16_t> bit_swapword("bit.swapword");
lua_bswap<ss_uint24_t> bit_swaphword("bit.swaphword");
lua_bswap<uint32_t> bit_swapdword("bit.swapdword");
lua_bswap<uint64_t> bit_swapqword("bit.swapqword");
lua_bswap<int16_t> bit_swapsword("bit.swapsword");
lua_bswap<ss_int24_t> bit_swapshword("bit.swapshword");
lua_bswap<int32_t> bit_swapsdword("bit.swapsdword");
lua_bswap<int64_t> bit_swapsqword("bit.swapsqword");
}