www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - core.checkedint?

reply "bearophile" <bearophileHUGS lycos.com> writes:
Do you want to make LDC2 recognize core.checkedint functions and 
implement them efficiently reading the overflow/carry flags of 
the CPU?

Bye,
bearophile
Oct 31 2014
next sibling parent reply David Nadlinger via digitalmars-d-ldc <digitalmars-d-ldc puremagic.com> writes:
On 1 Nov 2014, at 1:58, bearophile via digitalmars-d-ldc wrote:
 Do you want to make LDC2 recognize core.checkedint functions and 
 implement them efficiently reading the overflow/carry flags of the 
 CPU?
Sure, it maps very directly to the corresponding LLVM intrinsics (conditionally setting the by-ref parameter has to be done in a small wrapper, though). Pull request are very welcome. ;) David
Oct 31 2014
parent "Kai Nacke" <kai redstar.de> writes:
On Saturday, 1 November 2014 at 04:04:48 UTC, David Nadlinger via 
digitalmars-d-ldc wrote:
 On 1 Nov 2014, at 1:58, bearophile via digitalmars-d-ldc wrote:
 Do you want to make LDC2 recognize core.checkedint functions 
 and implement them efficiently reading the overflow/carry 
 flags of the CPU?
Sure, it maps very directly to the corresponding LLVM intrinsics (conditionally setting the by-ref parameter has to be done in a small wrapper, though). Pull request are very welcome. ;) David
See https://github.com/ldc-developers/ldc/pull/772 Regards, Kai
Nov 01 2014
prev sibling parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
I'm not terribly interested in preparing a PR for this, but I 
believe the code should be something like this (untested):

ldc/src/core/checkedint.d:

uint mulu(uint x, uint y, ref bool overflow)
{
+ version (LDC)
+ {
+     auto res = llvm_umul_with_overflow(x, y);
+     overflow |= res.overflow;
+     return res.result;
+ }
+ else
+ {
       ulong r = ulong(x) * ulong(y);
       if (r > uint.max)
           overflow = true;
       return cast(uint)r;
+ }
}

Rinse and repeat with:
llvm_uadd_with_overflow, llvm_sadd_with_overflow, 
llvm_usub_with_overflow, llvm_ssub_with_overflow, 
llvm_smul_with_overflow.
Nov 01 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
safety0ff:

 Rinse and repeat with:
 llvm_uadd_with_overflow, llvm_sadd_with_overflow, 
 llvm_usub_with_overflow, llvm_ssub_with_overflow, 
 llvm_smul_with_overflow.
Good. Is it 100% guaranteed ldc2 will inline this function? Bye, bearophile
Nov 01 2014
parent reply "safety0ff" <safety0ff.dev gmail.com> writes:
On Saturday, 1 November 2014 at 18:06:41 UTC, bearophile wrote:
 safety0ff:

 Rinse and repeat with:
 llvm_uadd_with_overflow, llvm_sadd_with_overflow, 
 llvm_usub_with_overflow, llvm_ssub_with_overflow, 
 llvm_smul_with_overflow.
Good. Is it 100% guaranteed ldc2 will inline this function? Bye, bearophile
They're intrinsics: https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/intrinsics.di#L462
Nov 01 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
safety0ff:

 They're intrinsics:
 https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/intrinsics.di#L462
Sorry, I meant the functions like mulu, etc. Bye, bearophile
Nov 01 2014