PipelineC icon indicating copy to clipboard operation
PipelineC copied to clipboard

Implement Various Math Optimizations

Open JulianKemmerer opened this issue 3 years ago • 4 comments

For example:

  • Check FP sign bit for < 0.0 compare
  • FP * 0.25 is FP shift >> 2 (div by 4) exponent math, and similar 1/pow2 consts
  • Integer > 255 check using only msbs, and similar for > 'all lsb bits ones' ~pow2 - 1 numbers
  • Make * -1.0 into '-' negate operator for easy FP sign flip (mult by 1.0 should optimize away)

Im sure @suarezvictor will continue with more :nerd_face:

JulianKemmerer avatar Dec 19 '21 05:12 JulianKemmerer

In regards to integers, all such optimization can be done without issues, in case of floating point operations I suggest to enable all this with a -ffast-math option, that in C compilers allows among other things to break IEEE compliance, ignore math with infinites/NaN or "negative zero", and relax rounding behavior

suarezvictor avatar Dec 20 '21 10:12 suarezvictor

Per https://github.com/JulianKemmerer/PipelineC/issues/57 Do something better than signed division using absolute value, unsigned div, and sign fix

JulianKemmerer avatar Mar 05 '22 17:03 JulianKemmerer

More math fun :smirk: Division algorithm in the CRAY-1 supercomputer. https://www.ed-thelen.org/comp-hist/CRAY-1-HardRefMan/CRAY-1-HRM.html#p3-18

JulianKemmerer avatar Sep 18 '23 16:09 JulianKemmerer

Implement comparison operators using adders: a > b a - b > 0 a + (-b) > 0 a + (~b + 1) > 0 a + ~b + 1 > 0 a + ~b > -1 a + ~b >= 0 !sign(a + ~b)

so a > b is the same as !sign(a + ~b)

then For the < operator, you can swap operands (b > a) For a <= b, you can use !(a>b) And for a>=b, replace by !(b>a)

Thus all comparisons will use just an adder

suarezvictor avatar Dec 21 '23 10:12 suarezvictor