BBC BASIC for Windows
Programming >> Assembly Language Programming >> frndint - FPU math.floor() or INT
http://bb4w.conforums.com/index.cgi?board=assembler&action=display&num=1257852422

frndint - FPU math.floor() or INT
Post by Michael Hutton on Nov 10th, 2009, 10:27am

I've recently come across a problem of trying to emulate the INT function in assembler. After doing a bit of looking I see that INT is eqivalent to the math.floor() C++ function. Unfortunately frnint rounds to the nearest integer, which is not what I want. The other problem is that the math.floor function is incredibly slow as the FPU rounding state must be changed before converting and then back again afterwards.


http://books.google.com.au/books?id=M2QYbTVd0VgC&pg=PA169&lpg=PA169&dq=fpu+tricks&source=bl&ots=K4r_fAtJVX&sig=TpIRX1gnNiYXklH4SQH99TtGtHc&hl=en&ei=xTf5SoGvMM6fkQX97LDCCw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA0Q6AEwAg#v=onepage&q=fpu%20tricks&f=false

Richard, how does BB4W implement INT? Or would it be faster if I implement the trick shown in the above link?

Michael
Re: frndint - FPU math.floor() or INT
Post by admin on Nov 10th, 2009, 5:06pm

Quote:
how does BB4W implement INT?

In terms of actual code it does it quite differently for 40-bit floats and 64-bit floats. For 40-bit floats it truncates towards zero, then if the original number was negative and non-integer it subtracts one from the result.

For 64-bit floats it switches the FPU to 'round towards negative infinity' mode, uses the frndint instruction, then switches the FPU back to its normal rounding mode.

Richard.