summaryrefslogtreecommitdiff
path: root/sincos.asm
blob: 5616c2f4e56753235ea207ab311acb8b13d3e474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
format ELF64

section '.text' executable

public main
extrn puts
extrn printf

main:
	finit

	push	rbp
	mov	rbp,rsp
	
	call	sin

	movlpd	xmm1,[y]
	movlpd	xmm0,[x]		; move floating point number into xmm0 for passage to printf
	mov	edi,sinmsg		; move formatted string to rdi (edi) (first argument) to send to printf
	mov	eax,2			; number of floating point arguments
	call	printf			; call printf
	
	call	cos
	
	movlpd	xmm1,[y]
	movlpd	xmm0,[x]
	mov	edi,cosmsg
	mov	eax,2
	call	printf
	
	xor	eax,eax			; return 0 code
	leave
	ret

sin:
	fld	[x]	; x
	fmul	st0,st0	; x^2
	fld	[rf13]	; 1/13! x^2
	fmul	st0,st1	; x^2/13! x^2
	fsub	[rf11]	; (-1/11! + x^2/13!) x^2
	fmul	st0,st1	; (-x^2/11! + x^4/13!) x^2
	fadd	[rf9]	; 1/9! x^2
	fmul	st0,st1	; x^2/9! x^2
	fsub	[rf7]	; (-1/7! + x^2/9!) x^2
	fmul	st0,st1	; (-x^2/7! + x^4/9!) x^2
	fadd	[rf5]	; (1/5! - x^2/7! + x^4/9!) x^2
	fmul	st0,st1	; (x^2/5! - x^4/7! + x^6/9!) x^2
	fsub	[rf3]	; (-1/3! + x^2/5! - x^4/7! + x^6/9!) x^2
	fmulp	st1,st0	; (-x^2/3! + x^4/5! - x^6/7! + x^8/9!) x^2
	fmul	[x]	; (-x^3/3! + x^5/5! - x^7/7! + x^9/9!)
	fadd	[x]	; (x - x^3/3! + x^5/5! - x^7/7! + x^9/9!)
	fstp	[y]	;
	ret

cos:
	fld	[x]	; x
	fmul	st0,st0	; x^2
	fld	[rf12]	; 1/12! x^2
	fmul	st0,st1	; x^2/12! x^2
	fsub	[rf10]	; (-1/10! + x^2/12!) x^2
	fmul	st0,st1 ; (-x^2/10! + x^4/12!) x^2
	fadd	[rf8]	; (1/8!) x^2
	fmul	st0,st1	; (x^2/8!) x^2
	fsub	[rf6]	; (-1/6! + x^2/8!) x^2
	fmul	st0,st1	; (-x^2/6! + x^4/8!) x^2
	fadd	[rf4]	; (1/4! - x^2/6! + x^4/8!) x^2
	fmul	st0,st1	; (x^2/4! - x^4/6! + x^6/8!) x^2
	fsub	[rf2]	; (-1/2 + x^2/4! - x^4/6! + x^6/8!) x^2
	fmulp	st1,st0	; (-x^2/2 + x^4/4! - x^6/6! + x^8/8!)
	fadd	[one]	; (1 - x^2/2 + x^4/4! - x^6/6! + x^8/8!)
	fstp	[y]	; 
	ret
	
section '.data' writable align 16

; reciprocals of factorials
rf13	dq	1.6059043836821614599392377170154e-10
rf12	dq	2.0876756987868098979210090321201e-9
rf11	dq	2.5052108385441718774420151415934e-8
rf10	dq	2.7557319223985890651862166557528e-7
rf9	dq	2.7557319223985890651862166557528e-6
rf8	dq	2.4801587301587301587301587301587e-5
rf7	dq	0.0001984126984126984126984126984127
rf6	dq	0.0013888888888888888888888888888889
rf5	dq	0.0083333333333333333333333333333333
rf4	dq	0.0416666666666666666666666666666667
rf3	dq	0.1666666666666666666666666666666667
rf2	dq	0.5

one	dq	1.0

x	dq	3.14159265358979323846264338327950288419716939937510
y	dq	?

sinmsg	db "The sine of %.32f is %.32f",0xA,0
cosmsg	db "The cosine of %.32f is %.32f",0xA,0