1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use llvm_sys::core::LLVMGetTypeKind;
use llvm_sys::prelude::LLVMTypeRef;
use llvm_sys::LLVMTypeKind;
use std::marker::PhantomData;

use crate::types::*;
use crate::{FromLLVMType, TypeRef};

/// The type kind for Float Type
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub enum FloatTypeKind {
  /// 16-bit
  Half,
  /// 32-bit
  Single,
  /// 64-bit
  Double,
  /// 128-bit
  FP128,
  /// 80-bit for X86
  X86_FP80,
  /// 128-bit for Power PC
  PPC_FP128,
}

impl FloatTypeKind {
  /// Get the bit-width of that type kind
  pub fn width(&self) -> u32 {
    match self {
      Self::Half => 16,
      Self::Single => 32,
      Self::Double => 64,
      Self::FP128 => 128,
      Self::X86_FP80 => 80,
      Self::PPC_FP128 => 128,
    }
  }

  pub(crate) fn from_llvm(tk: LLVMTypeKind) -> Option<Self> {
    use FloatTypeKind::*;
    use LLVMTypeKind::*;
    match tk {
      LLVMHalfTypeKind => Some(Half),
      LLVMFloatTypeKind => Some(Single),
      LLVMDoubleTypeKind => Some(Double),
      LLVMFP128TypeKind => Some(FP128),
      LLVMX86_FP80TypeKind => Some(X86_FP80),
      LLVMPPC_FP128TypeKind => Some(PPC_FP128),
      _ => None,
    }
  }
}

/// [Float Type](https://llvm.org/docs/LangRef.html#floating-point-types)
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct FloatType<'ctx>(FloatTypeKind, LLVMTypeRef, PhantomData<&'ctx ()>);

impl_send_sync!(FloatType);

impl<'ctx> FloatType<'ctx> {
  /// Get the kind
  pub fn kind(&self) -> FloatTypeKind {
    self.0
  }

  /// Get the bit-width for this float type
  pub fn width(&self) -> u32 {
    self.0.width()
  }
}

impl<'ctx> AsType<'ctx> for FloatType<'ctx> {
  fn as_type(&self) -> Type<'ctx> {
    Type::Float(self.clone())
  }
}

impl_positional_type_ref!(FloatType, 1);

impl<'ctx> FromLLVMType for FloatType<'ctx> {
  fn from_llvm(ptr: LLVMTypeRef) -> Self {
    let kind = FloatTypeKind::from_llvm(unsafe { LLVMGetTypeKind(ptr) }).unwrap();
    Self(kind, ptr, PhantomData)
  }
}