{
    "Function": "mulDiv128",
    "File": "contracts/libraries/Math.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [
        "mulmod(uint256,uint256,uint256)",
        "require(bool)",
        "mulmod(uint256,uint256,uint256)"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function mulDiv128(uint256 a, uint256 b) internal pure returns (uint256 result) {\n        unchecked {\n            // 512-bit multiply [prod1 prod0] = a * b\n            // Compute the product mod 2**256 and mod 2**256 - 1\n            // then use the Chinese Remainder Theorem to reconstruct\n            // the 512 bit result. The result is stored in two 256\n            // variables such that product = prod1 * 2**256 + prod0\n            uint256 prod0; // Least significant 256 bits of the product\n            uint256 prod1; // Most significant 256 bits of the product\n            assembly (\"memory-safe\") {\n                let mm := mulmod(a, b, not(0))\n                prod0 := mul(a, b)\n                prod1 := sub(sub(mm, prod0), lt(mm, prod0))\n            }\n\n            // Handle non-overflow cases, 256 by 256 division\n            if (prod1 == 0) {\n                assembly (\"memory-safe\") {\n                    // Right shift by n is equivalent and 2 gas cheaper than division by 2^n\n                    result := shr(128, prod0)\n                }\n                return result;\n            }\n\n            // Make sure the result is less than 2**256.\n            require(2 ** 128 > prod1);\n\n            ///////////////////////////////////////////////\n            // 512 by 256 division.\n            ///////////////////////////////////////////////\n\n            // Make division exact by subtracting the remainder from [prod1 prod0]\n            // Compute remainder using mulmod\n            uint256 remainder;\n            assembly (\"memory-safe\") {\n                remainder := mulmod(a, b, 0x100000000000000000000000000000000)\n            }\n            // Subtract 256 bit number from 512 bit number\n            assembly (\"memory-safe\") {\n                prod1 := sub(prod1, gt(remainder, prod0))\n                prod0 := sub(prod0, remainder)\n            }\n\n            // Divide [prod1 prod0] by the factors of two (note that this is just 2**128 since the denominator is a power of 2 itself)\n            assembly (\"memory-safe\") {\n                // Right shift by n is equivalent and 2 gas cheaper than division by 2^n\n                prod0 := shr(128, prod0)\n            }\n            // Shift in bits from prod1 into prod0. For this we need\n            // to flip `twos` such that it is 2**256 / twos.\n            // If twos is zero, then it becomes one\n            // Note that this is just 2**160 since 2**256 over the fixed denominator (2**128) equals 2**128\n            prod0 |= prod1 * 2 ** 128;\n\n            return prod0;\n        }\n    }"
}