https://git.reactos.org/?p=reactos.git;a=commitdiff;h=b09b5584e0778519bb2ee286a1afb04967295d18

commit b09b5584e0778519bb2ee286a1afb04967295d18
Author:     Timo Kreuzer <[email protected]>
AuthorDate: Mon Oct 28 18:14:55 2024 +0200
Commit:     Timo Kreuzer <[email protected]>
CommitDate: Sun Jan 26 18:08:55 2025 +0200

    [UCRT:MATH] Implement math error support functions
---
 sdk/lib/ucrt/CMakeLists.txt   |  2 +
 sdk/lib/ucrt/math/math.cmake  |  4 ++
 sdk/lib/ucrt/math/matherr.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)

diff --git a/sdk/lib/ucrt/CMakeLists.txt b/sdk/lib/ucrt/CMakeLists.txt
index a5106b5037c..a10b7b60029 100644
--- a/sdk/lib/ucrt/CMakeLists.txt
+++ b/sdk/lib/ucrt/CMakeLists.txt
@@ -94,6 +94,7 @@ include(initializers/initializers.cmake)
 include(internal/internal.cmake)
 include(locale/locale.cmake)
 include(lowio/lowio.cmake)
+include(math/math.cmake)
 include(mbstring/mbstring.cmake)
 include(misc/misc.cmake)
 include(startup/startup.cmake)
@@ -116,6 +117,7 @@ add_library(ucrt OBJECT
     ${UCRT_INTERNAL_SOURCES}
     ${UCRT_LOCALE_SOURCES}
     ${UCRT_LOWIO_SOURCES}
+    ${UCRT_MATH_SOURCES}
     ${UCRT_MBSTRING_SOURCES}
     ${UCRT_MISC_SOURCES}
     ${UCRT_STARTUP_SOURCES}
diff --git a/sdk/lib/ucrt/math/math.cmake b/sdk/lib/ucrt/math/math.cmake
new file mode 100644
index 00000000000..4eadd9f107d
--- /dev/null
+++ b/sdk/lib/ucrt/math/math.cmake
@@ -0,0 +1,4 @@
+
+list(APPEND UCRT_MATH_SOURCES
+    math/matherr.cpp
+)
diff --git a/sdk/lib/ucrt/math/matherr.cpp b/sdk/lib/ucrt/math/matherr.cpp
new file mode 100644
index 00000000000..500d8fd1a9b
--- /dev/null
+++ b/sdk/lib/ucrt/math/matherr.cpp
@@ -0,0 +1,90 @@
+//
+// matherr.cpp
+//
+//      Copyright (c) 2024 Timo Kreuzer
+//
+// User math error support.
+//
+// SPDX-License-Identifier: MIT
+//
+
+#include <math.h>
+#include <corecrt_startup.h>
+#include <corecrt_internal.h>
+
+static 
__crt_state_management::dual_state_global<_UserMathErrorFunctionPointer> 
__acrt_global_user_matherr;
+
+//
+// Declared in corecrt_internal.h
+// Called from initialize_pointers()
+//
+extern "C"
+void __cdecl __acrt_initialize_user_matherr(void* encoded_null)
+{
+    __acrt_global_user_matherr.initialize(
+        reinterpret_cast<_UserMathErrorFunctionPointer>(encoded_null));
+}
+
+//
+// Declared in corecrt_internal.h
+//
+extern "C"
+bool __cdecl __acrt_has_user_matherr(void)
+{
+    _UserMathErrorFunctionPointer user_matherr =
+        __crt_fast_decode_pointer(__acrt_global_user_matherr.value());
+    return user_matherr != nullptr;
+}
+
+//
+// Declared in corecrt_internal.h
+//
+extern "C"
+int  __cdecl __acrt_invoke_user_matherr(struct _exception* _Exp)
+{
+    _UserMathErrorFunctionPointer user_matherr =
+        __crt_fast_decode_pointer(__acrt_global_user_matherr.value());
+    if (user_matherr != nullptr)
+    {
+        return user_matherr(_Exp);
+    }
+
+    return 0;
+}
+
+//
+// Declared in corecrt_startup.h
+//
+extern "C"
+void
+__cdecl
+__setusermatherr(
+    _UserMathErrorFunctionPointer _UserMathErrorFunction)
+{
+    _UserMathErrorFunctionPointer encodedPtr =
+        __crt_fast_encode_pointer(_UserMathErrorFunction);
+
+    __acrt_global_user_matherr.value() = encodedPtr;
+}
+
+//
+// Used by libm
+//
+extern "C"
+int
+__cdecl
+_invoke_matherr(
+    int type,
+    char* name,
+    double arg1,
+    double arg2,
+    double retval)
+{
+    struct _exception excpt;
+    excpt.type = type;
+    excpt.name = name;
+    excpt.arg1 = arg1;
+    excpt.arg2 = arg2;
+    excpt.retval = retval;
+    return __acrt_invoke_user_matherr(&excpt);
+}

Reply via email to