It's a control flow stack the MCU in the FC does the math for to interpret what command to give the ESC in order to achieve the desired result.
The quad is sitting in front of you facing away from you when you tell it to set it's orientation, this we will call zero degrees. The quad now knows
that where ever it flies away from you it has to find 180 degrees to get back to you.
Let's begin:
The quad flies straight away from you, then you make a perfect right turn. The chip notes a 90 degree turn and retains +90 in memory.
You make a 50 degree left turn. The quad subtracts that leaving +40 in the memory. You now turn left again 80 degrees.
The quad's memory is now at -40. The quad flies out of sight and you hit RTH. The quad knows it's at -40 relative to it's zero setpoint,
and so adds a -140 control input to derive the 180 value, thereby sending it back from whence it came.
It chooses the lesser of any value needed to add to it's current memory in order to achieve the 180 value, be it negative or positive.
For example, if it's last heading was +90 from zero it would add +90 to get home (instead of -270). If it was still flying directly away
from you it would have to add 180 to zero to come back.