userspace: Allow building without SSE

I had added changes that had broken compilation without sse support
This commit is contained in:
Bananymous 2024-09-02 21:25:00 +03:00
parent 700c3444f5
commit 5f92807fdd
9 changed files with 45 additions and 6 deletions

View File

@ -15,9 +15,14 @@ add_custom_target(libraries)
foreach(library ${USERSPACE_LIBRARIES}) foreach(library ${USERSPACE_LIBRARIES})
string(TOLOWER ${library} library_lower) string(TOLOWER ${library} library_lower)
if (TARGET ${library_lower}) if (TARGET ${library_lower} AND NOT ${library_lower} STREQUAL "libc")
add_dependencies(libraries ${library_lower}) add_dependencies(libraries ${library_lower})
# This is to allow cmake to link when libc updates # This is to allow cmake to link when libc updates
target_link_options(${library_lower} PRIVATE -nolibc) 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() endif()
endforeach() endforeach()

View File

@ -17,6 +17,7 @@
__BEGIN_DECLS __BEGIN_DECLS
#if __enable_sse
BUILTINS1(acos) BUILTINS1(acos)
BUILTINS1(acosh) BUILTINS1(acosh)
BUILTINS1(asin) BUILTINS1(asin)
@ -65,5 +66,6 @@ BUILTINS1(tgamma)
BUILTINS1(trunc) BUILTINS1(trunc)
BUILTINS1(y0) BUILTINS1(y0)
BUILTINS1(y1) BUILTINS1(y1)
#endif
__END_DECLS __END_DECLS

View File

@ -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 = auto parse_floating_point_internal =
[&parse_integer_internal, &get_input, &in]<int BASE, typename T>(BASE_TYPE<BASE>, bool negative, int width, T* out, bool require_start = true) -> ConversionResult [&parse_integer_internal, &get_input, &in]<int BASE, typename T>(BASE_TYPE<BASE>, 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; return ConversionResult::MATCH_FAILURE;
} }
}; };
#endif
auto parse_string = 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 [&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<true> {}, conversion.suppress, conversion.field_width, conversion.length); break; case 'x': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED<true> {}, conversion.suppress, conversion.field_width, conversion.length); break;
case 'X': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED<true> {}, conversion.suppress, conversion.field_width, conversion.length); break; case 'X': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED<true> {}, conversion.suppress, conversion.field_width, conversion.length); break;
case 'p': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED<true> {}, conversion.suppress, conversion.field_width, LengthModifier::j); break; case 'p': result = parse_integer(BASE_TYPE<16>{}, IS_UNSIGNED<true> {}, conversion.suppress, conversion.field_width, LengthModifier::j); break;
#if __enable_sse
case 'a': case 'e': case 'f': case 'g': case 'a': case 'e': case 'f': case 'g':
result = parse_floating_point(conversion.suppress, conversion.field_width, conversion.length); result = parse_floating_point(conversion.suppress, conversion.field_width, conversion.length);
break; break;
#endif
case 'S': case 'S':
conversion.length = LengthModifier::l; conversion.length = LengthModifier::l;
// fall through // fall through

View File

@ -158,6 +158,7 @@ static T strtoT(const char* str, char** endp, int base, int& error)
return result; return result;
} }
#if __enable_sse
template<BAN::floating_point T> template<BAN::floating_point T>
static T strtoT(const char* str, char** endp, int& error) 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<T>((base == 10) ? 10 : 2, exponent); result *= BAN::Math::pow<T>((base == 10) ? 10 : 2, exponent);
return result; return result;
} }
#endif
#if __enable_sse
double atof(const char* str) double atof(const char* str)
{ {
return strtod(str, nullptr); return strtod(str, nullptr);
} }
#endif
int atoi(const char* str) int atoi(const char* str)
{ {
@ -324,6 +328,7 @@ long long atoll(const char* str)
return strtoll(str, nullptr, 10); return strtoll(str, nullptr, 10);
} }
#if __enable_sse
float strtof(const char* __restrict str, char** __restrict endp) float strtof(const char* __restrict str, char** __restrict endp)
{ {
return strtoT<float>(str, endp, errno); return strtoT<float>(str, endp, errno);
@ -338,6 +343,7 @@ long double strtold(const char* __restrict str, char** __restrict endp)
{ {
return strtoT<long double>(str, endp, errno); return strtoT<long double>(str, endp, errno);
} }
#endif
long strtol(const char* __restrict str, char** __restrict endp, int base) long strtol(const char* __restrict str, char** __restrict endp, int base)
{ {

View File

@ -87,10 +87,10 @@ namespace LibImage
constexpr Image::Color as_color() const constexpr Image::Color as_color() const
{ {
return Image::Color { return Image::Color {
.r = static_cast<uint8_t>(BAN::Math::clamp<double>(r, 0.0, 255.0)), .r = static_cast<uint8_t>(r < 0.0 ? 0.0 : r > 255.0 ? 255.0 : r),
.g = static_cast<uint8_t>(BAN::Math::clamp<double>(g, 0.0, 255.0)), .g = static_cast<uint8_t>(g < 0.0 ? 0.0 : g > 255.0 ? 255.0 : g),
.b = static_cast<uint8_t>(BAN::Math::clamp<double>(b, 0.0, 255.0)), .b = static_cast<uint8_t>(b < 0.0 ? 0.0 : b > 255.0 ? 255.0 : b),
.a = static_cast<uint8_t>(BAN::Math::clamp<double>(a, 0.0, 255.0)), .a = static_cast<uint8_t>(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_x = x * ratio_x;
const double src_y = y * ratio_y; const double src_y = y * ratio_y;
#if __enable_sse
const double weight_x = src_x - floor(src_x); const double weight_x = src_x - floor(src_x);
const double weight_y = src_y - floor(src_y); 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( const Color avg_t = Color::average(
get_clamped_color(src_x + 0.0, src_y), 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_x = x * ratio_x;
const double src_y = y * ratio_y; const double src_y = y * ratio_y;
#if __enable_sse
const double weight_x = src_x - floor(src_x); const double weight_x = src_x - floor(src_x);
const double weight_y = src_y - floor(src_y); 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]; FloatingColor values[4];
for (int64_t m = -1; m <= 2; m++) for (int64_t m = -1; m <= 2; m++)

View File

@ -22,7 +22,7 @@ namespace LibImage
// weight of 0.0 returns a and weight of 1.0 returns b // weight of 0.0 returns a and weight of 1.0 returns b
static Color average(Color a, Color b, double weight) 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; const double a_mult = 1.0 - b_mult;
return Color { return Color {
.r = static_cast<uint8_t>(a.r * a_mult + b.r * b_mult), .r = static_cast<uint8_t>(a.r * a_mult + b.r * b_mult),

View File

@ -43,4 +43,9 @@ foreach(project ${USERSPACE_PROGRAMS})
target_link_options(${project} PRIVATE -nolibc) target_link_options(${project} PRIVATE -nolibc)
# Default compile options # Default compile options
target_compile_options(${project} PRIVATE -g -O2) 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() endforeach()

View File

@ -499,6 +499,7 @@ BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, in
TEST("ffffffffffffffff", 16); TEST("ffffffffffffffff", 16);
TEST("10000000000000000", 16); TEST("10000000000000000", 16);
#undef TEST #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) #define TEST(num) do { errno = 0; printf("strtod(\"" num "\", nullptr) = %e ", strtod(num, nullptr)); puts(errno ? strerrorname_np(errno) : ""); } while (false)
TEST("0"); TEST("0");
TEST(".1"); TEST(".1");
@ -531,6 +532,7 @@ BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, in
TEST("-inf"); TEST("-inf");
TEST("nan"); TEST("nan");
#undef TEST #undef TEST
#endif
} }
else else
{ {

View File

@ -21,4 +21,9 @@ foreach(project ${USERSPACE_TESTS})
target_link_options(${project} PRIVATE -nolibc) target_link_options(${project} PRIVATE -nolibc)
# Default compile options # Default compile options
target_compile_options(${project} PRIVATE -g -O2) 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() endforeach()