function::fp_add - Linux


Overview

fp_add is a high-precision fixed-point addition command. It computes the fixed-point sum of two arguments and stores the result as an integer in the target register. This command is particularly useful for precise numerical calculations, such as signal processing, audio engineering, and financial applications.

Syntax

fp_add [options] <source_register> <source_register> <target_register>

Options/Flags

There are no available options or flags for this command.

Examples

Simple addition:

fp_add r1, r2, r3

This adds the values in registers r1 and r2 and stores the sum in r3.

Adding fractional values:

fp_add r1, r2, r3
fp_set_frac r3, 8

This adds the values in registers r1 and r2, stores the sum in r3, and sets the fractional part to 8 bits.

Common Issues

  • NaN (Not-a-Number) results: If either source register contains NaN, the result will also be NaN.
  • Overflow: If the sum of the two source registers exceeds the maximum integer value, the result will wrap around.
  • Underflow: If the sum of the two source registers is less than the minimum integer value, the result will be zero.

Integration

fp_add can be combined with other fixed-point commands, such as fp_mul, fp_div, and fp_sqrt, to build complex fixed-point calculations.

Example script: To calculate the quadratic equation of ax^2 + bx + c = 0, you can use the following script:

# declare coefficient registers
move r1, a
move r2, b
move r3, c

# compute (-b ± √(b^2 - 4ac)) / 2a
fp_mul r4, r2, r2   # r4 = b^2
fp_mul r5, r1, r3   # r5 = 4ac
fp_sub r6, r4, r5   # r6 = b^2 - 4ac
fp_sqrt r6, r6      # r6 = √(b^2 - 4ac)
fp_neg r7, r2       # r7 = -b
fp_add r8, r7, r6   # r8 = -b + √(b^2 - 4ac)
fp_sub r9, r7, r6   # r9 = -b - √(b^2 - 4ac)

fp_mul r10, r8, r1  # r10 = (-b + √(b^2 - 4ac)) / 2a
fp_mul r11, r9, r1  # r11 = (-b - √(b^2 - 4ac)) / 2a

Related Commands

  • fp_mul: High-precision fixed-point multiplication
  • fp_div: High-precision fixed-point division
  • fp_sqrt: High-precision fixed-point square root