Skip to content

Instantly share code, notes, and snippets.

View atarpara's full-sized avatar

Akshay Tarpara atarpara

View GitHub Profile
@atarpara
atarpara / README.md
Created April 12, 2022 14:37
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=

// this line is added to create a gist. Empty file is not allowed.

@atarpara
atarpara / clone.s.sol
Created August 20, 2022 16:19
clones using foundry script
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Script.sol";
import "src/Counter.sol";
import "src/LibClone.sol";
contract CounterScript is Script {
function setUp() public {
pragma solidity ^0.8.4;
library LibBit {
function msbPosition(uint256 value) internal pure returns(uint8 position) {
assembly {
if gt(value,0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {
value := shr(128,value)
position := add(128,position)
}
if gt(value,0xFFFFFFFFFFFFFFFF) {
@atarpara
atarpara / msb.sol
Created August 24, 2022 05:34
Most Significant Bit Index
pragma solidity ^0.8.4;
library LibBit {
function msbPosition(uint256 value) internal pure returns(uint8 position) {
assembly {
// value > 2*128 - 1
if gt(value,0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {
value := shr(128,value)
position := add(128,position)
}
@atarpara
atarpara / Lsbbit.sol
Last active August 26, 2022 08:55
LSB Bit Using Solmate Log2
pragma solidity ^0.8.4;
library LibBit {
function lsbsolmate(uint256 x) pure internal returns(uint256 r){
assembly {
// Set Only Right Most Bit all other 0
//x := xor(x,and(x,sub(x,1)))
x:= and(x,add(not(x),1))
// solmate log2 logic
r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x))
@atarpara
atarpara / gcd.sol
Created August 30, 2022 06:53
GCD in solidity
function gcd(uint256 x, uint256 y) internal pure returns(uint256 z){
assembly{
for {} 1 {} {
if eq(y,0) {
z := x
break
}
let temp := mod(x,y)
x := y
y := temp