[Dlang-internal] Complex numbers

Ilya Yaroshenko via Dlang-internal dlang-internal at puremagic.com
Mon Oct 17 03:48:29 PDT 2016


On Sunday, 16 October 2016 at 20:56:08 UTC, Walter Bright wrote:
> On 10/16/2016 1:05 PM, Ilya Yaroshenko wrote:
>> The site says they are deprecated. In the same time it is real 
>> pain to use
>> core.stdc.complex with std.complex.
>
> There's a tug of war going on to support C's ABI and not 
> supporting complex as a native type. Probably the best way 
> forward is to ensure that std.complex maps directly onto the C 
> types.

I would like to use native complex types in Mir instead of 
std.complex. D has not macro, only templates and mixins. This 
makes impossible to use complex arithmetic operations   the same 
way like normal arithmetic operations.

For example:

import ldc.attributes;
import std.complex;

pragma(LDC_no_moduleinfo);

@fastmath auto native_fma(cdouble a, cdouble b, cdouble c)
{
	return a * b + c;
}
->>>>
	.cfi_startproc
	vfmadd231sd	%xmm2, %xmm4, %xmm0
	vfnmadd231sd	%xmm3, %xmm5, %xmm0
	vfmadd231sd	%xmm2, %xmm5, %xmm1
	vfmadd231sd	%xmm3, %xmm4, %xmm1
	retq
	.cfi_endproc


@fastmath auto stdcomplex_fma(Complex!double a, Complex!double b, 
Complex!double c)
{
	return a * b + c;
}
->>>>
	.cfi_startproc
	vmulsd	%xmm4, %xmm2, %xmm6
	vmulsd	%xmm5, %xmm3, %xmm7
	vsubsd	%xmm7, %xmm6, %xmm6
	vmulsd	%xmm5, %xmm2, %xmm2
	vmulsd	%xmm4, %xmm3, %xmm3
	vaddsd	%xmm2, %xmm3, %xmm2
	vaddsd	%xmm6, %xmm0, %xmm0
	vaddsd	%xmm2, %xmm1, %xmm1
	retq
	.cfi_endproc

In addition, to generate stdcomplex_fma LDC generates 11900 
assembler LOC of bloat. I am going to replace std.compex in Mir 
with native complex numbers, which are more user friendly in 
terms of optimisation and syntax.

Best regards,
Ilya


More information about the Dlang-internal mailing list