Skip to main content

melior/ir/attribute/
float.rs

1use super::{Attribute, AttributeLike};
2use crate::{
3    ir::{Type, TypeLike},
4    Context, Error,
5};
6use mlir_sys::{mlirFloatAttrDoubleGet, mlirFloatAttrGetValueDouble, MlirAttribute};
7
8/// A float attribute.
9#[derive(Clone, Copy)]
10pub struct FloatAttribute<'c> {
11    attribute: Attribute<'c>,
12}
13
14impl<'c> FloatAttribute<'c> {
15    /// Creates a float attribute.
16    pub fn new(context: &'c Context, r#type: Type<'c>, number: f64) -> Self {
17        unsafe {
18            Self::from_raw(mlirFloatAttrDoubleGet(
19                context.to_raw(),
20                r#type.to_raw(),
21                number,
22            ))
23        }
24    }
25
26    /// Returns a value.
27    pub fn value(&self) -> f64 {
28        unsafe { mlirFloatAttrGetValueDouble(self.to_raw()) }
29    }
30}
31
32attribute_traits!(FloatAttribute, is_float, "float");
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::test::create_test_context;
38
39    #[test]
40    fn value() {
41        let context = create_test_context();
42
43        assert_eq!(
44            FloatAttribute::new(&context, Type::float64(&context), 42.0).value(),
45            42.0
46        );
47    }
48}