Skip to content

Commit

Permalink
math: Add f64 powf and powi
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Aug 21, 2023
1 parent 30d0e1b commit 6eb7973
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions libraries/math/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ pub enum MathInstruction {
argument: f32,
},

/// Pow two float values
///
/// No accounts required for this instruction
F64Pow {
/// The base
base: f64,
/// The exponent
exponent: f64,
},

/// Don't do anything for comparison
///
/// No accounts required for this instruction
Expand Down Expand Up @@ -219,6 +229,17 @@ pub fn f32_normal_cdf(argument: f32) -> Instruction {
}
}

/// Create F64Pow instruction
pub fn f64_pow(base: f64, exponent: f64) -> Instruction {
Instruction {
program_id: id(),
accounts: vec![],
data: MathInstruction::F64Pow { base, exponent }
.try_to_vec()
.unwrap(),
}
}

/// Create Noop instruction
pub fn noop() -> Instruction {
Instruction {
Expand Down
9 changes: 9 additions & 0 deletions libraries/math/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ pub fn process_instruction(
msg!("{}", result as u64);
Ok(())
}
MathInstruction::F64Pow { base, exponent } => {
msg!("Calculating f64 Pow");
sol_log_compute_units();
let _result = base.powi(5);
let result = base.powf(exponent);
sol_log_compute_units();
msg!("{}", result as u64);
Ok(())
}
MathInstruction::Noop => {
msg!("Do nothing");
msg!("{}", 0_u64);
Expand Down
16 changes: 16 additions & 0 deletions libraries/math/tests/instruction_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ async fn test_f32_normal_cdf() {
banks_client.process_transaction(transaction).await.unwrap();
}

#[tokio::test]
async fn test_f64_pow() {
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));

pc.set_bpf_compute_max_units(30_000);

let (mut banks_client, payer, recent_blockhash) = pc.start().await;

let mut transaction = Transaction::new_with_payer(
&[instruction::f64_pow(50_f64, 10.5_f64)],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
}

#[tokio::test]
async fn test_noop() {
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));
Expand Down

0 comments on commit 6eb7973

Please sign in to comment.