From e64557a46cf908fe664b4638787926bb116a7d53 Mon Sep 17 00:00:00 2001
From: Simon Walton <simon@highfidelity.io>
Date: Fri, 2 Nov 2018 11:52:40 -0700
Subject: [PATCH 1/2] Clamp the fixed-point network values to their limits

---
 libraries/shared/src/GLMHelpers.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libraries/shared/src/GLMHelpers.cpp b/libraries/shared/src/GLMHelpers.cpp
index 1a62227c31..39ee4d2d1d 100644
--- a/libraries/shared/src/GLMHelpers.cpp
+++ b/libraries/shared/src/GLMHelpers.cpp
@@ -9,6 +9,7 @@
 //  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
 //
 
+#include <limits>
 #include "GLMHelpers.h"
 #include <glm/gtc/matrix_transform.hpp>
 #include "NumericalConstants.h"
@@ -76,9 +77,11 @@ glm::quat safeMix(const glm::quat& q1, const glm::quat& q2, float proportion) {
 
 // Allows sending of fixed-point numbers: radix 1 makes 15.1 number, radix 8 makes 8.8 number, etc
 int packFloatScalarToSignedTwoByteFixed(unsigned char* buffer, float scalar, int radix) {
-    int16_t twoByteFixed = (int16_t)(scalar * (float)(1 << radix));
-    memcpy(buffer, &twoByteFixed, sizeof(int16_t));
-    return sizeof(int16_t);
+    using FixedType = int16_t;
+    FixedType twoByteFixed = (FixedType) glm::clamp(scalar * (1 << radix), (float)std::numeric_limits<FixedType>::min(),
+        (float)std::numeric_limits<FixedType>::max());
+    memcpy(buffer, &twoByteFixed, sizeof(FixedType));
+    return sizeof(FixedType);
 }
 
 int unpackFloatScalarFromSignedTwoByteFixed(const int16_t* byteFixedPointer, float* destinationPointer, int radix) {

From 26b9e2e162c96425250fca5f85dd93f21c1aeb49 Mon Sep 17 00:00:00 2001
From: Simon Walton <simon@highfidelity.io>
Date: Fri, 2 Nov 2018 14:47:40 -0700
Subject: [PATCH 2/2] Fix a URL while we're in the file

---
 libraries/shared/src/GLMHelpers.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libraries/shared/src/GLMHelpers.cpp b/libraries/shared/src/GLMHelpers.cpp
index 39ee4d2d1d..905bf3ccfd 100644
--- a/libraries/shared/src/GLMHelpers.cpp
+++ b/libraries/shared/src/GLMHelpers.cpp
@@ -44,8 +44,8 @@ const mat4 Matrices::X_180 { createMatFromQuatAndPos(Quaternions::X_180, Vectors
 const mat4 Matrices::Y_180 { createMatFromQuatAndPos(Quaternions::Y_180, Vectors::ZERO) };
 const mat4 Matrices::Z_180 { createMatFromQuatAndPos(Quaternions::Z_180, Vectors::ZERO) };
 
-//  Safe version of glm::mix; based on the code in Nick Bobick's article,
-//  http://www.gamasutra.com/features/19980703/quaternions_01.htm (via Clyde,
+//  Safe version of glm::mix; based on the code in Nick Bobic's article,
+//  https://www.gamasutra.com/view/feature/131686/rotating_objects_using_quaternions.php?page=1 (via Clyde,
 //  https://github.com/threerings/clyde/blob/master/src/main/java/com/threerings/math/Quaternion.java)
 glm::quat safeMix(const glm::quat& q1, const glm::quat& q2, float proportion) {
     float cosa = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;