Skip to content

Commit

Permalink
add floating point fadd code gen
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Feb 5, 2025
1 parent 0118e7c commit 1b630fb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/arm/cod1.d
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ static if (1)
int sz = _tysize[tym];
cs.Iflags = 0;
flags = outretregs & mPSW; /* save original */
forregs = outretregs & cgstate.allregs; // XMMREGS ?
forregs = outretregs & (cgstate.allregs | INSTR.FLOATREGS); // XMMREGS ?

Check warning on line 2277 in compiler/src/dmd/backend/arm/cod1.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod1.d#L2277

Added line #L2277 was not covered by tests
//if (outretregs & mSTACK)
//forregs |= DOUBLEREGS;
if (e.Eoper == OPconst)
Expand Down
24 changes: 15 additions & 9 deletions compiler/src/dmd/backend/arm/cod2.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import dmd.backend.divcoeff : choose_multiplier, udiv_coefficients;
void cdorth(ref CGstate cg, ref CodeBuilder cdb,elem* e,ref regm_t pretregs)
{
//printf("cdorth(e = %p, pretregs = %s)\n",e,regm_str(pretregs));
//elem_print(e);

elem* e1 = e.E1;
elem* e2 = e.E2;
Expand All @@ -82,7 +83,15 @@ void cdorth(ref CGstate cg, ref CodeBuilder cdb,elem* e,ref regm_t pretregs)
reg_t Rn = findreg(retregs1);

Check warning on line 83 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L83

Added line #L83 was not covered by tests

regm_t retregs2 = posregs & ~retregs1;

Check warning on line 85 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L85

Added line #L85 was not covered by tests
//printf("retregs1: %s retregs2: %s\n", regm_str(retregs1), regm_str(retregs2));
static if (0)
{
scodelem(cg, cdb, e2, retregs2, retregs1, false);
}
else
{
retregs2 = mask(33);

Check warning on line 93 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L93

Added line #L93 was not covered by tests
}
reg_t Rm = findreg(retregs2);

Check warning on line 95 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L95

Added line #L95 was not covered by tests

regm_t retregs = pretregs & posregs;

Check warning on line 97 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L97

Added line #L97 was not covered by tests
Expand All @@ -94,21 +103,18 @@ void cdorth(ref CGstate cg, ref CodeBuilder cdb,elem* e,ref regm_t pretregs)

if (tyfloating(ty1))

Check warning on line 104 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L104

Added line #L104 was not covered by tests
{
uint ftype = sz == 2 ? 3 :
sz == 4 ? 0 : 1;
switch (e.Eoper)

Check warning on line 108 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L106-L108

Added lines #L106 - L108 were not covered by tests
{
// FADD/FSUB (extended register)
// http://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#addsub_ext
case OPadd:
cdb.gen1(INSTR.fadd_float(ftype,Rm,Rn,Rd)); // FADD Rd,Rn,Rm
break;

Check warning on line 114 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L112-L114

Added lines #L112 - L114 were not covered by tests

case OPmin:
uint sf = sz == 8;
uint op = e.Eoper == OPadd ? 0 : 1;
uint S = PSW != 0;
uint opt = 0;
uint option = tyToExtend(ty);
uint imm3 = 0;
cdb.gen1(INSTR.addsub_ext(sf, op, S, opt, Rm, option, imm3, Rn, Rd));
PSW = 0;
pretregs &= ~mPSW;
cdb.gen1(INSTR.fsub_float(ftype,Rm,Rn,Rd)); // FSUB Rd,Rn,Rm
break;

Check warning on line 118 in compiler/src/dmd/backend/arm/cod2.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/cod2.d#L116-L118

Added lines #L116 - L118 were not covered by tests

default:
Expand Down
52 changes: 50 additions & 2 deletions compiler/src/dmd/backend/arm/instr.d
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,56 @@ struct INSTR
/* Floating-point compare
* Floating-point immediate
* Floating-point condistional compare
* Floating-point data-processing (2 source)
* Floating-point conditional select
*/

/* Floating-point data-processing (2 source) https://www.scs.stanford.edu/~zyedidia/arm64/encodingindex.html#floatdp2
*/
static uint floatdp2(uint M, uint S, uint ftype, reg_t Vm, uint opcode, reg_t Vn, reg_t Vd)
{
assert(Vm >= 32 && Vn >= 32 && Vd >= 32);
reg_t Rm = Vm & 31;
reg_t Rn = Vn & 31;
reg_t Rd = Vd & 31;
return (M << 31) | (S << 29) | (0x1E << 24) | (ftype << 22) | (1 << 21) | (Rm << 16) | (opcode << 12) | (2 << 10) | (Rn << 5) | Rd;

Check warning on line 750 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L746-L750

Added lines #L746 - L750 were not covered by tests
}

/* FMUL (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fmul_float.html
*/
static uint fmul_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,0,Vn,Vd); }

Check warning on line 755 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L755

Added line #L755 was not covered by tests

/* FDIV (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fdiv_float.html
*/
static uint fdiv_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,1,Vn,Vd); }

Check warning on line 759 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L759

Added line #L759 was not covered by tests

/* FADD (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fadd_float.html
*/
static uint fadd_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,2,Vn,Vd); }

Check warning on line 763 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L763

Added line #L763 was not covered by tests

/* FSUB (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fsub_float.html
*/
static uint fsub_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,3,Vn,Vd); }

Check warning on line 767 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L767

Added line #L767 was not covered by tests

/* FMAX (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fmax_float.html
*/
static uint fmax_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,4,Vn,Vd); }

Check warning on line 771 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L771

Added line #L771 was not covered by tests

/* FMIN (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fmin_float.html
*/
static uint fmin_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,5,Vn,Vd); }

Check warning on line 775 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L775

Added line #L775 was not covered by tests

/* FMAXNM (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fmaxnm_float.html
*/
static uint fmaxnm_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,6,Vn,Vd); }

Check warning on line 779 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L779

Added line #L779 was not covered by tests

/* FMINNM (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fminnm_float.html
*/
static uint fminnm_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,7,Vn,Vd); }

Check warning on line 783 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L783

Added line #L783 was not covered by tests

/* FNMUL (scalar) https://www.scs.stanford.edu/~zyedidia/arm64/fnmul_float.html
*/
static uint fnmul_float(uint ftype, reg_t Vm, reg_t Vn, reg_t Vd) { return floatdp2(0,0,ftype,Vm,8,Vn,Vd); }

Check warning on line 787 in compiler/src/dmd/backend/arm/instr.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/arm/instr.d#L787

Added line #L787 was not covered by tests

/* Floating-point conditional select
* Floating-point data-processing (3 source)
*/

Expand Down
5 changes: 4 additions & 1 deletion compiler/src/dmd/backend/debugprint.d
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ const(char)* tym_str(tym_t ty)
const tyb = tybasic(ty);
if (tyb >= TYMAX)
{
printf("TY %x\n",cast(int)ty);
if (tyb == TYMAX)
printf("TY TYMAX\n");

Check warning on line 157 in compiler/src/dmd/backend/debugprint.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/debugprint.d#L156-L157

Added lines #L156 - L157 were not covered by tests
else
printf("TY %x\n",cast(int)ty);

Check warning on line 159 in compiler/src/dmd/backend/debugprint.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/debugprint.d#L159

Added line #L159 was not covered by tests
assert(0);
}
strcat(p, "TY");
Expand Down
7 changes: 4 additions & 3 deletions compiler/src/dmd/backend/x86/cgcod.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import dmd.backend.ty;
import dmd.backend.type;

import dmd.backend.arm.disasmarm;
import dmd.backend.arm.instr;

import dmd.backend.x86.code_x86;
import dmd.backend.x86.disasm86;
Expand Down Expand Up @@ -1620,7 +1621,7 @@ static if (0)
}
tym = tybasic(tym);
uint size = _tysize[tym];
outretregs &= mES | cgstate.allregs | XMMREGS;
outretregs &= mES | cgstate.allregs | XMMREGS | INSTR.FLOATREGS;
regm_t retregs = outretregs;
regm_t[] lastRetregs = cgstate.lastRetregs[];

Expand All @@ -1630,7 +1631,7 @@ static if (0)
if ((retregs & cgstate.regcon.mvar) == retregs) // if exactly in reg vars
{
reg_t outreg;
if (size <= REGSIZE || (retregs & XMMREGS))
if (size <= REGSIZE || (retregs & XMMREGS) || (retregs & INSTR.FLOATREGS))
{
outreg = findreg(retregs);
assert(retregs == mask(outreg)); /* no more bits are set */
Expand Down Expand Up @@ -3002,7 +3003,7 @@ const(char)* regm_str(regm_t rm)
{
char[4] buf = void;
char c = j < 32 ? 'r' : 'f';
sprintf(buf.ptr, "c%u", c, j);
sprintf(buf.ptr, "%c%u", c, j);

Check warning on line 3006 in compiler/src/dmd/backend/x86/cgcod.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/x86/cgcod.d#L3006

Added line #L3006 was not covered by tests
strcat(p, buf.ptr);
}
}
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dmd/backend/x86/cod3.d
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,8 @@ static if (NTEXCEPTIONS)
case BC.retexp:
reg_t reg1, reg2;
retregs = allocretregs(cgstate, e.Ety, e.ET, funcsym_p.ty(), reg1, reg2);
//printf("allocretregs returns %s\n", regm_str(mask(reg1) | mask(reg2)));
//printf("reg1: %d, reg2: %d\n", reg1, reg2);
//printf("allocretregs returns %llx %s\n", retregs, regm_str(retregs));

reg_t lreg = NOREG;
reg_t mreg = NOREG;
Expand Down Expand Up @@ -1525,6 +1526,8 @@ regm_t allocretregs(ref CGstate cgstate, const tym_t ty, type* t, const tym_t ty
assert(tyfb == TYjfunc && I32);
return ST01;
}
else if (AArch64 && tyfloating(tym))
return rralloc.fpt();

Check warning on line 1530 in compiler/src/dmd/backend/x86/cod3.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/backend/x86/cod3.d#L1529-L1530

Added lines #L1529 - L1530 were not covered by tests
else if (tysimd(tym))
{
return rralloc.xmm();
Expand Down Expand Up @@ -1559,6 +1562,8 @@ regm_t allocretregs(ref CGstate cgstate, const tym_t ty, type* t, const tym_t ty
reg1 = allocreg(ty1);
reg2 = allocreg(ty2);

//printf("reg1: %d reg2: %d NOREG: %d\n", reg1, reg2, NOREG);
//printf("reg1: %llx reg2: %llx ~NOREG: %llx\n", mask(reg1), mask(reg2), ~mask(NOREG));
return (mask(reg1) | mask(reg2)) & ~mask(NOREG);
}

Expand Down

0 comments on commit 1b630fb

Please sign in to comment.