67 lines
2.1 KiB
Rust
67 lines
2.1 KiB
Rust
//! Program state processor
|
|
|
|
use {
|
|
crate::{approximations::sqrt, instruction::MathInstruction, precise_number::PreciseNumber},
|
|
borsh::BorshDeserialize,
|
|
solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, msg, pubkey::Pubkey},
|
|
};
|
|
|
|
/// Instruction processor
|
|
pub fn process_instruction(
|
|
_program_id: &Pubkey,
|
|
_accounts: &[AccountInfo],
|
|
input: &[u8],
|
|
) -> ProgramResult {
|
|
let instruction = MathInstruction::try_from_slice(input).unwrap();
|
|
match instruction {
|
|
MathInstruction::PreciseSquareRoot { radicand } => {
|
|
msg!("Calculating square root using PreciseNumber");
|
|
let radicand = PreciseNumber::new(radicand as u128).unwrap();
|
|
let result = radicand.sqrt().unwrap().to_imprecise().unwrap() as u64;
|
|
msg!("{}", result);
|
|
Ok(())
|
|
}
|
|
MathInstruction::SquareRootU64 { radicand } => {
|
|
msg!("Calculating u64 square root");
|
|
let result = sqrt(radicand).unwrap();
|
|
msg!("{}", result);
|
|
Ok(())
|
|
}
|
|
MathInstruction::SquareRootU128 { radicand } => {
|
|
msg!("Calculating u128 square root");
|
|
let result = sqrt(radicand).unwrap();
|
|
msg!("{}", result);
|
|
Ok(())
|
|
}
|
|
MathInstruction::U64Multiply {
|
|
multiplicand,
|
|
multiplier,
|
|
} => {
|
|
msg!("Calculating U64 Multiply");
|
|
let result = multiplicand * multiplier;
|
|
msg!("{}", result);
|
|
Ok(())
|
|
}
|
|
MathInstruction::F32Multiply {
|
|
multiplicand,
|
|
multiplier,
|
|
} => {
|
|
msg!("Calculating f32 Multiply");
|
|
let result = multiplicand * multiplier;
|
|
msg!("{}", result as u64);
|
|
Ok(())
|
|
}
|
|
MathInstruction::F32Divide { dividend, divisor } => {
|
|
msg!("Calculating f32 Divide");
|
|
let result = dividend / divisor;
|
|
msg!("{}", result as u64);
|
|
Ok(())
|
|
}
|
|
MathInstruction::Noop => {
|
|
msg!("Do nothing");
|
|
msg!("{}", 0_u64);
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|