digitalmars.D.learn - Which function returns a pair after division ? (integer,frac)
- Vitaliy Fadeev (10/10) Sep 18 2023 What D function or D operator does this?
- Ki Rill (30/40) Sep 18 2023 You can either use function `out` parameters with return value or
- Richard (Rikki) Andrew Cattermole (20/20) Sep 18 2023 There are no operators for this, not that you need one.
- Vitaliy Fadeev (5/25) Sep 18 2023 Thanks, Rikki. Optimization is good!
- claptrap (4/14) Sep 19 2023 If you use LDC it'll automatically optimize that for you
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?
Sep 18 2023
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); } ```
Sep 18 2023
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 idiv esi mov dword ptr [r8], eax mov dword ptr [rcx], edx ret ``` Embrace modern backends, they are amazing!
Sep 18 2023
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 idiv esi 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;``` ?
Sep 18 2023
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?If you use LDC it'll automatically optimize that for you https://d.godbolt.org/z/oz4h9ccbP
Sep 19 2023