Skip to main content

dec_number_sys/
dec_context.rs

1//! Safe bindings for decimal context.
2
3use crate::dec_context_c::*;
4use std::ffi::CStr;
5
6/// Initializes context to ANSI X3-274 defaults.
7pub const DEC_INIT_BASE: i32 = 0;
8
9/// Initializes context to IEEE 754 defaults, 32-bit.
10pub const DEC_INIT_DECIMAL32: i32 = 32;
11
12/// Initializes context to IEEE 754 defaults, 64-bit.
13pub const DEC_INIT_DECIMAL64: i32 = 64;
14
15/// Initializes context to IEEE 754 defaults, 128-bit.
16pub const DEC_INIT_DECIMAL128: i32 = 128;
17
18/// Decimal context.
19#[repr(C)]
20#[derive(Default, Debug, Copy, Clone)]
21pub struct DecContext {
22  /// Working precision.
23  pub digits: i32,
24  /// Maximum positive exponent.
25  pub emax: i32,
26  /// Minimum negative exponent.
27  pub emin: i32,
28  /// Rounding mode.
29  pub round: u32,
30  /// Trap-enabler flags.
31  pub traps: u32,
32  /// Status flags.
33  pub status: u32,
34  /// Flag: apply IEEE exponent clamp.
35  pub clamp: u8,
36}
37
38/// Returns decimal context initialized with maximum specified number of digits.
39pub fn dec_context_base(digits: i32) -> DecContext {
40  let mut context = DecContext::default();
41  unsafe {
42    decContextDefault(&mut context, DEC_INIT_BASE);
43    context.digits = digits;
44    context.traps = 0;
45  }
46  context
47}
48
49/// Returns decimal context initialized for 32-bit decimals.
50pub fn dec_context_32() -> DecContext {
51  let mut context = DecContext::default();
52  unsafe {
53    decContextDefault(&mut context, DEC_INIT_DECIMAL32);
54    context.traps = 0;
55  }
56  context
57}
58
59/// Returns decimal context initialized for 64-bit decimals.
60pub fn dec_context_64() -> DecContext {
61  let mut context = DecContext::default();
62  unsafe {
63    decContextDefault(&mut context, DEC_INIT_DECIMAL64);
64    context.traps = 0;
65  }
66  context
67}
68
69/// Returns decimal context initialized for 128-bit decimals.
70pub fn dec_context_128() -> DecContext {
71  let mut context = DecContext::default();
72  unsafe {
73    decContextDefault(&mut context, DEC_INIT_DECIMAL128);
74    context.traps = 0;
75  }
76  context
77}
78
79/// Safe binding to *decContextDefault* function.
80pub fn dec_context_default(kind: i32) -> DecContext {
81  unsafe {
82    let mut context = DecContext::default();
83    decContextDefault(&mut context, kind);
84    context.traps = 0;
85    context
86  }
87}
88
89/// Safe binding to *decContextGetStatus* function.
90pub fn dec_context_get_status(dc: &mut DecContext) -> u32 {
91  unsafe { decContextGetStatus(dc) }
92}
93
94/// Safe binding to *decContextStatusToString* function.
95pub fn dec_context_status_to_string(dc: &mut DecContext) -> String {
96  unsafe {
97    let s = decContextStatusToString(dc);
98    CStr::from_ptr(s).to_string_lossy().into_owned()
99  }
100}
101
102/// Safe binding to *decContextZeroStatus* function.
103pub fn dec_context_zero_status(dc: &mut DecContext) {
104  unsafe {
105    decContextZeroStatus(dc);
106  }
107}