[v3,3/4] sim: Add partial support for IEEE 754-2008

Message ID 20220111212406.32312-4-Dragan.Mladjenovic@syrmia.com
State New
Headers show
Series
  • sim: Add support for MIPS32/64 revision 6
Related show

Commit Message

Dragan Mladjenovic Jan. 11, 2022, 9:24 p.m.
From: Faraz Shahbazker <fshahbazker@wavecomp.com>


2022-01-11  Faraz Shahbazker  <fshahbazker@wavecomp.com>

sim/common/ChangeLog:
	* sim-fpu.c (sim_fpu_minmax_nan): New.
	(sim_fpu_max): Add variant behaviour for IEEE 754-2008.
	(sim_fpu_min): Likewise.
	(sim_fpu_is_un, sim_fpu_is_or): New.
	(sim_fpu_un, sim_fpu_or): New.
	(sim_fpu_current_mode): New.
	(sim_fpu_is_ieee754_2008, sim_fpu_is_ieee754_1985): New.
	(sim_fpu_set_mode): New.
	(sim_fpu_classify): New.
	* sim-fpu.h (sim_fpu_minmax_nan): New declaration.
	(sim_fpu_un, sim_fpu_or): New declarations.
	(sim_fpu_is_un, sim_fpu_is_or): New declarations.
	(sim_fpu_mode): New.
	(sim_fpu_is_ieee754_2008): New declaration.
	(sim_fpu_is_ieee754_1985): New declaration.
	(sim_fpu_set_mode): New declaration.
	(sim_fpu_classify): New declaration.
---
 sim/common/sim-fpu.c | 125 +++++++++++++++++++++++++++++++++++++++++--
 sim/common/sim-fpu.h |  19 ++++++-
 2 files changed, 139 insertions(+), 5 deletions(-)

Changes from v2: Fix Werror failures.

-- 
2.17.1

Comments

Simon Marchi via Gdb-patches Jan. 12, 2022, 10:47 a.m. | #1
On 11 Jan 2022 22:24, Dragan Mladjenovic wrote:
> --- a/sim/common/sim-fpu.c

> +++ b/sim/common/sim-fpu.c

>  

> +INLINE_SIM_FPU (int)

> +sim_fpu_is_un (const sim_fpu *l,

> +    const sim_fpu *r)


the arguments fit on one line

> +INLINE_SIM_FPU (int)

> +sim_fpu_is_or (const sim_fpu *l,

> +    const sim_fpu *r)


same here

> +INLINE_SIM_FPU (int)

> +sim_fpu_un (int *is, const sim_fpu *l, const sim_fpu *r)

> 

> +INLINE_SIM_FPU (int)

> +sim_fpu_or (int *is, const sim_fpu *l, const sim_fpu *r)


sorry, but what are these functions supposed to be doing ?

the sim_fpu_{lt,le,eq,ne,...} binary operations are all fairly obvious in
that they map directly to < <= == != ...

but what do un & or map to ?  they clearly aren't "Specific comparison
operators" like the others as they aren't actually testing the value of
the operands other than whether they're NaN's.

> +  if (sim_fpu_is_nan (l) || sim_fpu_is_nan (r))

> +  {

> +    *is = 1;

> +    return 0;

> +  }


the {} section should be indented one more level

> +INLINE_SIM_FPU(int)

> +sim_fpu_classify (const sim_fpu *f)

> +{

> +  switch (f->class)

> +    {

> +    case sim_fpu_class_snan: return SIM_FPU_IS_SNAN;

> +    case sim_fpu_class_qnan: return SIM_FPU_IS_QNAN;

> +    case sim_fpu_class_infinity:

> +      if (!f->sign)

> +	return SIM_FPU_IS_PINF;

> +      else

> +	return SIM_FPU_IS_NINF;

> +    case sim_fpu_class_zero:

> +      if (!f->sign)

> +	return SIM_FPU_IS_PZERO;

> +      else

> +	return SIM_FPU_IS_NZERO;

> +    case sim_fpu_class_number:

> +      if (!f->sign)

> +	return SIM_FPU_IS_PNUMBER;

> +      else

> +	return SIM_FPU_IS_NNUMBER;

> +    case sim_fpu_class_denorm:

> +      if (!f->sign)

> +	return SIM_FPU_IS_PDENORM;

> +      else

> +	return SIM_FPU_IS_NDENORM;


i think these would be a bit more succinct written as:
      return f->sign ? SIM_FPU_IS_NINF : SIM_FPU_IS_PINF;

>  /* A number of useful constants */

>  

>  #if EXTERN_SIM_FPU_P

> +static sim_fpu_mode sim_fpu_current_mode = sim_fpu_ieee754_1985;


this isn't a constant.  please don't use global variables to hold state.  same
as the earlier patch, this should go into the (new) sim_fpu_state structure.
-mike

Patch

diff --git a/sim/common/sim-fpu.c b/sim/common/sim-fpu.c
index 9e6abf01391..3559d474fed 100644
--- a/sim/common/sim-fpu.c
+++ b/sim/common/sim-fpu.c
@@ -1005,6 +1005,30 @@  sim_fpu_op_nan (sim_fpu *f, const sim_fpu *l, const sim_fpu *r)
   return 0;
 }
 
+/* NaN handling specific to min/max operations.  */
+
+INLINE_SIM_FPU (int)
+sim_fpu_minmax_nan (sim_fpu *f, const sim_fpu *l, const sim_fpu *r)
+{
+  if (sim_fpu_is_snan (l)
+      || sim_fpu_is_snan (r)
+      || sim_fpu_is_ieee754_1985 ())
+    return sim_fpu_op_nan (f, l, r);
+  else
+    /* if sim_fpu_is_ieee754_2008()
+       && ((sim_fpu_is_qnan (l) || sim_fpu_is_qnan (r))) */
+    {
+      /* In IEEE754-2008:
+       * "minNum/maxNum is ... the canonicalized number if one
+       * operand is a number and the other a quiet NaN." */
+      if (sim_fpu_is_qnan (l))
+	*f = *r;
+      else /* if (sim_fpu_is_qnan (r)) */
+	*f = *l;
+      return 0;
+    }
+}
+
 /* Arithmetic ops */
 
 INLINE_SIM_FPU (int)
@@ -1553,7 +1577,7 @@  sim_fpu_max (sim_fpu *f,
 	     const sim_fpu *r)
 {
   if (sim_fpu_is_nan (l) || sim_fpu_is_nan (r))
-    return sim_fpu_op_nan (f, l, r);
+    return sim_fpu_minmax_nan (f, l, r);
   if (sim_fpu_is_infinity (l))
     {
       if (sim_fpu_is_infinity (r)
@@ -1616,7 +1640,7 @@  sim_fpu_min (sim_fpu *f,
 	     const sim_fpu *r)
 {
   if (sim_fpu_is_nan (l) || sim_fpu_is_nan (r))
-    return sim_fpu_op_nan (f, l, r);
+    return sim_fpu_minmax_nan (f, l, r);
   if (sim_fpu_is_infinity (l))
     {
       if (sim_fpu_is_infinity (r)
@@ -1677,7 +1701,7 @@  INLINE_SIM_FPU (int)
 sim_fpu_neg (sim_fpu *f,
 	     const sim_fpu *r)
 {
-  if (sim_fpu_is_snan (r))
+  if (sim_fpu_is_ieee754_1985 () && sim_fpu_is_snan (r))
     {
       *f = *r;
       f->class = sim_fpu_class_qnan;
@@ -1700,7 +1724,7 @@  sim_fpu_abs (sim_fpu *f,
 {
   *f = *r;
   f->sign = 0;
-  if (sim_fpu_is_snan (r))
+  if (sim_fpu_is_ieee754_1985 () && sim_fpu_is_snan (r))
     {
       f->class = sim_fpu_class_qnan;
       return sim_fpu_status_invalid_snan;
@@ -2255,6 +2279,23 @@  sim_fpu_is_gt (const sim_fpu *l, const sim_fpu *r)
   return is;
 }
 
+INLINE_SIM_FPU (int)
+sim_fpu_is_un (const sim_fpu *l,
+    const sim_fpu *r)
+{
+  int is;
+  sim_fpu_un (&is, l, r);
+  return is;
+}
+
+INLINE_SIM_FPU (int)
+sim_fpu_is_or (const sim_fpu *l,
+    const sim_fpu *r)
+{
+  int is;
+  sim_fpu_or (&is, l, r);
+  return is;
+}
 
 /* Compare operators */
 
@@ -2378,10 +2419,68 @@  sim_fpu_gt (int *is,
   return sim_fpu_lt (is, r, l);
 }
 
+INLINE_SIM_FPU (int)
+sim_fpu_un (int *is, const sim_fpu *l, const sim_fpu *r)
+{
+  if (sim_fpu_is_nan (l) || sim_fpu_is_nan (r))
+  {
+    *is = 1;
+    return 0;
+  }
+
+  *is = 0;
+  return 0;
+}
+
+INLINE_SIM_FPU (int)
+sim_fpu_or (int *is, const sim_fpu *l, const sim_fpu *r)
+{
+  sim_fpu_un (is, l, r);
+
+  /* Invert result.  */
+  *is = !*is;
+  return 0;
+}
+
+INLINE_SIM_FPU(int)
+sim_fpu_classify (const sim_fpu *f)
+{
+  switch (f->class)
+    {
+    case sim_fpu_class_snan: return SIM_FPU_IS_SNAN;
+    case sim_fpu_class_qnan: return SIM_FPU_IS_QNAN;
+    case sim_fpu_class_infinity:
+      if (!f->sign)
+	return SIM_FPU_IS_PINF;
+      else
+	return SIM_FPU_IS_NINF;
+    case sim_fpu_class_zero:
+      if (!f->sign)
+	return SIM_FPU_IS_PZERO;
+      else
+	return SIM_FPU_IS_NZERO;
+    case sim_fpu_class_number:
+      if (!f->sign)
+	return SIM_FPU_IS_PNUMBER;
+      else
+	return SIM_FPU_IS_NNUMBER;
+    case sim_fpu_class_denorm:
+      if (!f->sign)
+	return SIM_FPU_IS_PDENORM;
+      else
+	return SIM_FPU_IS_NDENORM;
+    default:
+      fprintf (stderr, "Bad switch\n");
+      abort ();
+    }
+  return 0;
+}
 
 /* A number of useful constants */
 
 #if EXTERN_SIM_FPU_P
+static sim_fpu_mode sim_fpu_current_mode = sim_fpu_ieee754_1985;
+
 const sim_fpu sim_fpu_zero = {
   sim_fpu_class_zero, 0, 0, 0
 };
@@ -2410,6 +2509,24 @@  const sim_fpu sim_fpu_max64 = {
 bool sim_fpu_quiet_nan_inverted = false;
 #endif
 
+/* Specification swapping behaviour */
+INLINE_SIM_FPU (bool)
+sim_fpu_is_ieee754_1985 (void)
+{
+  return (sim_fpu_current_mode == sim_fpu_ieee754_1985);
+}
+
+INLINE_SIM_FPU (bool)
+sim_fpu_is_ieee754_2008 (void)
+{
+  return (sim_fpu_current_mode == sim_fpu_ieee754_2008);
+}
+
+INLINE_SIM_FPU (void)
+sim_fpu_set_mode (const sim_fpu_mode m)
+{
+  sim_fpu_current_mode = m;
+}
 
 /* For debugging */
 
diff --git a/sim/common/sim-fpu.h b/sim/common/sim-fpu.h
index 34a56efae22..c84b35811e9 100644
--- a/sim/common/sim-fpu.h
+++ b/sim/common/sim-fpu.h
@@ -252,6 +252,8 @@  INLINE_SIM_FPU (int) sim_fpu_sqrt (sim_fpu *f,
 
 INLINE_SIM_FPU (int) sim_fpu_op_nan (sim_fpu *f,
 				     const sim_fpu *l, const sim_fpu *r);
+INLINE_SIM_FPU (int) sim_fpu_minmax_nan (sim_fpu *f,
+					 const sim_fpu *l, const sim_fpu *r);
 
 
 
@@ -302,7 +304,8 @@  INLINE_SIM_FPU (double) sim_fpu_2d (const sim_fpu *d);
 /* INLINE_SIM_FPU (void) sim_fpu_f2 (sim_fpu *f, float s); */
 INLINE_SIM_FPU (void) sim_fpu_d2 (sim_fpu *f, double d);
 
-
+/* IEEE754-2008 classifiction function.  */
+INLINE_SIM_FPU (int) sim_fpu_classify (const sim_fpu *f);
 
 /* Specific number classes.
 
@@ -343,6 +346,8 @@  INLINE_SIM_FPU (int) sim_fpu_eq (int *is, const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_ne (int *is, const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_ge (int *is, const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_gt (int *is, const sim_fpu *l, const sim_fpu *r);
+INLINE_SIM_FPU (int) sim_fpu_un (int *is, const sim_fpu *l, const sim_fpu *r);
+INLINE_SIM_FPU (int) sim_fpu_or (int *is, const sim_fpu *l, const sim_fpu *r);
 
 INLINE_SIM_FPU (int) sim_fpu_is_lt (const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_is_le (const sim_fpu *l, const sim_fpu *r);
@@ -350,8 +355,20 @@  INLINE_SIM_FPU (int) sim_fpu_is_eq (const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_is_ne (const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_is_ge (const sim_fpu *l, const sim_fpu *r);
 INLINE_SIM_FPU (int) sim_fpu_is_gt (const sim_fpu *l, const sim_fpu *r);
+INLINE_SIM_FPU (int) sim_fpu_is_un (const sim_fpu *l, const sim_fpu *r);
+INLINE_SIM_FPU (int) sim_fpu_is_or (const sim_fpu *l, const sim_fpu *r);
 
+/* Changes the behaviour of the library to IEEE754-2008 or IEEE754-1985.
+ * The default for the library is IEEE754-1985. */
+typedef enum
+{
+  sim_fpu_ieee754_1985,
+  sim_fpu_ieee754_2008,
+} sim_fpu_mode;
 
+INLINE_SIM_FPU (bool) sim_fpu_is_ieee754_1985 (void);
+INLINE_SIM_FPU (bool) sim_fpu_is_ieee754_2008 (void);
+INLINE_SIM_FPU (void) sim_fpu_set_mode (const sim_fpu_mode m);
 
 /* General number class and comparison operators.