rust_decimal/
constants.rs

1// Sign mask for the flags field. A value of zero in this bit indicates a
2// positive Decimal value, and a value of one in this bit indicates a
3// negative Decimal value.
4pub const SIGN_MASK: u32 = 0x8000_0000;
5pub const UNSIGN_MASK: u32 = 0x4FFF_FFFF;
6
7// Scale mask for the flags field. This byte in the flags field contains
8// the power of 10 to divide the Decimal value by. The scale byte must
9// contain a value between 0 and 28 inclusive.
10pub const SCALE_MASK: u32 = 0x00FF_0000;
11pub const U8_MASK: u32 = 0x0000_00FF;
12pub const U32_MASK: u64 = u32::MAX as _;
13
14// Number of bits scale is shifted by.
15pub const SCALE_SHIFT: u32 = 16;
16// Number of bits sign is shifted by.
17pub const SIGN_SHIFT: u32 = 31;
18
19// The maximum string buffer size used for serialization purposes. 31 is optimal, however we align
20// to the byte boundary for simplicity.
21pub const MAX_STR_BUFFER_SIZE: usize = 32;
22
23// The maximum supported [`Decimal::scale`] value
24pub const MAX_SCALE: u8 = 28;
25#[cfg(not(feature = "legacy-ops"))]
26// u8 to i32 is infallible, therefore, this cast will never overflow
27pub const MAX_SCALE_I32: i32 = MAX_SCALE as _;
28// u8 to u32 is infallible, therefore, this cast will never overflow
29pub const MAX_SCALE_U32: u32 = MAX_SCALE as _;
30// 79,228,162,514,264,337,593,543,950,335
31pub const MAX_I128_REPR: i128 = 0x0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
32
33// Fast access for 10^n where n is 0-9
34pub const POWERS_10: [u32; 10] = [
35    1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
36];
37// Fast access for 10^n where n is 1-19
38pub const BIG_POWERS_10: [u64; 19] = [
39    10,
40    100,
41    1000,
42    10000,
43    100000,
44    1000000,
45    10000000,
46    100000000,
47    1000000000,
48    10000000000,
49    100000000000,
50    1000000000000,
51    10000000000000,
52    100000000000000,
53    1000000000000000,
54    10000000000000000,
55    100000000000000000,
56    1000000000000000000,
57    10000000000000000000,
58];
59
60#[cfg(not(feature = "legacy-ops"))]
61// The maximum power of 10 that a 32 bit integer can store
62pub const MAX_I32_SCALE: i32 = 9;
63#[cfg(not(feature = "legacy-ops"))]
64// The maximum power of 10 that a 64 bit integer can store
65pub const MAX_I64_SCALE: u32 = 19;
66#[cfg(not(feature = "legacy-ops"))]
67pub const U32_MAX: u64 = u32::MAX as u64;
68
69// Determines potential overflow for 128 bit operations
70pub const OVERFLOW_U96: u128 = 1u128 << 96;
71pub const WILL_OVERFLOW_U64: u64 = u64::MAX / 10 - u8::MAX as u64;
72pub const BYTES_TO_OVERFLOW_U64: usize = 18; // We can probably get away with less