Question by
madGlory · Oct 29, 2017 at 09:23 AM ·
optimizationintstructfunction call
How to optimize fixed point numbers?
I am trying to implement fixed point math in unity. I am in the process of benchmarking simple functions and comparing them to unity's built in integer functions. While doing this, I found that the fixed point functions were about twice as slow as the integer equivalent even if i didn't do any operations in them (ie. return 1;)
Here is the class:
using System;
using UnityEngine;
[Serializable]
public struct fp {
//the underlying int value of the fixed point number
[SerializeField]
public int rawValue;
public int RawValue { get { return rawValue; } }
public const int MAX_VALUE = int.MaxValue;
public const int MIN_VALUE = int.MinValue;
public const int TOTAL_BITS = 32;
public const int DECIMAL_BITS = 16;
public static implicit operator fp (int value) {
return new fp(value);
}
public fp (int _rawValue) {
rawValue = _rawValue;
}
public static int Sign (fp value) {
return
value.rawValue > 0 ? 1 :
value.rawValue < 0 ? -1 :
0;
}
public static int FastSign (fp value) {
return 1;
}
public static fp Abs (fp value) {
var mask = value.rawValue >> 31;
return new fp ((value.rawValue + mask) ^ mask);
}
}
Any thoughts on how I can speed these up?
Comment
Your answer

Follow this Question
Related Questions
Optimization Question - Floats or Ints? 0 Answers
Byte vs Int 3 Answers
Failed to Initialize 3D Graphics? 10 Answers
Dynamic Batching for SpritesRender 2 Answers
Anyway to check if a tree is billboard? Script to know if a tree is billboard? 0 Answers