Skip to content

Commit

Permalink
x86 gradient rounding implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed May 12, 2024
1 parent 22ff723 commit 9699faf
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions fidget/src/jit/x86_64/grad_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,24 +262,32 @@ impl Assembler for GradSliceAssembler {
);
}

// TODO hand-write these functions
fn build_floor(&mut self, out_reg: u8, lhs_reg: u8) {
extern "sysv64" fn grad_floor(v: Grad) -> Grad {
v.floor()
}
self.call_fn_unary(out_reg, lhs_reg, grad_floor);
dynasm!(self.0.ops
; vroundss xmm1, Rx(reg(lhs_reg)), Rx(reg(lhs_reg)), 1
; vpxor Rx(reg(out_reg)), Rx(reg(out_reg)), Rx(reg(out_reg))
; movss Rx(reg(out_reg)), xmm1
);
}
fn build_ceil(&mut self, out_reg: u8, lhs_reg: u8) {
extern "sysv64" fn grad_ceil(v: Grad) -> Grad {
v.ceil()
}
self.call_fn_unary(out_reg, lhs_reg, grad_ceil);
dynasm!(self.0.ops
; vroundss xmm1, Rx(reg(lhs_reg)), Rx(reg(lhs_reg)), 2
; vpxor Rx(reg(out_reg)), Rx(reg(out_reg)), Rx(reg(out_reg))
; movss Rx(reg(out_reg)), xmm1
);
}
fn build_round(&mut self, out_reg: u8, lhs_reg: u8) {
extern "sysv64" fn grad_round(v: Grad) -> Grad {
v.round()
}
self.call_fn_unary(out_reg, lhs_reg, grad_round);
// Shenanigans figured through Godbolt
dynasm!(self.0.ops
; mov eax, 0x80000000u32 as i32
; vmovd xmm1, eax
; vandps xmm1, xmm1, Rx(reg(lhs_reg))
; mov eax, 0x3effffffu32 as i32
; vmovd xmm2, eax
; vorps xmm1, xmm1, xmm2
; vaddss Rx(reg(out_reg)), xmm1, Rx(reg(lhs_reg))
; vroundss Rx(reg(out_reg)), Rx(reg(out_reg)), Rx(reg(out_reg)), 3
);
}

fn build_add(&mut self, out_reg: u8, lhs_reg: u8, rhs_reg: u8) {
Expand Down

0 comments on commit 9699faf

Please sign in to comment.