Re: How to use core.vararg to print D variadic arguments and their types without using ! (template instantiation)?

2023-09-18 Thread BoQsc via Digitalmars-d-learn


Note that this doesn't work in gdc.

The templated version is actually more akin to what C does.


Yeah it does not seem to work in gdc when tested using 
https://d.godbolt.org/


The errors produced:

```
:11:12: error: none of the overloads of template 
'core.stdc.stdarg.va_arg' are callable using argument types 
'!()(__va_list_tag[1], TypeInfo, int*)'

   11 |  va_arg(_argptr, typeid(i), &i);
  |^
/opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:179:7:
 note: Candidates are: 'va_arg(T)(ref va_list ap)'
  179 | T va_arg(T)(ref va_list ap); // intrinsic
  |   ^
/opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:291:10:
 note: 'va_arg(T)(ref va_list ap, ref T parmn)'
  291 | void va_arg(T)(ref va_list ap, ref T parmn); // 
intrinsic

  |  ^
:14:12: error: none of the overloads of template 
'core.stdc.stdarg.va_arg' are callable using argument types 
'!()(__va_list_tag[1], TypeInfo, double*)'

   14 |  va_arg(_argptr, typeid(d), &d);
  |^
/opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:179:7:
 note: Candidates are: 'va_arg(T)(ref va_list ap)'
  179 | T va_arg(T)(ref va_list ap); // intrinsic
  |   ^
/opt/compiler-explorer/gcc-trunk-20230917/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:291:10:
 note: 'va_arg(T)(ref va_list ap, ref T parmn)'
  291 | void va_arg(T)(ref va_list ap, ref T parmn); // 
intrinsic

  |  ^
ASM generation compiler returned: 1
:11:12: error: none of the overloads of template 
'core.stdc.stdarg.va_arg' are callable using argument types 
'!()(__va_list_tag[1], TypeInfo, int*)'

   11 |  va_arg(_argptr, typeid(i), &i);
  |^
/opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:179:7:
 note: Candidates are: 'va_arg(T)(ref va_list ap)'
  179 | T va_arg(T)(ref va_list ap); // intrinsic
  |   ^
/opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:291:10:
 note: 'va_arg(T)(ref va_list ap, ref T parmn)'
  291 | void va_arg(T)(ref va_list ap, ref T parmn); // 
intrinsic

  |  ^
:14:12: error: none of the overloads of template 
'core.stdc.stdarg.va_arg' are callable using argument types 
'!()(__va_list_tag[1], TypeInfo, double*)'

   14 |  va_arg(_argptr, typeid(d), &d);
  |^
/opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:179:7:
 note: Candidates are: 'va_arg(T)(ref va_list ap)'
  179 | T va_arg(T)(ref va_list ap); // intrinsic
  |   ^
/opt/compiler-explorer/gcc-trunk-20230918/lib/gcc/x86_64-linux-gnu/14.0.0/include/d/core/stdc/stdarg.d:291:10:
 note: 'va_arg(T)(ref va_list ap, ref T parmn)'
  291 | void va_arg(T)(ref va_list ap, ref T parmn); // 
intrinsic

  |  ^
Execution build compiler returned: 1
```


container vs standard array

2023-09-18 Thread vino via Digitalmars-d-learn

Hi All,

 I am trying to understand as to why the below code is throwing 
error


Code
```
import std.stdio: writeln;
import std.container.array;

void main () {
 //auto a = Array!string("Aname");   // throws error
 auto b = Array!char("Bname");// works
 auto c = Array!string("Aname", "Bname");   // works

 //writeln(a);
 writeln("Container Array :", b);
 writeln("Container Array :", c);

 writeln();

 string[] d = ["Dname"]; // works
 string[] e = ["Dname", "Ename"];  // works
 writeln("Standard Array :", d);
 writeln("Standard Array :", e);

}
```
From,
Vino


Which function returns a pair after division ? (integer,frac)

2023-09-18 Thread Vitaliy Fadeev via Digitalmars-d-learn

What D function or D operator does this?

```asm
IDIV EAX, r/m32
```

```
IDIV 5, 2
 EAX = 2
 EDX = 1
```

and returns (2,1) at once?


Re: Which function returns a pair after division ? (integer,frac)

2023-09-18 Thread Ki Rill via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 03:44:18 UTC, Vitaliy Fadeev 
wrote:

What D function or D operator does this?

```asm
IDIV EAX, r/m32
```

```
IDIV 5, 2
 EAX = 2
 EDX = 1
```

and returns (2,1) at once?


You can either use function `out` parameters with return value or 
`tuples`:

```D
import std.typecons;

// with tuples
auto getVal(int a, int b) {
// ...
return tuple(integer, frac);
}

// out params
int getVal2(int a, int b, out int frac) {
// ...

frac = _fraq;
return integer;
}

// USAGE
{
// with tuples
auto v = getVal(5, 2);
assert(v[0] == 2);
assert(v[1] == 1);

// with out params
int frac;
int integer = getVal2(5, 2, frac);
assert(integer == 2);
assert(frac == 1);
}

```


Re: Which function returns a pair after division ? (integer,frac)

2023-09-18 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

There are no operators for this, not that you need one.

```d
void func(int numerator, int denominator, out int quotient, out int 
remainder) {

quotient = numerator / denominator;
remainder = numerator % denominator;
}
```

This will produce with ldc2 -O3:

```
void example.func(int, int, out int, out int):
mov r8, rdx
mov eax, edi
cdq
idivesi
mov dword ptr [r8], eax
mov dword ptr [rcx], edx
ret
```

Embrace modern backends, they are amazing!


Re: Which function returns a pair after division ? (integer,frac)

2023-09-18 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 03:53:06 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

There are no operators for this, not that you need one.

```d
void func(int numerator, int denominator, out int quotient, out 
int remainder) {

quotient = numerator / denominator;
remainder = numerator % denominator;
}
```

This will produce with ldc2 -O3:

```
void example.func(int, int, out int, out int):
mov r8, rdx
mov eax, edi
cdq
idivesi
mov dword ptr [r8], eax
mov dword ptr [rcx], edx
ret
```

Embrace modern backends, they are amazing!


Thanks, Rikki. Optimization is good!
May be exist native D function or D-operator, like ```(a,b) = x 
/% y;``` ?




Re: Detect 8-bit alligned type TXY by TX,TY.

2023-09-18 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

I assume what you are wanting is to get the alignment for a given type?

https://dlang.org/spec/property.html#alignof

If instead you want the offset of a given field that's different.

``T.field.offsetof``

https://dlang.org/spec/struct.html#struct_field_properties

Note: alignment cannot be represented by a type, it can only be 
represented by a number (for instance it could be 3).


Re: container vs standard array

2023-09-18 Thread JG via Digitalmars-d-learn

On Tuesday, 19 September 2023 at 00:34:01 UTC, vino wrote:

Hi All,

 I am trying to understand as to why the below code is throwing 
error


Code
```
import std.stdio: writeln;
import std.container.array;

void main () {
 //auto a = Array!string("Aname");   // throws error
 auto b = Array!char("Bname");// works
 auto c = Array!string("Aname", "Bname");   // works

 //writeln(a);
 writeln("Container Array :", b);
 writeln("Container Array :", c);

 writeln();

 string[] d = ["Dname"]; // works
 string[] e = ["Dname", "Ename"];  // works
 writeln("Standard Array :", d);
 writeln("Standard Array :", e);

}
```
From,
Vino
Looks to me like when it receives a single range it expects the 
elements of that range to match the type. If I am correct in the 
first one if you replace the "Aname" by ["Aname"] it should work




Re: Detect 8-bit alligned type TXY by TX,TY.

2023-09-18 Thread Vitaliy Fadeev via Digitalmars-d-learn
On Tuesday, 19 September 2023 at 06:33:25 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
I assume what you are wanting is to get the alignment for a 
given type?


https://dlang.org/spec/property.html#alignof

If instead you want the offset of a given field that's 
different.


``T.field.offsetof``

https://dlang.org/spec/struct.html#struct_field_properties

Note: alignment cannot be represented by a type, it can only be 
represented by a number (for instance it could be 3).


Thank, Richard.

```.offsetof...``` mmm...

May be exists some like:

```d
// TXYXY = Detect!(TX,TX)
// Detect!(uint,uint) == ulong
template Detect(TX,TY)
{
static if ( TX.sizeof + TY.sizeof <= 8 )
alias Detect = ubyte;
else
static if ( TX.sizeof + TY.sizeof <= 16 )
alias Detect = ushort;
else
static if ( TX.sizeof + TY.sizeof <= 32 )
alias Detect= uint;
else
static if ( TX.sizeof + TY.sizeof <= 64 )
alias Detect= ulong;
else
static if ( TX.sizeof + TY.sizeof <= 128 )
alias Detect= ucent;
else
static assert( 0, "Expected size TX+TY <= 128" );
}
```