pub const Managed = struct allocator: AllocatorAllocator used by the Managed when requesting memory.
limbs: []LimbRaw digits. These are:
Accessing limbs directly should be avoided.
metadata: usizeHigh bit is the sign bit. If set, Managed is negative, else Managed is positive. The remaining bits represent the number of limbs used by Managed.
Default number of limbs to allocate on creation of a Managed.
pub fn init(allocator: Allocator) !ManagedCreates a new Managed. default_capacity limbs will be allocated immediately. The integer value after initializing is 0.
pub fn toMutable(self: Managed) Mutablepub fn toConst(self: Managed) Constpub fn initSet(allocator: Allocator, value: anytype) !ManagedCreates a new Managed with value value.
pub fn initCapacity(allocator: Allocator, capacity: usize) !ManagedCreates a new Managed with a specific capacity. If capacity < default_capacity then the default capacity will be used instead. The integer value after initializing is 0.
pub fn len(self: Managed) usizeReturns the number of limbs currently in use.
pub fn isPositive(self: Managed) boolReturns whether an Managed is positive.
pub fn setSign(self: *Managed, positive: bool) voidSets the sign of an Managed.
pub fn setLen(self: *Managed, new_len: usize) voidSets the length of an Managed.
If setLen is used, then the Managed must be normalized to suit.
pub fn setMetadata(self: *Managed, positive: bool, length: usize) voidpub fn ensureCapacity(self: *Managed, capacity: usize) !voidEnsures an Managed has enough space allocated for capacity limbs. If the Managed does not have sufficient capacity, the exact amount will be allocated. This occurs even if the requested capacity is only greater than the current capacity by one limb.
pub fn deinit(self: *Managed) voidFrees all associated memory.
pub fn clone(other: Managed) !ManagedReturns a Managed with the same value. The returned Managed is a deep copy and can be modified separately from the original, and its resources are managed separately from the original.
pub fn cloneWithDifferentAllocator(other: Managed, allocator: Allocator) !Managedpub fn copy(self: *Managed, other: Const) !voidCopies the value of the integer to an existing Managed so that they both have the same value. Extra memory will be allocated if the receiver does not have enough capacity.
pub fn swap(self: *Managed, other: *Managed) voidEfficiently swap a Managed with another. This swaps the limb pointers and a full copy is not performed. The address of the limbs field will not be the same after this function.
pub fn dump(self: Managed) voidDebugging tool: prints the state to stderr.
pub fn negate(self: *Managed) voidNegate the sign.
pub fn abs(self: *Managed) voidMake positive.
pub fn isOdd(self: Managed) boolpub fn isEven(self: Managed) boolpub fn bitCountAbs(self: Managed) usizeReturns the number of bits required to represent the absolute value of an integer.
pub fn bitCountTwosComp(self: Managed) usizeReturns the number of bits required to represent the integer in twos-complement form.
If the integer is negative the value returned is the number of bits needed by a signed integer to represent the value. If positive the value is the number of bits for an unsigned integer. Any unsigned integer will fit in the signed integer with bitcount one greater than the returned value.
e.g. -127 returns 8 as it will fit in an i8. 127 returns 7 since it fits in a u7.
pub fn fitsInTwosComp(self: Managed, signedness: Signedness, bit_count: usize) boolpub fn fits(self: Managed, comptime T: type) boolReturns whether self can fit into an integer of the requested type.
pub fn sizeInBaseUpperBound(self: Managed, base: usize) usizeReturns the approximate size of the integer in the given base. Negative values accommodate for the minus sign. This is used for determining the number of characters needed to print the value. It is inexact and may exceed the given value by ~1-2 bytes.
pub fn set(self: *Managed, value: anytype) Allocator.Error!voidSets an Managed to value. Value must be an primitive integer type.
pub fn toInt(self: Managed, comptime Int: type) ConvertError!IntReturns an error if self cannot be narrowed into the requested type without truncation.
pub fn setString(self: *Managed, base: u8, value: []const u8) !voidSet self from the string representation value.
value must contain only digits <= base and is case insensitive. Base prefixes are not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are ignored and can be used as digit separators.
Returns an error if memory could not be allocated or value has invalid digits for the requested base.
self's allocator is used for temporary storage to boost multiplication performance.
pub fn setTwosCompIntLimit(r: *Managed, limit: TwosCompIntLimit, signedness: Signedness, bit_count: usize) !voidSet self to either bound of a 2s-complement integer. Note: The result is still sign-magnitude, not twos complement! In order to convert the result to twos complement, it is sufficient to take the absolute value.
pub fn toString(self: Managed, allocator: Allocator, base: u8, case: std.fmt.Case) ![]u8Converts self to a string in the requested base. Memory is allocated from the provided allocator and not the one present in self.
pub fn format(self: Managed, w: *std.Io.Writer) std.Io.Writer.Error!voidTo allow std.fmt.format to work with Managed.
pub fn formatNumber(self: Managed, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!voidIf the absolute value of integer is greater than or equal to pow(2, 64 * @sizeOf(usize) * 8), this function will fail to print the string, printing "(BigInt)" instead of a number. This is because the rendering algorithm requires reversing a string, which requires O(N) memory. See toString and toStringAlloc for a way to print big integers without failure.
pub fn orderAbs(a: Managed, b: Managed) math.OrderReturns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| == |b| or |a| > |b| respectively.
pub fn order(a: Managed, b: Managed) math.OrderReturns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a > b respectively.
pub fn eqlZero(a: Managed) boolReturns true if a == 0.
pub fn eqlAbs(a: Managed, b: Managed) boolReturns true if |a| == |b|.
pub fn eql(a: Managed, b: Managed) boolReturns true if a == b.
pub fn normalize(r: *Managed, length: usize) voidNormalize a possible sequence of leading zeros.
[1, 2, 3, 4, 0] -> [1, 2, 3, 4] [1, 2, 0, 0, 0] -> [1, 2] [0, 0, 0, 0, 0] -> [0]
pub fn addScalar(r: *Managed, a: *const Managed, scalar: anytype) Allocator.Error!voidr = a + scalar
r and a may be aliases.
Returns an error if memory could not be allocated.
pub fn add(r: *Managed, a: *const Managed, b: *const Managed) Allocator.Error!voidr = a + b
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn addWrap(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!boolr = a + b with 2s-complement wrapping semantics. Returns whether any overflow occurred.
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn addSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!voidr = a + b with 2s-complement saturating semantics.
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn sub(r: *Managed, a: *const Managed, b: *const Managed) !voidr = a - b
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn subWrap(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!boolr = a - b with 2s-complement wrapping semantics. Returns whether any overflow occurred.
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn subSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!voidr = a - b with 2s-complement saturating semantics.
r, a and b may be aliases.
Returns an error if memory could not be allocated.
pub fn mul(rma: *Managed, a: *const Managed, b: *const Managed) !voidrma = a * b
rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
Returns an error if memory could not be allocated.
rma's allocator is used for temporary storage to speed up the multiplication.
pub fn mulWrap(rma: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) !voidrma = a * b with 2s-complement wrapping semantics.
rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
Returns an error if memory could not be allocated.
rma's allocator is used for temporary storage to speed up the multiplication.
pub fn ensureTwosCompCapacity(r: *Managed, bit_count: usize) !voidfn limbsAliasDistinct(a: *const Managed, b: *const Managed) boolTrue if two distinct Managed parameters share the same limbs buffer.
We specifically exclude the case where @intFromPtr(a) == @intFromPtr(b) (same object). When both pointers refer to the same Managed instance, ensureCapacity can reallocate the buffer (if needed) without creating dangling pointers for that object.
fn ensureAliasAwareCapacity(r: *Managed, needed: usize, aliased: bool) !voidWhen aliased is false (including when both pointers refer to the same object), ensureCapacity may reallocate; callers who rely on distinct Managed instances aliasing must ensure capacity before aliasing. See https://github.com/ziglang/zig/issues/6167
pub fn ensureAddScalarCapacity(r: *Managed, a: *const Managed, scalar: anytype) !voidUse this function before doing addScalar if some of your parameters alias each other
pub fn ensureAddCapacity(r: *Managed, a: *const Managed, b: *const Managed) !voidUse this function before doing add if some of your parameters alias each other
pub fn ensureMulCapacity(rma: *Managed, a: *const Managed, b: *const Managed) !voidUse this function before doing mul if some of your parameters alias each other
pub fn divFloor(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !voidq = a / b (rem r)
a / b are floored (rounded towards 0).
Returns an error if memory could not be allocated.
pub fn divCeil(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !voidq = a / b (rem r)
a / b are ceiled (rounded towards positive infinity).
Returns an error if memory could not be allocated.
pub fn divTrunc(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !voidq = a / b (rem r)
a / b are truncated (rounded towards -inf).
Returns an error if memory could not be allocated.
pub fn shiftLeft(r: *Managed, a: *const Managed, shift: usize) !voidr = a << shift, in other words, r = a * 2^shift r and a may alias.
pub fn shiftLeftSat(r: *Managed, a: *const Managed, shift: usize, signedness: Signedness, bit_count: usize) !voidr = a <<| shift with 2s-complement saturating semantics. r and a may alias.
pub fn shiftRight(r: *Managed, a: *const Managed, shift: usize) !voidr = a >> shift r and a may alias.
pub fn bitNotWrap(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !voidr = ~a under 2s-complement wrapping semantics. r and a may alias.
pub fn bitOr(r: *Managed, a: *const Managed, b: *const Managed) !voidr = a | b
a and b are zero-extended to the longer of a or b.
pub fn bitAnd(r: *Managed, a: *const Managed, b: *const Managed) !voidr = a & b
pub fn bitXor(r: *Managed, a: *const Managed, b: *const Managed) !voidr = a ^ b
pub fn gcd(rma: *Managed, x: *const Managed, y: *const Managed) !voidrma may alias x or y. x and y may alias each other.
rma's allocator is used for temporary storage to boost multiplication performance.
pub fn sqr(rma: *Managed, a: *const Managed) !voidr = a * a
pub fn pow(rma: *Managed, a: *const Managed, b: u32) !voidpub fn sqrt(rma: *Managed, a: *const Managed) !voidr = ⌊√a⌋
pub fn truncate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !voidr = truncate(Int(signedness, bit_count), a)
pub fn saturate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !voidr = saturate(Int(signedness, bit_count), a)
pub fn popCount(r: *Managed, a: *const Managed, bit_count: usize) !voidr = @popCount(a) with 2s-complement semantics. r and a may be aliases.
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623pub const Managed = struct {
pub const sign_bit: usize = 1 << (@typeInfo(usize).int.bits - 1);
/// Default number of limbs to allocate on creation of a `Managed`.
pub const default_capacity = 4;
/// Allocator used by the Managed when requesting memory.
allocator: Allocator,
/// Raw digits. These are:
///
/// * Little-endian ordered
/// * limbs.len >= 1
/// * Zero is represent as Managed.len() == 1 with limbs[0] == 0.
///
/// Accessing limbs directly should be avoided.
limbs: []Limb,
/// High bit is the sign bit. If set, Managed is negative, else Managed is positive.
/// The remaining bits represent the number of limbs used by Managed.
metadata: usize,
/// Creates a new `Managed`. `default_capacity` limbs will be allocated immediately.
/// The integer value after initializing is `0`.
pub fn init(allocator: Allocator) !Managed {
return initCapacity(allocator, default_capacity);
}
pub fn toMutable(self: Managed) Mutable {
return .{
.limbs = self.limbs,
.positive = self.isPositive(),
.len = self.len(),
};
}
pub fn toConst(self: Managed) Const {
return .{
.limbs = self.limbs[0..self.len()],
.positive = self.isPositive(),
};
}
/// Creates a new `Managed` with value `value`.
///
/// This is identical to an `init`, followed by a `set`.
pub fn initSet(allocator: Allocator, value: anytype) !Managed {
var s = try Managed.init(allocator);
errdefer s.deinit();
try s.set(value);
return s;
}
/// Creates a new Managed with a specific capacity. If capacity < default_capacity then the
/// default capacity will be used instead.
/// The integer value after initializing is `0`.
pub fn initCapacity(allocator: Allocator, capacity: usize) !Managed {
return .{
.allocator = allocator,
.metadata = 1,
.limbs = block: {
const limbs = try allocator.alloc(Limb, @max(default_capacity, capacity));
limbs[0] = 0;
break :block limbs;
},
};
}
/// Returns the number of limbs currently in use.
pub fn len(self: Managed) usize {
return self.metadata & ~sign_bit;
}
/// Returns whether an Managed is positive.
pub fn isPositive(self: Managed) bool {
return self.metadata & sign_bit == 0;
}
/// Sets the sign of an Managed.
pub fn setSign(self: *Managed, positive: bool) void {
if (positive) {
self.metadata &= ~sign_bit;
} else {
self.metadata |= sign_bit;
}
}
/// Sets the length of an Managed.
///
/// If setLen is used, then the Managed must be normalized to suit.
pub fn setLen(self: *Managed, new_len: usize) void {
self.metadata &= sign_bit;
self.metadata |= new_len;
}
pub fn setMetadata(self: *Managed, positive: bool, length: usize) void {
self.metadata = if (positive) length & ~sign_bit else length | sign_bit;
}
/// Ensures an Managed has enough space allocated for capacity limbs. If the Managed does not have
/// sufficient capacity, the exact amount will be allocated. This occurs even if the requested
/// capacity is only greater than the current capacity by one limb.
pub fn ensureCapacity(self: *Managed, capacity: usize) !void {
if (capacity <= self.limbs.len) {
return;
}
self.limbs = try self.allocator.realloc(self.limbs, capacity);
}
/// Frees all associated memory.
pub fn deinit(self: *Managed) void {
self.allocator.free(self.limbs);
self.* = undefined;
}
/// Returns a `Managed` with the same value. The returned `Managed` is a deep copy and
/// can be modified separately from the original, and its resources are managed
/// separately from the original.
pub fn clone(other: Managed) !Managed {
return other.cloneWithDifferentAllocator(other.allocator);
}
pub fn cloneWithDifferentAllocator(other: Managed, allocator: Allocator) !Managed {
return .{
.allocator = allocator,
.metadata = other.metadata,
.limbs = block: {
const limbs = try allocator.alloc(Limb, other.len());
@memcpy(limbs, other.limbs[0..other.len()]);
break :block limbs;
},
};
}
/// Copies the value of the integer to an existing `Managed` so that they both have the same value.
/// Extra memory will be allocated if the receiver does not have enough capacity.
pub fn copy(self: *Managed, other: Const) !void {
if (self.limbs.ptr == other.limbs.ptr) return;
try self.ensureCapacity(other.limbs.len);
@memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]);
self.setMetadata(other.positive, other.limbs.len);
}
/// Efficiently swap a `Managed` with another. This swaps the limb pointers and a full copy is not
/// performed. The address of the limbs field will not be the same after this function.
pub fn swap(self: *Managed, other: *Managed) void {
mem.swap(Managed, self, other);
}
/// Debugging tool: prints the state to stderr.
pub fn dump(self: Managed) void {
for (self.limbs[0..self.len()]) |limb| {
std.debug.print("{x} ", .{limb});
}
std.debug.print("len={} capacity={} positive={}\n", .{ self.len(), self.limbs.len, self.isPositive() });
}
/// Negate the sign.
pub fn negate(self: *Managed) void {
self.metadata ^= sign_bit;
}
/// Make positive.
pub fn abs(self: *Managed) void {
self.metadata &= ~sign_bit;
}
pub fn isOdd(self: Managed) bool {
return self.limbs[0] & 1 != 0;
}
pub fn isEven(self: Managed) bool {
return !self.isOdd();
}
/// Returns the number of bits required to represent the absolute value of an integer.
pub fn bitCountAbs(self: Managed) usize {
return self.toConst().bitCountAbs();
}
/// Returns the number of bits required to represent the integer in twos-complement form.
///
/// If the integer is negative the value returned is the number of bits needed by a signed
/// integer to represent the value. If positive the value is the number of bits for an
/// unsigned integer. Any unsigned integer will fit in the signed integer with bitcount
/// one greater than the returned value.
///
/// e.g. -127 returns 8 as it will fit in an i8. 127 returns 7 since it fits in a u7.
pub fn bitCountTwosComp(self: Managed) usize {
return self.toConst().bitCountTwosComp();
}
pub fn fitsInTwosComp(self: Managed, signedness: Signedness, bit_count: usize) bool {
return self.toConst().fitsInTwosComp(signedness, bit_count);
}
/// Returns whether self can fit into an integer of the requested type.
pub fn fits(self: Managed, comptime T: type) bool {
return self.toConst().fits(T);
}
/// Returns the approximate size of the integer in the given base. Negative values accommodate for
/// the minus sign. This is used for determining the number of characters needed to print the
/// value. It is inexact and may exceed the given value by ~1-2 bytes.
pub fn sizeInBaseUpperBound(self: Managed, base: usize) usize {
return self.toConst().sizeInBaseUpperBound(base);
}
/// Sets an Managed to value. Value must be an primitive integer type.
pub fn set(self: *Managed, value: anytype) Allocator.Error!void {
try self.ensureCapacity(calcLimbLen(value));
var m = self.toMutable();
m.set(value);
self.setMetadata(m.positive, m.len);
}
pub const ConvertError = Const.ConvertError;
/// Convert `self` to `Int`.
///
/// Returns an error if self cannot be narrowed into the requested type without truncation.
pub fn toInt(self: Managed, comptime Int: type) ConvertError!Int {
return self.toConst().toInt(Int);
}
/// Convert `self` to `Float`.
pub fn toFloat(self: Managed, comptime Float: type, round: Round) struct { Float, Exactness } {
return self.toConst().toFloat(Float, round);
}
/// Set self from the string representation `value`.
///
/// `value` must contain only digits <= `base` and is case insensitive. Base prefixes are
/// not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are
/// ignored and can be used as digit separators.
///
/// Returns an error if memory could not be allocated or `value` has invalid digits for the
/// requested base.
///
/// self's allocator is used for temporary storage to boost multiplication performance.
pub fn setString(self: *Managed, base: u8, value: []const u8) !void {
if (base < 2 or base > 36) return error.InvalidBase;
try self.ensureCapacity(calcSetStringLimbCount(base, value.len));
var m = self.toMutable();
try m.setString(base, value);
self.setMetadata(m.positive, m.len);
}
/// Set self to either bound of a 2s-complement integer.
/// Note: The result is still sign-magnitude, not twos complement! In order to convert the
/// result to twos complement, it is sufficient to take the absolute value.
pub fn setTwosCompIntLimit(
r: *Managed,
limit: TwosCompIntLimit,
signedness: Signedness,
bit_count: usize,
) !void {
try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
var m = r.toMutable();
m.setTwosCompIntLimit(limit, signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// Converts self to a string in the requested base. Memory is allocated from the provided
/// allocator and not the one present in self.
pub fn toString(self: Managed, allocator: Allocator, base: u8, case: std.fmt.Case) ![]u8 {
if (base < 2 or base > 36) return error.InvalidBase;
return self.toConst().toStringAlloc(allocator, base, case);
}
/// To allow `std.fmt.format` to work with `Managed`.
pub fn format(self: Managed, w: *std.Io.Writer) std.Io.Writer.Error!void {
return formatNumber(self, w, .{});
}
/// If the absolute value of integer is greater than or equal to `pow(2, 64 * @sizeOf(usize) * 8)`,
/// this function will fail to print the string, printing "(BigInt)" instead of a number.
/// This is because the rendering algorithm requires reversing a string, which requires O(N) memory.
/// See `toString` and `toStringAlloc` for a way to print big integers without failure.
pub fn formatNumber(self: Managed, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {
return self.toConst().formatNumber(w, n);
}
/// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==
/// |b| or |a| > |b| respectively.
pub fn orderAbs(a: Managed, b: Managed) math.Order {
return a.toConst().orderAbs(b.toConst());
}
/// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a > b
/// respectively.
pub fn order(a: Managed, b: Managed) math.Order {
return a.toConst().order(b.toConst());
}
/// Returns true if a == 0.
pub fn eqlZero(a: Managed) bool {
return a.toConst().eqlZero();
}
/// Returns true if |a| == |b|.
pub fn eqlAbs(a: Managed, b: Managed) bool {
return a.toConst().eqlAbs(b.toConst());
}
/// Returns true if a == b.
pub fn eql(a: Managed, b: Managed) bool {
return a.toConst().eql(b.toConst());
}
/// Normalize a possible sequence of leading zeros.
///
/// [1, 2, 3, 4, 0] -> [1, 2, 3, 4]
/// [1, 2, 0, 0, 0] -> [1, 2]
/// [0, 0, 0, 0, 0] -> [0]
pub fn normalize(r: *Managed, length: usize) void {
assert(length > 0);
assert(length <= r.limbs.len);
var j = length;
while (j > 0) : (j -= 1) {
if (r.limbs[j - 1] != 0) {
break;
}
}
// Handle zero
r.setLen(if (j != 0) j else 1);
}
/// r = a + scalar
///
/// r and a may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn addScalar(r: *Managed, a: *const Managed, scalar: anytype) Allocator.Error!void {
const needed = @max(a.len(), calcLimbLen(scalar)) + 1;
const aliased = limbsAliasDistinct(r, a);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.addScalar(a.toConst(), scalar);
r.setMetadata(m.positive, m.len);
}
/// r = a + b
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn add(r: *Managed, a: *const Managed, b: *const Managed) Allocator.Error!void {
const needed = @max(a.len(), b.len()) + 1;
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.add(a.toConst(), b.toConst());
r.setMetadata(m.positive, m.len);
}
/// r = a + b with 2s-complement wrapping semantics. Returns whether any overflow occurred.
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn addWrap(
r: *Managed,
a: *const Managed,
b: *const Managed,
signedness: Signedness,
bit_count: usize,
) Allocator.Error!bool {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
const wrapped = m.addWrap(a.toConst(), b.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
return wrapped;
}
/// r = a + b with 2s-complement saturating semantics.
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn addSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!void {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.addSat(a.toConst(), b.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// r = a - b
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn sub(r: *Managed, a: *const Managed, b: *const Managed) !void {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = @max(a.len(), b.len()) + 1;
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.sub(a.toConst(), b.toConst());
r.setMetadata(m.positive, m.len);
}
/// r = a - b with 2s-complement wrapping semantics. Returns whether any overflow occurred.
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn subWrap(
r: *Managed,
a: *const Managed,
b: *const Managed,
signedness: Signedness,
bit_count: usize,
) Allocator.Error!bool {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
const wrapped = m.subWrap(a.toConst(), b.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
return wrapped;
}
/// r = a - b with 2s-complement saturating semantics.
///
/// r, a and b may be aliases.
///
/// Returns an error if memory could not be allocated.
pub fn subSat(
r: *Managed,
a: *const Managed,
b: *const Managed,
signedness: Signedness,
bit_count: usize,
) Allocator.Error!void {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.subSat(a.toConst(), b.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// rma = a * b
///
/// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
///
/// Returns an error if memory could not be allocated.
///
/// rma's allocator is used for temporary storage to speed up the multiplication.
pub fn mul(rma: *Managed, a: *const Managed, b: *const Managed) !void {
var alias_count: usize = 0;
if (rma.limbs.ptr == a.limbs.ptr)
alias_count += 1;
if (rma.limbs.ptr == b.limbs.ptr)
alias_count += 1;
const needed = a.len() + b.len() + 1;
const capacity_alias = limbsAliasDistinct(rma, a) or limbsAliasDistinct(rma, b);
try rma.ensureAliasAwareCapacity(needed, capacity_alias);
var m = rma.toMutable();
if (alias_count == 0) {
m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator);
} else {
const limb_count = calcMulLimbsBufferLen(a.len(), b.len(), alias_count);
const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
defer rma.allocator.free(limbs_buffer);
m.mul(a.toConst(), b.toConst(), limbs_buffer, rma.allocator);
}
rma.setMetadata(m.positive, m.len);
}
/// rma = a * b with 2s-complement wrapping semantics.
///
/// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
///
/// Returns an error if memory could not be allocated.
///
/// rma's allocator is used for temporary storage to speed up the multiplication.
pub fn mulWrap(
rma: *Managed,
a: *const Managed,
b: *const Managed,
signedness: Signedness,
bit_count: usize,
) !void {
var alias_count: usize = 0;
if (rma.limbs.ptr == a.limbs.ptr)
alias_count += 1;
if (rma.limbs.ptr == b.limbs.ptr)
alias_count += 1;
const needed = calcTwosCompLimbCount(bit_count);
const capacity_alias = limbsAliasDistinct(rma, a) or limbsAliasDistinct(rma, b);
try rma.ensureAliasAwareCapacity(needed, capacity_alias);
var m = rma.toMutable();
if (alias_count == 0) {
m.mulWrapNoAlias(a.toConst(), b.toConst(), signedness, bit_count, rma.allocator);
} else {
const limb_count = calcMulWrapLimbsBufferLen(bit_count, a.len(), b.len(), alias_count);
const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
defer rma.allocator.free(limbs_buffer);
m.mulWrap(a.toConst(), b.toConst(), signedness, bit_count, limbs_buffer, rma.allocator);
}
rma.setMetadata(m.positive, m.len);
}
pub fn ensureTwosCompCapacity(r: *Managed, bit_count: usize) !void {
try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
}
/// True if two distinct `Managed` parameters share the same limbs buffer.
///
/// We specifically exclude the case where `@intFromPtr(a) == @intFromPtr(b)` (same object).
/// When both pointers refer to the same `Managed` instance, `ensureCapacity` can reallocate
/// the buffer (if needed) without creating dangling pointers for that object.
fn limbsAliasDistinct(a: *const Managed, b: *const Managed) bool {
return @intFromPtr(a) != @intFromPtr(b) and a.limbs.ptr == b.limbs.ptr;
}
/// When `aliased` is false (including when both pointers refer to the same object),
/// `ensureCapacity` may reallocate; callers who rely on distinct `Managed` instances
/// aliasing must ensure capacity before aliasing.
/// See https://github.com/ziglang/zig/issues/6167
fn ensureAliasAwareCapacity(r: *Managed, needed: usize, aliased: bool) !void {
if (aliased) {
assert(needed <= r.limbs.len);
} else {
try r.ensureCapacity(needed);
}
}
/// Use this function before doing `addScalar` if some of your parameters alias each other
pub fn ensureAddScalarCapacity(r: *Managed, a: *const Managed, scalar: anytype) !void {
try r.ensureCapacity(@max(a.len(), calcLimbLen(scalar)) + 1);
}
/// Use this function before doing `add` if some of your parameters alias each other
pub fn ensureAddCapacity(r: *Managed, a: *const Managed, b: *const Managed) !void {
try r.ensureCapacity(@max(a.len(), b.len()) + 1);
}
/// Use this function before doing `mul` if some of your parameters alias each other
pub fn ensureMulCapacity(rma: *Managed, a: *const Managed, b: *const Managed) !void {
try rma.ensureCapacity(a.len() + b.len() + 1);
}
/// q = a / b (rem r)
///
/// a / b are floored (rounded towards 0).
///
/// Returns an error if memory could not be allocated.
pub fn divFloor(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
const q_alias = limbsAliasDistinct(q, a) or limbsAliasDistinct(q, b);
const r_alias = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try q.ensureAliasAwareCapacity(a.len(), q_alias);
try r.ensureAliasAwareCapacity(b.len(), r_alias);
var mq = q.toMutable();
var mr = r.toMutable();
const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.len(), b.len()));
defer q.allocator.free(limbs_buffer);
mq.divFloor(&mr, a.toConst(), b.toConst(), limbs_buffer);
q.setMetadata(mq.positive, mq.len);
r.setMetadata(mr.positive, mr.len);
}
/// q = a / b (rem r)
///
/// a / b are ceiled (rounded towards positive infinity).
///
/// Returns an error if memory could not be allocated.
pub fn divCeil(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
const q_alias = limbsAliasDistinct(q, a) or limbsAliasDistinct(q, b);
const r_alias = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try q.ensureAliasAwareCapacity(a.len(), q_alias);
try r.ensureAliasAwareCapacity(b.len(), r_alias);
var mq = q.toMutable();
var mr = r.toMutable();
const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.len(), b.len()));
defer q.allocator.free(limbs_buffer);
mq.divCeil(&mr, a.toConst(), b.toConst(), limbs_buffer);
q.setMetadata(mq.positive, mq.len);
r.setMetadata(mr.positive, mr.len);
}
/// q = a / b (rem r)
///
/// a / b are truncated (rounded towards -inf).
///
/// Returns an error if memory could not be allocated.
pub fn divTrunc(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
const q_alias = limbsAliasDistinct(q, a) or limbsAliasDistinct(q, b);
const r_alias = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try q.ensureAliasAwareCapacity(a.len(), q_alias);
try r.ensureAliasAwareCapacity(b.len(), r_alias);
var mq = q.toMutable();
var mr = r.toMutable();
const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.len(), b.len()));
defer q.allocator.free(limbs_buffer);
mq.divTrunc(&mr, a.toConst(), b.toConst(), limbs_buffer);
q.setMetadata(mq.positive, mq.len);
r.setMetadata(mr.positive, mr.len);
}
/// r = a << shift, in other words, r = a * 2^shift
/// r and a may alias.
pub fn shiftLeft(r: *Managed, a: *const Managed, shift: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = a.len() + (shift / limb_bits) + 1;
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.shiftLeft(a.toConst(), shift);
r.setMetadata(m.positive, m.len);
}
/// r = a <<| shift with 2s-complement saturating semantics.
/// r and a may alias.
pub fn shiftLeftSat(r: *Managed, a: *const Managed, shift: usize, signedness: Signedness, bit_count: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.shiftLeftSat(a.toConst(), shift, signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// r = a >> shift
/// r and a may alias.
pub fn shiftRight(r: *Managed, a: *const Managed, shift: usize) !void {
if (a.len() <= shift / limb_bits) {
// Shifting negative numbers converges to -1 instead of 0
if (a.isPositive()) {
r.metadata = 1;
r.limbs[0] = 0;
} else {
r.metadata = 1;
r.setSign(false);
r.limbs[0] = 1;
}
return;
}
const aliased = limbsAliasDistinct(r, a);
const needed = a.len() - (shift / limb_bits);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.shiftRight(a.toConst(), shift);
r.setMetadata(m.positive, m.len);
}
/// r = ~a under 2s-complement wrapping semantics.
/// r and a may alias.
pub fn bitNotWrap(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.bitNotWrap(a.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// r = a | b
///
/// a and b are zero-extended to the longer of a or b.
pub fn bitOr(r: *Managed, a: *const Managed, b: *const Managed) !void {
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
const needed = @max(a.len(), b.len());
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.bitOr(a.toConst(), b.toConst());
r.setMetadata(m.positive, m.len);
}
/// r = a & b
pub fn bitAnd(r: *Managed, a: *const Managed, b: *const Managed) !void {
const cap = if (a.len() >= b.len())
if (b.isPositive()) b.len() else if (a.isPositive()) a.len() else a.len() + 1
else if (a.isPositive()) a.len() else if (b.isPositive()) b.len() else b.len() + 1;
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try r.ensureAliasAwareCapacity(cap, aliased);
var m = r.toMutable();
m.bitAnd(a.toConst(), b.toConst());
r.setMetadata(m.positive, m.len);
}
/// r = a ^ b
pub fn bitXor(r: *Managed, a: *const Managed, b: *const Managed) !void {
const cap = @max(a.len(), b.len()) + @intFromBool(a.isPositive() != b.isPositive());
const aliased = limbsAliasDistinct(r, a) or limbsAliasDistinct(r, b);
try r.ensureAliasAwareCapacity(cap, aliased);
var m = r.toMutable();
m.bitXor(a.toConst(), b.toConst());
r.setMetadata(m.positive, m.len);
}
/// rma may alias x or y.
/// x and y may alias each other.
///
/// rma's allocator is used for temporary storage to boost multiplication performance.
pub fn gcd(rma: *Managed, x: *const Managed, y: *const Managed) !void {
const aliased = limbsAliasDistinct(rma, x) or limbsAliasDistinct(rma, y);
const needed = @min(x.len(), y.len());
try rma.ensureAliasAwareCapacity(needed, aliased);
var m = rma.toMutable();
var limbs_buffer = std.array_list.Managed(Limb).init(rma.allocator);
defer limbs_buffer.deinit();
try m.gcd(x.toConst(), y.toConst(), &limbs_buffer);
rma.setMetadata(m.positive, m.len);
}
/// r = a * a
pub fn sqr(rma: *Managed, a: *const Managed) !void {
const needed_limbs = 2 * a.len() + 1;
const capacity_alias = limbsAliasDistinct(rma, a);
const same_buffer = rma.limbs.ptr == a.limbs.ptr;
try rma.ensureAliasAwareCapacity(needed_limbs, capacity_alias);
if (same_buffer) {
const a_len = a.len();
const tmp = try rma.allocator.alloc(Limb, a_len);
defer rma.allocator.free(tmp);
@memcpy(tmp[0..a_len], a.limbs[0..a_len]);
const a_const: Const = .{ .limbs = tmp[0..a_len], .positive = a.isPositive() };
var rma_mut = rma.toMutable();
rma_mut.sqrNoAlias(a_const, rma.allocator);
rma.setMetadata(rma_mut.positive, rma_mut.len);
} else {
var rma_mut = rma.toMutable();
rma_mut.sqrNoAlias(a.toConst(), rma.allocator);
rma.setMetadata(rma_mut.positive, rma_mut.len);
}
}
pub fn pow(rma: *Managed, a: *const Managed, b: u32) !void {
const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);
const capacity_alias = limbsAliasDistinct(rma, a);
const same_buffer = rma.limbs.ptr == a.limbs.ptr;
try rma.ensureAliasAwareCapacity(needed_limbs, capacity_alias);
const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
defer rma.allocator.free(limbs_buffer);
if (same_buffer) {
const a_len = a.len();
const tmp = try rma.allocator.alloc(Limb, a_len);
defer rma.allocator.free(tmp);
@memcpy(tmp[0..a_len], a.limbs[0..a_len]);
const a_const: Const = .{ .limbs = tmp[0..a_len], .positive = a.isPositive() };
var rma_mut = rma.toMutable();
rma_mut.pow(a_const, b, limbs_buffer);
rma.setMetadata(rma_mut.positive, rma_mut.len);
} else {
var rma_mut = rma.toMutable();
rma_mut.pow(a.toConst(), b, limbs_buffer);
rma.setMetadata(rma_mut.positive, rma_mut.len);
}
}
/// r = ⌊√a⌋
pub fn sqrt(rma: *Managed, a: *const Managed) !void {
const bit_count = a.bitCountAbs();
const aliased = limbsAliasDistinct(rma, a);
if (bit_count == 0) {
try rma.set(0);
rma.setMetadata(a.isPositive(), rma.len());
return;
}
if (!a.isPositive()) {
return error.SqrtOfNegativeNumber;
}
const needed_limbs = calcSqrtLimbsBufferLen(bit_count);
const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
defer rma.allocator.free(limbs_buffer);
const needed = (a.len() - 1) / 2 + 1;
try rma.ensureAliasAwareCapacity(needed, aliased);
var m = rma.toMutable();
m.sqrt(a.toConst(), limbs_buffer);
rma.setMetadata(m.positive, m.len);
}
/// r = truncate(Int(signedness, bit_count), a)
pub fn truncate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.truncate(a.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// r = saturate(Int(signedness, bit_count), a)
pub fn saturate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.saturate(a.toConst(), signedness, bit_count);
r.setMetadata(m.positive, m.len);
}
/// r = @popCount(a) with 2s-complement semantics.
/// r and a may be aliases.
pub fn popCount(r: *Managed, a: *const Managed, bit_count: usize) !void {
const aliased = limbsAliasDistinct(r, a);
const needed = calcTwosCompLimbCount(bit_count);
try r.ensureAliasAwareCapacity(needed, aliased);
var m = r.toMutable();
m.popCount(a.toConst(), bit_count);
r.setMetadata(m.positive, m.len);
}
}
An arbitrary-precision big integer along with an allocator which manages the memory.
Memory is allocated as needed to ensure operations never overflow. The range is bounded only by available memory.