C Fast Casting Data Type

I have a problem when try to change data type form one to another in C. For example, I try to casting float data type to integer or from double to integer. In the last time, I use command (int)float_number. Because this process is slow, I try to searching about fast casting data type. We can use command lrint (convert from double to integer) and lrintf (convert from float to integer). This is my simple code to compare my old casting data and use fast casting data type (from c library). Create a new file (give name tes_casting.c). Copy this code to that file :

Compile this code with command :

gcc tes_casting.c -lm -o tes_casting

This is how to use and output from this program :

You can see, lrintf and lrint can speed up your casting algorithm. Converting float to integer with or without lrintf command can 100% faster than (int)float_numb.

Source:
http://www.mega-nerd.com/FPcast/

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.