gpl_math/instruction.rs
1//! Program instructions, used for end-to-end testing and instruction counts
2
3use {
4 crate::id,
5 borsh::{BorshDeserialize, BorshSerialize},
6 gemachain_program::instruction::Instruction,
7};
8
9/// Instructions supported by the math program, used for testing instruction
10/// counts
11#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
12pub enum MathInstruction {
13 /// Calculate the square root of the given u64 with decimals
14 ///
15 /// No accounts required for this instruction
16 PreciseSquareRoot {
17 /// Number underneath the square root sign, whose square root will be
18 /// calculated
19 radicand: u64,
20 },
21 /// Calculate the integer square root of the given u64
22 ///
23 /// No accounts required for this instruction
24 SquareRootU64 {
25 /// Number underneath the square root sign, whose square root will be
26 /// calculated
27 radicand: u64,
28 },
29 /// Calculate the integer square root of the given u128
30 ///
31 /// No accounts required for this instruction
32 SquareRootU128 {
33 /// Number underneath the square root sign, whose square root will be
34 /// calculated
35 radicand: u128,
36 },
37 /// Multiply two u64 values
38 ///
39 /// No accounts required for this instruction
40 U64Multiply {
41 /// The multiplicand
42 multiplicand: u64,
43 /// The multipier
44 multiplier: u64,
45 },
46 /// Divide two u64 values
47 ///
48 /// No accounts required for this instruction
49 U64Divide {
50 /// The dividend
51 dividend: u64,
52 /// The divisor
53 divisor: u64,
54 },
55 /// Multiply two float values
56 ///
57 /// No accounts required for this instruction
58 F32Multiply {
59 /// The multiplicand
60 multiplicand: f32,
61 /// The multipier
62 multiplier: f32,
63 },
64 /// Divide two float values
65 ///
66 /// No accounts required for this instruction
67 F32Divide {
68 /// The dividend
69 dividend: f32,
70 /// The divisor
71 divisor: f32,
72 },
73 /// Don't do anything for comparison
74 ///
75 /// No accounts required for this instruction
76 Noop,
77}
78
79/// Create PreciseSquareRoot instruction
80pub fn precise_sqrt(radicand: u64) -> Instruction {
81 Instruction {
82 program_id: id(),
83 accounts: vec![],
84 data: MathInstruction::PreciseSquareRoot { radicand }
85 .try_to_vec()
86 .unwrap(),
87 }
88}
89
90/// Create SquareRoot instruction
91pub fn sqrt_u64(radicand: u64) -> Instruction {
92 Instruction {
93 program_id: id(),
94 accounts: vec![],
95 data: MathInstruction::SquareRootU64 { radicand }
96 .try_to_vec()
97 .unwrap(),
98 }
99}
100
101/// Create SquareRoot instruction
102pub fn sqrt_u128(radicand: u128) -> Instruction {
103 Instruction {
104 program_id: id(),
105 accounts: vec![],
106 data: MathInstruction::SquareRootU128 { radicand }
107 .try_to_vec()
108 .unwrap(),
109 }
110}
111
112/// Create PreciseSquareRoot instruction
113pub fn u64_multiply(multiplicand: u64, multiplier: u64) -> Instruction {
114 Instruction {
115 program_id: id(),
116 accounts: vec![],
117 data: MathInstruction::U64Multiply {
118 multiplicand,
119 multiplier,
120 }
121 .try_to_vec()
122 .unwrap(),
123 }
124}
125
126/// Create PreciseSquareRoot instruction
127pub fn u64_divide(dividend: u64, divisor: u64) -> Instruction {
128 Instruction {
129 program_id: id(),
130 accounts: vec![],
131 data: MathInstruction::U64Divide { dividend, divisor }
132 .try_to_vec()
133 .unwrap(),
134 }
135}
136
137/// Create PreciseSquareRoot instruction
138pub fn f32_multiply(multiplicand: f32, multiplier: f32) -> Instruction {
139 Instruction {
140 program_id: id(),
141 accounts: vec![],
142 data: MathInstruction::F32Multiply {
143 multiplicand,
144 multiplier,
145 }
146 .try_to_vec()
147 .unwrap(),
148 }
149}
150
151/// Create PreciseSquareRoot instruction
152pub fn f32_divide(dividend: f32, divisor: f32) -> Instruction {
153 Instruction {
154 program_id: id(),
155 accounts: vec![],
156 data: MathInstruction::F32Divide { dividend, divisor }
157 .try_to_vec()
158 .unwrap(),
159 }
160}
161
162/// Create PreciseSquareRoot instruction
163pub fn noop() -> Instruction {
164 Instruction {
165 program_id: id(),
166 accounts: vec![],
167 data: MathInstruction::Noop.try_to_vec().unwrap(),
168 }
169}