Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math: Add f64 powf and powi #2463

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
buffalojoec marked this conversation as resolved.
Show resolved Hide resolved
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_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
Loading