Inline Assembler - Example of Optimization and Processor-specific Instructions

Example of Optimization and Processor-specific Instructions

This example of inline assembly is from the D programming language and computes the tangent of x using the x86's FPU instructions. This is faster than using the floating-point operations that would be emitted by the compiler, and it allows the programmer to make use of the fldpi instruction, which loads the closest approximation of pi possible on the x86 architecture.

// Compute the tangent of x real tan(real x) { asm { fld x ; // load x fxam ; // test for oddball values fstsw AX ; sahf ; jc trigerr ; // x is NAN, infinity, or empty // 387's can handle denormals SC18: fptan ; fstp ST(0) ; // dump X, which is always 1 fstsw AX ; sahf ; jnp Lret ; // C2 = 1 (x is out of range) // Do argument reduction to bring x into range fldpi ; fxch ; SC17: fprem1 ; fstsw AX ; sahf ; jp SC17 ; fstp ST(1) ; // remove pi from stack jmp SC18 ; } trigerr: return real.nan; Lret: ; }

Read more about this topic:  Inline Assembler

Famous quotes containing the word instructions:

    Realizing that his time was nearly spent, he gave full oral instructions about his burial and the manner in which he wished to be remembered.... A few minutes later, feeling very tired, he left the room, remarking, ‘I have no disposition to leave this precious circle. I love to be here surrounded by my family and friends.’ Then he gave them his blessing and said, ‘I am ready to go and I wish you goodnight.’
    —For the State of New Hampshire, U.S. public relief program (1935-1943)