Skip to content

Commit

Permalink
hotplace rev.539 study ASN.1
Browse files Browse the repository at this point in the history
  • Loading branch information
princeb612 committed Jun 8, 2024
1 parent 6c4a62d commit 9578c3a
Show file tree
Hide file tree
Showing 38 changed files with 3,257 additions and 952 deletions.
31 changes: 31 additions & 0 deletions sdk/base/basic/ieee754.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,35 @@ uint16 fp16_ieee_from_fp32_value(uint32 x) {
return (x_sgn >> 16) + h_exp + h_sig;
}

ieee754_typeof_t is_typeof(float f) {
ieee754_typeof_t ret = ieee754_typeof_t::is_finite;
uint32 b32 = binary32_from_fp32(f);
if (ieee754_t::fp32_pinf == (b32 & ieee754_t::fp32_pinf)) {
if (b32 & 0x80000000) {
ret = ieee754_typeof_t::is_ninf;
} else if (b32 & ~fp32_ninf) {
ret = ieee754_typeof_t::is_nan;
} else {
ret = ieee754_typeof_t::is_pinf;
}
}
return ret;
}

ieee754_typeof_t is_typeof(double d) {
ieee754_typeof_t ret = ieee754_typeof_t::is_finite;
uint64 b64 = binary64_from_fp64(d);
if (ieee754_t::fp64_pinf == (b64 & ieee754_t::fp64_pinf)) {
uint32 b32 = (b64 >> 32);
if (b32 & 0x80000000) {
ret = ieee754_typeof_t::is_ninf;
} else if (b32 & 0x000fffff) {
ret = ieee754_typeof_t::is_nan;
} else {
ret = ieee754_typeof_t::is_pinf;
}
}
return ret;
}

} // namespace hotplace
9 changes: 9 additions & 0 deletions sdk/base/basic/ieee754.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ float fp32_from_fp16(uint16 half);
*/
uint16 fp16_ieee_from_fp32_value(uint32 single);

enum ieee754_typeof_t {
is_finite = 0,
is_pinf,
is_ninf,
is_nan,
};
ieee754_typeof_t is_typeof(float f);
ieee754_typeof_t is_typeof(double d);

} // namespace hotplace

#endif
38 changes: 38 additions & 0 deletions sdk/base/basic/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,44 @@ namespace hotplace {

variant::variant() {}

variant::variant(const void *value) { set_pointer(value); }

variant::variant(const char *value) { set_str_new(value); }

variant::variant(const char *value, size_t n) { set_strn_new(value, n); }

variant::variant(const unsigned char *value, size_t n) { set_bstr_new(value, n); }

variant::variant(const std::string &rhs) { set_strn_new(rhs.c_str(), rhs.size()); }

variant::variant(const binary_t &rhs) { set_bstr_new(&rhs[0], rhs.size()); }

variant::variant(bool value) { set_bool(value); }

variant::variant(int8 value) { set_int8(value); }

variant::variant(uint8 value) { set_uint8(value); }

variant::variant(int16 value) { set_int16(value); }

variant::variant(uint16 value) { set_uint16(value); }

variant::variant(int32 value) { set_int32(value); }

variant::variant(uint32 value) { set_uint32(value); }

variant::variant(int64 value) { set_int64(value); }

variant::variant(uint64 value) { set_uint64(value); }

variant::variant(int128 value) { set_int128(value); }

variant::variant(uint128 value) { set_uint128(value); }

variant::variant(float value) { set_float(value); }

variant::variant(double value) { set_double(value); }

variant::variant(const variant_t &rhs) : _vt(rhs) {}

variant::variant(variant_t &&rhs) : _vt(std::move(rhs)) {}
Expand Down
19 changes: 19 additions & 0 deletions sdk/base/basic/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ T t_to_int(const variant_t& vt) {
class variant {
public:
variant();
variant(const void* value);
variant(const char* value);
variant(const char* value, size_t n);
variant(const unsigned char* value, size_t n);
variant(const std::string& rhs);
variant(const binary_t& rhs);
variant(bool value);
variant(int8 value);
variant(uint8 value);
variant(int16 value);
variant(uint16 value);
variant(int32 value);
variant(uint32 value);
variant(int64 value);
variant(uint64 value);
variant(int128 value);
variant(uint128 value);
variant(float value);
variant(double value);
variant(const variant_t& rhs);
variant(variant_t&& rhs);
variant(const variant& rhs);
Expand Down
85 changes: 66 additions & 19 deletions sdk/base/nostd/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,41 +87,88 @@ namespace hotplace {
template <typename T = char>
class t_kmp_pattern {
public:
/**
* comparator for pointer type - t_kmp_pattern<object*>
*
* struct object {
* int value;
* friend bool operator==(const object& lhs, const object& rhs) { return lhs.value == rhs.value; }
* }
* auto comparator = [](const object* lhs, const object* rhs) -> bool {
* return (lhs->value == rhs->value);
* };
*
* std::vector<objec*> data1; // 1 2 3 4 5 by new object
* std::vector<objec*> data2; // 3 4 by new object
*
* t_kmp_pattern<object*> search;
* search.match(data1, data2);
* // if (pattern[j] == data[i]) - incorrect
* // return -1
*
* search.match(data1, data2, 0, comparator);
* // if (comparator(pattern[j], data[i])) - correct
* // return 2
*/
typedef typename std::function<bool(const T&, const T&)> comparator_t;

t_kmp_pattern() {}

int match(const T* data, size_t data_size, const T* pattern, size_t pattern_size, int pos = 0) {
int match(const std::vector<T>& data, const std::vector<T>& pattern, unsigned int pos = 0, comparator_t comparator = nullptr) {
return match(&data[0], data.size(), &pattern[0], pattern.size(), pos, comparator);
}

/**
* @brief match
* @return index, -1 (not found)
*/
int match(const T* data, size_t size_data, const T* pattern, size_t size_pattern, unsigned int pos = 0, comparator_t comparator = nullptr) {
int ret = -1;
int n = data_size;
int m = pattern_size;
std::vector<int> fail = failure(pattern, pattern_size);
int i = pos;
int j = 0;
while (i < n) {
if (pattern[j] == data[i]) {
if (j == m - 1) {
ret = i - m + 1;
break;
if (data && pattern && size_pattern) {
unsigned int n = size_data;
unsigned int m = size_pattern;
std::vector<int> fail = failure(pattern, m, comparator);
unsigned int i = pos;
unsigned int j = 0;
while (i < n) {
bool test = false;
if (comparator) {
test = comparator(pattern[j], data[i]);
} else {
test = (pattern[j] == data[i]);
}
if (test) {
if (j == m - 1) {
ret = i - m + 1;
break;
}
i++;
j++;
} else if (j > 0) {
j = fail[j - 1];
} else {
i++;
}
i++;
j++;
} else if (j > 0) {
j = fail[j - 1];
} else {
i++;
}
}
return ret;
}

protected:
std::vector<int> failure(const T* pattern, size_t size) {
std::vector<int> failure(const T* pattern, size_t size, comparator_t comparator = nullptr) {
std::vector<int> fail(size);
fail[0] = 0;
size_t m = size;
size_t j = 0;
size_t i = 1;
while (i < m) {
if (pattern[j] == pattern[i]) {
bool test = false;
if (comparator) {
test = comparator(pattern[j], pattern[i]);
} else {
test = (pattern[j] == pattern[i]);
}
if (test) {
fail[i] = j + 1;
i++;
j++;
Expand Down
63 changes: 38 additions & 25 deletions sdk/base/stream/printf_charset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* 2018.06.15 Soo Han, Kim printf %zi, %zu, %zd (codename.grape)
* 2020.02.06 Soo Han, Kim printf %I128i, %I128u (codename.unicorn)
* 2021.06.29 Soo Han, Kim printf unicode (codename.unicorn)
* 2024.06.07 Soo Han, Kim inf, -inf, nan (codename.hotplace)
*
* printf license
* Copyright (c) 1990 Regents of the University of California.
Expand All @@ -38,6 +39,7 @@
#include <stdio.h>
#include <string.h>

#include <sdk/base/basic/ieee754.hpp>
#include <sdk/base/stream/printf.hpp>

namespace hotplace {
Expand Down Expand Up @@ -422,6 +424,8 @@ int vprintf_runtimew(void *context, CALLBACK_PRINTFW runtime_printf, const wchar
0,
}; /* space for 0x hex-prefix */

ieee754_typeof_t ieee754_type = ieee754_typeof_t::is_finite;

if (!runtime_printf) {
goto error;
}
Expand Down Expand Up @@ -596,32 +600,41 @@ int vprintf_runtimew(void *context, CALLBACK_PRINTFW runtime_printf, const wchar
case _T('g'):
case _T('G'):
_double = va_arg(ap, double);
/*
* don't do unrealistic precision; just pad it with
* zeroes later, so buffer size stays rational.
*/
if (prec > MAXFRACT) {
if ((ch != _T('g') && ch != _T('G')) || (flags & ALT)) {
fpprec = prec - MAXFRACT;
ieee754_type = is_typeof(_double);
if (ieee754_typeof_t::is_pinf == ieee754_type) {
PRINT(_T("inf"), (sizeof(TCHAR) * 3));
} else if (ieee754_typeof_t::is_ninf == ieee754_type) {
PRINT(_T("-inf"), (sizeof(TCHAR) * 4));
} else if (ieee754_typeof_t::is_nan == ieee754_type) {
PRINT(_T("nan"), (sizeof(TCHAR) * 3));
} else {
/*
* don't do unrealistic precision; just pad it with
* zeroes later, so buffer size stays rational.
*/
if (prec > MAXFRACT) {
if ((ch != _T('g') && ch != _T('G')) || (flags & ALT)) {
fpprec = prec - MAXFRACT;
}
prec = MAXFRACT;
} else if (prec == -1) {
prec = DEFPREC;
}
/* __cvt_double may have to stdround up before the
"start" of its buffer, i.e.
``intf("%.2f", (double)9.999);'';
if the first character is still NUL, it did.
softsign avoids negative 0 if _double < 0 but
no significant digits will be shown. */
cp = buf;
*cp = _T('\0');
size = __cvt_double(_double, prec, flags, &softsign, ch, cp, buf + sizeof(buf));
if (softsign) {
sign = _T('-');
}
if (*cp == _T('\0')) {
cp++;
}
prec = MAXFRACT;
} else if (prec == -1) {
prec = DEFPREC;
}
/* __cvt_double may have to stdround up before the
"start" of its buffer, i.e.
``intf("%.2f", (double)9.999);'';
if the first character is still NUL, it did.
softsign avoids negative 0 if _double < 0 but
no significant digits will be shown. */
cp = buf;
*cp = _T('\0');
size = __cvt_double(_double, prec, flags, &softsign, ch, cp, buf + sizeof(buf));
if (softsign) {
sign = _T('-');
}
if (*cp == _T('\0')) {
cp++;
}
break;

Expand Down
5 changes: 5 additions & 0 deletions sdk/base/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ enum encoding_t {
#define RTL_NUMBER_OF(x) (sizeof(x) / sizeof(x[0]))
#define RTL_FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
#define FIELD_OFFSET(type, field) ((int32)(arch_t) & (((type *)0)->field))
#define RTL_SIZEOF_THROUGH_FIELD(type, field) (FIELD_OFFSET(type, field) + RTL_FIELD_SIZE(type, field))
#define RTL_NUMBER_OF_FIELD(type, field) (RTL_NUMBER_OF(RTL_FIELD_TYPE(type, field)))
#define RTL_PADDING_BETWEEN_FIELDS(type, field1, field2) \
((FIELD_OFFSET(type, field2) > FIELD_OFFSET(type, field1)) ? (FIELD_OFFSET(type, field2) - FIELD_OFFSET(type, field1) - RTL_FIELD_SIZE(type, field1)) \
: (FIELD_OFFSET(type, field1) - FIELD_OFFSET(type, field2) - RTL_FIELD_SIZE(type, field2)))
#endif

#define adjust_range(var, minimum, maximum) \
Expand Down
14 changes: 14 additions & 0 deletions sdk/base/unittest/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ logger& logger::dump(const char* addr, size_t size, unsigned hexpart, unsigned i

logger& logger::dump(const binary_t& msg, unsigned hexpart, unsigned indent) { return do_dump(&msg[0], msg.size(), hexpart, indent, true); }

logger& logger::dump(const std::string& msg, unsigned hexpart, unsigned indent) { return do_dump((byte_t*)msg.c_str(), msg.size(), hexpart, indent, true); }

logger& logger::dump(const basic_stream& msg, unsigned hexpart, unsigned indent) { return do_dump(msg.data(), msg.size(), hexpart, indent, true); }

logger& logger::hdump(const std::string& header, const byte_t* addr, size_t size, unsigned hexpart, unsigned indent) {
return do_hdump(header, addr, size, hexpart, indent, true);
}
Expand All @@ -192,6 +196,16 @@ logger& logger::hdump(const std::string& header, const binary_t& msg, unsigned h
return do_hdump(header, &msg[0], msg.size(), hexpart, indent, true);
}

logger& logger::hdump(const std::string& header, const std::string& msg, unsigned hexpart, unsigned indent) {
return do_hdump(header, (byte_t*)msg.c_str(), msg.size(), hexpart, indent, true);
}

logger& logger::hdump(const std::string& header, const basic_stream& msg, unsigned hexpart, unsigned indent) {
return do_hdump(header, msg.data(), msg.size(), hexpart, indent, true);
}

logger& logger::operator<<(const char* msg) { return do_write_raw(msg, msg ? strlen(msg) : 0, false); }

logger& logger::operator<<(const std::string& msg) { return do_write_raw(msg.c_str(), msg.size(), false); }

logger& logger::operator<<(const basic_stream& msg) { return do_write_raw(msg.c_str(), msg.size(), false); }
Expand Down
5 changes: 5 additions & 0 deletions sdk/base/unittest/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ class logger {
logger& dump(const byte_t* addr, size_t size, unsigned hexpart = 16, unsigned indent = 0);
logger& dump(const char* addr, size_t size, unsigned hexpart = 16, unsigned indent = 0);
logger& dump(const binary_t& msg, unsigned hexpart = 16, unsigned indent = 0);
logger& dump(const std::string& msg, unsigned hexpart = 16, unsigned indent = 0);
logger& dump(const basic_stream& msg, unsigned hexpart = 16, unsigned indent = 0);

logger& hdump(const std::string& header, const byte_t* addr, size_t size, unsigned hexpart = 16, unsigned indent = 0);
logger& hdump(const std::string& header, const char* addr, size_t size, unsigned hexpart = 16, unsigned indent = 0);
logger& hdump(const std::string& header, const binary_t& msg, unsigned hexpart = 16, unsigned indent = 0);
logger& hdump(const std::string& header, const std::string& msg, unsigned hexpart = 16, unsigned indent = 0);
logger& hdump(const std::string& header, const basic_stream& msg, unsigned hexpart = 16, unsigned indent = 0);

logger& operator<<(const char* msg);
logger& operator<<(const std::string& msg);
logger& operator<<(const basic_stream& msg);

Expand Down
1 change: 1 addition & 0 deletions sdk/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* basic */
#include <sdk/io/basic/json.hpp>
#include <sdk/io/basic/mlfq.hpp>
#include <sdk/io/basic/parser.hpp>
#include <sdk/io/basic/payload.hpp>
#include <sdk/io/basic/sdk.hpp>
#include <sdk/io/basic/zlib.hpp>
Expand Down
Loading

0 comments on commit 9578c3a

Please sign in to comment.