Complete.Org: Mailing Lists: Archives: freeciv-dev: January 1999: [Freeciv-Dev] PATCH: inefficient math in common/map.c

# [Freeciv-Dev] PATCH: inefficient math in common/map.c

[Top] [All Lists]

 To: freeciv-dev@xxxxxxxxxxxx Subject: [Freeciv-Dev] PATCH: inefficient math in common/map.c From: Tony Stuckey Date: Fri, 29 Jan 1999 12:31:12 -0600

```        sq_map_distance in common/map.c has some inefficient math.
Since the quantity is squared, we don't care if y1-y0 is positive
or negative, so there's no reason to sort those values.  Also, not
performing the MIN operation should generate better assembly code.

Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.16
diff -u -r1.16 map.c
--- map.c       1998/10/31 10:22:21     1.16
+++ map.c       1999/01/29 18:18:37
@@ -124,11 +124,11 @@
if(x0>x1)
-    tmp=x0, x0=x1, x1=tmp;
-  if(y0>y1)
-    tmp=y0, y0=y1, y1=tmp;
-  return (((y1 - y0) * (y1 - y0)) +
-         (MIN(x1 - x0, map.xsize - x1 + x0) * MIN(x1 - x0, map.xsize - x1 +
x0)));
+    tmp=MIN(x0 - x1, map.xsize - x0 + x1);
+  else
+    tmp=MIN(x1 - x0, map.xsize - x1 + x0);
+       /* larger-smaller, and wraparound -larger + smaller. -AJS */
+  return (((y1 - y0) * (y1 - y0)) + (tmp * tmp));
}
/***************************************************************
...

--
Anthony J. Stuckey                              stuckey@xxxxxxxxxxxxxxxxx
"When I was young, the sky was filled with stars.
I watched them burn out one by one."  -Warren Zevon

```