From 5f92807fdd50387cff8ed6c1fe0c1b558e99b640 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 2 Sep 2024 21:25:00 +0300 Subject: [PATCH] userspace: Allow building without SSE I had added changes that had broken compilation without sse support --- userspace/libraries/CMakeLists.txt | 7 ++++++- userspace/libraries/LibC/math.cpp | 2 ++ userspace/libraries/LibC/scanf_impl.cpp | 4 ++++ userspace/libraries/LibC/stdlib.cpp | 6 ++++++ userspace/libraries/LibImage/Image.cpp | 18 ++++++++++++++---- .../LibImage/include/LibImage/Image.h | 2 +- userspace/programs/CMakeLists.txt | 5 +++++ userspace/programs/Shell/main.cpp | 2 ++ userspace/tests/CMakeLists.txt | 5 +++++ 9 files changed, 45 insertions(+), 6 deletions(-) diff --git a/userspace/libraries/CMakeLists.txt b/userspace/libraries/CMakeLists.txt index fa2169f6..3cb90d6d 100644 --- a/userspace/libraries/CMakeLists.txt +++ b/userspace/libraries/CMakeLists.txt @@ -15,9 +15,14 @@ add_custom_target(libraries) foreach(library ${USERSPACE_LIBRARIES}) string(TOLOWER ${library} library_lower) - if (TARGET ${library_lower}) + if (TARGET ${library_lower} AND NOT ${library_lower} STREQUAL "libc") add_dependencies(libraries ${library_lower}) # This is to allow cmake to link when libc updates target_link_options(${library_lower} PRIVATE -nolibc) + + target_compile_definitions(${library_lower} PRIVATE __enable_sse=${BANAN_ENABLE_SSE}) + if (NOT BANAN_ENABLE_SSE) + target_compile_options(${library_lower} PRIVATE -mno-sse -mno-sse2) + endif () endif() endforeach() diff --git a/userspace/libraries/LibC/math.cpp b/userspace/libraries/LibC/math.cpp index 04435c6b..b9053042 100644 --- a/userspace/libraries/LibC/math.cpp +++ b/userspace/libraries/LibC/math.cpp @@ -17,6 +17,7 @@ __BEGIN_DECLS +#if __enable_sse BUILTINS1(acos) BUILTINS1(acosh) BUILTINS1(asin) @@ -65,5 +66,6 @@ BUILTINS1(tgamma) BUILTINS1(trunc) BUILTINS1(y0) BUILTINS1(y1) +#endif __END_DECLS diff --git a/userspace/libraries/LibC/scanf_impl.cpp b/userspace/libraries/LibC/scanf_impl.cpp index 6a50e211..0a3afb9e 100644 --- a/userspace/libraries/LibC/scanf_impl.cpp +++ b/userspace/libraries/LibC/scanf_impl.cpp @@ -262,6 +262,7 @@ int scanf_impl(const char* format, va_list arguments, int (*__getc_fun)(void*), } }; +#if __enable_sse auto parse_floating_point_internal = [&parse_integer_internal, &get_input, &in](BASE_TYPE, bool negative, int width, T* out, bool require_start = true) -> ConversionResult { @@ -436,6 +437,7 @@ int scanf_impl(const char* format, va_list arguments, int (*__getc_fun)(void*), return ConversionResult::MATCH_FAILURE; } }; +#endif auto parse_string = [&arguments, &get_input, &in](uint8_t* mask, bool exclude, bool suppress, bool allocate, int min_len, int max_len, bool terminate) -> ConversionResult @@ -520,9 +522,11 @@ int scanf_impl(const char* format, va_list arguments, int (*__getc_fun)(void*), case 'x': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED {}, conversion.suppress, conversion.field_width, conversion.length); break; case 'X': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED {}, conversion.suppress, conversion.field_width, conversion.length); break; case 'p': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED {}, conversion.suppress, conversion.field_width, LengthModifier::j); break; +#if __enable_sse case 'a': case 'e': case 'f': case 'g': result = parse_floating_point(conversion.suppress, conversion.field_width, conversion.length); break; +#endif case 'S': conversion.length = LengthModifier::l; // fall through diff --git a/userspace/libraries/LibC/stdlib.cpp b/userspace/libraries/LibC/stdlib.cpp index 3ed29bd6..3be7c717 100644 --- a/userspace/libraries/LibC/stdlib.cpp +++ b/userspace/libraries/LibC/stdlib.cpp @@ -158,6 +158,7 @@ static T strtoT(const char* str, char** endp, int base, int& error) return result; } +#if __enable_sse template static T strtoT(const char* str, char** endp, int& error) { @@ -303,11 +304,14 @@ static T strtoT(const char* str, char** endp, int& error) result *= BAN::Math::pow((base == 10) ? 10 : 2, exponent); return result; } +#endif +#if __enable_sse double atof(const char* str) { return strtod(str, nullptr); } +#endif int atoi(const char* str) { @@ -324,6 +328,7 @@ long long atoll(const char* str) return strtoll(str, nullptr, 10); } +#if __enable_sse float strtof(const char* __restrict str, char** __restrict endp) { return strtoT(str, endp, errno); @@ -338,6 +343,7 @@ long double strtold(const char* __restrict str, char** __restrict endp) { return strtoT(str, endp, errno); } +#endif long strtol(const char* __restrict str, char** __restrict endp, int base) { diff --git a/userspace/libraries/LibImage/Image.cpp b/userspace/libraries/LibImage/Image.cpp index 58e41fbe..fe37af06 100644 --- a/userspace/libraries/LibImage/Image.cpp +++ b/userspace/libraries/LibImage/Image.cpp @@ -87,10 +87,10 @@ namespace LibImage constexpr Image::Color as_color() const { return Image::Color { - .r = static_cast(BAN::Math::clamp(r, 0.0, 255.0)), - .g = static_cast(BAN::Math::clamp(g, 0.0, 255.0)), - .b = static_cast(BAN::Math::clamp(b, 0.0, 255.0)), - .a = static_cast(BAN::Math::clamp(a, 0.0, 255.0)), + .r = static_cast(r < 0.0 ? 0.0 : r > 255.0 ? 255.0 : r), + .g = static_cast(g < 0.0 ? 0.0 : g > 255.0 ? 255.0 : g), + .b = static_cast(b < 0.0 ? 0.0 : b > 255.0 ? 255.0 : b), + .a = static_cast(a < 0.0 ? 0.0 : a > 255.0 ? 255.0 : a), }; } }; @@ -133,8 +133,13 @@ namespace LibImage { const double src_x = x * ratio_x; const double src_y = y * ratio_y; +#if __enable_sse const double weight_x = src_x - floor(src_x); const double weight_y = src_y - floor(src_y); +#else + const double weight_x = src_x - (uint64_t)src_x; + const double weight_y = src_y - (uint64_t)src_y; +#endif const Color avg_t = Color::average( get_clamped_color(src_x + 0.0, src_y), @@ -173,8 +178,13 @@ namespace LibImage { const double src_x = x * ratio_x; const double src_y = y * ratio_y; +#if __enable_sse const double weight_x = src_x - floor(src_x); const double weight_y = src_y - floor(src_y); +#else + const double weight_x = src_x - (uint64_t)src_x; + const double weight_y = src_y - (uint64_t)src_y; +#endif FloatingColor values[4]; for (int64_t m = -1; m <= 2; m++) diff --git a/userspace/libraries/LibImage/include/LibImage/Image.h b/userspace/libraries/LibImage/include/LibImage/Image.h index bb097ed0..02b2e38f 100644 --- a/userspace/libraries/LibImage/include/LibImage/Image.h +++ b/userspace/libraries/LibImage/include/LibImage/Image.h @@ -22,7 +22,7 @@ namespace LibImage // weight of 0.0 returns a and weight of 1.0 returns b static Color average(Color a, Color b, double weight) { - const double b_mult = BAN::Math::clamp(weight, 0.0, 1.0); + const double b_mult = weight < 0.0 ? 0.0 : weight > 1.0 ? 1.0 : weight; const double a_mult = 1.0 - b_mult; return Color { .r = static_cast(a.r * a_mult + b.r * b_mult), diff --git a/userspace/programs/CMakeLists.txt b/userspace/programs/CMakeLists.txt index a368f5d9..4aa04fec 100644 --- a/userspace/programs/CMakeLists.txt +++ b/userspace/programs/CMakeLists.txt @@ -43,4 +43,9 @@ foreach(project ${USERSPACE_PROGRAMS}) target_link_options(${project} PRIVATE -nolibc) # Default compile options target_compile_options(${project} PRIVATE -g -O2) + + target_compile_definitions(${project} PRIVATE __enable_sse=${BANAN_ENABLE_SSE}) + if (NOT BANAN_ENABLE_SSE) + target_compile_options(${project} PRIVATE -mno-sse -mno-sse2) + endif () endforeach() diff --git a/userspace/programs/Shell/main.cpp b/userspace/programs/Shell/main.cpp index 3e5c0615..a2df0763 100644 --- a/userspace/programs/Shell/main.cpp +++ b/userspace/programs/Shell/main.cpp @@ -499,6 +499,7 @@ BAN::Optional execute_builtin(BAN::Vector& args, int fd_in, in TEST("ffffffffffffffff", 16); TEST("10000000000000000", 16); #undef TEST +#if __enable_sse #define TEST(num) do { errno = 0; printf("strtod(\"" num "\", nullptr) = %e ", strtod(num, nullptr)); puts(errno ? strerrorname_np(errno) : ""); } while (false) TEST("0"); TEST(".1"); @@ -531,6 +532,7 @@ BAN::Optional execute_builtin(BAN::Vector& args, int fd_in, in TEST("-inf"); TEST("nan"); #undef TEST +#endif } else { diff --git a/userspace/tests/CMakeLists.txt b/userspace/tests/CMakeLists.txt index 26a8912f..bbbba1cb 100644 --- a/userspace/tests/CMakeLists.txt +++ b/userspace/tests/CMakeLists.txt @@ -21,4 +21,9 @@ foreach(project ${USERSPACE_TESTS}) target_link_options(${project} PRIVATE -nolibc) # Default compile options target_compile_options(${project} PRIVATE -g -O2) + + target_compile_definitions(${project} PRIVATE __enable_sse=${BANAN_ENABLE_SSE}) + if (NOT BANAN_ENABLE_SSE) + target_compile_options(${project} PRIVATE -mno-sse -mno-sse2) + endif () endforeach()