--- /dev/null
+use crate::traits::{Context, Float};
+
+pub fn abs_f16<Ctx: Context>(ctx: Ctx, x: Ctx::VecF16) -> Ctx::VecF16 {
+ Ctx::VecF16::from_bits(x.to_bits() & ctx.make(0x7FFFu16))
+}
+
+pub fn abs_f32<Ctx: Context>(ctx: Ctx, x: Ctx::VecF32) -> Ctx::VecF32 {
+ Ctx::VecF32::from_bits(x.to_bits() & ctx.make(!(1u32 << 31)))
+}
+
+pub fn abs_f64<Ctx: Context>(ctx: Ctx, x: Ctx::VecF64) -> Ctx::VecF64 {
+ Ctx::VecF64::from_bits(x.to_bits() & ctx.make(!(1u64 << 63)))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ f16::F16,
+ scalar::{Scalar, Value},
+ };
+
+ #[test]
+ #[cfg_attr(
+ not(feature = "f16"),
+ should_panic(expected = "f16 feature is not enabled")
+ )]
+ fn test_abs_f16() {
+ for bits in 0..=u16::MAX {
+ let v = F16::from_bits(bits);
+ let expected = v.abs();
+ let result = abs_f16(Scalar, Value(v)).0;
+ assert_eq!(expected.to_bits(), result.to_bits());
+ }
+ }
+
+ #[test]
+ fn test_abs_f32() {
+ for bits in (0..=u32::MAX).step_by(10001) {
+ let v = f32::from_bits(bits);
+ let expected = v.abs();
+ let result = abs_f32(Scalar, Value(v)).0;
+ assert_eq!(expected.to_bits(), result.to_bits());
+ }
+ }
+
+ #[test]
+ fn test_abs_f64() {
+ for bits in (0..=u64::MAX).step_by(100_000_000_000_001) {
+ let v = f64::from_bits(bits);
+ let expected = v.abs();
+ let result = abs_f64(Scalar, Value(v)).0;
+ assert_eq!(expected.to_bits(), result.to_bits());
+ }
+ }
+}