[Gnucash-changes] -- make the gnc-numeric error codes be a typdef enum -- try to inline

Linas Vepstas linas at cvs.gnucash.org
Sat May 29 18:43:50 EDT 2004


Log Message:
-----------
-- make the gnc-numeric error codes be a typdef enum
-- try to inline the numeric-check for slightly better performance

Modified Files:
--------------
    gnucash/src/engine:
        gnc-numeric.c
        gnc-numeric.h

Revision Data
-------------
Index: gnc-numeric.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-numeric.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -Lsrc/engine/gnc-numeric.c -Lsrc/engine/gnc-numeric.c -u -r1.29 -r1.30
--- src/engine/gnc-numeric.c
+++ src/engine/gnc-numeric.c
@@ -53,6 +53,30 @@
 
 static gint64 gnc_numeric_lcd(gnc_numeric a, gnc_numeric b);
 
+/* This function is small, simple, and used everywhere below, 
+ * lets try to inline it.
+ */
+inline GNCNumericErrorCode
+gnc_numeric_check(gnc_numeric in) 
+{
+  if(in.denom != 0) 
+  {
+    return GNC_ERROR_OK;
+  }
+  else if(in.num) 
+  {
+    if ((0 < in.num) || (-4 > in.num))
+    {
+       in.num = (gint64) GNC_ERROR_OVERFLOW;
+    }
+    return (GNCNumericErrorCode) in.num;
+  }
+  else 
+  {
+    return GNC_ERROR_ARG;
+  }
+}
+
 /********************************************************************
  *  gnc_numeric_zero_p
  ********************************************************************/
@@ -375,10 +399,12 @@
   product.num   = a.num*b.num;
   product.denom = a.denom*b.denom;
   
+#if 0  /* currently, product denom won't ever be zero */
   if(product.denom < 0) {
     product.num   = -product.num;
     product.denom = -product.denom;
   }
+#endif
   
   if((denom == GNC_DENOM_AUTO) &&
      ((how & GNC_NUMERIC_DENOM_MASK) == GNC_DENOM_LCD)) 
@@ -963,10 +989,8 @@
  ********************************************************************/
 
 gnc_numeric
-gnc_numeric_error(int error_code) {
-  if(abs(error_code) < 5) {
-    /*    PERR("%s", _numeric_error_strings[ - error_code]); */
-  }
+gnc_numeric_error(GNCNumericErrorCode error_code) 
+{
   return gnc_numeric_create(error_code, 0LL);
 }
 
@@ -1084,19 +1108,6 @@
   return quot;
 }
 
-int
-gnc_numeric_check(gnc_numeric in) {
-  if(in.denom != 0) {
-    return GNC_ERROR_OK;
-  }
-  else if(in.num) {
-    return in.num;
-  }
-  else {
-    return GNC_ERROR_ARG;
-  }
-}
-
 /********************************************************************
  *  gnc_numeric text IO
  ********************************************************************/
Index: gnc-numeric.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-numeric.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -Lsrc/engine/gnc-numeric.h -Lsrc/engine/gnc-numeric.h -u -r1.15 -r1.16
--- src/engine/gnc-numeric.h
+++ src/engine/gnc-numeric.h
@@ -54,7 +54,10 @@
  * This is a rational number, defined by nominator and denominator. */
 typedef struct _gnc_numeric gnc_numeric;
 
-/** bitmasks for HOW flags */
+/** bitmasks for HOW flags.
+ * bits 8-15 of 'how' are reserved for the number of significant
+ * digits to use in the output with GNC_DENOM_SIGFIG */ 
+
 #define GNC_NUMERIC_RND_MASK     0x0000000f
 #define GNC_NUMERIC_DENOM_MASK   0x000000f0
 #define GNC_NUMERIC_SIGFIGS_MASK 0x0000ff00
@@ -80,17 +83,14 @@
   GNC_DENOM_SIGFIG = 0x50
 };
 
-/** bits 8-15 of 'how' are reserved for the number of significant
- * digits to use in the output with GNC_DENOM_SIGFIG */ 
-
-/** errors */
-enum {
-  GNC_ERROR_OK         =  0,
-  GNC_ERROR_ARG        = -1,
-  GNC_ERROR_OVERFLOW   = -2,
-  GNC_ERROR_DENOM_DIFF = -3,
-  GNC_ERROR_REMAINDER  = -4
-};
+/** Error codes */
+typedef enum {
+  GNC_ERROR_OK         =  0,   /**< No error */
+  GNC_ERROR_ARG        = -1,   /**< Argument is not a valid number */
+  GNC_ERROR_OVERFLOW   = -2,   /**< Intermediate result overflow */
+  GNC_ERROR_DENOM_DIFF = -3,   /**< Argument denoms differ in GNC_DENOM_FIXED operation */
+  GNC_ERROR_REMAINDER  = -4    /**< Remainder part in GNC_RND_NEVER operation */
+} GNCNumericErrorCode;
 
 #define GNC_DENOM_AUTO 0
 
@@ -116,7 +116,7 @@
 const gchar *string_to_gnc_numeric(const gchar* str, gnc_numeric *n);
 
 /** make a special error-signalling gnc_numeric */
-gnc_numeric gnc_numeric_error(int error_code);
+gnc_numeric gnc_numeric_error(GNCNumericErrorCode error_code);
 /*@}*/
 
 /** @name Value accessors */
@@ -137,9 +137,10 @@
 /** @name Tests */
 /*@{*/
 /** Check for error signal in value. Returns GNC_ERROR_OK (==0) if
- * there is no error, or any error code if there is one
- * (e.g. GNC_ERROR_OVERFLOW) */ 
-int         gnc_numeric_check(gnc_numeric a);
+ *  the number appears to be valid, otherwise it returns the
+ *  type of error.
+ */ 
+GNCNumericErrorCode  gnc_numeric_check(gnc_numeric a);
 
 /** Returns 1 if the given gnc_numeric is 0 (zeros), else returns 0. */
 int gnc_numeric_zero_p(gnc_numeric a);                


More information about the gnucash-changes mailing list