Skip to content

Commit 1a6ee70

Browse files
committedFeb 25, 2025·
Handle f32.neg and f64.neg
1 parent 82a3e85 commit 1a6ee70

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
 

‎crates/floretta/src/reverse.rs

+12
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ impl Func {
893893
self.push_i64();
894894
self.fwd.instructions().i64_rotr();
895895
}
896+
Operator::F32Neg => {
897+
self.pop();
898+
self.push_f32();
899+
self.fwd.instructions().f32_neg();
900+
self.bwd.instructions(|insn| insn.f32_neg());
901+
}
896902
Operator::F32Add => {
897903
self.pop2();
898904
self.push_f32();
@@ -922,6 +928,12 @@ impl Func {
922928
self.fwd.instructions().call(FUNC_F32_DIV_FWD);
923929
self.bwd.instructions(|insn| insn.call(FUNC_F32_DIV_BWD));
924930
}
931+
Operator::F64Neg => {
932+
self.pop();
933+
self.push_f64();
934+
self.fwd.instructions().f64_neg();
935+
self.bwd.instructions(|insn| insn.f64_neg());
936+
}
925937
Operator::F64Add => {
926938
self.pop2();
927939
self.push_f64();

‎crates/floretta/src/reverse/tests.rs

+26
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,19 @@ fn test_i64_rotr() {
771771
.test()
772772
}
773773

774+
#[test]
775+
fn test_f32_neg() {
776+
Backprop {
777+
wat: include_str!("../wat/f32_neg.wat"),
778+
name: "neg",
779+
input: 3.0f32,
780+
output: -3.0f32,
781+
cotangent: 1.0f32,
782+
gradient: -1.0f32,
783+
}
784+
.test()
785+
}
786+
774787
#[test]
775788
fn test_f32_add() {
776789
Backprop {
@@ -823,6 +836,19 @@ fn test_f32_div() {
823836
.test()
824837
}
825838

839+
#[test]
840+
fn test_f64_neg() {
841+
Backprop {
842+
wat: include_str!("../wat/f64_neg.wat"),
843+
name: "neg",
844+
input: 3.,
845+
output: -3.,
846+
cotangent: 1.,
847+
gradient: -1.,
848+
}
849+
.test()
850+
}
851+
826852
#[test]
827853
fn test_f64_add() {
828854
Backprop {

‎crates/floretta/src/wat/f32_neg.wat

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(func (export "neg") (param f32) (result f32)
3+
(f32.neg
4+
(local.get 0))))

‎crates/floretta/src/wat/f64_neg.wat

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(func (export "neg") (param f64) (result f64)
3+
(f64.neg
4+
(local.get 0))))

0 commit comments

Comments
 (0)
Please sign in to comment.