Instruction Syntax
Mnemonic | Format | Flags |
fsqrt | frD,frB | Rc = 0 |
fsqrt. | frD,frB | Rc = 1 |
Instruction Encoding
Field | Bits | Description |
Primary Opcode | 0-5 | 111111 (0x3F) |
frD | 6-10 | Destination floating-point register |
Reserved | 11-15 | 00000 |
frB | 16-20 | Source floating-point register B |
Reserved | 21-25 | 00000 |
XO | 26-30 | 010110 (22) |
Rc | 31 | Record Condition Register |
Operation
frD ← √frB
The square root of the contents of floating-point register frB is placed into floating-point register frD. The result is computed in double-precision format according to IEEE-754 standard.
Note: This instruction computes the exact square root with double-precision accuracy. If the contents of frB is negative (other than -0.0), the result is a NaN (Not a Number), and the VXSQRT exception is signaled. For performance-critical applications, consider using the fast reciprocal square root estimate instruction (frsqrte) combined with Newton-Raphson refinement.
Affected Registers
Condition Register (CR1 field)
(if Rc = 1)
- Reflects floating-point exception summary and status
Floating-Point Status and Control Register (FPSCR)
Affected fields:
- FPRF (Floating-Point Result Flags)
- FX (Floating-Point Exception Summary)
- XX (Inexact Exception) - if result cannot be represented exactly
- VXSNAN (Invalid Operation Exception for SNaN)
- VXSQRT (Invalid Operation Exception for square root of negative number)
- FR (Fraction Rounded)
- FI (Fraction Inexact)
For more information on floating-point status see Section 2.1.4, "Floating-Point Status and Control Register (FPSCR)," in the PowerPC Microprocessor Family: The Programming Environments manual.
Examples
Basic Square Root Calculation
lfd f1, input_value(r0) # Load double-precision input fsqrt f2, f1 # f2 = √f1 stfd f2, sqrt_result(r0) # Store square root result
Distance Calculation: Euclidean Distance
# Calculate distance: d = √((x₁-x₂)² + (y₁-y₂)²) lfd f1, point1_x(r0) # Load point 1 X coordinate lfd f2, point1_y(r0) # Load point 1 Y coordinate lfd f3, point2_x(r0) # Load point 2 X coordinate lfd f4, point2_y(r0) # Load point 2 Y coordinate fsub f5, f1, f3 # f5 = x₁ - x₂ fsub f6, f2, f4 # f6 = y₁ - y₂ fmul f7, f5, f5 # f7 = (x₁ - x₂)² fmadd f8, f6, f6, f7 # f8 = (x₁ - x₂)² + (y₁ - y₂)² fsqrt f9, f8 # f9 = √((x₁ - x₂)² + (y₁ - y₂)²) = distance stfd f9, distance(r0) # Store calculated distance
3D Graphics: Vector Magnitude
# Calculate 3D vector magnitude: |v| = √(x² + y² + z²) lfd f1, vector_x(r0) # Load vector X component lfd f2, vector_y(r0) # Load vector Y component lfd f3, vector_z(r0) # Load vector Z component fmul f4, f1, f1 # f4 = x² fmadd f4, f2, f2, f4 # f4 = x² + y² fmadd f4, f3, f3, f4 # f4 = x² + y² + z² = magnitude² fsqrt f5, f4 # f5 = √(x² + y² + z²) = |v| stfd f5, vector_magnitude(r0) # Store vector magnitude
Scientific Computing: Standard Deviation
# Calculate standard deviation: σ = √(variance) lfd f1, variance(r0) # Load variance value fsqrt f2, f1 # f2 = √variance = standard deviation stfd f2, std_deviation(r0) # Store standard deviation
Physics: RMS (Root Mean Square) Calculation
# Calculate RMS value: RMS = √(mean of squares) lfd f1, sum_of_squares(r0) # Load sum of squared values lfd f2, sample_count(r0) # Load number of samples fdiv f3, f1, f2 # f3 = sum/count = mean of squares fsqrt f4, f3 # f4 = √(mean of squares) = RMS stfd f4, rms_value(r0) # Store RMS value
Audio Processing: Quadratic Mean
# Calculate quadratic mean for audio analysis lfd f1, sum_squared_samples(r0) # Load sum of squared audio samples lfd f2, total_samples(r0) # Load total number of samples fdiv f3, f1, f2 # f3 = mean of squared samples fsqrt f4, f3 # f4 = √(mean of squared samples) = quadratic mean stfd f4, quad_mean(r0) # Store quadratic mean
Machine Learning: Euclidean Norm
# Calculate L2 norm (Euclidean norm) of a 2D vector lfd f1, feature_1(r0) # Load first feature lfd f2, feature_2(r0) # Load second feature fmul f3, f1, f1 # f3 = feature_1² fmadd f4, f2, f2, f3 # f4 = feature_1² + feature_2² fsqrt f5, f4 # f5 = √(feature_1² + feature_2²) = L2 norm stfd f5, l2_norm(r0) # Store L2 norm
Financial Computing: Volatility Calculation
# Calculate volatility from variance lfd f1, price_variance(r0) # Load price variance fsqrt f2, f1 # f2 = √variance = volatility stfd f2, volatility(r0) # Store volatility (standard deviation)
Game Physics: Collision Detection Distance
# Calculate distance between two objects for collision detection lfd f1, obj1_x(r0) # Load object 1 X position lfd f2, obj1_y(r0) # Load object 1 Y position lfd f3, obj1_z(r0) # Load object 1 Z position lfd f4, obj2_x(r0) # Load object 2 X position lfd f5, obj2_y(r0) # Load object 2 Y position lfd f6, obj2_z(r0) # Load object 2 Z position fsub f7, f1, f4 # f7 = x_diff fsub f8, f2, f5 # f8 = y_diff fsub f9, f3, f6 # f9 = z_diff fmul f10, f7, f7 # f10 = x_diff² fmadd f10, f8, f8, f10 # f10 = x_diff² + y_diff² fmadd f10, f9, f9, f10 # f10 = x_diff² + y_diff² + z_diff² fsqrt f11, f10 # f11 = distance between objects stfd f11, collision_distance(r0) # Store collision distance
Signal Processing: Magnitude Response
# Calculate magnitude of complex frequency response: |H(ω)| = √(real² + imag²) lfd f1, real_part(r0) # Load real part of frequency response lfd f2, imag_part(r0) # Load imaginary part of frequency response fmul f3, f1, f1 # f3 = real² fmadd f4, f2, f2, f3 # f4 = real² + imag² fsqrt f5, f4 # f5 = √(real² + imag²) = magnitude stfd f5, magnitude_response(r0) # Store magnitude response
Engineering: Geometric Mean
# Calculate geometric mean of two values: GM = √(a × b) lfd f1, value_a(r0) # Load first value lfd f2, value_b(r0) # Load second value fmul f3, f1, f2 # f3 = a × b fsqrt f4, f3 # f4 = √(a × b) = geometric mean stfd f4, geometric_mean(r0) # Store geometric mean
Computer Graphics: Lighting Calculations
# Calculate distance for light attenuation: intensity ∝ 1/distance² lfd f1, light_x(r0) # Load light source X position lfd f2, light_y(r0) # Load light source Y position lfd f3, light_z(r0) # Load light source Z position lfd f4, surface_x(r0) # Load surface point X position lfd f5, surface_y(r0) # Load surface point Y position lfd f6, surface_z(r0) # Load surface point Z position fsub f7, f4, f1 # f7 = surface_x - light_x fsub f8, f5, f2 # f8 = surface_y - light_y fsub f9, f6, f3 # f9 = surface_z - light_z fmul f10, f7, f7 # f10 = x_diff² fmadd f10, f8, f8, f10 # f10 = x_diff² + y_diff² fmadd f10, f9, f9, f10 # f10 = distance² fsqrt f11, f10 # f11 = distance to light stfd f11, light_distance(r0) # Store distance for attenuation calculation