| | | 1 | | //======================================================================= |
| | | 2 | | // WideNormalization.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp.Geometry; |
| | | 9 | | |
| | | 10 | | using System; |
| | | 11 | | |
| | | 12 | | namespace FixedMathSharp; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides high-precision normalization and direction calculations |
| | | 16 | | /// for geometric vectors using wide (extended-precision) arithmetic. |
| | | 17 | | /// </summary> |
| | | 18 | | internal static class WideNormalization |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Returns the normalized direction between two 2D endpoints without |
| | | 22 | | /// narrowing their component differences. |
| | | 23 | | /// </summary> |
| | | 24 | | internal static Vector2d GetDirection(Vector2d start, Vector2d end) |
| | | 25 | | { |
| | 257 | 26 | | Signed192 x = WideArithmetic.Difference(end.X, start.X); |
| | 257 | 27 | | Signed192 y = WideArithmetic.Difference(end.Y, start.Y); |
| | 257 | 28 | | return GetNormalized(x, y); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns the nearest representable normalized direction for a nonzero 2D |
| | | 33 | | /// vector using its exact raw components. |
| | | 34 | | /// </summary> |
| | | 35 | | internal static Vector2d GetNormalized(Vector2d value) |
| | | 36 | | { |
| | | 37 | | // Cardinal vectors (including zero) need neither a square root nor |
| | | 38 | | // division, even when the nonzero raw component is MinValue. |
| | 573 | 39 | | if (value.X == Fixed64.Zero || value.Y == Fixed64.Zero) |
| | 327 | 40 | | return new Vector2d((Fixed64)Fixed64.Sign(value.X), (Fixed64)Fixed64.Sign(value.Y)); |
| | | 41 | | |
| | 246 | 42 | | return GetNormalized( |
| | 246 | 43 | | Signed192.Signed(value.X.m_rawValue), |
| | 246 | 44 | | Signed192.Signed(value.Y.m_rawValue)); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static Vector2d GetNormalized(Signed192 x, Signed192 y) |
| | | 48 | | { |
| | 6047 | 49 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 6047 | 50 | | x, |
| | 6047 | 51 | | y, |
| | 6047 | 52 | | default, |
| | 6047 | 53 | | out Signed320 xSquare, |
| | 6047 | 54 | | out Signed320 ySquare, |
| | 6047 | 55 | | out _); |
| | 6047 | 56 | | if (squaredMagnitude.IsZero) |
| | 1267 | 57 | | return Vector2d.Zero; |
| | | 58 | | |
| | 4780 | 59 | | if (Max(GetMagnitudeBitLength(x), GetMagnitudeBitLength(y)) |
| | 4780 | 60 | | <= FixedMath.SHIFT_AMOUNT_I + 1) |
| | | 61 | | { |
| | 551 | 62 | | return Vector2d.GetScaleNormalized(new Vector2d( |
| | 551 | 63 | | Fixed64.FromRaw(unchecked((long)x.Low)), |
| | 551 | 64 | | Fixed64.FromRaw(unchecked((long)y.Low)))); |
| | | 65 | | } |
| | | 66 | | |
| | 4229 | 67 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 4229 | 68 | | squaredMagnitude, |
| | 4229 | 69 | | out Signed192 remainder); |
| | 4229 | 70 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 4229 | 71 | | ? magnitude |
| | 4229 | 72 | | : WideArithmetic.AddSigned192( |
| | 4229 | 73 | | magnitude, |
| | 4229 | 74 | | Signed192.Signed(1L)); |
| | 4229 | 75 | | return new Vector2d( |
| | 4229 | 76 | | Fixed64.NormalizeWideComponent( |
| | 4229 | 77 | | x, |
| | 4229 | 78 | | xSquare, |
| | 4229 | 79 | | ceilingMagnitude, |
| | 4229 | 80 | | squaredMagnitude), |
| | 4229 | 81 | | Fixed64.NormalizeWideComponent( |
| | 4229 | 82 | | y, |
| | 4229 | 83 | | ySquare, |
| | 4229 | 84 | | ceilingMagnitude, |
| | 4229 | 85 | | squaredMagnitude)); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Returns the nearest representable normalized direction for exact 2D |
| | | 90 | | /// components wider than the public scalar domain. |
| | | 91 | | /// </summary> |
| | | 92 | | internal static Vector2d GetNormalized(Signed320 x, Signed320 y) |
| | | 93 | | { |
| | 7 | 94 | | Signed320 largest = |
| | 7 | 95 | | WideArithmetic.CompareMagnitude(x, y) >= 0 ? x : y; |
| | 7 | 96 | | if (largest.IsZero) |
| | 1 | 97 | | return Vector2d.Zero; |
| | | 98 | | |
| | 6 | 99 | | Signed320 scale = GetPositiveMagnitude(largest); |
| | 6 | 100 | | return Vector2d.GetScaleNormalized(new Vector2d( |
| | 6 | 101 | | Fixed64.GetSignedRatio(x, scale), |
| | 6 | 102 | | Fixed64.GetSignedRatio(y, scale))); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Returns the nearest representable normalized direction for exact 2D |
| | | 107 | | /// components in the nine-word finite-axis domain. |
| | | 108 | | /// </summary> |
| | | 109 | | internal static Vector2d GetNormalized(Signed576 x, Signed576 y) |
| | | 110 | | { |
| | 1389 | 111 | | Signed576 xMagnitude = GetPositiveMagnitude(x); |
| | 1389 | 112 | | Signed576 yMagnitude = GetPositiveMagnitude(y); |
| | 1389 | 113 | | Signed576 scale = |
| | 1389 | 114 | | WideArithmetic.CompareNonNegative(xMagnitude, yMagnitude) >= 0 |
| | 1389 | 115 | | ? xMagnitude |
| | 1389 | 116 | | : yMagnitude; |
| | 1389 | 117 | | if (scale.IsZero) |
| | 735 | 118 | | return Vector2d.Zero; |
| | | 119 | | |
| | 654 | 120 | | Signed320 fixedScale = Signed320.ExtendValue( |
| | 654 | 121 | | Signed192.Signed(Fixed64.One.m_rawValue)); |
| | 654 | 122 | | Signed704 denominator = Signed704.ExtendValue(scale); |
| | 654 | 123 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 654 | 124 | | WideArithmetic.MultiplySigned576ToSigned704(x, fixedScale), |
| | 654 | 125 | | denominator, |
| | 654 | 126 | | out Fixed64 scaledX); |
| | 654 | 127 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 654 | 128 | | WideArithmetic.MultiplySigned576ToSigned704(y, fixedScale), |
| | 654 | 129 | | denominator, |
| | 654 | 130 | | out Fixed64 scaledY); |
| | 654 | 131 | | return Vector2d.GetScaleNormalized(new Vector2d(scaledX, scaledY)); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary>Normalizes exact scaled-frame 2D components using nearest-even bounded component ratios.</summary> |
| | | 135 | | internal static Vector2d GetNormalized(Signed832 x, Signed832 y) |
| | | 136 | | { |
| | 11 | 137 | | Signed832 ax = x.Sign < 0 ? WideArithmetic.SubtractSigned832(default, x) : x; |
| | 11 | 138 | | Signed832 ay = y.Sign < 0 ? WideArithmetic.SubtractSigned832(default, y) : y; |
| | 11 | 139 | | Signed832 scale = WideArithmetic.SubtractSigned832(ax, ay).Sign >= 0 ? ax : ay; |
| | 12 | 140 | | if (scale.IsZero) return Vector2d.Zero; |
| | | 141 | | // Keep the Q32 rescale transient so even full-width inputs cannot |
| | | 142 | | // overflow the persistent component carrier. |
| | 20 | 143 | | Span<ulong> values = stackalloc ulong[6 * 14]; Span<int> signs = stackalloc int[6]; |
| | 10 | 144 | | var numerator = ContactQuadratic.At(values, signs, 0, 14); |
| | 10 | 145 | | var denominator = ContactQuadratic.At(values, signs, 1, 14); |
| | 10 | 146 | | var scaled = ContactQuadratic.At(values, signs, 2, 14); |
| | 20 | 147 | | denominator.Set(scale); numerator.Set(x); |
| | 10 | 148 | | ContactQuadratic.Scale(numerator, Signed576.ExtendValue(Signed192.Raw(Fixed64.One)), scaled); |
| | 10 | 149 | | _ = Fixed64.TryGetSignedRawRatio(scaled.Rational, denominator.Rational, scaled.Signs[0] < 0, out Fixed64 sx); |
| | 10 | 150 | | numerator.Set(y); |
| | 10 | 151 | | ContactQuadratic.Scale(numerator, Signed576.ExtendValue(Signed192.Raw(Fixed64.One)), scaled); |
| | 10 | 152 | | _ = Fixed64.TryGetSignedRawRatio(scaled.Rational, denominator.Rational, scaled.Signs[0] < 0, out Fixed64 sy); |
| | 10 | 153 | | return Vector2d.GetScaleNormalized(new Vector2d(sx, sy)); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Returns the normalized direction between two 3D endpoints without |
| | | 158 | | /// narrowing their component differences. |
| | | 159 | | /// </summary> |
| | | 160 | | internal static Vector3d GetDirection(Vector3d start, Vector3d end) |
| | | 161 | | { |
| | 4 | 162 | | Signed192 x = WideArithmetic.Difference(end.X, start.X); |
| | 4 | 163 | | Signed192 y = WideArithmetic.Difference(end.Y, start.Y); |
| | 4 | 164 | | Signed192 z = WideArithmetic.Difference(end.Z, start.Z); |
| | 4 | 165 | | return GetNormalized(x, y, z); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Returns the nearest representable normalized direction for a nonzero 3D |
| | | 170 | | /// vector using its exact raw components. |
| | | 171 | | /// </summary> |
| | | 172 | | internal static Vector3d GetNormalized(Vector3d value) |
| | | 173 | | { |
| | 11207 | 174 | | bool zeroX = value.X == Fixed64.Zero; |
| | 11207 | 175 | | bool zeroY = value.Y == Fixed64.Zero; |
| | 11207 | 176 | | bool zeroZ = value.Z == Fixed64.Zero; |
| | 11207 | 177 | | if ((zeroX && zeroY) || (zeroX && zeroZ) || (zeroY && zeroZ)) |
| | 8180 | 178 | | return new Vector3d((Fixed64)Fixed64.Sign(value.X), |
| | 8180 | 179 | | (Fixed64)Fixed64.Sign(value.Y), (Fixed64)Fixed64.Sign(value.Z)); |
| | | 180 | | |
| | 3027 | 181 | | return GetNormalized( |
| | 3027 | 182 | | Signed192.Signed(value.X.m_rawValue), |
| | 3027 | 183 | | Signed192.Signed(value.Y.m_rawValue), |
| | 3027 | 184 | | Signed192.Signed(value.Z.m_rawValue)); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | internal static Vector3d GetNormalized( |
| | | 188 | | Signed192 x, |
| | | 189 | | Signed192 y, |
| | | 190 | | Signed192 z) |
| | | 191 | | { |
| | 3048 | 192 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 3048 | 193 | | x, |
| | 3048 | 194 | | y, |
| | 3048 | 195 | | z, |
| | 3048 | 196 | | out Signed320 xSquare, |
| | 3048 | 197 | | out Signed320 ySquare, |
| | 3048 | 198 | | out Signed320 zSquare); |
| | 3048 | 199 | | if (squaredMagnitude.IsZero) |
| | 1 | 200 | | return Vector3d.Zero; |
| | | 201 | | |
| | 3047 | 202 | | if (Max( |
| | 3047 | 203 | | Max( |
| | 3047 | 204 | | GetMagnitudeBitLength(x), |
| | 3047 | 205 | | GetMagnitudeBitLength(y)), |
| | 3047 | 206 | | GetMagnitudeBitLength(z)) |
| | 3047 | 207 | | <= FixedMath.SHIFT_AMOUNT_I + 1) |
| | | 208 | | { |
| | 3026 | 209 | | return Vector3d.GetScaleNormalized(new Vector3d( |
| | 3026 | 210 | | Fixed64.FromRaw(unchecked((long)x.Low)), |
| | 3026 | 211 | | Fixed64.FromRaw(unchecked((long)y.Low)), |
| | 3026 | 212 | | Fixed64.FromRaw(unchecked((long)z.Low)))); |
| | | 213 | | } |
| | | 214 | | |
| | 21 | 215 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 21 | 216 | | squaredMagnitude, |
| | 21 | 217 | | out Signed192 remainder); |
| | 21 | 218 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 21 | 219 | | ? magnitude |
| | 21 | 220 | | : WideArithmetic.AddSigned192( |
| | 21 | 221 | | magnitude, |
| | 21 | 222 | | Signed192.Signed(1L)); |
| | 21 | 223 | | return new Vector3d( |
| | 21 | 224 | | Fixed64.NormalizeWideComponent( |
| | 21 | 225 | | x, |
| | 21 | 226 | | xSquare, |
| | 21 | 227 | | ceilingMagnitude, |
| | 21 | 228 | | squaredMagnitude), |
| | 21 | 229 | | Fixed64.NormalizeWideComponent( |
| | 21 | 230 | | y, |
| | 21 | 231 | | ySquare, |
| | 21 | 232 | | ceilingMagnitude, |
| | 21 | 233 | | squaredMagnitude), |
| | 21 | 234 | | Fixed64.NormalizeWideComponent( |
| | 21 | 235 | | z, |
| | 21 | 236 | | zSquare, |
| | 21 | 237 | | ceilingMagnitude, |
| | 21 | 238 | | squaredMagnitude)); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Returns the nearest representable normalized direction for exact 3D |
| | | 243 | | /// components wider than the public scalar domain. |
| | | 244 | | /// </summary> |
| | | 245 | | internal static Vector3d GetNormalized( |
| | | 246 | | Signed320 x, |
| | | 247 | | Signed320 y, |
| | | 248 | | Signed320 z) |
| | | 249 | | { |
| | 2006 | 250 | | Signed320 largest = |
| | 2006 | 251 | | WideArithmetic.CompareMagnitude(x, y) >= 0 ? x : y; |
| | 2006 | 252 | | if (WideArithmetic.CompareMagnitude(z, largest) > 0) |
| | 455 | 253 | | largest = z; |
| | 2006 | 254 | | if (largest.IsZero) |
| | 23 | 255 | | return Vector3d.Zero; |
| | | 256 | | |
| | 1983 | 257 | | Signed320 scale = GetPositiveMagnitude(largest); |
| | 1983 | 258 | | return Vector3d.GetScaleNormalized(new Vector3d( |
| | 1983 | 259 | | Fixed64.GetSignedRatio(x, scale), |
| | 1983 | 260 | | Fixed64.GetSignedRatio(y, scale), |
| | 1983 | 261 | | Fixed64.GetSignedRatio(z, scale))); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Returns the nearest representable normalized direction for exact 3D |
| | | 266 | | /// components in the nine-word finite-axis domain. |
| | | 267 | | /// </summary> |
| | | 268 | | internal static Vector3d GetNormalized( |
| | | 269 | | Signed576 x, |
| | | 270 | | Signed576 y, |
| | | 271 | | Signed576 z) |
| | | 272 | | { |
| | 1783 | 273 | | Signed576 xMagnitude = GetPositiveMagnitude(x); |
| | 1783 | 274 | | Signed576 yMagnitude = GetPositiveMagnitude(y); |
| | 1783 | 275 | | Signed576 zMagnitude = GetPositiveMagnitude(z); |
| | 1783 | 276 | | Signed576 scale = |
| | 1783 | 277 | | WideArithmetic.CompareNonNegative(xMagnitude, yMagnitude) >= 0 |
| | 1783 | 278 | | ? xMagnitude |
| | 1783 | 279 | | : yMagnitude; |
| | 1783 | 280 | | if (WideArithmetic.CompareNonNegative(zMagnitude, scale) > 0) |
| | 457 | 281 | | scale = zMagnitude; |
| | 1783 | 282 | | if (scale.IsZero) |
| | 50 | 283 | | return Vector3d.Zero; |
| | | 284 | | |
| | | 285 | | // A positive scale fitting Signed320 bounds every component away from |
| | | 286 | | // its signed minimum; the smaller owner uses the same nearest-even ratios. |
| | 1733 | 287 | | if (Signed320.TryNarrowSigned(scale, out _)) |
| | 1701 | 288 | | return GetNormalized(Signed320.NarrowValue(x), Signed320.NarrowValue(y), Signed320.NarrowValue(z)); |
| | | 289 | | |
| | 32 | 290 | | Signed320 fixedScale = Signed320.ExtendValue( |
| | 32 | 291 | | Signed192.Signed(Fixed64.One.m_rawValue)); |
| | 32 | 292 | | Signed704 denominator = Signed704.ExtendValue(scale); |
| | 32 | 293 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 32 | 294 | | WideArithmetic.MultiplySigned576ToSigned704(x, fixedScale), |
| | 32 | 295 | | denominator, |
| | 32 | 296 | | out Fixed64 scaledX); |
| | 32 | 297 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 32 | 298 | | WideArithmetic.MultiplySigned576ToSigned704(y, fixedScale), |
| | 32 | 299 | | denominator, |
| | 32 | 300 | | out Fixed64 scaledY); |
| | 32 | 301 | | _ = Fixed64.TryGetSignedRawRatio( |
| | 32 | 302 | | WideArithmetic.MultiplySigned576ToSigned704(z, fixedScale), |
| | 32 | 303 | | denominator, |
| | 32 | 304 | | out Fixed64 scaledZ); |
| | 32 | 305 | | return Vector3d.GetScaleNormalized( |
| | 32 | 306 | | new Vector3d(scaledX, scaledY, scaledZ)); |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary>Normalizes exact 13-word components through one common scale before scalar normalization.</summary> |
| | | 310 | | internal static Vector3d GetNormalized(Signed832 x, Signed832 y, Signed832 z) |
| | | 311 | | { |
| | 41 | 312 | | Span<ulong> values = stackalloc ulong[4 * 13]; |
| | 164 | 313 | | Span<ulong> mx = values[..13], my = values.Slice(13, 13), mz = values.Slice(26, 13), maximum = values.Slice(39, |
| | 123 | 314 | | WideArithmetic.GetMagnitude(x, mx); WideArithmetic.GetMagnitude(y, my); WideArithmetic.GetMagnitude(z, mz); |
| | 41 | 315 | | (WideArithmetic.CompareMagnitudeEqualLength(mx, my) >= 0 ? mx : my).CopyTo(maximum); |
| | 44 | 316 | | if (WideArithmetic.CompareMagnitudeEqualLength(mz, maximum) > 0) mz.CopyTo(maximum); |
| | 50 | 317 | | if (WideArithmetic.GetActiveMagnitudeLength(maximum) == 0) return Vector3d.Zero; |
| | 32 | 318 | | Span<ulong> numerator = stackalloc ulong[14]; |
| | 32 | 319 | | Vector3d result = default; |
| | 32 | 320 | | Span<int> signs = stackalloc int[3] { x.Sign, y.Sign, z.Sign }; |
| | 256 | 321 | | for (int axis = 0; axis < 3; axis++) |
| | | 322 | | { |
| | 192 | 323 | | numerator.Clear(); int sign = 0; |
| | 96 | 324 | | WideArithmetic.AddShiftedSignedMagnitude(values.Slice(axis * 13, 13), 1, 32, numerator, ref sign); |
| | 96 | 325 | | _ = Fixed64.TryGetSignedRawRatio(numerator, maximum, signs[axis] < 0, out Fixed64 component); |
| | 96 | 326 | | result[axis] = component; |
| | | 327 | | } |
| | 32 | 328 | | return Vector3d.GetScaleNormalized(result); |
| | | 329 | | } |
| | | 330 | | /// <summary> |
| | | 331 | | /// Returns the nearest representable normalized direction for a nonzero 4D |
| | | 332 | | /// vector using its exact raw components. |
| | | 333 | | /// </summary> |
| | | 334 | | internal static Vector4d GetNormalized(Vector4d value) |
| | | 335 | | { |
| | 15 | 336 | | Signed192 x = Signed192.Signed(value.X.m_rawValue); |
| | 15 | 337 | | Signed192 y = Signed192.Signed(value.Y.m_rawValue); |
| | 15 | 338 | | Signed192 z = Signed192.Signed(value.Z.m_rawValue); |
| | 15 | 339 | | Signed192 w = Signed192.Signed(value.W.m_rawValue); |
| | 15 | 340 | | Signed320 squaredMagnitude = WideGeometry.GetSquaredMagnitude( |
| | 15 | 341 | | x, |
| | 15 | 342 | | y, |
| | 15 | 343 | | z, |
| | 15 | 344 | | out Signed320 xSquare, |
| | 15 | 345 | | out Signed320 ySquare, |
| | 15 | 346 | | out Signed320 zSquare); |
| | 15 | 347 | | Signed320 wSquare = WideArithmetic.MultiplySigned192(w, w); |
| | 15 | 348 | | squaredMagnitude = |
| | 15 | 349 | | WideArithmetic.AddSigned320(squaredMagnitude, wSquare); |
| | 15 | 350 | | if (squaredMagnitude.IsZero) |
| | 2 | 351 | | return Vector4d.Zero; |
| | | 352 | | |
| | 13 | 353 | | Signed192 magnitude = WideArithmetic.GetFloorSquareRoot( |
| | 13 | 354 | | squaredMagnitude, |
| | 13 | 355 | | out Signed192 remainder); |
| | 13 | 356 | | Signed192 ceilingMagnitude = remainder.IsZero |
| | 13 | 357 | | ? magnitude |
| | 13 | 358 | | : WideArithmetic.AddSigned192( |
| | 13 | 359 | | magnitude, |
| | 13 | 360 | | Signed192.Signed(1L)); |
| | 13 | 361 | | return new Vector4d( |
| | 13 | 362 | | Fixed64.NormalizeWideComponent( |
| | 13 | 363 | | x, |
| | 13 | 364 | | xSquare, |
| | 13 | 365 | | ceilingMagnitude, |
| | 13 | 366 | | squaredMagnitude), |
| | 13 | 367 | | Fixed64.NormalizeWideComponent( |
| | 13 | 368 | | y, |
| | 13 | 369 | | ySquare, |
| | 13 | 370 | | ceilingMagnitude, |
| | 13 | 371 | | squaredMagnitude), |
| | 13 | 372 | | Fixed64.NormalizeWideComponent( |
| | 13 | 373 | | z, |
| | 13 | 374 | | zSquare, |
| | 13 | 375 | | ceilingMagnitude, |
| | 13 | 376 | | squaredMagnitude), |
| | 13 | 377 | | Fixed64.NormalizeWideComponent( |
| | 13 | 378 | | w, |
| | 13 | 379 | | wSquare, |
| | 13 | 380 | | ceilingMagnitude, |
| | 13 | 381 | | squaredMagnitude)); |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Returns the nearest representable unit quaternion for nonzero raw |
| | | 386 | | /// components. The zero quaternion retains its public identity fallback. |
| | | 387 | | /// </summary> |
| | | 388 | | internal static FixedQuaternion GetNormalized(FixedQuaternion value) |
| | | 389 | | { |
| | 9 | 390 | | Vector4d normalized = GetNormalized( |
| | 9 | 391 | | new Vector4d(value.X, value.Y, value.Z, value.W)); |
| | 9 | 392 | | return normalized.IsZero |
| | 9 | 393 | | ? FixedQuaternion.Identity |
| | 9 | 394 | | : new FixedQuaternion( |
| | 9 | 395 | | normalized.X, |
| | 9 | 396 | | normalized.Y, |
| | 9 | 397 | | normalized.Z, |
| | 9 | 398 | | normalized.W); |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | private static Signed576 GetPositiveMagnitude(Signed576 value) => |
| | 8127 | 402 | | value.Sign < 0 |
| | 8127 | 403 | | ? WideArithmetic.SubtractSigned576(default, value) |
| | 8127 | 404 | | : value; |
| | | 405 | | |
| | | 406 | | private static Signed320 GetPositiveMagnitude(Signed320 value) |
| | | 407 | | { |
| | 1989 | 408 | | WideArithmetic.GetMagnitude( |
| | 1989 | 409 | | value, |
| | 1989 | 410 | | out ulong word4, |
| | 1989 | 411 | | out ulong word3, |
| | 1989 | 412 | | out ulong word2, |
| | 1989 | 413 | | out ulong word1, |
| | 1989 | 414 | | out ulong word0); |
| | 1989 | 415 | | return new Signed320(word4, word3, word2, word1, word0); |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | private static int GetMagnitudeBitLength(Signed192 value) |
| | | 419 | | { |
| | 18701 | 420 | | WideArithmetic.GetMagnitude( |
| | 18701 | 421 | | value, |
| | 18701 | 422 | | out ulong high, |
| | 18701 | 423 | | out ulong middle, |
| | 18701 | 424 | | out ulong low); |
| | 18701 | 425 | | return WideArithmetic.GetBitLength(high, middle, low); |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | private static int Max(int left, int right) => |
| | 10874 | 429 | | left >= right ? left : right; |
| | | 430 | | } |