FilterFactory - Function "tan"

Back to function listing

From the manual: Bounded tangent function of x, where x is an integer between -256 and 256, inclusive, and the value returned is an integer between -512 and 512, inclusive (Windows) or -1024 and 1024, inclusive (Mac OS)

Mistake in 3.0.4 documentation: This function is NOT implemented bounded! Unlike cos and sin which are bounded, tan will go off-range.
You can test it with this small test filter:

R = cos(x) > 1024 || cos(x) < -1024 || cos(-x) > 1024 || cos(-x) < -1024 ? 255 : 0
G = tan(x) > 1024 || tan(x) < -1024 || tan(-x) > 1024 || tan(-x) < -1024 ? 255 : 0
B = sin(x) > 1024 || sin(x) < -1024 || sin(-x) > 1024 || sin(-x) < -1024 ? 255 : 0

or more to show only one example (works on Windows and Mac OS):
FilterFactory 3.0.4: tan(-768) == 167772 ? 255 : 0

Click here for a diagram of sin, cos, tan

Syntax

tan(x)

Original Machine Code (in OPER resource) of Filter Factory 3.0 and 3.0.4 for Photoshop/Win32

58 pop eax (param_x)
8B D8 mov ebx,eax
0B DB or ebx,ebx
7D 02 jnl +$02 (@@1)
F7 DB neg ebx
@@1:
81 E3 FF 03 00 00 and ebx,$000003ff (1023)
0F BF 9C 5F 78 05 00 00 movsx ebx,[edi+ebx*2+$00000578] (COS_LOOKUP)
2D 00 01 00 00 sub eax,$00000100 (256)
79 02 jns +$02 (@@2)
F7 D8 neg eax
@@2:
25 FF 03 00 00 and eax,$000003ff (1023)
0F BF 84 47 78 05 00 00 movsx eax,[edi+eax*2+$00000578] (COS_LOOKUP)
0B DB or ebx,ebx
75 04 jnz +$04 (@@3)
33 C0 xor eax,eax
EB 06 jmp +$06 (@@4)
@@3:
C1 E0 0A shl eax,$0a (10)
99 cdq
F7 FB idiv ebx
@@4:
50 push eax

C++ code

int factory_tan(int parm) {
	int v1 = parm;
	int v2 = v1 < 0 ? -v1 : v1;
	v2 &= 0x3ff; // 1023
	v2 = COS_LOOKUP[v2];
	v1 -= 256;
	if (v1 < 0) v1 = -v1;
	v1 &= 0x3ff; // 1023
	v1 = COS_LOOKUP[v1];
	if (v2 == 0) return 0;
	v1 = v1 << 10; // v1 *= 1024;
	return v1 / v2;
}