Skip to content

Commit

Permalink
PRBMaths added
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhat1308 committed Oct 2, 2023
1 parent 5ac4fbf commit ff696ae
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Complex.huff
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
#define macro LN() = takes(2) returns(1) {
//INPUT STACK => [Re(A),Im(A)]
TO_POLAR() // [r,T]

LOGE_PRB() // [ln(r),T]
}

#define macro SQRT() = takes(2) returns(1){
Expand Down
6 changes: 4 additions & 2 deletions src/Constants.Huff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define constant X2 = 0xD9FB2F2E0004000 // 9817e14
#define constant X3 = 0xDE0B6B3A7640000 // 1e18
#define constant X4 = 0x9C2007651B2500000 //180e18
#define constant e = 0x25B946EBC0B36173
#define constant e = 0x25B946EBC0B36173 //euler's number
#define constant X5 = 0xAE4E15744AE8000 //7.851e17
#define constant X6 = 0x365595A8089C000 //2.447e17
#define constant X7 = 0xEB8B7FAC9BC000 //6.63e16
#define constant X8 = 0x5F5E100 //1e8
#define constant X8 = 0x5F5E100 //1e8
#define constant LOG2_E = 0x14057B7EF767814F //1.4426950408889634e18
#define constant HALF_SCALE = 0x6F05B59D3B20000 //5e17
75 changes: 75 additions & 0 deletions src/PRBMath.huff
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#import './PRBMathHelper.huff'

#define macro SQRT_PRB() = takes(1) returns(1){
//Input Stack = [x]
dup1 // [x,x]
iszero // [x==0?,x]
jumpi
zero_found
[X3] // [1e18,x]
mul // [1e18*x]
dup1 // [1e18*x,1e18*x]
0x01 // [1,1e18*x,1e18*x]
GATE1()
GATE2()
GATE3()
GATE4()
GATE5()
GATE6()
GATE7() // [result,xnum,x']
swap1 // [xnum,result,x']
pop // [result,x']
0x00 // [0,result,x']
loop
jump

loop: // [i,result,x']
0x01 // [1,i,result,x']
add // [i+1,result,x']
swap1 // [result,i+1,x']
dup1 // [result,result,i+1,x']
dup4 // [x',result,result,i+1,x']
add // [x'+result,result,i+1,x']
div // [(x'+result)/result,i+1,x'] , Let (x'+result)/result = result
dup2 // [i+1,result,i+1,x']
0x07 // [7,i+1,result,i+1,x']
eq // [i+1==7?,result,i+1,x']
done
jumpi
loop
jump

done: // [result,i+1,x']
swap1 // [i+1,result,x']
pop // [result,x']
dup1 // [result,result,x']
swap2 // [x',result,result]
div // [x'/result,result]
dup1 // [x'/result,x'/result,result]
dup3 // [result,x'/result,x'/result,result]
lt // [result < x'/result,x'/result,result]
iszero
final
jumpi
pop

final: // [x'/result,result]
swap1 // [result,x'/result]
pop // [x'/result]

zero_found: //[x]
pop // []
0x00 // [0]

}

#define macro LOGE_PRB() = takes(1) returns(1) {
//Input stack => [x]
LOG2() // [log2(x)]
[X3] // [1e18,log2(x)]
mul // [1e18*log2(x)]
[LOG2_E] // [Log2(e),1e18*log2(x)]
swap1 // [1e18*log2(x),Log2(e)]
sdiv // [1e18*log2(x)/Log2(e)]
}

116 changes: 116 additions & 0 deletions src/PRBMathHelper.huff
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

#define macro GATE1() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x100000000000000000000000000000000 // [0x100000000000000000000000000000000,xn,result,xn,x]
gt // [0x100000000000000000000000000000000>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
64 shl // [result<<64,xn,x]
swap1 // [xn,result<<64,x]
128 shr // [xn >> 128,result<<64,x]
swap1 // [result<<64,xn >> 128]
}

#define macro GATE2() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x10000000000000000 // [0x10000000000000000,xn,result,xn,x]
gt // [0x10000000000000000>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
32 shl // [result<<32,xn,x]
swap1 // [xn,result<<32,x]
64 shr // [xn >> 64,result<<32,x]
swap1 // [result<<32,xn>>64,x]
}

#define macro GATE3() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x100000000 // [0x100000000,xn,result,xn,x]
gt // [0x100000000>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
16 shl // [result<<16,xn,x]
swap1 // [xn,result<<16,x]
32 shr // [xn >> 32,result<<16,x]
swap1 // [result<<16,xn >> 32,x]
}

#define macro GATE4() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x10000 // [0x10000,xn,result,xn,x]
gt // [0x10000>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
8 shl // [result<<8,xn,x]
swap1 // [xn,result<<8,x]
16 shr // [xn >> 16,result<<8,x]
swap1 // [result<<16,xn >> 8]
}

#define macro GATE5() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x100 // [0x100,xn,result,xn,x]
gt // [0x100>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
4 shl // [result<<4,xn,x]
swap1 // [xn,result<<4,x]
8 shr // [xn >> 8,result<<4,x]
swap1 // [result<<4,xn >>8]
}
#define macro GATE6() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x10 // [0x10,xn,result,xn,x]
gt // [0x10>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
2 shl // [result<<2,xn,x]
swap1 // [xn,result<<2,x]
4 shr // [xn >> 4,result<<2,x]
swap1 // [result<<2,xn >>4]
}

#define macro GATE7() = takes(0) returns(0) {
//Input Stack [result,xn,x]
dup2 // [xn,result,xn,x]
0x4 // [0x4,xn,result,xn,x]
gt // [0x4>xn,result,xn,x]
iszero
satisfied
jumpi

satisfied:
1 shl // [result<<1,xn,x]
}

#define macro LOG2() = takes(0) returns(0) {

}

#define macro MOST_SIGNIFICANT_BIT() = takes(0) returns(0) {
// Input Stack => [x]
}
2 changes: 1 addition & 1 deletion src/WRAPPER.huff
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include 'Complex.huff'
#include './Complex.huff'

#define function addZ(int256 , int 256 , int256 , int256) returns (int256 ,int256)
#define function subZ(int256 , int 256 , int256 , int256) returns (int256 ,int256)
Expand Down

0 comments on commit ff696ae

Please sign in to comment.