Importance of saturation instructions | Cypress Semiconductor
Importance of saturation instructions
Many of you may not be aware of what saturation instructions do in PSoC 5LP. PSoC 5LP uses Cortex-M3 core which has two special instructions called "SSAT" (Signed saturation) and "USAT" (Unsigned saturation). These instructions are used to limit a variable or a value to a certain number of bits. (Reference:
For example, if you have an 8 bit variable and for sure if you know that the value of the variable will not exceed a number 127 say, you would like to round off any value above 127 as 127 itself ( saturation) and any value below 0 as zero itself. In this case you need to write a big C code using if-else conditional statement to compare the variable with 127 and take the necessary action. Instead, to simplify this logic, you can make use of the saturation assembly instructions which Cortex-M3 (core of PSoC 5LP) supports. Please note that both GCC and MDK compilers do not append this instruction by itself for an equivalent C code.
For example, in the above example, the number of bits to restrict my result to would be 7 (2^7-1 = 127). Assuming I have the unsaturated value in register r0 and if I would like to store the saturated result in register r3, my assembly instruction would be
"usat r3, 8, r0"
Similar syntax is for signed saturation for specifying a positive limit and a negative limit for a signed variable. I hope you know the syntax to include an assembly instruction in C code.
FYI, if you would like to assign a variable to a particular register, the syntax is "register (data type)* variable_name asm("register name");"
For example: register uint8* output asm ("r0");