From b4d2f7a9b0490bbd408a8ee509a01444dcda4e39 Mon Sep 17 00:00:00 2001
From: Niklas Sombert <niklas.sombert@uni-duesseldorf.de>
Date: Tue, 3 Sep 2024 21:55:26 +0200
Subject: [PATCH] Actually implement the float functions

---
 Cargo.lock           |  7 +++++++
 towboot/Cargo.toml   |  4 ++++
 towboot/src/hacks.rs | 15 +++++++--------
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 3b4408e..3623c5f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1010,6 +1010,12 @@ dependencies = [
  "pkg-config",
 ]
 
+[[package]]
+name = "libm"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
+
 [[package]]
 name = "libredox"
 version = "0.1.3"
@@ -1945,6 +1951,7 @@ dependencies = [
  "acpi",
  "built",
  "goblin",
+ "libm",
  "log",
  "multiboot12",
  "scroll",
diff --git a/towboot/Cargo.toml b/towboot/Cargo.toml
index e082f58..ded8509 100644
--- a/towboot/Cargo.toml
+++ b/towboot/Cargo.toml
@@ -26,5 +26,9 @@ scroll = { version = "0.12", default-features = false }
 
 towboot_config = { path = "../towboot_config" }
 
+# i686-unknown-uefi currently lacks float functions, see hacks.rs
+[target.'cfg(target_arch = "x86")'.dependencies]
+libm = "0.2"
+
 [build-dependencies]
 built = { version = "0.7", features = ["git2"] }
diff --git a/towboot/src/hacks.rs b/towboot/src/hacks.rs
index 5c44bd7..2f6619d 100644
--- a/towboot/src/hacks.rs
+++ b/towboot/src/hacks.rs
@@ -5,19 +5,18 @@
 //! This module contains missing symbols.
 //! 
 //! The fmod and fmodf functions are [currently missing](https://github.com/rust-lang/rust/issues/128533)
-//! on i686-unknown-uefi. In the long run, this should be fixed in
-//! `compiler_builtins`. For now, this monkeypatching seems to be enough.
+//! on i686-unknown-uefi, so let's use the ones of libm.
+//! In the long run, this should be fixed in `compiler_builtins`.
+//! For now, this monkeypatching seems to be enough.
 //!
 //! see https://github.com/rust-lang/compiler-builtins/blob/master/src/math.rs
-//! We could also use libm::fmod{,f} here, but then we'd need __truncdfsf2.
-//! Let's just hope they are never called.
 #[cfg(target_arch = "x86")]
 #[no_mangle]
-pub extern "C" fn fmod(_x: f64, _y: f64) -> f64 {
-    unimplemented!();
+pub extern "C" fn fmod(x: f64, y: f64) -> f64 {
+    libm::fmod(x, y)
 }
 #[cfg(target_arch = "x86")]
 #[no_mangle]
-pub extern "C" fn fmodf(_x: f32, _y: f32) -> f32 {
-    unimplemented!();
+pub extern "C" fn fmodf(x: f32, y: f32) -> f32 {
+    libm::fmodf(x, y)
 }