Skip to content

Commit

Permalink
Additional Helpers Added
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhat1308 committed Oct 1, 2023
1 parent e037e9c commit 1c80b0e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
73 changes: 62 additions & 11 deletions src/Complex.huff
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include './Helper.huff'
#include './Constants.huff'

// @notice addition
/// @notice addition
// a + b

#define macro ADD_Z() = takes(4) returns(2) {
Expand All @@ -14,7 +14,7 @@

}

// @notice subtraction
/// @notice subtraction
// a - b

#define macro SUB_Z() = takes(4) returns(2) {
Expand All @@ -27,7 +27,7 @@

}

// @notice multiplication
/// @notice multiplication
// a * b

#define macro MUL_Z() = takes(4) returns(2){
Expand All @@ -49,7 +49,7 @@

}

// @notice division
/// @notice division
// a / b

#define macro DIV_Z() = takes(4) returns(2) {
Expand Down Expand Up @@ -94,7 +94,7 @@
}


// @notice
/// @notice
// a**2 + b**2 = r**2
// a and b are int
// return r
Expand All @@ -109,7 +109,7 @@
SQRT()
}

// @notice
/// @notice
//cartesian to polar

#define macro TO_POLAR() = takes(2) returns(2) {
Expand All @@ -136,22 +136,69 @@

}

// @notice
/// @notice
// polar to cartesian

#define macro FROM_POLAR() = takes(2) returns(2) {
//INPUT STACK => [T,r]
dup1 // [T,T,r]
0x00 // [0,T,T,r]
slt // [0<T,T,r]
case1
case2
jumpi

case1: //T is positive
dup2 // [r,T,r]
dup2 // [T,r,T,r]
PUT_NEG1() // [-1,T,r,T,r]
mul // [-1T,r,T,r]
SIN() // [sin(-T),r,T,r]
mul // [sin(-T)*r,T,r]
PUT_NEG1() // [-1,sin(-T)*r,T,r]
mul // [-sin(-T)*r,T,r]
[X3] // [1e18,-sin(-T)*r,T,r]
swap1 // [-sin(-T)*r,1e18,T,r]
sdiv // [-sin(-T)*r/1e18,T,r]
swap1 // [T,-sin(-T)*r/1e18,r]
COS() // [cos(T),-sin(-T)*r/1e18,r]
swap1
swap2 // [r,cos(T),-sin(-T)*r/1e18]
mul // [r*cos(T),-sin(-T)*r/1e18]
[X3] // [1e18,r*cos(T),-sin(-T)*r/1e18]
swap1 // [r*cos(T),1e18,-sin(-T)*r/1e18]]
sdiv // [r*cos(T),1e18,-sin(-T)*r/1e18]

case2: //T is positive
dup2 // [r,T,r]
dup2 // [T,r,T,r]
SIN() // [sin(T),r,T,r]
mul // [sin(T)*r,T,r]
[X3] // [1e18,sin(T)*r,T,r]
swap1 // [sin(T)*r,1e18,T,r]
sdiv // [sin(T)*r/1e18,T,r]
swap1 // [T,sin(T)*r/1e18,r]
COS() // [cos(T),sin(T)*r/1e18,r]
swap1
swap2 // [r,cos(T),sin(T)*r/1e18]
mul // [r*cos(T),sin(T)*r/1e18]
[X3] // [1e18,r*cos(T),sin(T)*r/1e18]
swap1 // [r*cos(T),1e18,sin(T)*r/1e18]]
sdiv // [r*cos(T),1e18,sin(T)*r/1e18]
}

#define macro P_ATAN2() = takes(2) returns(1) {

// INPUT STACK => [y,x]
dup1 // [y,y,x]
swap2 // [x,y,y]
P_ATAN2_INNER_CALC() // [T,y]
swap1 // [y,T]
0x00 // [0,y,T]
sgt // [0>y,T]
case3
jumpi

case3:
PUT_NEG1() // [-1,T]
mul // [-T]

}

#define macro ATAN1TO1() = takes(1) returns(1){
Expand Down Expand Up @@ -195,6 +242,8 @@

}

///@notice e^(a+bi) calculation

#define macro EXP_Z() = takes(2) returns(2) {
//INPUT STACK => [Re(A),Im(A)]
[e] // [e,Re(A),Im(A)]
Expand All @@ -203,6 +252,8 @@

}

///@notice power of complex numbers

#define macro POW() = takes(3) returns(2) {
//INPUT STACK => [Re(a),Im(b),n]
TO_POLAR() // [r,T,n]
Expand Down
3 changes: 2 additions & 1 deletion src/Constants.Huff
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
#define constant e = 0x25B946EBC0B36173
#define constant X5 = 0xAE4E15744AE8000 //7.851e17
#define constant X6 = 0x365595A8089C000 //2.447e17
#define constant X7 = 0xEB8B7FAC9BC000 //6.63e16
#define constant X7 = 0xEB8B7FAC9BC000 //6.63e16
#define constant X8 = 0x5F5E100 //1e8
22 changes: 20 additions & 2 deletions src/Helper.huff
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,26 @@
sub //[-x]
}

#define macro PUT_X3_CUBE = takes(0) returns(1){
#define macro PUT_X3_CUBE() = takes(0) returns(1){
0x03 //[2]
[X3] //[1e18,3]
exp //[1e54]
}
}

#define macro PUT_NEG1() = takes(0) returns(1){
0x01 // [1]
0x00 // [0,1]
sub // [-1]
}

#define macro P_ATAN2_ABS() = takes(1) returns(1) {
// Input Stack => [x]
ABS() // [abs(y)]
[X8] // [1e8,abs(y)]
add // [1e8+abs(y)]
}

#define macro P_ATAN2_INNER_CALC() = takes(2) returns(1){
//Input Stack => [x,y]

}

0 comments on commit 1c80b0e

Please sign in to comment.