How to implement gender, ceil and round in LLVM bitcode?

I am writing math functions for a small LLVM-based programming language, and I am currently dead end on how to implement common rounding functions, gender and round (up to parity). Firstly, because I did not find a description of the algorithms for these functions, and secondly, because I am not familiar with the capabilities of LLVM w. rounding.

The ability to round negative numbers correctly is a necessity; rounding to a certain accuracy is not. It will be rounded to a holistic value. Just pointing out any existing implementations that can be used from the LLVM bit code will also work.

+4
source share
3 answers

You need to start with the LLVM language reference guide .

You can start by implementing trunc( ) , like something in this direction (warning, do not actually use this, it is intended as an example and is incorrect. See discussion below):

 define float @trunc(float %x) { %rounded = fptosi float %x to i32 %asFloat = sitofp i32 %rounded to float ret float %asFloat } 

The fptosi ... to ... documented as rounding a floating point value to an integer value according to a rounding cycle to zero. The sitofp ... to ... command converts this value back to a floating-point return value.

However, there is a problem with this implementation; after reading the link to the language I'm associated with, "the behavior of fptosi ... to ... is undefined if the result of rounding to the nearest integer cannot fit in the destination type."

This is fairly easy to get around, though, since all large enough floating point numbers are already integers and don't need to be rounded; if the absolute value of x greater than or equal to 2 ^ 23, you can simply return x itself.

(All this for single precision; for double, you most likely want to use i64 , and you will need to use a threshold of 2 ^ 52)

For other operations, such as floor and round , you can start with trunc , then check the residual x - trunc(x) and adjust the result accordingly.

Alternatively, you can refer to the host platform's C library, which already includes these features. This is an approach used by many programming languages.

+1
source

If you look in Google Code Search, you will see several results . A related example assumes IEEE floating point numbers. Typically, compilers for regular PCs simply compile floor in a floating point instruction. For example, the original 387 arithmetic processor has an FPREM instruction that more or less does part of what you need for floor .

+1
source

I applied the floor to the float vectors as follows: "crop" the value of x, then compare x and trunc (x). Where trunc (x)> x, subtract 1 because gender (x) should always be at most x. I encoded this in Haskell. I do not know if this will help you. See FloorLogical in http://code.haskell.org/~thielema/llvm-extra/src/LLVM/Extra/Vector.hs

Round to even expensive and not very useful. I just use gender (x + 0.5). In SSE4.1 there is also roundss, roundps et.al.

+1
source

All Articles