Skip to content

Commit eb31b01

Browse files
my4ngTheDan64
andauthored
Add get_bit_width method for float type (#589)
* feat: add `get_bit_width` method for float type * test: add `test_float_type` * fix: change bfloat to only llvm11+ --------- Co-authored-by: Dan Kolsoi <ThaDan64@gmail.com>
1 parent e3369e7 commit eb31b01

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/types/float_type.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use llvm_sys::core::{LLVMConstReal, LLVMConstRealOfStringAndSize};
1+
use llvm_sys::core::{LLVMConstReal, LLVMConstRealOfStringAndSize, LLVMGetTypeKind};
22
use llvm_sys::execution_engine::LLVMCreateGenericValueOfFloat;
33
use llvm_sys::prelude::LLVMTypeRef;
44

@@ -254,6 +254,41 @@ impl<'ctx> FloatType<'ctx> {
254254
self.float_type.ptr_type(address_space)
255255
}
256256

257+
/// Gets the bit width of a `FloatType`.
258+
///
259+
/// # Example
260+
/// ```no_run
261+
/// use inkwell::context::Context;
262+
///
263+
/// let context = Context::create();
264+
/// let f128_type = context.f128_type();
265+
///
266+
/// assert_eq!(f128_type.get_bit_width(), 128);
267+
/// ```
268+
pub fn get_bit_width(self) -> u32 {
269+
let type_kind = unsafe { LLVMGetTypeKind(self.as_type_ref()) };
270+
271+
match type_kind {
272+
llvm_sys::LLVMTypeKind::LLVMHalfTypeKind => 16,
273+
#[cfg(any(
274+
feature = "llvm11-0",
275+
feature = "llvm12-0",
276+
feature = "llvm13-0",
277+
feature = "llvm14-0",
278+
feature = "llvm15-0",
279+
feature = "llvm16-0",
280+
feature = "llvm17-0",
281+
feature = "llvm18-1"
282+
))]
283+
llvm_sys::LLVMTypeKind::LLVMBFloatTypeKind => 16,
284+
llvm_sys::LLVMTypeKind::LLVMFloatTypeKind => 32,
285+
llvm_sys::LLVMTypeKind::LLVMDoubleTypeKind => 64,
286+
llvm_sys::LLVMTypeKind::LLVMX86_FP80TypeKind => 80,
287+
llvm_sys::LLVMTypeKind::LLVMFP128TypeKind | llvm_sys::LLVMTypeKind::LLVMPPC_FP128TypeKind => 128,
288+
_ => unreachable!(),
289+
}
290+
}
291+
257292
/// Print the definition of a `FloatType` to `LLVMString`.
258293
pub fn print_to_string(self) -> LLVMString {
259294
self.float_type.print_to_string()

tests/all/test_types.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,25 @@ fn test_const_zero() {
489489
);
490490
}
491491

492+
#[test]
493+
fn test_float_type() {
494+
let context = Context::create();
495+
496+
let f16_type = context.f16_type();
497+
let f32_type = context.f32_type();
498+
let f64_type = context.f64_type();
499+
let f128_type = context.f128_type();
500+
let x86_f80_type = context.x86_f80_type();
501+
let ppc_f128_type = context.ppc_f128_type();
502+
503+
assert_eq!(f16_type.get_bit_width(), 16);
504+
assert_eq!(f32_type.get_bit_width(), 32);
505+
assert_eq!(f64_type.get_bit_width(), 64);
506+
assert_eq!(f128_type.get_bit_width(), 128);
507+
assert_eq!(x86_f80_type.get_bit_width(), 80);
508+
assert_eq!(ppc_f128_type.get_bit_width(), 128);
509+
}
510+
492511
#[test]
493512
fn test_vec_type() {
494513
let context = Context::create();

0 commit comments

Comments
 (0)