Pārlūkot izejas kodu

Optimisations:
Make Rast_is_*_null_value() macros
Make G_incr_void_ptr() a macro
Replace Rast__check_null_bit() with macro in get_row.c
Move Rast_cell_size() calls out of loops


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@40464 15284696-431f-4ddb-bdfa-cd5b030d7da7

Glynn Clements 15 gadi atpakaļ
vecāks
revīzija
7c8c38685b

+ 6 - 1
include/gisdefs.h

@@ -54,11 +54,16 @@ const char *G_adjust_Cell_head3(struct Cell_head *, int, int, int);
 const char *G_align_window(struct Cell_head *, const struct Cell_head *);
 
 /* alloc.c */
+#define G_incr_void_ptr(ptr, size) \
+    ((void *)((const unsigned char *)(ptr) + (size)))
+
 void *G__malloc(const char *, int, size_t);
 void *G__calloc(const char *, int, size_t, size_t);
 void *G__realloc(const char *, int, void *, size_t);
 void G_free(void *);
-void *G_incr_void_ptr(const void *, const size_t);
+#ifndef G_incr_void_ptr
+void *G_incr_void_ptr(const void *, size_t);
+#endif
 
 #define G_malloc(n)     G__malloc(__FILE__, __LINE__, (n))
 #define G_calloc(m, n)  G__calloc(__FILE__, __LINE__, (m), (n))

+ 13 - 0
include/rasterdefs.h

@@ -364,15 +364,28 @@ int Rast__mask_info(char *, char *);
 int Rast_maskfd(void);
 
 /* null_val.c */
+#define Rast_is_c_null_value(cellVal)	\
+    (*(const CELL *)(cellVal) == (CELL) 0x80000000)
+#define Rast_is_f_null_value(fcellVal)	\
+    (*(const FCELL *)(fcellVal) != *(const FCELL *)(fcellVal))
+#define Rast_is_d_null_value(dcellVal)	\
+    (*(const DCELL *)(dcellVal) != *(const DCELL *)(dcellVal))
+
 void Rast__set_null_value(void *, int, int, RASTER_MAP_TYPE);
 void Rast_set_null_value(void *, int, RASTER_MAP_TYPE);
 void Rast_set_c_null_value(CELL *, int);
 void Rast_set_f_null_value(FCELL *, int);
 void Rast_set_d_null_value(DCELL *, int);
 int Rast_is_null_value(const void *, RASTER_MAP_TYPE);
+#ifndef Rast_is_c_null_value
 int Rast_is_c_null_value(const CELL *);
+#endif
+#ifndef Rast_is_f_null_value
 int Rast_is_f_null_value(const FCELL *);
+#endif
+#ifndef Rast_is_f_null_value
 int Rast_is_d_null_value(const DCELL *);
+#endif
 void Rast_insert_null_values(void *, char *, int, RASTER_MAP_TYPE);
 void Rast_insert_c_null_values(CELL *, char *, int);
 void Rast_insert_f_null_values(FCELL *, char *, int);

+ 3 - 1
lib/gis/alloc.c

@@ -161,8 +161,10 @@ void G_free(void *buf)
  *
  * \return pointer to the data
  */
-void *G_incr_void_ptr(const void *ptr, const size_t size)
+#ifndef G_incr_void_ptr
+void *G_incr_void_ptr(const void *ptr, size_t size)
 {
     /* assuming that the size of unsigned char is 1 */
     return (void *)((const unsigned char *)ptr + size);
 }
+#endif

+ 4 - 3
lib/raster/alloc_cell.c

@@ -15,6 +15,7 @@
 
 #include <grass/gis.h>
 #include <grass/raster.h>
+#include <grass/glocale.h>
 
 /* convert type "RASTER_MAP_TYPE" into index */
 #define F2I(map_type) \
@@ -142,13 +143,13 @@ unsigned char *Rast__allocate_null_bits(int cols)
  *
  * \param cols number of columns
  *
- * \return -1 if <i>cols</i> is invalid (<= 0)
  * \return size of null bistream
  */
 int Rast__null_bitstream_size(int cols)
 {
     if (cols <= 0)
-	return -1;
+	G_fatal_error(_("Rast__null_bitstream_size: cols (%d) is negative"),
+		      cols);
 
-    return (cols / 8 + (cols % 8 != 0));
+    return (cols + 7) / 8;
 }

+ 2 - 1
lib/raster/cats.c

@@ -554,6 +554,7 @@ int Rast_mark_cats(const void *rast_row,
 		   int ncols, struct Categories *pcats,
 		   RASTER_MAP_TYPE data_type)
 {
+    size_t size = Rast_cell_size(data_type);
     CELL i;
 
     while (ncols-- > 0) {
@@ -564,7 +565,7 @@ int Rast_mark_cats(const void *rast_row,
 	if (i > pcats->ncats)
 	    return -1;
 	pcats->marks[i]++;
-	rast_row = G_incr_void_ptr(rast_row, Rast_cell_size(data_type));
+	rast_row = G_incr_void_ptr(rast_row, size);
     }
     return 1;
 }

+ 4 - 4
lib/raster/color_look.c

@@ -206,7 +206,8 @@ void Rast__lookup_colors(const void *raster, unsigned char *red,
     int found, r, g, b;
     int cell_type;
     int lookup, max_ind, min_ind, try;
-    int (*lower) ();
+    int (*lower)();
+    size_t size = Rast_cell_size(data_type);
 
     if (mod)
 	cp = &colors->modular;
@@ -246,9 +247,8 @@ void Rast__lookup_colors(const void *raster, unsigned char *red,
     ptr = raster;
 
     for (; n-- > 0;
-	 ptr =
-	 G_incr_void_ptr(ptr, Rast_cell_size(data_type)), red++, grn++, blu++,
-	 *set++ = found) {
+	 ptr = G_incr_void_ptr(ptr, size),
+	     red++, grn++, blu++, *set++ = found) {
 	/* if the cell is the same as last one, use the prev color values */
 	if (ptr != raster && Rast_raster_cmp(ptr, last_ptr, data_type) == 0) {
 	    *red = *(red - 1);

+ 5 - 4
lib/raster/get_row.c

@@ -872,6 +872,8 @@ static int read_null_bits(int fd, int row)
     return 1;
 }
 
+#define check_null_bit(flags, bit_num) ((flags)[(bit_num)>>3] & ((unsigned char)0x80>>((bit_num)&7)) ? 1 : 0)
+
 static void get_null_value_row_nomask(int fd, char *flags, int row)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
@@ -913,9 +915,7 @@ static void get_null_value_row_nomask(int fd, char *flags, int row)
 	if (!fcb->col_map[j])
 	    flags[j] = 1;
 	else
-	    flags[j] = Rast__check_null_bit(fcb->null_bits,
-					    fcb->col_map[j] - 1,
-					    fcb->cellhd.cols);
+	    flags[j] = check_null_bit(fcb->null_bits, fcb->col_map[j] - 1);
     }
 }
 
@@ -991,6 +991,7 @@ static void embed_nulls(int fd, void *buf, int row, RASTER_MAP_TYPE map_type,
 			int null_is_zero, int with_mask)
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
+    size_t size = Rast_cell_size(map_type);
     char *null_buf;
     int i;
 
@@ -1012,7 +1013,7 @@ static void embed_nulls(int fd, void *buf, int row, RASTER_MAP_TYPE map_type,
 	       is not set and calls G_set_[f/d]_null_value() otherwise */
 	    Rast__set_null_value(buf, 1, null_is_zero, map_type);
 	}
-	buf = G_incr_void_ptr(buf, Rast_cell_size(map_type));
+	buf = G_incr_void_ptr(buf, size);
     }
 
     G__freea(null_buf);

+ 19 - 16
lib/raster/null_val.c

@@ -205,11 +205,13 @@ int Rast_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
    \return TRUE if CELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_c_null_value
 int Rast_is_c_null_value(const CELL * cellVal)
 {
     /* Check if the CELL value matches the null pattern */
     return *cellVal == (CELL) 0x80000000;
 }
+#endif
 
 /*!
    \brief To check if a FCELL raster value is set to NULL
@@ -236,10 +238,12 @@ int Rast_is_c_null_value(const CELL * cellVal)
    \return TRUE if FCELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_f_null_value
 int Rast_is_f_null_value(const FCELL * fcellVal)
 {
     return *fcellVal != *fcellVal;
 }
+#endif
 
 /*!
    \brief To check if a DCELL raster value is set to NULL
@@ -253,10 +257,12 @@ int Rast_is_f_null_value(const FCELL * fcellVal)
    \return TRUE if DCELL raster value is NULL
    \return FALSE otherwise
  */
+#ifndef Rast_is_f_null_value
 int Rast_is_d_null_value(const DCELL * dcellVal)
 {
     return *dcellVal != *dcellVal;
 }
+#endif
 
 /*!
    \brief To insert null values into a map.
@@ -323,32 +329,29 @@ void Rast_insert_d_null_values(DCELL * dcellVal, char *null_row, int ncols)
 
    Note: Only for internal use.
 
-   \param flags ?
-   \param bit_num ?
-   \param n ?
+   \param flags null bitmap
+   \param bit_num index of bit to check
+   \param n size of null bitmap (in bits)
 
-   \return -1 on error
+   \return 1 if set, 0 if unset
  */
 int Rast__check_null_bit(const unsigned char *flags, int bit_num, int n)
 {
     int ind;
     int offset;
 
+    /* check that bit_num is in range */
+    if (bit_num < 0 || bit_num >= n)
+	G_fatal_error("Rast__check_null_bit: index %d out of range (size = %d).",
+		      bit_num, n);
+
+
     /* find the index of the unsigned char in which this bit appears */
-    ind = Rast__null_bitstream_size(bit_num + 1) - 1;
-
-    /* find how many unsigned chars the buffer with bit_num+1 (counting from 0
-       has and subtract 1 to get unsigned char index */
-    if (ind > Rast__null_bitstream_size(n) - 1) {
-	G_warning("Rast__check_null_bit: Unable to access index %d. "
-		  "Size of flags is %d (bit # is %d)",
-		  ind, Rast__null_bitstream_size(n) - 1, bit_num);
-	return -1;
-    }
+    ind = bit_num / 8;
 
-    offset = (ind + 1) * 8 - bit_num - 1;
+    offset = bit_num & 7;
 
-    return ((flags[ind] & ((unsigned char)1 << offset)) != 0);
+    return ((flags[ind] & ((unsigned char)0x80 >> offset)) != 0);
 }
 
 /*!

+ 4 - 3
lib/raster/range.c

@@ -463,7 +463,8 @@ void Rast_row_update_fp_range(const void *rast, int n,
 			      struct FPRange *range,
 			      RASTER_MAP_TYPE data_type)
 {
-    DCELL val = 0L;
+    size_t size = Rast_cell_size(data_type);
+    DCELL val = 0.0;
 
     while (n-- > 0) {
 	switch (data_type) {
@@ -479,7 +480,7 @@ void Rast_row_update_fp_range(const void *rast, int n,
 	}
 
 	if (Rast_is_null_value(rast, data_type)) {
-	    rast = G_incr_void_ptr(rast, Rast_cell_size(data_type));
+	    rast = G_incr_void_ptr(rast, size);
 	    continue;
 	}
 	if (range->first_time) {
@@ -494,7 +495,7 @@ void Rast_row_update_fp_range(const void *rast, int n,
 		range->max = val;
 	}
 
-	rast = G_incr_void_ptr(rast, Rast_cell_size(data_type));
+	rast = G_incr_void_ptr(rast, size);
     }
 }
 

+ 2 - 3
lib/raster/set_window.c

@@ -96,9 +96,8 @@ int Rast_set_window(struct Cell_head *window)
 	else {
 	    /* opened for writing */
 	    G_free(fcb->data);
-	    fcb->data = (unsigned char *)G_calloc(G__.window.cols,
-						  Rast_cell_size(fcb->
-								 map_type));
+	    fcb->data = G_calloc(G__.window.cols,
+				 Rast_cell_size(fcb-> map_type));
 	}
 
 	/* allocate null bitstream buffers for reading/writing null rows */

+ 3 - 10
lib/raster/zero_cell.c

@@ -11,6 +11,7 @@
  * \author Original author CERL
  */
 
+#include <string.h>
 #include <grass/gis.h>
 #include <grass/raster.h>
 
@@ -40,17 +41,9 @@ void Rast_zero_c_buf(CELL * buf)
  * using Rast_allocate_c_buf().
  *
  * \param rast data buffer
- * \param data_type raster type (CELL, FCELL, DCELL)
+ * \param data_type raster type (CELL_TYPE, FCELL_TYPE, DCELL_TYPE)
  */
 void Rast_zero_buf(void *rast, RASTER_MAP_TYPE data_type)
 {
-    int i;
-    unsigned char *ptr;
-
-    /* assuming that the size of unsigned char is 1 byte */
-    i = G_window_cols() * Rast_cell_size(data_type);
-    ptr = (unsigned char *)rast;
-
-    while (i--)
-	*ptr++ = 0;
+    memset(rast, 0, G_window_cols() * Rast_cell_size(data_type));
 }