LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
APIntHelper.cpp
Go to the documentation of this file.
1//===-- APIntHelper.cpp - APInt helpers -------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
11
12namespace llzk {
13
14llvm::APSInt expandingAdd(const llvm::APSInt &lhs, const llvm::APSInt &rhs) {
15 unsigned requiredBits = std::max(lhs.getActiveBits(), rhs.getActiveBits()) + 1;
16 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
17 return lhs.extend(newBitwidth) + rhs.extend(newBitwidth);
18}
19
20llvm::APSInt expandingSub(const llvm::APSInt &lhs, const llvm::APSInt &rhs) {
21 unsigned requiredBits = std::max(lhs.getActiveBits(), rhs.getActiveBits()) + 1;
22 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
23 return lhs.extend(newBitwidth) - rhs.extend(newBitwidth);
24}
25
26llvm::APSInt expandingMul(const llvm::APSInt &lhs, const llvm::APSInt &rhs) {
27 unsigned requiredBits = lhs.getActiveBits() + rhs.getActiveBits();
28 unsigned newBitwidth = std::max({requiredBits, lhs.getBitWidth(), rhs.getBitWidth()});
29 return lhs.extend(newBitwidth) * rhs.extend(newBitwidth);
30}
31
32std::strong_ordering safeCmp(const llvm::APSInt &lhs, const llvm::APSInt &rhs) {
33 unsigned requiredBits = std::max(lhs.getBitWidth(), rhs.getBitWidth());
34 auto a = lhs.extend(requiredBits), b = rhs.extend(requiredBits);
35 if (a < b) {
36 return std::strong_ordering::less;
37 } else if (a > b) {
38 return std::strong_ordering::greater;
39 } else {
40 return std::strong_ordering::equal;
41 }
42}
43
44} // namespace llzk
This file defines helpers for manipulating APInts/APSInts for large numbers and operations over those...
llvm::APSInt expandingSub(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely subtract lhs and rhs, expanding the width of the result as necessary.
std::strong_ordering safeCmp(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Compares lhs and rhs, regardless of the bitwidth of lhs and rhs.
llvm::APSInt expandingAdd(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely add lhs and rhs, expanding the width of the result as necessary.
llvm::APSInt expandingMul(const llvm::APSInt &lhs, const llvm::APSInt &rhs)
Safely multiple lhs and rhs, expanding the width of the result as necessary.