# Non-SIMD Q15 saturation ALU Instructions¶

`__STATIC_FORCEINLINE long __RV_KADDH(int a, int b)`
`__STATIC_FORCEINLINE long __RV_KHMBB(unsigned int a, unsigned int b)`
`__STATIC_FORCEINLINE long __RV_KHMBT(unsigned int a, unsigned int b)`
`__STATIC_FORCEINLINE long __RV_KHMTT(unsigned int a, unsigned int b)`
`__STATIC_FORCEINLINE long __RV_KSUBH(int a, int b)`
`__STATIC_FORCEINLINE unsigned long __RV_UKADDH(unsigned int a, unsigned int b)`
`__STATIC_FORCEINLINE unsigned long __RV_UKSUBH(unsigned int a, unsigned int b)`
group `NMSIS_Core_DSP_Intrinsic_NON_SIMD_Q15_SAT_ALU`

Non-SIMD Q15 saturation ALU Instructions.

there are 7 Non-SIMD Q15 saturation ALU Instructions

Functions

`__STATIC_FORCEINLINE long __RV_KADDH(int a, int b)`

Type: DSP

Syntax:

```KADDH Rd, Rs1, Rs2
```

Purpose:

Add the signed lower 32-bit content of two registers with Q15 saturation.

Description:

The signed lower 32-bit content of Rs1 is added with the signed lower 32-bit content of Rs2. And the result is saturated to the 16-bit signed integer range of [-2^15, 2^15-1] and then sign- extended and written to Rd. If saturation happens, this instruction sets the OV flag.

Operations:

```tmp = Rs1.W + Rs2.W;
if (tmp > 32767) {
res = 32767;
OV = 1;
} else if (tmp < -32768) {
res = -32768;
OV = 1
} else {
res = tmp;
}
Rd = SE(tmp[15:0]);
```

Return

value stored in long type

Parameters
• `[in] a`: int type of value stored in a

• `[in] b`: int type of value stored in b

`__STATIC_FORCEINLINE long __RV_KHMBB(unsigned int a, unsigned int b)`

KHMBB (Signed Saturating Half Multiply B16 x B16)

Type: DSP

Syntax:

```KHMxy Rd, Rs1, Rs2 (xy = BB, BT, TT)
```

Purpose:

Multiply the signed Q15 number contents of two 16-bit data in the corresponding portion of the lower 32-bit chunk in registers and then right-shift 15 bits to turn the Q30 result into a Q15 number again and saturate the Q15 result into the destination register. If saturation happens, an overflow flag OV will be set.

Description:

Multiply the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs1 with the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs2. The Q30 result is then right- shifted 15-bits and saturated into a Q15 value. The Q15 value is then sing-extended and written into Rd. When both the two Q15 inputs are 0x8000, saturation will happen. The result will be saturated to 0x7FFF and the overflow flag OV will be set.

Operations:

```aop = Rs1.H; bop = Rs2.H; // KHMBB
aop = Rs1.H; bop = Rs2.H; // KHMBT
aop = Rs1.H; bop = Rs2.H; // KHMTT
If (0x8000 != aop | 0x8000 != bop) {
Mresult[31:0] = aop * bop;
res[15:0] = Mresult[30:15];
} else {
res[15:0] = 0x7FFF;
OV = 1;
}
Rd = SE32(res[15:0]); // Rv32
Rd = SE64(res[15:0]); // RV64
```

Return

value stored in long type

Parameters
• `[in] a`: unsigned int type of value stored in a

• `[in] b`: unsigned int type of value stored in b

`__STATIC_FORCEINLINE long __RV_KHMBT(unsigned int a, unsigned int b)`

KHMBT (Signed Saturating Half Multiply B16 x T16)

Type: DSP

Syntax:

```KHMxy Rd, Rs1, Rs2 (xy = BB, BT, TT)
```

Purpose:

Multiply the signed Q15 number contents of two 16-bit data in the corresponding portion of the lower 32-bit chunk in registers and then right-shift 15 bits to turn the Q30 result into a Q15 number again and saturate the Q15 result into the destination register. If saturation happens, an overflow flag OV will be set.

Description:

Multiply the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs1 with the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs2. The Q30 result is then right- shifted 15-bits and saturated into a Q15 value. The Q15 value is then sing-extended and written into Rd. When both the two Q15 inputs are 0x8000, saturation will happen. The result will be saturated to 0x7FFF and the overflow flag OV will be set.

Operations:

```aop = Rs1.H; bop = Rs2.H; // KHMBB
aop = Rs1.H; bop = Rs2.H; // KHMBT
aop = Rs1.H; bop = Rs2.H; // KHMTT
If (0x8000 != aop | 0x8000 != bop) {
Mresult[31:0] = aop * bop;
res[15:0] = Mresult[30:15];
} else {
res[15:0] = 0x7FFF;
OV = 1;
}
Rd = SE32(res[15:0]); // Rv32
Rd = SE64(res[15:0]); // RV64
```

Return

value stored in long type

Parameters
• `[in] a`: unsigned int type of value stored in a

• `[in] b`: unsigned int type of value stored in b

`__STATIC_FORCEINLINE long __RV_KHMTT(unsigned int a, unsigned int b)`

KHMTT (Signed Saturating Half Multiply T16 x T16)

Type: DSP

Syntax:

```KHMxy Rd, Rs1, Rs2 (xy = BB, BT, TT)
```

Purpose:

Multiply the signed Q15 number contents of two 16-bit data in the corresponding portion of the lower 32-bit chunk in registers and then right-shift 15 bits to turn the Q30 result into a Q15 number again and saturate the Q15 result into the destination register. If saturation happens, an overflow flag OV will be set.

Description:

Multiply the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs1 with the top or bottom 16-bit Q15 content of the lower 32-bit portion in Rs2. The Q30 result is then right- shifted 15-bits and saturated into a Q15 value. The Q15 value is then sing-extended and written into Rd. When both the two Q15 inputs are 0x8000, saturation will happen. The result will be saturated to 0x7FFF and the overflow flag OV will be set.

Operations:

```aop = Rs1.H; bop = Rs2.H; // KHMBB
aop = Rs1.H; bop = Rs2.H; // KHMBT
aop = Rs1.H; bop = Rs2.H; // KHMTT
If (0x8000 != aop | 0x8000 != bop) {
Mresult[31:0] = aop * bop;
res[15:0] = Mresult[30:15];
} else {
res[15:0] = 0x7FFF;
OV = 1;
}
Rd = SE32(res[15:0]); // Rv32
Rd = SE64(res[15:0]); // RV64
```

Return

value stored in long type

Parameters
• `[in] a`: unsigned int type of value stored in a

• `[in] b`: unsigned int type of value stored in b

`__STATIC_FORCEINLINE long __RV_KSUBH(int a, int b)`

KSUBH (Signed Subtraction with Q15 Saturation)

Type: DSP

Syntax:

```KSUBH Rd, Rs1, Rs2
```

Purpose:

Subtract the signed lower 32-bit content of two registers with Q15 saturation.

Description:

The signed lower 32-bit content of Rs2 is subtracted from the signed lower 32-bit content of Rs1. And the result is saturated to the 16-bit signed integer range of [-2^15, 2^15-1] and then sign-extended and written to Rd. If saturation happens, this instruction sets the OV flag.

Operations:

```tmp = Rs1.W - Rs2.W;
if (tmp > (2^15)-1) {
res = (2^15)-1;
OV = 1;
} else if (tmp < -2^15) {
res = -2^15;
OV = 1
} else {
res = tmp;
}
Rd = SE(res[15:0]);
```

Return

value stored in long type

Parameters
• `[in] a`: int type of value stored in a

• `[in] b`: int type of value stored in b

`__STATIC_FORCEINLINE unsigned long __RV_UKADDH(unsigned int a, unsigned int b)`

Type: DSP

Syntax:

```UKADDH Rd, Rs1, Rs2
```

Purpose:

Add the unsigned lower 32-bit content of two registers with U16 saturation.

Description:

The unsigned lower 32-bit content of Rs1 is added with the unsigned lower 32-bit content of Rs2. And the result is saturated to the 16-bit unsigned integer range of [0, 2^16-1] and then sign-extended and written to Rd. If saturation happens, this instruction sets the OV flag.

Operations:

```tmp = Rs1.W + Rs2.W;
if (tmp > (2^16)-1) {
tmp = (2^16)-1;
OV = 1;
}
Rd = SE(tmp[15:0]);
```

Return

value stored in unsigned long type

Parameters
• `[in] a`: unsigned int type of value stored in a

• `[in] b`: unsigned int type of value stored in b

`__STATIC_FORCEINLINE unsigned long __RV_UKSUBH(unsigned int a, unsigned int b)`

UKSUBH (Unsigned Subtraction with U16 Saturation)

Type: DSP

Syntax:

```UKSUBH Rd, Rs1, Rs2
```

Purpose:

Subtract the unsigned lower 32-bit content of two registers with U16 saturation.

Description:

The unsigned lower 32-bit content of Rs2 is subtracted from the unsigned lower 32-bit content of Rs1. And the result is saturated to the 16-bit unsigned integer range of [0, 2^16-1] and then sign-extended and written to Rd. If saturation happens, this instruction sets the OV flag.

Operations:

```tmp = Rs1.W - Rs2.W;
if (tmp > (2^16)-1) {
tmp = (2^16)-1;
OV = 1;
}
else if (tmp < 0) {
tmp = 0;
OV = 1;
}
Rd = SE(tmp[15:0]);
```

Return

value stored in unsigned long type

Parameters
• `[in] a`: unsigned int type of value stored in a

• `[in] b`: unsigned int type of value stored in b