• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Byte vs Int 3 Answers

Optimization Question - Floats or Ints? 0 Answers

Failed to Initialize 3D Graphics? 10 Answers

UI disabled objects keep raycasting in EventSystem 0 Answers

Device.present, Overhead, camera.render slowing the fps 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges