Skip to content

Commit

Permalink
✨ New bit-8, endian-aware load/store and memreverse!
Browse files Browse the repository at this point in the history
— ✨ assume_aligned helpers
— ✨ _Static_assert fixes for MSVC in C++ mode
  • Loading branch information
ThePhD committed Nov 13, 2021
1 parent e7a65c9 commit 7da7a78
Show file tree
Hide file tree
Showing 24 changed files with 1,340 additions and 57 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,18 @@ export(TARGETS ztd.tag_invoke
"${CMAKE_CURRENT_BINARY_DIR}/cmake/ztd.tag_invoke/ztd.tag_invoke-targets.cmake")

# ztd.idk
file(GLOB_RECURSE ztd.idk.includes CONFIGURE_DEPENDS include/idk*.hpp)
file(GLOB_RECURSE ztd.idk.includes CONFIGURE_DEPENDS include/ztd/idk**.hpp include/ztd/idk**.h)
file(GLOB_RECURSE ztd.idk.sources CONFIGURE_DEPENDS source/ztd/idk/**.cpp source/ztd/idk/**.c)

add_library(ztd.idk INTERFACE)
add_library(ztd.idk ${ztd.idk.sources})
add_library(ztd::idk ALIAS ztd.idk)
target_include_directories(ztd.idk
INTERFACE
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_sources(ztd.idk INTERFACE ${ztd.idk.includes})
target_sources(ztd.idk PUBLIC ${ztd.idk.includes} ${ztd.idk.sources})
target_link_libraries(ztd.idk
INTERFACE
PUBLIC
ztd::version
ztd::tag_invoke
)
Expand Down
5 changes: 2 additions & 3 deletions documentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ include(GNUInstallDirs)
# Find all the public headers
file(GLOB_RECURSE ztd_idk_headers
LIST_DIRECTORIES NO
CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/include/**.*)
CONFIGURE_DEPENDS ../include/**.*)
# First, make a list copy for use with dependency tracking later down below
# Remove every single public header
# separate text with spaces and surround them with quotes for Doxygen to understand them all
Expand All @@ -48,11 +48,10 @@ list(FILTER ztd_idk_doxygen_public_headers EXCLUDE REGEX /detail/.*)
list(TRANSFORM ztd_idk_doxygen_public_headers REPLACE "(.+)" [["\1"]])
list(JOIN ztd_idk_doxygen_public_headers " " ztd_idk_doxygen_public_headers)
# do the same for the include paths
get_target_property(ztd_idk_doxygen_include_paths ztd::idk INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(ztd_idk_doxygen_include_paths ztd::idk INCLUDE_DIRECTORIES)
list(TRANSFORM ztd_idk_doxygen_include_paths REPLACE "(.+)" [["\1"]])
list(JOIN ztd_idk_doxygen_include_paths " " ztd_idk_doxygen_include_paths)


set(ZTD_IDK_DOXYGEN_PROJECT_DESCRIPTION ${PROJECT_DESCRIPTION})
set(ZTD_IDK_DOXYGEN_PROJECT_VERSION ${PROJECT_VERSION})
set(ZTD_IDK_DOXYGEN_PROJECT_NAME ${PROJECT_NAME})
Expand Down
5 changes: 3 additions & 2 deletions documentation/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,14 @@ def generate_doxygen_xml(app):
"""Run the doxygen make commands if we're on the ReadTheDocs server"""

read_the_docs_build = os.environ.get('READTHEDOCS', None) == 'True'

if read_the_docs_build:
print(
"[ztd.idk/documentation/conf.py] Detected READTHEDOCS environment variable: running cmake from conf.py"
)
run_cmake_doxygen()


def setup(app):
"""Sphinx / ReadTheDocs hook for when the build system is initialized."""

# Add hook for building doxygen xml when needed
app.connect("builder-inited", generate_doxygen_xml)
63 changes: 63 additions & 0 deletions include/ztd/idk/assume_aligned.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// =============================================================================
//
// ztd.idk
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.idk licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#pragma once

#ifndef ZTD_IDK_ASSUME_ALIGNED_H
#define ZTD_IDK_ASSUME_ALIGNED_H

#include <ztd/idk/version.h>

// clang-format off
#if ZTD_HAS_BUILTIN_I_(__builtin_assume_aligned)
#define ZTD_ASSUME_ALIGNED_C(_ALIGNMENT, ...) __builtin_assume_aligned((__VA_ARGS__), _ALIGNMENT)
#else
#define ZTD_ASSUME_ALIGNED_C(_ALIGNMENT, ...) __VA_ARGS__
#endif

#if ZTD_IS_ON(ZTD_C_I_)
#define ZTD_ASSUME_ALIGNED(_ALIGNMENT, ...) ZTD_ASSUME_ALIGNED_C(_ALIGNMENT, __VA_ARGS__)
#define ZTD_ASSUME_ALIGNED_CXX(_ALIGNMENT, ...) ZTD_ASSUME_ALIGNED(_ALIGNMENT, __VA_ARGS__)
#else
#include <ztd/idk/version.hpp>

#if ZTD_IS_ON(ZTD_STD_LIBRARY_ASSUME_ALIGNED_I_)
#include <memory>

#define ZTD_ASSUME_ALIGNED_CXX(_ALIGNMENT, ...) ::std::assume_aligned<_ALIGNMENT>(__VA_ARGS__);
#else
#define ZTD_ASSUME_ALIGNED_CXX(_ALIGNMENT, ...) ZTD_ASSUME_ALIGNED_C(__VA_ARGS__)
#endif

#define ZTD_ASSUME_ALIGNED(_ALIGNMENT, ...) ZTD_ASSUME_ALIGNED_CXX(_ALIGNMENT, __VA_ARGS__)
#endif
// clang-format on

#endif // ZTD_IDK_ASSUME_ALIGNED_H
40 changes: 40 additions & 0 deletions include/ztd/idk/assume_aligned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// =============================================================================
//
// ztd.idk
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.idk licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#pragma once

#ifndef ZTD_IDK_ASSUME_ALIGNED_HPP
#define ZTD_IDK_ASSUME_ALIGNED_HPP

#include <ztd/idk/version.hpp>

#include <ztd/idk/assume_aligned.h>

#endif // ZTD_IDK_ASSUME_ALIGNED_HPP
168 changes: 168 additions & 0 deletions include/ztd/idk/bit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// =============================================================================
//
// ztd.idk
// Copyright © 2021 JeanHeyd "ThePhD" Meneide and Shepherd's Oasis, LLC
// Contact: opensource@soasis.org
//
// Commercial License Usage
// Licensees holding valid commercial ztd.idk licenses may use this file in
// accordance with the commercial license agreement provided with the
// Software or, alternatively, in accordance with the terms contained in
// a written agreement between you and Shepherd's Oasis, LLC.
// For licensing terms and conditions see your agreement. For
// further information contact opensource@soasis.org.
//
// Apache License Version 2 Usage
// Alternatively, this file may be used under the terms of Apache License
// Version 2.0 (the "License") for non-commercial use; you may not use this
// file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ============================================================================>

#pragma once

#ifndef ZTD_IDK_BIT_H
#define ZTD_IDK_BIT_H

#include <ztd/idk/version.h>

#include <ztd/idk/static_assert.h>

#if ZTD_IS_ON(ZTD_C_I_)
#include <stddef.h>
#include <limits.h>
#include <stdint.h>
#else
#include <cstddef>
#include <climits>
#include <cstdint>
#endif

ZTD_EXTERN_C_OPEN_I_

#define ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(_N) \
extern uint_least##_N##_t ztdc_load8_leu##_N(const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) \
ZTD_CXX_NOEXCEPT_I_; \
extern uint_least##_N##_t ztdc_load8_beu##_N(const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) \
ZTD_CXX_NOEXCEPT_I_; \
extern uint_least##_N##_t ztdc_load8_aligned_leu##_N( \
const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern uint_least##_N##_t ztdc_load8_aligned_beu##_N( \
const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern int_least##_N##_t ztdc_load8_les##_N(const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) \
ZTD_CXX_NOEXCEPT_I_; \
extern int_least##_N##_t ztdc_load8_bes##_N(const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) \
ZTD_CXX_NOEXCEPT_I_; \
extern int_least##_N##_t ztdc_load8_aligned_les##_N( \
const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern int_least##_N##_t ztdc_load8_aligned_bes##_N( \
const unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
\
extern void ztdc_store8_leu##_N(const uint_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_beu##_N(const uint_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_aligned_leu##_N(const uint_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_aligned_beu##_N(const uint_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_les##_N(const int_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_bes##_N(const int_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_aligned_les##_N(const int_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
extern void ztdc_store8_aligned_bes##_N(const int_least##_N##_t __value, \
unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(_N / CHAR_BIT)]) ZTD_CXX_NOEXCEPT_I_; \
ztd_static_assert(((_N % 8) == 0), "👍")

#if ((CHAR_BIT % 8) == 0)
#if defined(UINT_LEAST8_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(8);
#endif // 8 bits
#if defined(UINT_LEAST16_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(16);
#endif // 16 bits
#if defined(UINT_LEAST24_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(24);
#endif // 24 bits
#if defined(UINT_LEAST32_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(32);
#endif // 32 bits
#if defined(UINT_LEAST48_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(48);
#endif // 48 bits
#if defined(UINT_LEAST56_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(56);
#endif // 56 bits
#if defined(UINT_LEAST64_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(64);
#endif // 64 bits
#if defined(UINT_LEAST72_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(72);
#endif // 72 bits
#if defined(UINT_LEAST80_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(80);
#endif // 80 bits
#if defined(UINT_LEAST88_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(88);
#endif // 88 bits
#if defined(UINT_LEAST96_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(96);
#endif // 96 bits
#if defined(UINT_LEAST104_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(104);
#endif // 104 bits
#if defined(UINT_LEAST112_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(112);
#endif // 112 bits
#if defined(UINT_LEAST120_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(120);
#endif // 120 bits
#if defined(UINT_LEAST128_MAX)
ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS(128);
#endif // 128 bits
#endif // Only on 8-modulus bit systems

#undef ZTDC_GENERATE_LOAD8_STORE8_DECLARATIONS

#if (CHAR_BIT % 8 == 0)
extern void ztdc_memreverse8(size_t __n, unsigned char __ptr[ZTD_STATIC_PTR_EXTENT_I_(__n)]) ZTD_CXX_NOEXCEPT_I_;
#if defined(UINT8_MAX)
extern uint8_t ztdc_memreverse8u8(uint8_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 8 bits
#if defined(UINT16_MAX)
extern uint16_t ztdc_memreverse8u16(uint16_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 16 bits
#if defined(UINT24_MAX)
extern uint24_t ztdc_memreverse824(uint24_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 24 bits
#if defined(UINT32_MAX)
extern uint32_t ztdc_memreverse8u32(uint32_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 32 bits
#if defined(UINT40_MAX)
extern uint40_t ztdc_memreverse8u40(uint40_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 40 bits
#if defined(UINT48_MAX)
extern uint48_t ztdc_memreverse8u48(uint48_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 48 bits
#if defined(UINT56_MAX)
extern uint56_t ztdc_memreverse8u56(uint56_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 48 bits
#if defined(UINT64_MAX)
extern uint64_t ztdc_memreverse8u64(uint64_t __value) ZTD_CXX_NOEXCEPT_I_;
#endif // 64 bits
#endif

ZTD_EXTERN_C_CLOSE_I_

#endif // ZTD_IDK_BIT_H

0 comments on commit 7da7a78

Please sign in to comment.