Browse Source

Move vectorised functions from r.mapcalc into a separate library

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67658 15284696-431f-4ddb-bdfa-cd5b030d7da7
Glynn Clements 9 years ago
parent
commit
87274dfb09
71 changed files with 384 additions and 421 deletions
  1. 2 0
      include/Make/Grass.make
  2. 45 0
      include/calc.h
  3. 107 0
      include/defs/calc.h
  4. 2 1
      lib/Makefile
  5. 8 0
      lib/calc/Makefile
  6. 62 0
      lib/calc/calc.c
  7. 1 2
      raster/r.mapcalc/check.c
  8. 79 0
      lib/calc/function.c
  9. 1 3
      raster/r.mapcalc/xabs.c
  10. 1 3
      raster/r.mapcalc/xacos.c
  11. 1 3
      raster/r.mapcalc/xadd.c
  12. 1 3
      raster/r.mapcalc/xand.c
  13. 1 3
      raster/r.mapcalc/xand2.c
  14. 1 3
      raster/r.mapcalc/xasin.c
  15. 1 3
      raster/r.mapcalc/xatan.c
  16. 1 3
      raster/r.mapcalc/xbitand.c
  17. 1 3
      raster/r.mapcalc/xbitnot.c
  18. 1 3
      raster/r.mapcalc/xbitor.c
  19. 1 3
      raster/r.mapcalc/xbitxor.c
  20. 1 3
      raster/r.mapcalc/xcos.c
  21. 1 3
      raster/r.mapcalc/xdiv.c
  22. 1 3
      raster/r.mapcalc/xdouble.c
  23. 1 3
      raster/r.mapcalc/xeq.c
  24. 1 3
      raster/r.mapcalc/xeval.c
  25. 1 3
      raster/r.mapcalc/xexp.c
  26. 1 3
      raster/r.mapcalc/xfloat.c
  27. 1 3
      raster/r.mapcalc/xge.c
  28. 1 3
      raster/r.mapcalc/xgraph.c
  29. 1 3
      raster/r.mapcalc/xgt.c
  30. 1 3
      raster/r.mapcalc/xif.c
  31. 1 3
      raster/r.mapcalc/xint.c
  32. 1 3
      raster/r.mapcalc/xisnull.c
  33. 1 3
      raster/r.mapcalc/xle.c
  34. 1 3
      raster/r.mapcalc/xlog.c
  35. 1 3
      raster/r.mapcalc/xlt.c
  36. 1 3
      raster/r.mapcalc/xmax.c
  37. 1 3
      raster/r.mapcalc/xmedian.c
  38. 1 3
      raster/r.mapcalc/xmin.c
  39. 1 3
      raster/r.mapcalc/xmod.c
  40. 1 3
      raster/r.mapcalc/xmode.c
  41. 1 3
      raster/r.mapcalc/xmul.c
  42. 1 3
      raster/r.mapcalc/xne.c
  43. 1 3
      raster/r.mapcalc/xneg.c
  44. 1 3
      raster/r.mapcalc/xnmax.c
  45. 1 3
      raster/r.mapcalc/xnmedian.c
  46. 1 3
      raster/r.mapcalc/xnmin.c
  47. 1 3
      raster/r.mapcalc/xnmode.c
  48. 1 3
      raster/r.mapcalc/xnot.c
  49. 1 3
      raster/r.mapcalc/xnull.c
  50. 1 3
      raster/r.mapcalc/xor.c
  51. 1 3
      raster/r.mapcalc/xor2.c
  52. 1 3
      raster/r.mapcalc/xpow.c
  53. 1 3
      raster/r.mapcalc/xrand.c
  54. 1 3
      raster/r.mapcalc/xround.c
  55. 1 3
      raster/r.mapcalc/xshiftl.c
  56. 1 3
      raster/r.mapcalc/xshiftr.c
  57. 1 3
      raster/r.mapcalc/xshiftru.c
  58. 1 3
      raster/r.mapcalc/xsin.c
  59. 1 3
      raster/r.mapcalc/xsqrt.c
  60. 1 3
      raster/r.mapcalc/xsub.c
  61. 1 3
      raster/r.mapcalc/xtan.c
  62. 2 2
      raster/r.mapcalc/Makefile
  63. 1 1
      raster/r.mapcalc/evaluate.c
  64. 7 3
      raster/r.mapcalc/expression.c
  65. 3 27
      raster/r.mapcalc/expression.h
  66. 0 96
      raster/r.mapcalc/func_proto.h
  67. 7 75
      raster/r.mapcalc/function.c
  68. 1 4
      raster/r.mapcalc/globals.h
  69. 0 51
      raster/r.mapcalc/main.c
  70. 2 0
      raster/r.mapcalc/map.c
  71. 2 0
      raster/r.mapcalc/map3.c

+ 2 - 0
include/Make/Grass.make

@@ -114,6 +114,7 @@ libs = \
 	BTREE:btree \
 	BTREE2:btree2 \
 	CAIRODRIVER:cairodriver \
+	CALC:calc \
 	CDHC:cdhc \
 	CCMATH:ccmath \
 	CLUSTER:cluster \
@@ -183,6 +184,7 @@ ARRAYSTATSDEPS   = $(GISLIB) $(MATHLIB)
 BITMAPDEPS       = $(LINKMLIB)
 BTREE2DEPS       = $(GISLIB)
 CAIRODRIVERDEPS  = $(DRIVERLIB) $(GISLIB) $(CAIROLIB) $(FCLIB) $(ICONVLIB)
+CALCDEPS         = $(RASTERLIB) $(GISLIB) $(MATHLIB)
 CDHCDEPS         = $(MATHLIB)
 CLUSTERDEPS      = $(IMAGERYLIB) $(RASTERLIB) $(GISLIB) $(MATHLIB)
 DBMIBASEDEPS     = $(GISLIB)

+ 45 - 0
include/calc.h

@@ -0,0 +1,45 @@
+
+#ifndef GRASS_CALC_H
+#define GRASS_CALC_H
+
+#include <grass/gis.h>
+#include <grass/raster.h>
+
+typedef int func_t(int argc, const int *argt, void **args);
+typedef int args_t(int argc, int *argt);
+
+enum {
+    E_ARG_LO	= 1,
+    E_ARG_HI	= 2,
+    E_ARG_TYPE	= 3,
+    E_RES_TYPE	= 4,
+    E_INV_TYPE	= 5,
+    E_ARG_NUM	= 6,
+    E_WTF	= 99
+};
+
+typedef struct func_desc
+{
+    const char *name;
+    args_t *check_args;
+    func_t *func;
+} func_desc;
+
+#define IS_NULL_C(x) (Rast_is_c_null_value((x)))
+#define IS_NULL_F(x) (Rast_is_f_null_value((x)))
+#define IS_NULL_D(x) (Rast_is_d_null_value((x)))
+
+#define SET_NULL_C(x) (Rast_set_c_null_value((x),1))
+#define SET_NULL_F(x) (Rast_set_f_null_value((x),1))
+#define SET_NULL_D(x) (Rast_set_d_null_value((x),1))
+
+extern volatile int floating_point_exception;
+extern volatile int floating_point_exception_occurred;
+
+extern int columns;
+
+extern func_desc calc_func_descs[];
+
+#include <grass/defs/calc.h>
+
+#endif

+ 107 - 0
include/defs/calc.h

@@ -0,0 +1,107 @@
+
+#ifndef GRASS_CALCDEFS_H
+#define GRASS_CALCDEFS_H
+
+extern void calc_init(int);
+extern void pre_exec(void);
+extern void post_exec(void);
+
+extern func_t f_add;
+extern func_t f_sub;
+extern func_t f_mul;
+extern func_t f_div;
+extern func_t f_mod;
+extern func_t f_pow;
+extern args_t c_binop;
+
+extern func_t f_neg;
+extern func_t f_abs;
+extern args_t c_unop;
+
+extern func_t f_gt;
+extern func_t f_ge;
+extern func_t f_lt;
+extern func_t f_le;
+extern func_t f_eq;
+extern func_t f_ne;
+extern args_t c_cmpop;
+
+extern func_t f_and;
+extern func_t f_or;
+extern func_t f_and2;
+extern func_t f_or2;
+extern func_t f_bitand;
+extern func_t f_bitor;
+extern func_t f_bitxor;
+extern args_t c_logop;
+
+extern func_t f_shiftl;
+extern func_t f_shiftr;
+extern func_t f_shiftru;
+extern args_t c_shiftop;
+
+extern func_t f_not;
+extern func_t f_bitnot;
+extern args_t c_not;
+
+extern func_t f_sqrt;
+extern func_t f_sin;
+extern func_t f_cos;
+extern func_t f_tan;
+extern func_t f_acos;
+extern func_t f_asin;
+extern args_t c_double1;
+
+extern func_t f_exp;
+extern func_t f_log;
+extern func_t f_atan;
+extern args_t c_double12;
+
+extern func_t f_int;
+extern args_t c_int;
+
+extern func_t f_float;
+extern args_t c_float;
+
+extern func_t f_double;
+extern args_t c_double;
+
+extern func_t f_round;
+extern args_t c_round;
+
+extern func_t f_eval;
+extern args_t c_eval;
+
+extern func_t f_if;
+extern args_t c_if;
+
+extern func_t f_isnull;
+extern args_t c_isnull;
+
+extern func_t f_graph;
+extern func_t f_graph2;
+extern args_t c_graph;
+
+extern func_t f_min;
+extern func_t f_max;
+extern func_t f_nmin;
+extern func_t f_nmax;
+extern args_t c_varop;
+
+extern func_t f_median;
+extern func_t f_nmedian;
+extern args_t c_median;
+
+extern func_t f_mode;
+extern func_t f_nmode;
+extern args_t c_mode;
+
+extern func_t f_rand;
+extern args_t c_binop;
+
+extern func_t f_null;
+extern args_t c_int0;
+
+extern args_t c_double0;
+
+#endif

+ 2 - 1
lib/Makefile

@@ -43,7 +43,8 @@ SUBDIRS = \
 	temporal \
 	python \
 	iostream \
-	manage
+	manage \
+	calc
 
 include $(MODULE_TOPDIR)/include/Make/Dir.make
 include $(MODULE_TOPDIR)/include/Make/Doxygen.make

+ 8 - 0
lib/calc/Makefile

@@ -0,0 +1,8 @@
+MODULE_TOPDIR = ../..
+
+
+LIB = CALC
+
+include $(MODULE_TOPDIR)/include/Make/Lib.make
+
+default: lib

+ 62 - 0
lib/calc/calc.c

@@ -0,0 +1,62 @@
+
+#include <unistd.h>
+#include <signal.h>
+
+#include <grass/calc.h>
+
+/****************************************************************************/
+
+volatile int floating_point_exception;
+volatile int floating_point_exception_occurred;
+
+int columns;
+
+/****************************************************************************/
+
+static RETSIGTYPE handle_fpe(int n)
+{
+    floating_point_exception = 1;
+    floating_point_exception_occurred = 1;
+}
+
+void pre_exec(void)
+{
+#ifndef __MINGW32__
+#ifdef SIGFPE
+    struct sigaction act;
+
+    act.sa_handler = &handle_fpe;
+    act.sa_flags = 0;
+    sigemptyset(&act.sa_mask);
+
+    sigaction(SIGFPE, &act, NULL);
+#endif
+#endif
+
+    floating_point_exception_occurred = 0;
+}
+
+void post_exec(void)
+{
+#ifndef __MINGW32__
+#ifdef SIGFPE
+    struct sigaction act;
+
+    act.sa_handler = SIG_DFL;
+    act.sa_flags = 0;
+    sigemptyset(&act.sa_mask);
+
+    sigaction(SIGFPE, &act, NULL);
+#endif
+#endif
+}
+
+/****************************************************************************/
+
+void calc_init(int cols)
+{
+    columns = cols;
+}
+
+/****************************************************************************/
+

+ 1 - 2
raster/r.mapcalc/check.c

@@ -1,6 +1,5 @@
 
-#include "mapcalc.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 int c_int0(int argc, int *argt)
 {

+ 79 - 0
lib/calc/function.c

@@ -0,0 +1,79 @@
+
+#include <grass/calc.h>
+
+func_desc calc_func_descs[] = {
+    {"add", c_varop, f_add},
+    {"sub", c_binop, f_sub},
+    {"mul", c_varop, f_mul},
+    {"div", c_binop, f_div},
+    {"mod", c_binop, f_mod},
+    {"pow", c_binop, f_pow},
+
+    {"neg", c_unop, f_neg},
+    {"abs", c_unop, f_abs},
+
+    {"gt", c_cmpop, f_gt},
+    {"ge", c_cmpop, f_ge},
+    {"lt", c_cmpop, f_lt},
+    {"le", c_cmpop, f_le},
+    {"eq", c_cmpop, f_eq},
+    {"ne", c_cmpop, f_ne},
+
+    {"and", c_logop, f_and},
+    {"or", c_logop, f_or},
+
+    {"and2", c_logop, f_and2},
+    {"or2", c_logop, f_or2},
+
+    {"not", c_not, f_not},
+
+    {"bitand", c_logop, f_bitand},
+    {"bitor", c_logop, f_bitor},
+    {"xor", c_logop, f_bitxor},
+
+    {"shiftl", c_shiftop, f_shiftl},
+    {"shiftr", c_shiftop, f_shiftr},
+    {"shiftru", c_shiftop, f_shiftru},
+
+    {"bitnot", c_not, f_bitnot},
+
+    {"sqrt", c_double1, f_sqrt},
+    {"sin", c_double1, f_sin},
+    {"cos", c_double1, f_cos},
+    {"tan", c_double1, f_tan},
+    {"acos", c_double1, f_acos},
+    {"asin", c_double1, f_asin},
+
+    {"exp", c_double12, f_exp},
+    {"log", c_double12, f_log},
+    {"atan", c_double12, f_atan},
+
+    {"int", c_int, f_int},
+    {"float", c_float, f_float},
+    {"double", c_double, f_double},
+    {"round", c_round, f_round},
+
+    {"eval", c_eval, f_eval},
+    {"if", c_if, f_if},
+    {"isnull", c_isnull, f_isnull},
+
+    {"max", c_varop, f_max},
+    {"min", c_varop, f_min},
+    {"median", c_varop, f_median},
+    {"mode", c_varop, f_mode},
+
+    {"nmax", c_varop, f_nmax},
+    {"nmin", c_varop, f_nmin},
+    {"nmedian", c_varop, f_nmedian},
+    {"nmode", c_varop, f_nmode},
+
+    {"graph", c_graph, f_graph},
+    {"graph2", c_graph, f_graph2},
+
+    {"rand", c_binop, f_rand},
+
+    {"null", c_int0, f_null},
+
+    {NULL, NULL, NULL}
+};
+

+ 1 - 3
raster/r.mapcalc/xabs.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 abs(x)

+ 1 - 3
raster/r.mapcalc/xacos.c

@@ -4,9 +4,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 acos(x)  [0 and PI]

+ 1 - 3
raster/r.mapcalc/xadd.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 add(a,b,c,...) = a + b + c + ...

+ 1 - 3
raster/r.mapcalc/xand.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 and(a,b,c,...) = a && b && c && ...

+ 1 - 3
raster/r.mapcalc/xand2.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 and2(a,b,c,...) = a && b && c && ...

+ 1 - 3
raster/r.mapcalc/xasin.c

@@ -4,9 +4,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 asin(x)  range [-90,90]

+ 1 - 3
raster/r.mapcalc/xatan.c

@@ -4,9 +4,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 atan(x)     range [-90,90]

+ 1 - 3
raster/r.mapcalc/xbitand.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 bitand(a,b,c,...) = a & b & c & ...

+ 1 - 3
raster/r.mapcalc/xbitnot.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 bitnot(a) = ~a

+ 1 - 3
raster/r.mapcalc/xbitor.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 bitor(a,b,c,...) = a | b | c | ...

+ 1 - 3
raster/r.mapcalc/xbitxor.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 bitxor(a,b,c,...) = a ^ b ^ c ^ ...

+ 1 - 3
raster/r.mapcalc/xcos.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 cos(x) 

+ 1 - 3
raster/r.mapcalc/xdiv.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 div(a,b) = a / b

+ 1 - 3
raster/r.mapcalc/xdouble.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 double(x)

+ 1 - 3
raster/r.mapcalc/xeq.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 eq(a,b) = a == b

+ 1 - 3
raster/r.mapcalc/xeval.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 eval(..,..,..,x) = x

+ 1 - 3
raster/r.mapcalc/xexp.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 exp(x)   computes e raised to power x

+ 1 - 3
raster/r.mapcalc/xfloat.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 float(x)

+ 1 - 3
raster/r.mapcalc/xge.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 ge(a,b) = a >= b

+ 1 - 3
raster/r.mapcalc/xgraph.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 graph(x, x1,y1, x2,y2, ... xn,yn)  returns y value based on graph

+ 1 - 3
raster/r.mapcalc/xgt.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 gt(a,b) = a > b

+ 1 - 3
raster/r.mapcalc/xif.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /********************************************************************
  if(a)        1,0,1  1 if a is non zero, 0 otherwise

+ 1 - 3
raster/r.mapcalc/xint.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 int(x)

+ 1 - 3
raster/r.mapcalc/xisnull.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 isnull(x)

+ 1 - 3
raster/r.mapcalc/xle.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 le(a,b) = a <= b

+ 1 - 3
raster/r.mapcalc/xlog.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 log(x) 

+ 1 - 3
raster/r.mapcalc/xlt.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 lt(a,b) = a < b

+ 1 - 3
raster/r.mapcalc/xmax.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 max(x0,x1,...,xn) returns maximum value

+ 1 - 3
raster/r.mapcalc/xmedian.c

@@ -4,9 +4,7 @@
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 median(x1,x2,..,xn)

+ 1 - 3
raster/r.mapcalc/xmin.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 min(x0,x1,...,xn) returns minimum value

+ 1 - 3
raster/r.mapcalc/xmod.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 mod(a,b) = a % b

+ 1 - 3
raster/r.mapcalc/xmode.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 mode(x1,x2,..,xn)

+ 1 - 3
raster/r.mapcalc/xmul.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 mul(a,b) = a * b

+ 1 - 3
raster/r.mapcalc/xne.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 ne(a,b) = a != b

+ 1 - 3
raster/r.mapcalc/xneg.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 neg(x) = -x

+ 1 - 3
raster/r.mapcalc/xnmax.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 max(x0,x1,...,xn) returns maximum value

+ 1 - 3
raster/r.mapcalc/xnmedian.c

@@ -4,9 +4,7 @@
 #include <grass/gis.h>
 #include <grass/raster.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 median(x1,x2,..,xn)

+ 1 - 3
raster/r.mapcalc/xnmin.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 min(x0,x1,...,xn) returns minimum value

+ 1 - 3
raster/r.mapcalc/xnmode.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 mode(x1,x2,..,xn)

+ 1 - 3
raster/r.mapcalc/xnot.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 not(a) = !a

+ 1 - 3
raster/r.mapcalc/xnull.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 null() null values

+ 1 - 3
raster/r.mapcalc/xor.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 or(a,b,c,...) = a || b || c || ...

+ 1 - 3
raster/r.mapcalc/xor2.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 or2(a,b,c,...) = a || b || c || ...

+ 1 - 3
raster/r.mapcalc/xpow.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 pow(a,b)

+ 1 - 3
raster/r.mapcalc/xrand.c

@@ -4,9 +4,7 @@
 #include <grass/config.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 rand(lo,hi) random values between a and b

+ 1 - 3
raster/r.mapcalc/xround.c

@@ -4,9 +4,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 round(x, step, start)

+ 1 - 3
raster/r.mapcalc/xshiftl.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 shiftl(a,b) = a << b

+ 1 - 3
raster/r.mapcalc/xshiftr.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 shiftr(a,b) = a >> b

+ 1 - 3
raster/r.mapcalc/xshiftru.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 shiftru(a,b) = (unsigned) a >> b

+ 1 - 3
raster/r.mapcalc/xsin.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 sin(x) 

+ 1 - 3
raster/r.mapcalc/xsqrt.c

@@ -3,9 +3,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 sqrt(x) 

+ 1 - 3
raster/r.mapcalc/xsub.c

@@ -1,9 +1,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /****************************************************************
 sub(a,b) = a - b

+ 1 - 3
raster/r.mapcalc/xtan.c

@@ -4,9 +4,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
-#include "globals.h"
-#include "expression.h"
-#include "func_proto.h"
+#include <grass/calc.h>
 
 /**********************************************************************
 tan(x) 

+ 2 - 2
raster/r.mapcalc/Makefile

@@ -12,8 +12,8 @@ r3_mapcalc_OBJS := $(filter-out map.o xcoor.o xres.o, $(AUTO_OBJS))
 include $(MODULE_TOPDIR)/include/Make/Multi.make
 
 EXTRA_CFLAGS = $(READLINEINCPATH) $(PTHREADINCPATH)
-LIBES2 =           $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
-LIBES3 = $(RASTER3DLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
+LIBES2 = $(CALCLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
+LIBES3 = $(CALCLIB) $(RASTER3DLIB) $(GISLIB) $(RASTERLIB) $(BTREELIB) $(READLINELIBPATH) $(READLINELIB) $(HISTORYLIB) $(PTHREADLIBPATH) $(PTHREADLIB)
 
 default: multi
 

+ 1 - 1
raster/r.mapcalc/evaluate.c

@@ -13,7 +13,7 @@
 /****************************************************************************/
 
 int current_depth, current_row;
-int depths, rows, columns;
+int depths, rows;
 
 /****************************************************************************/
 

+ 7 - 3
raster/r.mapcalc/expression.c

@@ -19,9 +19,13 @@ static func_desc *find_func(const char *name)
 {
     int i;
 
-    for (i = 0; func_descs[i].name; i++) {
-	if (strcmp(name, func_descs[i].name) == 0)
-	    return &func_descs[i];
+    for (i = 0; local_func_descs[i].name; i++) {
+	if (strcmp(name, local_func_descs[i].name) == 0)
+	    return &local_func_descs[i];
+    }
+    for (i = 0; calc_func_descs[i].name; i++) {
+	if (strcmp(name, calc_func_descs[i].name) == 0)
+	    return &calc_func_descs[i];
     }
     return NULL;
 }

+ 3 - 27
raster/r.mapcalc/expression.h

@@ -2,18 +2,9 @@
 #ifndef __EXPRESSION_H_
 #define __EXPRESSION_H_
 
-struct expr_list;
-
-typedef int func_t(int argc, const int *argt, void **args);
-typedef int args_t(int argc, int *argt);
+#include <grass/calc.h>
 
-#define E_ARG_LO	1
-#define E_ARG_HI	2
-#define E_ARG_TYPE	3
-#define E_RES_TYPE	4
-#define E_INV_TYPE	5
-#define E_ARG_NUM	6
-#define E_WTF		99
+struct expr_list;
 
 typedef enum expr_t
 {
@@ -85,13 +76,6 @@ typedef struct expr_list
     struct expr_list *next;
 } expr_list;
 
-typedef struct func_desc
-{
-    const char *name;
-    args_t *check_args;
-    func_t *func;
-} func_desc;
-
 extern int list_length(expr_list * l);
 extern void define_variable(expression * e);
 extern char *composite(const char *name, const char *mapset);
@@ -110,14 +94,6 @@ extern expression *operator(const char *name, const char *oper, int prec,
 extern expression *function(const char *name, expr_list * args);
 extern expression *binding(const char *var, expression * val);
 
-extern func_desc func_descs[];
-
-#define IS_NULL_C(x) (Rast_is_c_null_value((x)))
-#define IS_NULL_F(x) (Rast_is_f_null_value((x)))
-#define IS_NULL_D(x) (Rast_is_d_null_value((x)))
-
-#define SET_NULL_C(x) (Rast_set_c_null_value((x),1))
-#define SET_NULL_F(x) (Rast_set_f_null_value((x),1))
-#define SET_NULL_D(x) (Rast_set_d_null_value((x),1))
+extern func_desc local_func_descs[];
 
 #endif /* __EXPRESSION_H_ */

+ 0 - 96
raster/r.mapcalc/func_proto.h

@@ -1,104 +1,9 @@
 
 #include "expression.h"
 
-extern func_t f_add;
-extern func_t f_sub;
-extern func_t f_mul;
-extern func_t f_div;
-extern func_t f_mod;
-extern func_t f_pow;
-extern args_t c_binop;
-
-extern func_t f_neg;
-extern func_t f_abs;
-extern args_t c_unop;
-
-extern func_t f_gt;
-extern func_t f_ge;
-extern func_t f_lt;
-extern func_t f_le;
-extern func_t f_eq;
-extern func_t f_ne;
-extern args_t c_cmpop;
-
-extern func_t f_and;
-extern func_t f_or;
-extern func_t f_and2;
-extern func_t f_or2;
-extern func_t f_bitand;
-extern func_t f_bitor;
-extern func_t f_bitxor;
-extern args_t c_logop;
-
-extern func_t f_shiftl;
-extern func_t f_shiftr;
-extern func_t f_shiftru;
-extern args_t c_shiftop;
-
-extern func_t f_not;
-extern func_t f_bitnot;
-extern args_t c_not;
-
-extern func_t f_sqrt;
-extern func_t f_sin;
-extern func_t f_cos;
-extern func_t f_tan;
-extern func_t f_acos;
-extern func_t f_asin;
-extern args_t c_double1;
-
-extern func_t f_exp;
-extern func_t f_log;
-extern func_t f_atan;
-extern args_t c_double12;
-
-extern func_t f_int;
-extern args_t c_int;
-
-extern func_t f_float;
-extern args_t c_float;
-
-extern func_t f_double;
-extern args_t c_double;
-
-extern func_t f_round;
-extern args_t c_round;
-
-extern func_t f_eval;
-extern args_t c_eval;
-
-extern func_t f_if;
-extern args_t c_if;
-
-extern func_t f_isnull;
-extern args_t c_isnull;
-
-extern func_t f_graph;
-extern func_t f_graph2;
-extern args_t c_graph;
-
-extern func_t f_min;
-extern func_t f_max;
-extern func_t f_nmin;
-extern func_t f_nmax;
-extern args_t c_varop;
-
-extern func_t f_median;
-extern func_t f_nmedian;
-extern args_t c_median;
-
-extern func_t f_mode;
-extern func_t f_nmode;
-extern args_t c_mode;
-
-extern func_t f_rand;
-extern args_t c_binop;
-
-extern func_t f_null;
 extern func_t f_col;
 extern func_t f_row;
 extern func_t f_depth;
-extern args_t c_int0;
 
 extern func_t f_x;
 extern func_t f_y;
@@ -106,4 +11,3 @@ extern func_t f_z;
 extern func_t f_ewres;
 extern func_t f_nsres;
 extern func_t f_tbres;
-extern args_t c_double0;

+ 7 - 75
raster/r.mapcalc/function.c

@@ -7,79 +7,7 @@
 #include "expression.h"
 #include "func_proto.h"
 
-func_desc func_descs[] = {
-    {"add", c_varop, f_add},
-    {"sub", c_binop, f_sub},
-    {"mul", c_varop, f_mul},
-    {"div", c_binop, f_div},
-    {"mod", c_binop, f_mod},
-    {"pow", c_binop, f_pow},
-
-    {"neg", c_unop, f_neg},
-    {"abs", c_unop, f_abs},
-
-    {"gt", c_cmpop, f_gt},
-    {"ge", c_cmpop, f_ge},
-    {"lt", c_cmpop, f_lt},
-    {"le", c_cmpop, f_le},
-    {"eq", c_cmpop, f_eq},
-    {"ne", c_cmpop, f_ne},
-
-    {"and", c_logop, f_and},
-    {"or", c_logop, f_or},
-
-    {"and2", c_logop, f_and2},
-    {"or2", c_logop, f_or2},
-
-    {"not", c_not, f_not},
-
-    {"bitand", c_logop, f_bitand},
-    {"bitor", c_logop, f_bitor},
-    {"xor", c_logop, f_bitxor},
-
-    {"shiftl", c_shiftop, f_shiftl},
-    {"shiftr", c_shiftop, f_shiftr},
-    {"shiftru", c_shiftop, f_shiftru},
-
-    {"bitnot", c_not, f_bitnot},
-
-    {"sqrt", c_double1, f_sqrt},
-    {"sin", c_double1, f_sin},
-    {"cos", c_double1, f_cos},
-    {"tan", c_double1, f_tan},
-    {"acos", c_double1, f_acos},
-    {"asin", c_double1, f_asin},
-
-    {"exp", c_double12, f_exp},
-    {"log", c_double12, f_log},
-    {"atan", c_double12, f_atan},
-
-    {"int", c_int, f_int},
-    {"float", c_float, f_float},
-    {"double", c_double, f_double},
-    {"round", c_round, f_round},
-
-    {"eval", c_eval, f_eval},
-    {"if", c_if, f_if},
-    {"isnull", c_isnull, f_isnull},
-
-    {"max", c_varop, f_max},
-    {"min", c_varop, f_min},
-    {"median", c_varop, f_median},
-    {"mode", c_varop, f_mode},
-
-    {"nmax", c_varop, f_nmax},
-    {"nmin", c_varop, f_nmin},
-    {"nmedian", c_varop, f_nmedian},
-    {"nmode", c_varop, f_nmode},
-
-    {"graph", c_graph, f_graph},
-    {"graph2", c_graph, f_graph2},
-
-    {"rand", c_binop, f_rand},
-
-    {"null", c_int0, f_null},
-
+func_desc local_func_descs[] = {
     {"col", c_int0, f_col},
     {"row", c_int0, f_row},
     {"depth", c_int0, f_depth},
@@ -87,9 +15,11 @@ func_desc func_descs[] = {
     {"x", c_double0, f_x},
     {"y", c_double0, f_y},
     {"z", c_double0, f_z},
+
     {"ewres", c_double0, f_ewres},
     {"nsres", c_double0, f_nsres},
     {"tbres", c_double0, f_tbres},
+
     {NULL, NULL, NULL}
 };
 
@@ -98,7 +28,9 @@ void print_function_names(void)
     int i;
 
     fprintf(stderr, _("Known functions:"));
-    for (i = 0; func_descs[i].name; i++)
-	fprintf(stderr, "%c%-10s", i % 7 ? ' ' : '\n', func_descs[i].name);
+    for (i = 0; calc_func_descs[i].name; i++)
+	fprintf(stderr, "%c%-10s", i % 7 ? ' ' : '\n', calc_func_descs[i].name);
+    for (i = 0; local_func_descs[i].name; i++)
+	fprintf(stderr, "%c%-10s", i % 7 ? ' ' : '\n', local_func_descs[i].name);
     fprintf(stderr, "\n");
 }

+ 1 - 4
raster/r.mapcalc/globals.h

@@ -2,14 +2,11 @@
 #ifndef __GLOBALS_H_
 #define __GLOBALS_H_
 
-extern volatile int floating_point_exception;
-extern volatile int floating_point_exception_occurred;
-extern int overflow_occurred;
 extern int overwrite_flag;
 extern long seed_value;
 extern long seeded;
 
 extern int current_depth, current_row;
-extern int depths, rows, columns;
+extern int depths, rows;
 
 #endif /* __GLOBALS_H_ */

+ 0 - 51
raster/r.mapcalc/main.c

@@ -14,7 +14,6 @@
  *****************************************************************************/
 
 #include <unistd.h>
-#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -25,12 +24,8 @@
 
 /****************************************************************************/
 
-int overflow_occurred;
 int overwrite_flag;
 
-volatile int floating_point_exception;
-volatile int floating_point_exception_occurred;
-
 long seed_value;
 long seeded;
 
@@ -40,47 +35,6 @@ static expr_list *result;
 
 /****************************************************************************/
 
-static RETSIGTYPE handle_fpe(int n)
-{
-    floating_point_exception = 1;
-    floating_point_exception_occurred = 1;
-}
-
-static void pre_exec(void)
-{
-#ifndef __MINGW32__
-#ifdef SIGFPE
-    struct sigaction act;
-
-    act.sa_handler = &handle_fpe;
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-
-    sigaction(SIGFPE, &act, NULL);
-#endif
-#endif
-
-    floating_point_exception_occurred = 0;
-    overflow_occurred = 0;
-}
-
-static void post_exec(void)
-{
-#ifndef __MINGW32__
-#ifdef SIGFPE
-    struct sigaction act;
-
-    act.sa_handler = SIG_DFL;
-    act.sa_flags = 0;
-    sigemptyset(&act.sa_mask);
-
-    sigaction(SIGFPE, &act, NULL);
-#endif
-#endif
-}
-
-/****************************************************************************/
-
 static expr_list *parse_file(const char *filename)
 {
     expr_list *res;
@@ -206,11 +160,6 @@ int main(int argc, char **argv)
 	all_ok = 0;
     }
 
-    if (overflow_occurred) {
-	G_warning(_("Overflow occurred in the calculation"));
-	all_ok = 0;
-    }
-
     return all_ok ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 

+ 2 - 0
raster/r.mapcalc/map.c

@@ -14,6 +14,7 @@
 #include <grass/raster.h>
 #include <grass/btree.h>
 #include <grass/glocale.h>
+#include <grass/calc.h>
 
 #include "mapcalc.h"
 #include "globals.h"
@@ -29,6 +30,7 @@ void setup_region(void)
 
     rows = Rast_window_rows();
     columns = Rast_window_cols();
+    calc_init(columns);
     depths = 1;
 }
 

+ 2 - 0
raster/r.mapcalc/map3.c

@@ -10,6 +10,7 @@
 #include <grass/raster3d.h>
 #include <grass/btree.h>
 #include <grass/glocale.h>
+#include <grass/calc.h>
 
 #include "mapcalc.h"
 #include "globals.h"
@@ -27,6 +28,7 @@ void setup_region(void)
     rows = current_region3.rows;
     columns = current_region3.cols;
     depths = current_region3.depths;
+    calc_init(columns);
 }
 
 /****************************************************************************/