From 45c17f712b6a0424970bcac3405efca824615dcd Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 5 Sep 2025 18:14:40 +0200 Subject: feat: `lib/simd`: alternative SIMD library — a work in progress library to manipulate SIMD data. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Amlal El Mahrouss --- dev/examples/fix/fix.cc | 2 +- dev/lib/fix/fix.hpp | 216 ++++++++++++++++++++++++++++++++++++++++ dev/lib/fix/parser.hpp | 216 ---------------------------------------- dev/lib/net/url.hpp | 8 ++ dev/lib/simd/simd.hpp | 18 ++++ dev/tests/fix_basic/fix_test.cc | 4 +- 6 files changed, 245 insertions(+), 219 deletions(-) create mode 100644 dev/lib/fix/fix.hpp delete mode 100644 dev/lib/fix/parser.hpp create mode 100644 dev/lib/simd/simd.hpp diff --git a/dev/examples/fix/fix.cc b/dev/examples/fix/fix.cc index e3d669b..ec6668a 100644 --- a/dev/examples/fix/fix.cc +++ b/dev/examples/fix/fix.cc @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include #include diff --git a/dev/lib/fix/fix.hpp b/dev/lib/fix/fix.hpp new file mode 100644 index 0000000..723506e --- /dev/null +++ b/dev/lib/fix/fix.hpp @@ -0,0 +1,216 @@ +/* + * File: fix/parser.hpp + * Purpose: Financial Information Exchange parser in C++ + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss + */ + +#ifndef _OCL_FIX_PARSER_HPP +#define _OCL_FIX_PARSER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ocl::fix +{ + template + class basic_visitor; + + template + struct basic_range; + + template + class basic_range_data; + + /// @brief Buffer+Length structure + template + using range_ptr_t = basic_range; + + namespace detail + { + template + const char_type* begin_fix(); + + template <> + inline const char* begin_fix() + { + return "FIX."; + } + + template <> + inline const char16_t* begin_fix() + { + return u"FIX."; + } + + template <> + inline const char8_t* begin_fix() + { + return u8"FIX."; + } + } // namespace detail + + template + struct basic_range final + { + char_type* bytes_{nullptr}; + size_t length_{}; + + bool is_valid() noexcept + { + return this->bytes_ && this->length_ > 0; + } + + explicit operator bool() + { + return this->is_valid(); + } + }; + + /// @brief Convert basic_range to usable string. + /// @note This function assumes that the basic_range is valid and contains ASCII bytes. + template + inline std::basic_string to_string(basic_range& basic_range) noexcept + { + if (basic_range.length_ < 0) + return std::basic_string{}; + + return std::basic_string(basic_range.ascii_bytes_, basic_range.length_); + } + + /// @brief a basic_range object containing the FIX packet values. + template + class basic_range_data final + { + public: + std::size_t magic_len_; + std::basic_string magic_; + std::size_t body_len_; + std::vector, std::basic_string>> body_; + + static inline const char_type* begin = detail::begin_fix(); + + explicit basic_range_data() = default; + ~basic_range_data() = default; + + basic_range_data& operator=(const basic_range_data&) = default; + basic_range_data(const basic_range_data&) = default; + + std::basic_string operator[](const std::basic_string& key) + { + if (key.empty()) + { + return std::basic_string{}; + } + + for (const auto& pair : this->body_) + { + if (pair.first == key) + { + return pair.second; + } + } + + return std::basic_string{}; + } + + bool is_valid() + { + return magic_.starts_with(basic_range_data::begin); + } + + explicit operator bool() + { + return this->is_valid(); + } + }; + + /// @brief basic_visitor object which returns a fix::basic_range_data instance. + template + class basic_visitor final + { + public: + static constexpr const char_type soh = '|'; + static constexpr const char_type eq = '='; + static constexpr uint32_t base = 10U; + + explicit basic_visitor() = default; + ~basic_visitor() = default; + + basic_visitor& operator=(const basic_visitor&) = default; + basic_visitor(const basic_visitor&) = default; + + basic_range operator()(const std::basic_string& in) + { + return this->visit(in); + } + + basic_range_data visit(const std::basic_string& in) + { + thread_local basic_range_data ret{}; + + if (in.empty()) + return ret; + + static thread_local std::basic_string in_tmp{}; + + in_tmp.reserve(in.size()); + + try + { + for (auto& ch : in) + { + if (ch != basic_visitor::soh) + { + in_tmp += ch; + continue; + } + + std::basic_string key = in_tmp.substr(0, in_tmp.find(basic_visitor::eq)); + std::basic_string val = in_tmp.substr(in_tmp.find(basic_visitor::eq) + 1); + + if (ret.magic_.empty()) + { + ret.magic_ = val; + ret.magic_len_ = ret.magic_.size(); + } + else + { + ret.body_.emplace_back(std::make_pair(key, val)); + ret.body_len_ += in_tmp.size(); + } + + in_tmp.clear(); + } + } + catch (...) + { + in_tmp.clear(); + return ret; + } + + in_tmp.clear(); + return ret; + } + }; + + template + inline void must_pass(basic_range_data& basic_range, error_handler& handler) + { + if (!basic_range.is_valid()) + { + handler("Invalid FIX packet"); + } + } + + using fix_tag_type = std::uint32_t; +} // namespace ocl::fix + +#endif // ifndef _OCL_FIX_PARSER_HPP diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp deleted file mode 100644 index 723506e..0000000 --- a/dev/lib/fix/parser.hpp +++ /dev/null @@ -1,216 +0,0 @@ -/* - * File: fix/parser.hpp - * Purpose: Financial Information Exchange parser in C++ - * Author: Amlal El Mahrouss (amlal@nekernel.org) - * Copyright 2025, Amlal El Mahrouss - */ - -#ifndef _OCL_FIX_PARSER_HPP -#define _OCL_FIX_PARSER_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ocl::fix -{ - template - class basic_visitor; - - template - struct basic_range; - - template - class basic_range_data; - - /// @brief Buffer+Length structure - template - using range_ptr_t = basic_range; - - namespace detail - { - template - const char_type* begin_fix(); - - template <> - inline const char* begin_fix() - { - return "FIX."; - } - - template <> - inline const char16_t* begin_fix() - { - return u"FIX."; - } - - template <> - inline const char8_t* begin_fix() - { - return u8"FIX."; - } - } // namespace detail - - template - struct basic_range final - { - char_type* bytes_{nullptr}; - size_t length_{}; - - bool is_valid() noexcept - { - return this->bytes_ && this->length_ > 0; - } - - explicit operator bool() - { - return this->is_valid(); - } - }; - - /// @brief Convert basic_range to usable string. - /// @note This function assumes that the basic_range is valid and contains ASCII bytes. - template - inline std::basic_string to_string(basic_range& basic_range) noexcept - { - if (basic_range.length_ < 0) - return std::basic_string{}; - - return std::basic_string(basic_range.ascii_bytes_, basic_range.length_); - } - - /// @brief a basic_range object containing the FIX packet values. - template - class basic_range_data final - { - public: - std::size_t magic_len_; - std::basic_string magic_; - std::size_t body_len_; - std::vector, std::basic_string>> body_; - - static inline const char_type* begin = detail::begin_fix(); - - explicit basic_range_data() = default; - ~basic_range_data() = default; - - basic_range_data& operator=(const basic_range_data&) = default; - basic_range_data(const basic_range_data&) = default; - - std::basic_string operator[](const std::basic_string& key) - { - if (key.empty()) - { - return std::basic_string{}; - } - - for (const auto& pair : this->body_) - { - if (pair.first == key) - { - return pair.second; - } - } - - return std::basic_string{}; - } - - bool is_valid() - { - return magic_.starts_with(basic_range_data::begin); - } - - explicit operator bool() - { - return this->is_valid(); - } - }; - - /// @brief basic_visitor object which returns a fix::basic_range_data instance. - template - class basic_visitor final - { - public: - static constexpr const char_type soh = '|'; - static constexpr const char_type eq = '='; - static constexpr uint32_t base = 10U; - - explicit basic_visitor() = default; - ~basic_visitor() = default; - - basic_visitor& operator=(const basic_visitor&) = default; - basic_visitor(const basic_visitor&) = default; - - basic_range operator()(const std::basic_string& in) - { - return this->visit(in); - } - - basic_range_data visit(const std::basic_string& in) - { - thread_local basic_range_data ret{}; - - if (in.empty()) - return ret; - - static thread_local std::basic_string in_tmp{}; - - in_tmp.reserve(in.size()); - - try - { - for (auto& ch : in) - { - if (ch != basic_visitor::soh) - { - in_tmp += ch; - continue; - } - - std::basic_string key = in_tmp.substr(0, in_tmp.find(basic_visitor::eq)); - std::basic_string val = in_tmp.substr(in_tmp.find(basic_visitor::eq) + 1); - - if (ret.magic_.empty()) - { - ret.magic_ = val; - ret.magic_len_ = ret.magic_.size(); - } - else - { - ret.body_.emplace_back(std::make_pair(key, val)); - ret.body_len_ += in_tmp.size(); - } - - in_tmp.clear(); - } - } - catch (...) - { - in_tmp.clear(); - return ret; - } - - in_tmp.clear(); - return ret; - } - }; - - template - inline void must_pass(basic_range_data& basic_range, error_handler& handler) - { - if (!basic_range.is_valid()) - { - handler("Invalid FIX packet"); - } - } - - using fix_tag_type = std::uint32_t; -} // namespace ocl::fix - -#endif // ifndef _OCL_FIX_PARSER_HPP diff --git a/dev/lib/net/url.hpp b/dev/lib/net/url.hpp index ff6aebe..7263a52 100644 --- a/dev/lib/net/url.hpp +++ b/dev/lib/net/url.hpp @@ -61,4 +61,12 @@ namespace ocl::net return ss_.size() > 0; } }; + + enum class protocol + { + http, + https, + mailto, + bad + }; } // namespace ocl::net diff --git a/dev/lib/simd/simd.hpp b/dev/lib/simd/simd.hpp new file mode 100644 index 0000000..6374a51 --- /dev/null +++ b/dev/lib/simd/simd.hpp @@ -0,0 +1,18 @@ +/* + * File: simd/simd.hpp + * Purpose: SIMD C++ library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss, and SNU Systems, Corp, licensed under the BSL 1.0 license. + */ + +#pragma once + +#include + +namespace ocl::snu::simd +{ + struct simd_backend; + + template + struct basic_simd; +} diff --git a/dev/tests/fix_basic/fix_test.cc b/dev/tests/fix_basic/fix_test.cc index bdde392..1c80716 100644 --- a/dev/tests/fix_basic/fix_test.cc +++ b/dev/tests/fix_basic/fix_test.cc @@ -5,7 +5,7 @@ * Copyright 2025, Amlal El Mahrouss */ -#include +#include #include TEST(FIXTest, BasicFIXUsage) @@ -15,4 +15,4 @@ TEST(FIXTest, BasicFIXUsage) EXPECT_EQ(fix.magic_, ocl::fix::detail::begin_fix()); EXPECT_TRUE(fix.is_valid()); -} \ No newline at end of file +} -- cgit v1.2.3 From 2ecbd2bfb27060b6f45196d8481fd0ccdbea730c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 8 Sep 2025 23:18:52 +0200 Subject: feat: wip: `simd` module in progress. Signed-off-by: Amlal El Mahrouss --- dev/lib/simd/avx.hpp | 0 dev/lib/simd/simd.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 dev/lib/simd/avx.hpp diff --git a/dev/lib/simd/avx.hpp b/dev/lib/simd/avx.hpp new file mode 100644 index 0000000..e69de29 diff --git a/dev/lib/simd/simd.hpp b/dev/lib/simd/simd.hpp index 6374a51..0f8f451 100644 --- a/dev/lib/simd/simd.hpp +++ b/dev/lib/simd/simd.hpp @@ -9,10 +9,51 @@ #include +/// @author Amlal El Mahrouss +/// @brief Basic SIMD processor. + namespace ocl::snu::simd { - struct simd_backend; + template + class basic_simd_processor + { + private: + SimdBackend processor_; + + enum opcode + { + bad, + add, + mul, + div, + }; + + public: + basic_simd_processor() = default; + virtual ~basic_simd_processor() = default; + + basic_simd_processor& operator=(const basic_simd_processor&) = delete; + basic_simd_processor(const basic_simd_processor&) = delete; + + typename SimdBackend::Register& call(const opcode& op, typename SimdBackend::Register& lhs, typename SimdBackend::Register& rhs) + { + switch (op) + { + case add: + return processor_.add(lhs, rhs); + case mul: + return processor_.mul(lhs, rhs); + case div: + return processor_.div(lhs, rhs); + default: + break; + } + return SimdBackend::Register::bad; + } - template - struct basic_simd; -} + std::basic_string isa() + { + return processor_.isa(); + } + }; +} // namespace ocl::snu::simd -- cgit v1.2.3 From 3f9c5017abf3c4279780f685580a334574e6d760 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 10 Sep 2025 03:28:35 +0200 Subject: feat:! breaking changes in the `opt` module. Fix `println` in `io` module. Signed-off-by: Amlal El Mahrouss --- README.md | 10 +++++----- dev/examples/opt/opt.cc | 2 +- dev/lib/io/print.hpp | 1 + dev/lib/logic/opt.hpp | 17 +++++++++++++++-- dev/lib/simd/avx.hpp | 21 +++++++++++++++++++++ 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b7790c3..0e876d7 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# Open C++ Library +# SIMD C++ Library [![License: GPL-2.0](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) ## Brief: -A C++ library with additional modules for your C++ SDLC. +A C++ library with additional modules for your C++ SDLC. Based on the Open C++ Library. ## Requirements: @@ -21,10 +21,10 @@ A C++ library with additional modules for your C++ SDLC. int main(int argc, char** argv) { - auto opt = ocl::opt(ocl::eval_eq(50, 50)).expect("ocl::eval_eq, does not match!"); + auto opt = ocl::opt(ocl::eval_eq(50, 50)).try_or_throw("ocl::eval_eq, does not match!"); opt = ocl::opt(ocl::eval_eq(50, 40)); - opt.expect("this time it doesn't."); - + opt.try_or_throw("this time it doesn't."); + return EXIT_SUCCESS; } ``` diff --git a/dev/examples/opt/opt.cc b/dev/examples/opt/opt.cc index b34f2c7..8a74fa2 100644 --- a/dev/examples/opt/opt.cc +++ b/dev/examples/opt/opt.cc @@ -38,7 +38,7 @@ int main(int argc, char** argv) ocl::io::println("Testing data..."); auto opt = do_some("Ohio", "Ohio"); - opt.expect("Checksum failed, Ohio isn't Ohio!"); + opt.try_or_throw("Checksum failed, Ohio isn't Ohio!"); return 0; diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index 4b32ddb..40ae1bb 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -35,6 +35,7 @@ namespace ocl::io inline void println(T... fmt) noexcept { print(fmt...); + print(); } } // namespace ocl::io diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp index 442756c..137460c 100644 --- a/dev/lib/logic/opt.hpp +++ b/dev/lib/logic/opt.hpp @@ -20,6 +20,7 @@ namespace ocl count = err - okay + 1, }; + template struct opt final { explicit opt(const return_type& return_type) @@ -27,11 +28,23 @@ namespace ocl { } - opt& expect(const char* input) + template + opt& try_or_handle(const char_type* input) { if (m_ret == return_type::err) { - throw std::runtime_error(input); + ErrorHandler handler; + handler(input ? input : ""); + } + + return *this; + } + + opt& try_or_throw(const char_type* input) + { + if (m_ret == return_type::err) + { + throw std::runtime_error(input ? input : ""); } return *this; diff --git a/dev/lib/simd/avx.hpp b/dev/lib/simd/avx.hpp index e69de29..16e495e 100644 --- a/dev/lib/simd/avx.hpp +++ b/dev/lib/simd/avx.hpp @@ -0,0 +1,21 @@ +/* + * File: simd/avx.hpp + * Purpose: AVX C++ library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss, and SNU Systems, Corp, licensed under the BSL 1.0 license. + */ + +#pragma once + +#include + +namespace ocl::snu::simd +{ + struct avx_256_backend final + { + std::basic_string isa() + { + return "AVX-256"; + } + }; +} // namespace ocl::snu::simd -- cgit v1.2.3 From fa4748e414e9494442f9bcde9c659d3951af19c0 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 12 Sep 2025 20:23:39 +0200 Subject: feat: dev/lib: new `basic_simd` class, better `error_handler` class. Signed-off-by: Amlal El Mahrouss --- dev/lib/core/error_handler.hpp | 4 +++- dev/lib/io/print.hpp | 2 +- dev/lib/simd/avx.hpp | 21 ------------------ dev/lib/simd/basic_simd.hpp | 50 ++++++++++++++++++++++++++++++++++++++++++ dev/lib/simd/simd.hpp | 9 ++++---- 5 files changed, 59 insertions(+), 27 deletions(-) delete mode 100644 dev/lib/simd/avx.hpp create mode 100644 dev/lib/simd/basic_simd.hpp diff --git a/dev/lib/core/error_handler.hpp b/dev/lib/core/error_handler.hpp index 593e54a..1a1515d 100644 --- a/dev/lib/core/error_handler.hpp +++ b/dev/lib/core/error_handler.hpp @@ -9,6 +9,7 @@ #define _OCL_ERROR_HANDLER_HPP #include +#include #include namespace ocl @@ -25,11 +26,12 @@ namespace ocl virtual void operator()(const std::basic_string& msg) { - ((void)msg); + ocl::io::print(msg); } }; using standard_error_handler = basic_error_handler; + using error_handler_type = basic_error_handler; } // namespace ocl #endif // ifndef _OCL_ERROR_HANDLER_HPP diff --git a/dev/lib/io/print.hpp b/dev/lib/io/print.hpp index 40ae1bb..307c06f 100644 --- a/dev/lib/io/print.hpp +++ b/dev/lib/io/print.hpp @@ -35,7 +35,7 @@ namespace ocl::io inline void println(T... fmt) noexcept { print(fmt...); - print(); + print(); } } // namespace ocl::io diff --git a/dev/lib/simd/avx.hpp b/dev/lib/simd/avx.hpp deleted file mode 100644 index 16e495e..0000000 --- a/dev/lib/simd/avx.hpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * File: simd/avx.hpp - * Purpose: AVX C++ library. - * Author: Amlal El Mahrouss (founder@snu.systems) - * Copyright 2025, Amlal El Mahrouss, and SNU Systems, Corp, licensed under the BSL 1.0 license. - */ - -#pragma once - -#include - -namespace ocl::snu::simd -{ - struct avx_256_backend final - { - std::basic_string isa() - { - return "AVX-256"; - } - }; -} // namespace ocl::snu::simd diff --git a/dev/lib/simd/basic_simd.hpp b/dev/lib/simd/basic_simd.hpp new file mode 100644 index 0000000..d748d0c --- /dev/null +++ b/dev/lib/simd/basic_simd.hpp @@ -0,0 +1,50 @@ +/* + * File: simd/basic_simd.hpp + * Purpose: Basic SIMD backend C++ library. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss, and SNU Systems, Corp, licensed under the BSL 1.0 license. + */ + +#pragma once + +#include +#include + +#ifdef __x86_64__ +#include + +using simd_type = __m256; +#endif + +#ifdef __aarch64__ +#include + +using simd_type = float32x4_t; +#endif + +namespace ocl::snu::simd +{ + struct basic_simd_backend final + { + struct simd_traits final + { + simd_type __val; + + private: + static bool bad; + friend class basic_simd_backend; + }; + + using register_type = simd_traits; + + const bool& is_bad() noexcept + { + return register_type::bad; + } + + std::basic_string isa() + { + return "basic-backend"; + } + }; +} // namespace ocl::snu::simd diff --git a/dev/lib/simd/simd.hpp b/dev/lib/simd/simd.hpp index 0f8f451..779d020 100644 --- a/dev/lib/simd/simd.hpp +++ b/dev/lib/simd/simd.hpp @@ -14,11 +14,11 @@ namespace ocl::snu::simd { - template + template class basic_simd_processor { private: - SimdBackend processor_; + backend_type processor_; enum opcode { @@ -35,7 +35,7 @@ namespace ocl::snu::simd basic_simd_processor& operator=(const basic_simd_processor&) = delete; basic_simd_processor(const basic_simd_processor&) = delete; - typename SimdBackend::Register& call(const opcode& op, typename SimdBackend::Register& lhs, typename SimdBackend::Register& rhs) + typename backend_type::register_type& call(const opcode& op, typename backend_type::register_type& lhs, typename backend_type::register_type& rhs) { switch (op) { @@ -48,7 +48,8 @@ namespace ocl::snu::simd default: break; } - return SimdBackend::Register::bad; + + return processor_.is_bad(); } std::basic_string isa() -- cgit v1.2.3