mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add bitorder functions store_le, load_le, store_be, store_le.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by the MIT license
|
||||
// a copy of which can be found in the LICENSE_STDLIB file.
|
||||
module std::core::bitorder;
|
||||
|
||||
import std::bits;
|
||||
// This module contains types of different endianness.
|
||||
// *BE types represent big-endian types
|
||||
// *LE types represent little-endian types.
|
||||
@@ -87,6 +87,56 @@ bitstruct UInt128LE : uint128 @littleendian
|
||||
uint128 val : 0..127;
|
||||
}
|
||||
|
||||
<*
|
||||
@require $defined(*bytes) : "Pointer must be possible to dereference"
|
||||
@require types::is_intlike($typeof(*bytes)) : "Type must be an integer or int vector"
|
||||
*>
|
||||
macro load_be(bytes)
|
||||
{
|
||||
$if env::BIG_ENDIAN:
|
||||
return mem::load(bytes, $align: 1);
|
||||
$else
|
||||
return bswap(mem::load(bytes, $align: 1));
|
||||
$endif
|
||||
}
|
||||
|
||||
<*
|
||||
@require $defined(*bytes) : "Pointer must be possible to dereference"
|
||||
@require types::is_intlike($typeof(*bytes)) : "Type must be an integer or int vector"
|
||||
*>
|
||||
macro load_le(bytes)
|
||||
{
|
||||
$if env::BIG_ENDIAN:
|
||||
return bswap(mem::load(bytes, $align: 1));
|
||||
$else
|
||||
return mem::load(bytes, $align: 1);
|
||||
$endif
|
||||
}
|
||||
|
||||
<*
|
||||
@require types::is_intlike($typeof(value)) : "Type must be an integer or int vector"
|
||||
*>
|
||||
macro void store_be(void* dst, value)
|
||||
{
|
||||
$if env::BIG_ENDIAN:
|
||||
mem::store(($typeof(value)*)dst, value, $align: 1);
|
||||
$else
|
||||
mem::store(($typeof(value)*)dst, bswap(value), $align: 1);
|
||||
$endif
|
||||
}
|
||||
|
||||
<*
|
||||
@require types::is_intlike($typeof(value)) : "Type must be an integer or int vector"
|
||||
*>
|
||||
macro void store_le(void* dst, value)
|
||||
{
|
||||
$if env::BIG_ENDIAN:
|
||||
mem::store(($typeof(value)*)dst, bswap(value), $align: 1);
|
||||
$else
|
||||
mem::store(($typeof(value)*)dst, value, $align: 1);
|
||||
$endif
|
||||
}
|
||||
|
||||
<*
|
||||
@require @is_array_or_slice_of_char(bytes) : "argument must be an array, a pointer to an array or a slice of char"
|
||||
@require is_bitorder($Type) : "type must be a bitorder integer"
|
||||
|
||||
Reference in New Issue
Block a user