Skip to content

Commit

Permalink
math: Add safe asuint64 function
Browse files Browse the repository at this point in the history
  • Loading branch information
colluca committed Nov 16, 2023
1 parent 815cc26 commit c092a10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
34 changes: 34 additions & 0 deletions sw/deps/patches/musl/0005-math-Add-safe-asuint64-function.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
From f468669a93f85ac490418217213e4aa211b51e31 Mon Sep 17 00:00:00 2001
From: Luca Colagrande <[email protected]>
Date: Thu, 16 Nov 2023 10:07:46 +0100
Subject: [PATCH] math: Add safe `asuint64` function

---
src/internal/libm.h | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/internal/libm.h b/src/internal/libm.h
index c96c0ece..5bcad979 100644
--- a/src/internal/libm.h
+++ b/src/internal/libm.h
@@ -138,10 +138,13 @@ inline void snrt_fpu_fence() {

/* Synch-secure double to uint64 conversion functions. */
static inline uint64_t asuint64(double f) {
- uint64_t result;
- snrt_fpu_fence();
- result = *(uint64_t *)&f;
- return result;
+ uint64_t *ptr;
+ asm volatile("fsd %[f], 0(%[ptr]) \n"
+ "fld ft3, 0(%[ptr]) \n"
+ "fmv.x.w t0, ft3 \n"
+ "mv t0, t0 \n"
+ : : [f]"f"(f), [ptr]"r"(ptr): "ft3", "t0", "memory");
+ return *result;
}

/* Synch-secure float to uint conversion functions. */
--
2.31.1

11 changes: 7 additions & 4 deletions sw/math/src/internal/libm.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ inline void snrt_fpu_fence() {

/* Synch-secure double to uint64 conversion functions. */
static inline uint64_t asuint64(double f) {
uint64_t result;
snrt_fpu_fence();
result = *(uint64_t *)&f;
return result;
uint64_t *ptr;
asm volatile("fsd %[f], 0(%[ptr]) \n"
"fld ft3, 0(%[ptr]) \n"
"fmv.x.w t0, ft3 \n"
"mv t0, t0 \n"
: : [f]"f"(f), [ptr]"r"(ptr): "ft3", "t0", "memory");
return *result;
}

/* Synch-secure float to uint conversion functions. */
Expand Down

0 comments on commit c092a10

Please sign in to comment.