[compiler-rt] [Hexagon] Add v79 QFloat HVX runtime conversion funcs (PR #229481)

Fateme Hosseini via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 09:54:42 PDT 2026


https://github.com/fhossein-quic created https://github.com/llvm/llvm-project/pull/229481

Add four assembly files for half-float to integer
conversions using QFloat (qf16) on Hexagon V79.

Each function provides IEEE round-to-nearest-even or fast round-half-away-from-zero conversion for signed and unsigned 8-bit and 16-bit integer destinations.

V79 lacks direct half-float to integer conversion. V75 and below have native IEEE vcvt instructions,
while V81 and newer have dedicated hardware support.

Assembly is ported from libqcc.a. Some instruction packets are split because upstream LLVM assembler
enforces stricter HVX slot constraints than the SDK assembler.

Co-authored-by: Sumanth Gundapaneni <sgundapa at quicinc.com>

>From 77c9498f2ac66055225e9dd024fb21442469b219 Mon Sep 17 00:00:00 2001
From: Fateme Hosseini <fhossein at qti.qualcomm.com>
Date: Tue, 6 Oct 2026 09:21:03 -0700
Subject: [PATCH] [Hexagon] Add v79 QFloat HVX runtime conversion funcs

Add four assembly files for half-float to integer
conversions using QFloat (qf16) on Hexagon V79.

Each function provides IEEE round-to-nearest-even or
fast round-half-away-from-zero conversion for signed
and unsigned 8-bit and 16-bit integer destinations.

V79 lacks direct half-float to integer conversion.
V75 and below have native IEEE vcvt instructions,
while V81 and newer have dedicated hardware support.

Assembly is ported from libqcc.a. Some instruction
packets are split because upstream LLVM assembler
enforces stricter HVX slot constraints than the SDK
assembler.

Co-authored-by: Sumanth Gundapaneni <sgundapa at quicinc.com>
---
 compiler-rt/lib/builtins/CMakeLists.txt       |   4 +
 .../hexagon/ieee_qfloat_conv_hf_to_b_rne.S    | 187 +++++++++++++++++
 .../hexagon/ieee_qfloat_conv_hf_to_h_rne.S    | 114 +++++++++++
 .../hexagon/ieee_qfloat_conv_hf_to_ub_rne.S   | 184 +++++++++++++++++
 .../hexagon/ieee_qfloat_conv_hf_to_uh_rne.S   | 188 ++++++++++++++++++
 5 files changed, 677 insertions(+)
 create mode 100644 compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_b_rne.S
 create mode 100644 compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_h_rne.S
 create mode 100644 compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_ub_rne.S
 create mode 100644 compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_uh_rne.S

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 12a3b068b33736..65a679a992651a 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -940,6 +940,10 @@ set(hexagon_SOURCES
   hexagon/udivsi3.S
   hexagon/umoddi3.S
   hexagon/umodsi3.S
+  hexagon/ieee_qfloat_conv_hf_to_b_rne.S
+  hexagon/ieee_qfloat_conv_hf_to_h_rne.S
+  hexagon/ieee_qfloat_conv_hf_to_ub_rne.S
+  hexagon/ieee_qfloat_conv_hf_to_uh_rne.S
   ${GENERIC_SOURCES}
   ${GENERIC_TF_SOURCES}
 )
diff --git a/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_b_rne.S b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_b_rne.S
new file mode 100644
index 00000000000000..788a40b0cb1984
--- /dev/null
+++ b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_b_rne.S
@@ -0,0 +1,187 @@
+//===-- ieee_qfloat_conv_hf_to_b_rne.S - HVX hf-to-int8 conversion -------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Half-float to signed int8 conversion with IEEE round-to-nearest-even.
+// Takes two HVX vectors of half-floats (V0, V1) and produces one vector
+// of signed bytes. Uses QFloat (qf16) as an intermediate format on
+// Hexagon V79.
+//
+//===----------------------------------------------------------------------===//
+
+	.macro FUNCTION_BEGIN name
+	.text
+	.p2align 5
+	.globl \name
+	.type  \name, @function
+\name:
+	.endm
+
+	.macro FUNCTION_END name
+	.size  \name, . - \name
+	.endm
+
+#if __HEXAGON_ARCH__ == 79
+FUNCTION_BEGIN __qf_convert_hf_to_b_rne
+//V0 and V1 have input
+//First rnd, replaces  V0.h=V0.hf
+//Convert input vector to half word
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V7.h=V0.hf //Convert and truncate half precision
+}
+{
+	V6=#0
+	R3=#0x00010001
+	R4=#0x37FF
+}
+{
+	V10=vsplat(R3)
+	V12.h=vsplat(R4)
+	V15.hf=V7.h //Convert truncated back to hf
+}
+{
+	V23.h=vsplat(R1) //V3 = +.5
+	V24.h=vsplat(R2) //V4 = -.5
+	V5.qf16=vsub(V0.hf,V15.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+	V22.h=vsplat(R0) //Splat R0 to Mask off sign bit
+}
+{
+	V15=vmux(Q0,V23,V24) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V22,V5) //V2=abs(frac)
+	V13=vand(V0,V22) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V23.h) // frac == .5 (0x3800)
+	V15=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V15.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V15.h,V15.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V15.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V15=vmux(Q1,V15,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	//Second rnd, replaces  V1.h=V1.hf //Convert input vector to half word
+	V7.h=V1.hf //Convert and truncate half precision
+}
+{
+	V16.h = vadd(V4.h,V15.h) //Add +1/-1 or 0. V4 has the result
+	V1.hf=V7.h //Convert truncated back to hf
+}
+{
+	V5.qf16=vsub(V0.hf,V1.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+}
+{
+	V1=vmux(Q0,V23,V24) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V22,V5) //V2=abs(frac)
+	V13=vand(V0,V22) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V23.h) // frac == .5 (0x3800)
+	V1=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V1.h,V1.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V1.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V1=vmux(Q1,V1,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	V17.h = vadd(V4.h,V1.h) //Add +1/-1 or 0. V4 has the result
+}
+{
+        V0.b = vpack(V17.h,V16.h):sat
+}
+{
+	V0.h=vshuff(V0.h)
+	jumpr R31
+}
+
+FUNCTION_END __qf_convert_hf_to_b_rne
+
+FUNCTION_BEGIN __qf_fast_convert_hf_to_b_rne
+//V0.hf and V1.hf has value to be converted
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V2=#0
+}
+{
+	Q0=vcmp.gt(V0.hf,V2.hf) // hf value is pos (hf > 0)
+	Q1=vcmp.gt(V1.hf,V2.hf) // hf value is pos (hf > 0)
+	V2.h=vsplat(R1) //V1 = +.5
+	V3.h=vsplat(R2) //V2 = -.5
+}
+{
+	V2=vmux(Q0,V2,V3) //If pos value, use +.5. If neg value, use -.5
+	V3=vmux(Q1,V2,V3) //If pos value, use +.5. If neg value, use -.5
+}
+{
+	V4.qf16=vadd(V0.hf,V2.hf) // Add +.5 or -.5 to V0
+}
+{
+	V5.qf16=vadd(V1.hf,V3.hf) // Add +.5 or -.5 to V1
+}
+{
+	V4.hf=V4.qf16
+}
+{
+	V5.hf=V5.qf16
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+}
+{
+	V5.h=V5.hf //Truncate, V5 has converted value
+}
+{
+	V0.ub = vpack(V4.h,V5.h):sat
+}
+{
+	V0.h = vshuff(V0.h)
+        jumpr LR
+}
+FUNCTION_END __qf_fast_convert_hf_to_b_rne
+#endif
diff --git a/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_h_rne.S b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_h_rne.S
new file mode 100644
index 00000000000000..3955244073c851
--- /dev/null
+++ b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_h_rne.S
@@ -0,0 +1,114 @@
+//===-- ieee_qfloat_conv_hf_to_h_rne.S - HVX hf-to-int16 conversion ------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Half-float to signed int16 conversion with IEEE round-to-nearest-even.
+// Uses QFloat (qf16) as an intermediate format on Hexagon V79.
+//
+//===----------------------------------------------------------------------===//
+
+	.macro FUNCTION_BEGIN name
+	.text
+	.p2align 5
+	.globl \name
+	.type  \name, @function
+\name:
+	.endm
+
+	.macro FUNCTION_END name
+	.size  \name, . - \name
+	.endm
+
+#if __HEXAGON_ARCH__ == 79
+FUNCTION_BEGIN __qf_convert_hf_to_h_rne
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V7.h=V0.hf //Convert and truncate half precision
+}
+{
+	V6=#0
+	R3=#0x00010001
+	R4=#0x37FF
+}
+{
+	V10=vsplat(R3)
+	V12.h=vsplat(R4)
+	V1.hf=V7.h //Convert truncated back to hf
+}
+{
+	V3.h=vsplat(R1) //V3 = +.5
+	V4.h=vsplat(R2) //V4 = -.5
+	V5.qf16=vsub(V0.hf,V1.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+	V2.h=vsplat(R0) //Splat R0 to Mask off sign bit
+}
+{
+	V1=vmux(Q0,V3,V4) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V2,V5) //V2=abs(frac)
+	V13=vand(V0,V2) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V3.h) // frac == .5 (0x3800)
+	V1=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V1.h,V1.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V1.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V1=vmux(Q1,V1,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	V0.h = vadd(V4.h,V1.h) //Add +1/-1 or 0. V4 has the result
+	jumpr LR
+}
+FUNCTION_END __qf_convert_hf_to_h_rne
+
+FUNCTION_BEGIN __qf_fast_convert_hf_to_h_rne
+//V0.hf has value to be converted
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V1=#0
+}
+{
+	Q0=vcmp.gt(V0.hf,V1.hf) // hf value is pos (hf > 0)
+	V1.h=vsplat(R1) //V1 = +.5
+	V2.h=vsplat(R2) //V2 = -.5
+}
+{
+	V1=vmux(Q0,V1,V2) //If pos value, use +.5. If neg value, use -.5
+}
+{
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5 or -.5 to V0
+}
+{
+	V4.hf=V4.qf16
+}
+{
+	V0.h=V4.hf //Truncate, V4 has converted value
+	jumpr LR
+}
+FUNCTION_END __qf_fast_convert_hf_to_h_rne
+#endif
diff --git a/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_ub_rne.S b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_ub_rne.S
new file mode 100644
index 00000000000000..c7f2a7fe586242
--- /dev/null
+++ b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_ub_rne.S
@@ -0,0 +1,184 @@
+//===-- ieee_qfloat_conv_hf_to_ub_rne.S - HVX hf-to-uint8 conversion -----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Half-float to unsigned int8 conversion with IEEE round-to-nearest-even.
+// Takes two HVX vectors of half-floats (V0, V1) and produces one vector
+// of unsigned bytes. Uses QFloat (qf16) as an intermediate format on
+// Hexagon V79.
+//
+//===----------------------------------------------------------------------===//
+
+	.macro FUNCTION_BEGIN name
+	.text
+	.p2align 5
+	.globl \name
+	.type  \name, @function
+\name:
+	.endm
+
+	.macro FUNCTION_END name
+	.size  \name, . - \name
+	.endm
+
+#if __HEXAGON_ARCH__ == 79
+FUNCTION_BEGIN __qf_convert_hf_to_ub_rne
+//V0 and V1 have input
+//First rnd, replaces  V0.h=V0.hf
+//Convert input vector to half word
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V7.h=V0.hf //Convert and truncate half precision
+}
+{
+	V6=#0
+	R3=#0x00010001
+	R4=#0x37FF
+}
+{
+	V10=vsplat(R3)
+	V12.h=vsplat(R4)
+	V15.hf=V7.h //Convert truncated back to hf
+}
+{
+	V23.h=vsplat(R1) //V3 = +.5
+	V24.h=vsplat(R2) //V4 = -.5
+	V5.qf16=vsub(V0.hf,V15.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+	V22.h=vsplat(R0) //Splat R0 to Mask off sign bit
+}
+{
+	V15=vmux(Q0,V23,V24) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V22,V5) //V2=abs(frac)
+	V13=vand(V0,V22) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V23.h) // frac == .5 (0x3800)
+	V15=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V15.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V15.h,V15.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V15.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V15=vmux(Q1,V15,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	//Second rnd, replaces  V1.h=V1.hf //Convert input vector to half word
+	V7.h=V1.hf //Convert and truncate half precision
+}
+{
+	V16.h = vadd(V4.h,V15.h) //Add +1/-1 or 0. V4 has the result
+	V1.hf=V7.h //Convert truncated back to hf
+}
+{
+	V5.qf16=vsub(V0.hf,V1.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+}
+{
+	V1=vmux(Q0,V23,V24) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V22,V5) //V2=abs(frac)
+	V13=vand(V0,V22) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V23.h) // frac == .5 (0x3800)
+	V1=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V1.h,V1.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V1.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V1=vmux(Q1,V1,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	V17.h = vadd(V4.h,V1.h) //Add +1/-1 or 0. V4 has the result
+}
+{
+        V0.ub = vpack(V17.h,V16.h):sat
+}
+{
+	V0.h=vshuff(V0.h)
+        jumpr LR
+}
+FUNCTION_END __qf_convert_hf_to_ub_rne
+
+FUNCTION_BEGIN __qf_fast_convert_hf_to_ub_rne
+//V0.hf and V1.hf have value to be converted
+{
+	R1=#0x3800 //+.5
+	V3=#0
+}
+{
+	Q0=vcmp.gt(V0.hf,V3.hf) // hf value is pos (hf > 0)
+	Q1=vcmp.gt(V1.hf,V3.hf) // hf value is pos (hf > 0)
+	V2.h=vsplat(R1) //V1 = +.5
+}
+{
+	V4.qf16=vadd(V0.hf,V2.hf) // Add +.5
+}
+{
+	V5.qf16=vadd(V1.hf,V2.hf) // Add +.5
+}
+{
+	V4.hf=V4.qf16
+}
+{
+	V5.hf=V5.qf16
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+}
+{
+	V5.h=V5.hf //Truncate, V5 has converted value
+}
+{
+	V4=vmux(Q0,V4,V3) //If original pos value, use converted. Else 0
+	V5=vmux(Q0,V5,V3) //If original pos value, use converted. Else 0
+}
+{
+	V0.ub = vpack(V4.h,V5.h):sat
+}
+{
+        V0.h = vshuff(V0.h)
+        jumpr LR
+}
+FUNCTION_END __qf_fast_convert_hf_to_ub_rne
+#endif
diff --git a/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_uh_rne.S b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_uh_rne.S
new file mode 100644
index 00000000000000..96521004ce40f4
--- /dev/null
+++ b/compiler-rt/lib/builtins/hexagon/ieee_qfloat_conv_hf_to_uh_rne.S
@@ -0,0 +1,188 @@
+//===-- ieee_qfloat_conv_hf_to_uh_rne.S - HVX hf-to-uint16 conversion ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Half-float to unsigned int16 conversion with IEEE round-to-nearest-even.
+// Uses QFloat (qf16) as an intermediate format on Hexagon V79. This is
+// the most complex variant because unsigned int16 range [0, 65535] exceeds
+// half-float precision, requiring a scale-by-0.5 trick.
+//
+//===----------------------------------------------------------------------===//
+
+	.macro FUNCTION_BEGIN name
+	.text
+	.p2align 5
+	.globl \name
+	.type  \name, @function
+\name:
+	.endm
+
+	.macro FUNCTION_END name
+	.size  \name, . - \name
+	.endm
+
+#if __HEXAGON_ARCH__ == 79
+FUNCTION_BEGIN __qf_convert_hf_to_uh_rne
+{
+        R20=#0x38003800 //.5
+        R21=#0x77FF //Number right before 2^15
+        R22=#1
+        R23=#0x7BFF //num right under Inf
+}
+{
+        V21=#0
+        V23.qf16=vmpy(V0.hf,R20.hf) //Multiply .5
+}
+//First rnd, replaces  V22.h=V0.hf
+//Convert input vector to half word
+{
+	R1=#0x3800 //+.5
+	R2=#0xB800 //-.5
+	V7.h=V0.hf //Convert and truncate half precision
+}
+{
+	V6=#0
+	R3=#0x00010001
+	R4=#0x37FF
+}
+{
+	V10=vsplat(R3)
+	V12.h=vsplat(R4)
+	V1.hf=V7.h //Convert truncated back to hf
+}
+{
+	V26.h=vsplat(R1) //V3 = +.5
+	V27.h=vsplat(R2) //V4 = -.5
+	V5.qf16=vsub(V0.hf,V1.hf) //Sub truncated value from original value
+	R0=#0x7FFF //Mask off sign bit of IEEE-754
+}
+{
+	Q0=vcmp.gt(V0.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+	V25.h=vsplat(R0) //Splat R0 to Mask off sign bit
+}
+{
+	V1=vmux(Q0,V26,V27) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V25,V5) //V2=abs(frac)
+	V13=vand(V0,V25) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V26.h) // frac == .5 (0x3800)
+	V1=vsplat(R3) //V1=1
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V1.h,V1.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V1.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V1=vmux(Q1,V1,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	V22.h = vadd(V4.h,V1.h) //Add +1/-1 or 0. V4 has the result
+}
+{
+        V23.hf=V23.qf16 //Convert multiply back to hf
+        V21.h=vmax(V21.h,V22.h) //Filter out anything lower than 0
+}
+{
+        V22.h=vsplat(R21)  //Splat overflow value for compare
+//Second rnd, replaces  V23.h=V23.hf
+//Convert input vector to half word
+        V7.h=V23.hf //Convert and truncate half precision
+}
+{
+	V1.hf=V7.h //Convert truncated back to hf
+}
+{
+	V5.qf16=vsub(V23.hf,V1.hf) //Sub truncated value from original value
+}
+{
+	Q0=vcmp.gt(V23.hf,V6.hf) // hf value is pos (hf > 0)
+	V5.hf=V5.qf16 //V5 is the fractional of original value
+}
+{
+	V1=vmux(Q0,V26,V27) //If input is pos value, use +.5. If neg value, use -.5 (Q in prev packet)
+	V2=vand(V25,V5) //V2=abs(frac)
+	V13=vand(V23,V25) //V13 = abs(input)
+}
+{
+	Q1=vcmp.eq(V2.h,V26.h) // frac == .5 (0x3800)
+	V1=vsplat(R3) //V1=1
+	V4.qf16=vadd(V23.hf,V1.hf) // Add +.5 or -.5 to V0
+	Q3=vcmp.eq(V13.h,V12.h) //Q3 = if(input == abs(.4998))
+}
+{
+	V11=vand(V7,V10) //Mask off all the lower bits
+	V3.h = vadd(V1.h,V1.h) //V3 is 2
+}
+{
+	Q2=vcmp.eq(V11.h,V10.h) //Is input value odd if halfword equals 0x1
+	V4.hf=V4.qf16 //hf value after add/sub .5
+}
+{
+	Q1=and(Q1,!Q2) //Is frac .5 and input is even
+	if (Q0) V1.h-=V3.h //If input is pos, sub 2 so V1 is -1
+	V4=vmux(Q3,V6,V4)
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+	V1=vmux(Q1,V1,V6) //If frac == .5 and input is even, use +1/-1. Else 0
+}
+{
+	V23.h = vadd(V4.h,V1.h) //Add +1/-1 or 0. V4 has the result
+}
+{
+        Q1=vcmp.gt(V22.hf,V0.hf) //Q1 is if value doesn't overflow on hf=h convert (V0 is input, V2 is right before 2^15)
+        V23.h=vasl(V23.h,R22)  //Left shift converted value
+        V24.h=vsplat(R23)  //Splat inf
+}
+{
+        Q1|=vcmp.gt(V0.uh,V24.uh) //Q1 is value is < inf/nan
+}
+{
+        V0=vmux(Q1,V21,V23) //If value is not overflow or input is Inf, set to converted input. Else, set to overflow
+	jumpr LR
+}
+FUNCTION_END __qf_convert_hf_to_uh_rne
+
+FUNCTION_BEGIN __qf_fast_convert_hf_to_uh_rne
+//V0.hf has value to be converted
+{
+	R1=#0x3800 //+.5
+	V3=#0
+}
+{
+	Q0=vcmp.gt(V0.hf,V3.hf) // hf value is pos (hf > 0)
+	V1.h=vsplat(R1) //V1 = +.5
+}
+{
+	V4.qf16=vadd(V0.hf,V1.hf) // Add +.5
+}
+{
+	V4.hf=V4.qf16
+}
+{
+	V4.h=V4.hf //Truncate, V4 has converted value
+}
+{
+	V0=vmux(Q0,V4,V3) //If original pos value, use converted. Else 0
+	jumpr LR
+}
+FUNCTION_END __qf_fast_convert_hf_to_uh_rne
+#endif



More information about the llvm-commits mailing list