From e32e03d7977fc069ac65a388f4488867ac812724 Mon Sep 17 00:00:00 2001 From: jiangyingxin Date: Fri, 21 Jul 2023 16:15:59 +0800 Subject: [PATCH 1/2] glibc: Optimizing the rand function on arm64 platforms --- ...the-rand-function-on-arm64-platforms.patch | 64 +++++++++++++++++++ glibc.spec | 6 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 0001-Optimizing-the-rand-function-on-arm64-platforms.patch diff --git a/0001-Optimizing-the-rand-function-on-arm64-platforms.patch b/0001-Optimizing-the-rand-function-on-arm64-platforms.patch new file mode 100644 index 0000000..b2f115f --- /dev/null +++ b/0001-Optimizing-the-rand-function-on-arm64-platforms.patch @@ -0,0 +1,64 @@ +From a71c4a25086acf6f36b18607dfc9d19b3d62bc86 Mon Sep 17 00:00:00 2001 +From: Tian Tao +Date: Thu, 20 Jul 2023 16:25:44 +0800 +Subject: [PATCH] Optimizing the rand function on arm64 platforms + +Armv8.5 adds the FEAT_RNG feature so that true random numbers can be +obtained from the cpu. This optimization is therefore proposed for the +arm64 platform. + +Signed-off-by: Tian Tao +--- + stdlib/rand.c | 32 ++++++++++++++++++++++++++++++++ + 1 file changed, 32 insertions(+) + +diff --git a/stdlib/rand.c b/stdlib/rand.c +index c250c065e4..f199dbd65f 100644 +--- a/stdlib/rand.c ++++ b/stdlib/rand.c +@@ -19,10 +19,42 @@ + + #undef rand + ++#ifdef __aarch64__ ++// A global variable to indicate whether rndr instruction is supported ++static int rndr_supported = 0; ++// A function prototype to check rndr support ++static void check_rndr_support (void) __attribute__ ((constructor)); ++#endif + + /* Return a random integer between 0 and RAND_MAX. */ + int + rand (void) + { ++ // Use armv8.5 rndr instruction if supported ++#ifdef __aarch64__ ++ if (rndr_supported) { ++ unsigned int r; ++ asm volatile("mrs %0, s3_3_c2_c4_0" : "=r" (r)); ++ // Discard the least random bit ++ r >>=1; ++ return (int) r; ++ } ++#endif + return (int) __random (); + } ++ ++#ifdef __aarch64__ ++// A function to check rndr support and set the global variable accordingly ++static void ++check_rndr_support (void) ++{ ++ unsigned long isar0; ++ // Read the ID_AA64ISAR0_EL1 register to check the rndr feature ++ asm volatile("mrs %0, id_aa64isar0_el1" : "=r" (isar0)); ++ // Check the bits [63:60] for the rndr feature ++ if ((isar0 >> 60) & 0xf) { ++ // Set the global variable to indicate rndr support ++ rndr_supported = 1; ++ } ++} ++#endif +-- +2.33.0 + diff --git a/glibc.spec b/glibc.spec index f3aee52..f618496 100644 --- a/glibc.spec +++ b/glibc.spec @@ -65,7 +65,7 @@ ############################################################################## Name: glibc Version: 2.36 -Release: 18 +Release: 19 Summary: The GNU libc libraries License: %{all_license} URL: http://www.gnu.org/software/glibc/ @@ -115,6 +115,7 @@ Patch9014: strcmp-delete-align-for-loop_aligned.patch Patch9015: add-pthread_cond_clockwait-GLIBC_2_28.patch Patch9016: add-GB18030-2022-charmap-BZ-30243.patch Patch9017: 0001-Optimizing-__random-for-single-threaded-scenarios.patch +Patch9018: 0001-Optimizing-the-rand-function-on-arm64-platforms.patch Provides: ldconfig rtld(GNU_HASH) bundled(gnulib) @@ -1274,6 +1275,9 @@ fi %endif %changelog +* Thu Jul 20 2023 jiangyingxin - 2.36-19 +- Optimizing the rand function on arm64 platforms + * Tue Jul 11 2023 jiangyingxin - 2.36-18 - Optimizing __random for single-threaded scenarios -- Gitee From 78d1e7bbbdf7d43a4cc613a0e4bab9e1f6da9147 Mon Sep 17 00:00:00 2001 From: jiangyingxin Date: Mon, 24 Jul 2023 01:00:29 +0000 Subject: [PATCH 2/2] update 0001-Optimizing-the-rand-function-on-arm64-platforms.patch. Signed-off-by: jiangyingxin --- 0001-Optimizing-the-rand-function-on-arm64-platforms.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0001-Optimizing-the-rand-function-on-arm64-platforms.patch b/0001-Optimizing-the-rand-function-on-arm64-platforms.patch index b2f115f..7f05994 100644 --- a/0001-Optimizing-the-rand-function-on-arm64-platforms.patch +++ b/0001-Optimizing-the-rand-function-on-arm64-platforms.patch @@ -51,7 +51,7 @@ index c250c065e4..f199dbd65f 100644 +{ + unsigned long isar0; + // Read the ID_AA64ISAR0_EL1 register to check the rndr feature -+ asm volatile("mrs %0, id_aa64isar0_el1" : "=r" (isar0)); ++ asm volatile("mrs %0, s3_0_c0_c6_0" : "=r" (isar0)); + // Check the bits [63:60] for the rndr feature + if ((isar0 >> 60) & 0xf) { + // Set the global variable to indicate rndr support -- Gitee