[feat] get equation of time
[azan.git] / azan-nasm.s
blob3ef801ad93e48e418a52178de7383f03059cb58c
1 ; See LICENSE file for copyright and license details.
2 ; azan-nasm is simple muslim prayers calculator.
3 ; print next prayer left duration or today's all prayers.
5 BITS 64
6 %include "syscalls.s"
7 %include "macros.s"
8 CHECK_OPENBSD
10 section .rodata
11 sec_inday: dq 86400.0
12 jul1970: dq 2440587.5
13 offset: dq 0xc142b42c80000000 ;double -2451545
14 to_rad: dq 0x3f91df46a2529d39 ;double pi / 180 to radian
15 to_deg: dq 0x404ca5dc1a63c1f8 ;double 180 / pi to degree
16 g_1: dq 0x3fef8a099930e901 ;double 0.98560027999999999
17 g_2: dq 0x40765876c8b43958 ;double 357.529
18 e_3: dq 0xbe9828c0be769dc1 ;double -3.5999999999999999E-7
19 e_4: dq 0x403770624dd2f1aa ;double 23.439
20 q_5: dq 0x3fef8a6c5512d6f2 ;double 0.98564735999999997
21 q_6: dq 0x4071875810624dd3 ;double 280.459
22 sing_1: dq 0x3FFEA3D70A3D70A4 ;double 1.915
23 sing_2: dq 0x3F947AE147AE147B ;double 0.020
24 RA_1: dq 0x402E000000000000 ;double 15.0
25 asin_1: dq 0x3FF0000000000000 ;double 1
26 eqt_1: dq 0x4076800000000000 ;double 360.0
28 section .bss
29 tstamp: resb 12
30 julian: resq 1
31 EqT: resq 1
32 g: resq 1
33 g2: resq 1
34 sing: resq 1
35 sin2g: resq 1
36 sine: resq 1
37 cose: resq 1
38 sinL: resq 1
39 cosL: resq 1
40 RA_2: resq 1
41 asin_x: resq 1
43 section .text
44 global _start
46 _start:
47 ; get_timestamp:
48 mov rax, SYS_gettimeofday ;sys_gettimeofday(
49 mov rdi, tstamp ;struct timeval *tv,
50 mov rsi, rsi ;struct timezone* tz
51 syscall
53 ; calc_julian:(timestamp / sec_in_day) + jul1970
54 cvtsi2sd xmm0, [tstamp] ;convert timestamp to double
55 divsd xmm0, [sec_inday] ;timestamp / sec_in_day
56 addsd xmm0, [jul1970] ;div result + jul1970
57 ; xmm0 = julian
59 ; calc_equation_of_time:
60 addsd xmm0, [offset] ;d = julian - offset
61 ; xmm0 = d
63 movsd xmm2, [to_rad]
64 movsd xmm1, [g_1] ;g = to_rad * ((d * 0.98560028) + 357.529)
65 mulsd xmm1, xmm0
66 addsd xmm1, [g_2]
67 mulsd xmm1, xmm2
68 ; xmm1 = g
69 ; xmm2 = to_rad
71 movsd xmm3, [e_3] ;e = to_rad * (23.439 - (d * 0.00000036))
72 mulsd xmm3, xmm0
73 addsd xmm3, [e_4]
74 mulsd xmm3, xmm2
75 ; xmm3 = e
77 movsd xmm4, [q_5] ;q = (d * 0.98564736) + 280.459
78 mulsd xmm4, xmm0
79 addsd xmm4, [q_6]
80 ; xmm4 = q
82 ; sing: ;sing = 1.915 * sin(g);
83 movsd xmm5, [sing_1]
84 finit
85 movsd [g], xmm1
86 fld qword [g]
87 fsin
88 fstp qword [sing]
89 mulsd xmm5, [sing]
90 ; xmm5 = sing
92 ; sin2g: ;sin2g = 0.020 * sin(2.0*g)
93 movsd xmm6, [sing_2]
94 addsd xmm1, xmm1
95 movsd [g2], xmm1
96 fld qword [g2]
97 fsin
98 fstp qword [sin2g]
99 mulsd xmm6, [sin2g]
100 ; xmm1 = 2 * g
101 ; xmm6 = sin2g
103 ; sine: ;sin(e)
104 movsd [sine], xmm3
105 fld qword [sine]
106 fsin
107 fstp qword [sine]
109 ; cose: ;cos(e)
110 movsd [cose], xmm3
111 fld qword [cose]
112 fcos
113 fstp qword [cose]
115 ; L: ;L = to_rad(q + sing + sin2g);
116 addsd xmm5, xmm6 ;sing + sin2g
117 addsd xmm5, xmm4 ;+ q
118 mulsd xmm5, xmm2 ;* to_rad
119 ; xmm5 = L
121 ; sinL: ;sin(L)
122 movsd [sinL], xmm5
123 fld qword [sinL]
124 fsin
125 fstp qword [sinL]
127 ; cosL: ;cos(L)
128 movsd [cosL], xmm5
129 fld qword [cosL]
130 fcos
131 fstp qword [cosL]
133 ; RA: ;RA = to_deg(atan2(cose * sinL, cosL) / 15.0);
134 movsd xmm7, [cose]
135 mulsd xmm7, [sinL]
136 movsd [cose], xmm7
137 fld qword [cose] ;load cose
138 fld qword [cosL] ;load cosL
139 fpatan ;calc atan2 (cose / cosL)
140 fstp qword [RA_2] ;save angle
141 movsd xmm7, [RA_2]
142 divsd xmm7, [RA_1] ;atan2 result /15.0
143 mulsd xmm7, [to_deg] ;* to_deg
144 ; xmm7 = RA
146 ; D: ;D = to_deg(asin(sine * sinL));
147 ; asin = arctan(x / sqrt(1 - x * x))
148 movsd xmm8, [sine]
149 mulsd xmm8, [sinL]
150 movsd [asin_x], xmm8
151 ; xmm8 = x
153 movsd xmm9, xmm8 ; xmm8 = x
154 mulsd xmm9, xmm8 ; xmm9 = x * x
155 movsd xmm10, [asin_1] ; xmm10 = 1
156 subsd xmm10, xmm9 ; 1 - xmm9
157 movsd [asin_1], xmm10 ; asin_1 = (1-x*x)
158 movsd [asin_x], xmm8 ; asin_1 = (1-x*x)
159 ; xmm9 = x * x
160 ; xmm10 = 1 - xmm9
162 fld qword [asin_x]
163 fld qword [asin_1]
164 fsqrt
165 fpatan
166 fstp qword [asin_1]
167 movsd xmm8, [asin_1]
168 mulsd xmm8, [to_deg]
169 ; xmm8 = D
171 ; Eqt ;EqT = q / 15.0 - RA;
172 movsd xmm9, xmm4 ; move q to xmm9
173 divsd xmm9, [RA_1] ; q / 15.0
174 subsd xmm9, xmm7 ; - RA
175 ; xmm9 = EqT
176 ; EqT = EqT - 360.0
177 subsd xmm9, [eqt_1]
179 EEXIT EXIT_SUCCESS