From f093bc5fd019b72813e4aac3eab4c8bb1eb0d018 Mon Sep 17 00:00:00 2001
From: Christian Himpe <christian.himpe@wwu.de>
Date: Fri, 2 Jun 2023 22:58:47 +0200
Subject: [PATCH 1/2] Added hyperbolic and inverse hyperbolic functions in
 c-std lib: sinh, cosh, tanh, asinh, acosh, atanh

---
 chicken.h               | 18 ++++++++++++++
 lfa2.scm                | 12 +++++++++
 library.scm             | 24 ++++++++++++++++++
 runtime.c               | 54 +++++++++++++++++++++++++++++++++++++++++
 tests/library-tests.scm | 26 ++++++++++++++++++++
 5 files changed, 134 insertions(+)

diff --git a/chicken.h b/chicken.h
index 928066ed..6461241d 100644
--- a/chicken.h
+++ b/chicken.h
@@ -966,6 +966,12 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) C_noret;
 # define C_asin                     asin
 # define C_acos                     acos
 # define C_atan                     atan
+# define C_sinh                     sinh
+# define C_cosh                     cosh
+# define C_tanh                     tanh
+# define C_asinh                    asinh
+# define C_acosh                    acosh
+# define C_atanh                    atanh
 # define C_atan2                    atan2
 # define C_log                      log
 # define C_exp                      exp
@@ -1629,6 +1635,12 @@ typedef void (C_ccall *C_proc)(C_word, C_word *) C_noret;
 #define C_a_i_flonum_acos(ptr, c, x)    C_flonum(ptr, C_acos(C_flonum_magnitude(x)))
 #define C_a_i_flonum_atan(ptr, c, x)    C_flonum(ptr, C_atan(C_flonum_magnitude(x)))
 #define C_a_i_flonum_atan2(ptr, c, x, y)  C_flonum(ptr, C_atan2(C_flonum_magnitude(x), C_flonum_magnitude(y)))
+#define C_a_i_flonum_sinh(ptr, c, x)     C_flonum(ptr, C_sinh(C_flonum_magnitude(x)))
+#define C_a_i_flonum_cosh(ptr, c, x)     C_flonum(ptr, C_cosh(C_flonum_magnitude(x)))
+#define C_a_i_flonum_tanh(ptr, c, x)     C_flonum(ptr, C_tanh(C_flonum_magnitude(x)))
+#define C_a_i_flonum_asinh(ptr, c, x)    C_flonum(ptr, C_asinh(C_flonum_magnitude(x)))
+#define C_a_i_flonum_acosh(ptr, c, x)    C_flonum(ptr, C_acosh(C_flonum_magnitude(x)))
+#define C_a_i_flonum_atanh(ptr, c, x)    C_flonum(ptr, C_atanh(C_flonum_magnitude(x)))
 #define C_a_i_flonum_exp(ptr, c, x)     C_flonum(ptr, C_exp(C_flonum_magnitude(x)))
 #define C_a_i_flonum_expt(ptr, c, x, y)  C_flonum(ptr, C_pow(C_flonum_magnitude(x), C_flonum_magnitude(y)))
 #define C_a_i_flonum_log(ptr, c, x)     C_flonum(ptr, C_log(C_flonum_magnitude(x)))
@@ -2074,6 +2086,12 @@ C_fctexport C_word C_fcall C_a_i_asin(C_word **a, int c, C_word n) C_regparm;
 C_fctexport C_word C_fcall C_a_i_acos(C_word **a, int c, C_word n) C_regparm;
 C_fctexport C_word C_fcall C_a_i_atan(C_word **a, int c, C_word n) C_regparm;
 C_fctexport C_word C_fcall C_a_i_atan2(C_word **a, int c, C_word n1, C_word n2) C_regparm;
+C_fctexport C_word C_fcall C_a_i_sinh(C_word **a, int c, C_word n) C_regparm;
+C_fctexport C_word C_fcall C_a_i_cosh(C_word **a, int c, C_word n) C_regparm;
+C_fctexport C_word C_fcall C_a_i_tanh(C_word **a, int c, C_word n) C_regparm;
+C_fctexport C_word C_fcall C_a_i_asinh(C_word **a, int c, C_word n) C_regparm;
+C_fctexport C_word C_fcall C_a_i_acosh(C_word **a, int c, C_word n) C_regparm;
+C_fctexport C_word C_fcall C_a_i_atanh(C_word **a, int c, C_word n) C_regparm;
 C_fctexport C_word C_fcall C_a_i_sqrt(C_word **a, int c, C_word n) C_regparm;
 C_fctexport C_word C_fcall C_i_o_fixnum_plus(C_word x, C_word y) C_regparm;
 C_fctexport C_word C_fcall C_i_o_fixnum_difference(C_word x, C_word y) C_regparm;
diff --git a/lfa2.scm b/lfa2.scm
index 38ed4da2..e47f1a23 100644
--- a/lfa2.scm
+++ b/lfa2.scm
@@ -167,12 +167,16 @@
     ("C_a_i_bignum2" bignum)
     ("C_a_i_flonum_abs" float)
     ("C_a_i_flonum_acos" float)
+    ("C_a_i_flonum_acosh" float)
     ("C_a_i_flonum_actual_quotient_checked" float)
     ("C_a_i_flonum_asin" float)
+    ("C_a_i_flonum_asinh" float)
     ("C_a_i_flonum_atan2" float)
     ("C_a_i_flonum_atan" float)
+    ("C_a_i_flonum_atanh" float)
     ("C_a_i_flonum_ceiling" float)
     ("C_a_i_flonum_cos" float)
+    ("C_a_i_flonum_cosh" float)
     ("C_a_i_flonum_difference" float)
     ("C_a_i_flonum_exp" float)
     ("C_a_i_flonum_expt" float)
@@ -188,8 +192,10 @@
     ("C_a_i_flonum_round" float)
     ("C_a_i_flonum_round_proper" float)
     ("C_a_i_flonum_sin" float)
+    ("C_a_i_flonum_sinh" float)
     ("C_a_i_flonum_sqrt" float)
     ("C_a_i_flonum_tan" float)
+    ("C_a_i_flonum_tanh" float)
     ("C_a_i_flonum_times" float)
     ("C_a_i_flonum_multiply_add" float)
     ("C_a_i_flonum_truncate" float)
@@ -219,6 +225,12 @@
     ("C_a_i_flonum_acos" "C_acos" op)
     ("C_a_i_flonum_atan" "C_atan" op)
     ("C_a_i_flonum_atan2" "C_atan2" op)
+    ("C_a_i_flonum_sinh" "C_sinh" op)
+    ("C_a_i_flonum_cosh" "C_cosh" op)
+    ("C_a_i_flonum_tanh" "C_tanh" op)
+    ("C_a_i_flonum_asinh" "C_asinh" op)
+    ("C_a_i_flonum_acosh" "C_acosh" op)
+    ("C_a_i_flonum_atanh" "C_atanh" op)
     ("C_a_i_flonum_exp" "C_exp" op)
     ("C_a_i_flonum_expr" "C_pow" op)
     ("C_a_i_flonum_log" "C_log" op)
diff --git a/library.scm b/library.scm
index 819dacfe..07694c44 100644
--- a/library.scm
+++ b/library.scm
@@ -1680,6 +1680,30 @@ EOF
   (fp-check-flonums x y 'fpatan2)
   (##core#inline_allocate ("C_a_i_flonum_atan2" 4) x y))
 
+(define (fpsinh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_sinh" 4) x))
+
+(define (fpcosh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_cosh" 4) x))
+
+(define (fptanh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_tanh" 4) x))
+
+(define (fpasinh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_asinh" 4) x))
+
+(define (fpacosh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_acosh" 4) x))
+
+(define (fpatanh x)
+  (fp-check-flonum x 'fpsinh)
+  (##core#inline_allocate ("C_a_i_flonum_atanh" 4) x))
+
 (define (fpexp x)
   (fp-check-flonum x 'fpexp)
   (##core#inline_allocate ("C_a_i_flonum_exp" 4) x))
diff --git a/runtime.c b/runtime.c
index ac7f2392..5358bdc0 100644
--- a/runtime.c
+++ b/runtime.c
@@ -7029,6 +7029,60 @@ C_regparm C_word C_fcall C_a_i_atan2(C_word **a, int c, C_word n1, C_word n2)
 }
 
 
+C_regparm C_word C_fcall C_a_i_sinh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "sinh", f);
+  return C_flonum(a, sinh(f));
+}
+
+
+C_regparm C_word C_fcall C_a_i_cosh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "cosh", f);
+  return C_flonum(a, cosh(f));
+}
+
+
+C_regparm C_word C_fcall C_a_i_tanh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "tanh", f);
+  return C_flonum(a, tanh(f));
+}
+
+
+C_regparm C_word C_fcall C_a_i_asinh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "asinh", f);
+  return C_flonum(a, asinh(f));
+}
+
+
+C_regparm C_word C_fcall C_a_i_acosh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "acosh", f);
+  return C_flonum(a, acosh(f));
+}
+
+
+C_regparm C_word C_fcall C_a_i_atanh(C_word **a, int c, C_word n)
+{
+  double f;
+
+  C_check_real(n, "atanh", f);
+  return C_flonum(a, atanh(f));
+}
+
+
 C_regparm C_word C_fcall C_a_i_sqrt(C_word **a, int c, C_word n)
 {
   double f;
diff --git a/tests/library-tests.scm b/tests/library-tests.scm
index bb7ae8a2..9e73dcb9 100644
--- a/tests/library-tests.scm
+++ b/tests/library-tests.scm
@@ -336,6 +336,32 @@
 (assert (nan? (fp*+ 0.0 +inf.0 1.0)))
 (assert (nan? (fp*+ -inf.0 0.0 1.0)))
 
+;; Hyperbolic function tests
+
+(assert (inexact= (fpsinh -inf.0) -inf.0))
+(assert (inexact= (fpsinh 0.0) 0.0))
+(assert (inexact= (fpsinh +inf.0) +inf.0))
+
+(assert (inexact= (fpcosh -inf.0) +inf.0))
+(assert (inexact= (fpcosh 0.0) 1.0))
+(assert (inexact= (fpcosh +inf.0) +inf.0))
+
+(assert (inexact= (fptanh -inf.0) -1.0))
+(assert (inexact= (fptanh 0.0) 0.0))
+(assert (inexact= (fptanh +inf.0) 1.0))
+
+(assert (inexact= (fpasinh -inf.0) -inf.0))
+(assert (inexact= (fpasinh 0.0) 0.0))
+(assert (inexact= (fpasinh +inf.0) +inf.0))
+
+(assert (inexact= (fpacosh 1.0) 0.0))
+(assert (inexact= (fpacosh +inf.0) +inf.0))
+(assert (nan? (fpacosh 0.0)))
+
+(assert (inexact= (fpatanh -inf.0) -1.0))
+(assert (inexact= (fpatanh 0.0) 0.0))
+(assert (inexact= (fpatanh +inf.0) 1.0))
+
 ;; string->symbol
 
 ;; by Jim Ursetto
-- 
2.40.1


From 280e03ab552e2e318b0169fe889a26da587e5f28 Mon Sep 17 00:00:00 2001
From: Christian Himpe <christian.himpe@wwu.de>
Date: Fri, 2 Jun 2023 23:36:25 +0200
Subject: [PATCH 2/2] Fix tests for hyperbolic flonum functions

---
 library.scm             | 10 +++++-----
 tests/library-tests.scm | 34 +++++++++++++++++-----------------
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/library.scm b/library.scm
index 07694c44..04445321 100644
--- a/library.scm
+++ b/library.scm
@@ -1685,23 +1685,23 @@ EOF
   (##core#inline_allocate ("C_a_i_flonum_sinh" 4) x))
 
 (define (fpcosh x)
-  (fp-check-flonum x 'fpsinh)
+  (fp-check-flonum x 'fpcosh)
   (##core#inline_allocate ("C_a_i_flonum_cosh" 4) x))
 
 (define (fptanh x)
-  (fp-check-flonum x 'fpsinh)
+  (fp-check-flonum x 'fptanh)
   (##core#inline_allocate ("C_a_i_flonum_tanh" 4) x))
 
 (define (fpasinh x)
-  (fp-check-flonum x 'fpsinh)
+  (fp-check-flonum x 'fpasinh)
   (##core#inline_allocate ("C_a_i_flonum_asinh" 4) x))
 
 (define (fpacosh x)
-  (fp-check-flonum x 'fpsinh)
+  (fp-check-flonum x 'fpacosh)
   (##core#inline_allocate ("C_a_i_flonum_acosh" 4) x))
 
 (define (fpatanh x)
-  (fp-check-flonum x 'fpsinh)
+  (fp-check-flonum x 'fpatanh)
   (##core#inline_allocate ("C_a_i_flonum_atanh" 4) x))
 
 (define (fpexp x)
diff --git a/tests/library-tests.scm b/tests/library-tests.scm
index 9e73dcb9..93e048f7 100644
--- a/tests/library-tests.scm
+++ b/tests/library-tests.scm
@@ -338,29 +338,29 @@
 
 ;; Hyperbolic function tests
 
-(assert (inexact= (fpsinh -inf.0) -inf.0))
-(assert (inexact= (fpsinh 0.0) 0.0))
-(assert (inexact= (fpsinh +inf.0) +inf.0))
+(assert (fp= (fpsinh -inf.0) -inf.0))
+(assert (fp= (fpsinh 0.0) 0.0))
+(assert (fp= (fpsinh +inf.0) +inf.0))
 
-(assert (inexact= (fpcosh -inf.0) +inf.0))
-(assert (inexact= (fpcosh 0.0) 1.0))
-(assert (inexact= (fpcosh +inf.0) +inf.0))
+(assert (fp= (fpcosh -inf.0) +inf.0))
+(assert (fp= (fpcosh 0.0) 1.0))
+(assert (fp= (fpcosh +inf.0) +inf.0))
 
-(assert (inexact= (fptanh -inf.0) -1.0))
-(assert (inexact= (fptanh 0.0) 0.0))
-(assert (inexact= (fptanh +inf.0) 1.0))
+(assert (fp= (fptanh -inf.0) -1.0))
+(assert (fp= (fptanh 0.0) 0.0))
+(assert (fp= (fptanh +inf.0) 1.0))
 
-(assert (inexact= (fpasinh -inf.0) -inf.0))
-(assert (inexact= (fpasinh 0.0) 0.0))
-(assert (inexact= (fpasinh +inf.0) +inf.0))
+(assert (fp= (fpasinh -inf.0) -inf.0))
+(assert (fp= (fpasinh 0.0) 0.0))
+(assert (fp= (fpasinh +inf.0) +inf.0))
 
-(assert (inexact= (fpacosh 1.0) 0.0))
-(assert (inexact= (fpacosh +inf.0) +inf.0))
+(assert (fp= (fpacosh 1.0) 0.0))
+(assert (fp= (fpacosh +inf.0) +inf.0))
 (assert (nan? (fpacosh 0.0)))
 
-(assert (inexact= (fpatanh -inf.0) -1.0))
-(assert (inexact= (fpatanh 0.0) 0.0))
-(assert (inexact= (fpatanh +inf.0) 1.0))
+(assert (fp= (fpatanh -1.0) -inf.0))
+(assert (fp= (fpatanh 0.0) 0.0))
+(assert (fp= (fpatanh 1.0) +inf.0))
 
 ;; string->symbol
 
-- 
2.40.1

