From 12576d2eb55cb4591d9a2b6ecad5e83515f6ebb8 Mon Sep 17 00:00:00 2001
From: Randy Heit <rheit@zdoom.fake>
Date: Tue, 8 Sep 2009 23:48:34 +0000
Subject: [PATCH] - Because entryway timed it, here is a new version of
 R_PointToAngle2 that is   closer to the original. The old code was shorter
 but a little slower. The   new code is a bit faster than the original with
 VC++ and about the same   with GCC. Interestingly, GCC produces code for
 Killough's version that   performs about the same as the original, but when
 compiled with VC++,   Killough's is notably worse.

SVN r1813 (trunk)
---
 docs/rh-log.txt |  8 +++++++
 src/r_main.cpp  | 58 +++++++++++++++++++++++++++++--------------------
 src/tables.h    | 15 -------------
 3 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/docs/rh-log.txt b/docs/rh-log.txt
index 6bf28cda4..1ccb3031b 100644
--- a/docs/rh-log.txt
+++ b/docs/rh-log.txt
@@ -1,3 +1,11 @@
+September 8, 2009
+- Because entryway timed it, here is a new version of R_PointToAngle2 that is
+  closer to the original. The old code was shorter but a little slower. The
+  new code is a bit faster than the original with VC++ and about the same
+  with GCC. Interestingly, GCC produces code for Killough's version that
+  performs about the same as the original, but when compiled with VC++,
+  Killough's is notably worse.
+
 September 8, 2009  (Changes by Graf Zahl)
 - moved ENDOOM lump name definition into gameinfo.
 - moved default item drop style into gameinfo.
diff --git a/src/r_main.cpp b/src/r_main.cpp
index adac3dc19..d20ec9670 100644
--- a/src/r_main.cpp
+++ b/src/r_main.cpp
@@ -221,6 +221,27 @@ static bool NoInterpolateView;
 
 // CODE --------------------------------------------------------------------
 
+//==========================================================================
+//
+// SlopeDiv
+//
+// Utility function, called by R_PointToAngle.
+//
+//==========================================================================
+
+angle_t SlopeDiv (unsigned int num, unsigned den)
+{
+	unsigned int ans;
+
+	if (den < 512)
+		return (ANG45 - 1); //tantoangle[SLOPERANGE]
+
+	ans = (num << 3) / (den >> 8);
+
+	return ans <= SLOPERANGE ? tantoangle[ans] : (ANG45 - 1);
+}
+
+
 //==========================================================================
 //
 // R_PointToAngle
@@ -242,65 +263,56 @@ angle_t R_PointToAngle2 (fixed_t x1, fixed_t y1, fixed_t x, fixed_t y)
 		return 0;
 	}
 
-	fixed_t ax = abs (x);
-	fixed_t ay = abs (y);
-	int div;
-	angle_t angle;
-
-	if (ax > ay)
-	{
-		swap (ax, ay);
-	}
-	div = SlopeDiv (ax, ay);
-	angle = tantoangle[div];
-
 	if (x >= 0)
 	{
 		if (y >= 0)
 		{
 			if (x > y)
 			{ // octant 0
-				return angle;
+				return SlopeDiv(y, x);
 			}
 			else
 			{ // octant 1
-				return ANG90 - 1 - angle;
+				return ANG90 - 1 - SlopeDiv(x, y);
 			}
 		}
 		else // y < 0
 		{
-			if (x > -y)
+			y = -y;
+			if (x > y)
 			{ // octant 8
-				return (angle_t)-(SDWORD)angle;
+				return 0 - SlopeDiv(y, x);
 			}
 			else
 			{ // octant 7
-				return ANG270 + angle;
+				return ANG270 + SlopeDiv(x, y);
 			}
 		}
 	}
 	else // x < 0
 	{
+		x = -x;
 		if (y >= 0)
 		{
-			if (-x > y)
+			if (x > y)
 			{ // octant 3
-				return ANG180 - 1 - angle;
+				return ANG180 - 1 - SlopeDiv(y, x);
 			}
 			else
 			{ // octant 2
-				return ANG90 + angle;
+				return ANG90 + SlopeDiv(x, y);
 			}
 		}
 		else // y < 0
 		{
-			if (x < y)
+			y = -y;
+			if (x > y)
 			{ // octant 4
-				return ANG180 + angle;
+				return ANG180 + SlopeDiv(y, x);
 			}
 			else
 			{ // octant 5
-				return ANG270 - 1 - angle;
+				return ANG270 - 1 - SlopeDiv(x, y);
 			}
 		}
 	}
diff --git a/src/tables.h b/src/tables.h
index 826333a1a..72a79383d 100644
--- a/src/tables.h
+++ b/src/tables.h
@@ -105,21 +105,6 @@ inline angle_t abs (angle_t ang)
 //	without additional checking.
 extern angle_t			tantoangle[SLOPERANGE+1];
 
-
-// Utility function,
-//	called by R_PointToAngle.
-inline int SlopeDiv (unsigned int num, unsigned den)
-{
-	unsigned int ans;
-
-	if (den < 512)
-		return SLOPERANGE;
-
-	ans = (num << 3) / (den >> 8);
-
-	return ans <= SLOPERANGE ? ans : SLOPERANGE;
-}
-
 inline double bam2rad(angle_t ang)
 {
 	return double(ang >> 1) * (PI / ANGLE_90);