Thread overview
core.checkedint?
Nov 01, 2014
bearophile
Nov 01, 2014
David Nadlinger
Nov 01, 2014
Kai Nacke
Nov 01, 2014
safety0ff
Nov 01, 2014
bearophile
Nov 01, 2014
safety0ff
Nov 01, 2014
bearophile
November 01, 2014
Do you want to make LDC2 recognize core.checkedint functions and implement them efficiently reading the overflow/carry flags of the CPU?

Bye,
bearophile
November 01, 2014
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
November 01, 2014
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.
November 01, 2014
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
November 01, 2014
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
November 01, 2014
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
November 01, 2014
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