Skip to content
Newer
Older
100644 222 lines (157 sloc) 4.22 KB
4dcecaa @dharmatech README and more md
authored
1
2 # Calculate the Julian Day
3
4 # C
5
6 ```c
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11
12 int main ( int argc , char * argv[] )
13 {
14 double y ;
15 double m ;
16 double d ;
17 double a ;
18 double b ;
19 double c ;
20 double e ;
21 double res ;
22
23 y = atof( argv[1] ) ;
24 m = atof( argv[2] ) ;
25 d = atof( argv[3] ) ;
26
27 if ( m == 1.0 || m == 2.0 )
28 {
29 y = y - 1.0 ;
30 m = m + 12.0 ;
31 }
32
33 if ( ( y*10000 + m*100 + d ) >= ( 1582*10000 + 10*100 + 15 ) )
34 {
35 a = trunc( y / 100.0 ) ;
36
37 b = 2 - a + trunc( a / 4.0 ) ;
38 } else {
39 b = 0 ;
40 }
41
42 if ( y < 0 )
43 c = trunc( 365.25 * y - 0.75 ) ;
44 else
45 c = trunc( 365.25 * y ) ;
46
47 e = trunc( 30.6001 * (m+1) ) ;
48
49 res = b + c + e + d + 1720994.5 ;
50
51 printf( "%f\n" , res ) ;
52
53 return 0 ;
54 }
55 ```
56
57 # Assembly
58
59 ```s
60
61 .include "utils.s"
62
63 global_text main
64
65 prepare_stack
66
67 .set offset , 0
68
69 stack_allocate ARGV , 8
70 stack_allocate Y , 8
71 stack_allocate M , 8
72 stack_allocate D , 8
73 stack_allocate A , 8
74 stack_allocate B , 8
75 stack_allocate C , 8
76 stack_allocate E , 8
77 stack_allocate RES , 16
78
79 mov %rsi , ARGV(%rbp)
80
81 ref8 ARGV , 1 , %rdi ; call atof ; movsd %xmm0 , Y(%rbp)
82 ref8 ARGV , 2 , %rdi ; call atof ; movsd %xmm0 , M(%rbp)
83 ref8 ARGV , 3 , %rdi ; call atof ; movsd %xmm0 , D(%rbp)
84
85 literal_double 0e1.0 , %xmm0
86 ucomisd M(%rbp) , %xmm0
87 je 2f
88 jmp 1f
89
90 1:
91 literal_double 0e2.0 , %xmm0
92 ucomisd M(%rbp) , %xmm0
93 je 2f
94 jmp 3f
95
96 2:
97 movsd Y(%rbp) , %xmm0
98 literal_double 0e1.0 , %xmm1
99 subsd %xmm1 , %xmm0
100 movsd %xmm0 , Y(%rbp)
101
102 movsd M(%rbp) , %xmm0
103 literal_double 0e12.0 , %xmm1
104 addsd %xmm1 , %xmm0
105 movsd %xmm0 , M(%rbp)
106
107 3:
108 # ----------------------------------------------------------------------
109
110 literal_double 0e0.0 , %xmm0
111
112 movsd Y(%rbp) , %xmm1 ; literal_double 0e10000.0 , %xmm2 ; mulsd %xmm2 , %xmm1
113 addsd %xmm1 , %xmm0
114
115 movsd M(%rbp) , %xmm1 ; literal_double 0e100.0 , %xmm2 ; mulsd %xmm2 , %xmm1
116 addsd %xmm1 , %xmm0
117
118 movsd D(%rbp) , %xmm1
119 addsd %xmm1 , %xmm0
120
121 literal_double 0e15821015.0 %xmm1
122
123 ucomisd %xmm1 , %xmm0
124
125 jae 1f
126 jmp 2f
127
128 1:
129 movsd Y(%rbp) , %xmm0
130 literal_double 0e100.0 , %xmm1
131 divsd %xmm1 , %xmm0
132 call trunc
133 movsd %xmm0 , A(%rbp)
134
135 literal_double 0e2.0 , %xmm3
136
137 movsd A(%rbp) , %xmm1
138 literal_double 0e-1.0 , %xmm2
139 mulsd %xmm2 , %xmm1
140
141 addsd %xmm1 , %xmm3
142
143 movsd A(%rbp) , %xmm0
144 literal_double 0e4.0 , %xmm2
145 divsd %xmm2 , %xmm0
146 call trunc
147 addsd %xmm0 , %xmm3
148 movsd %xmm3 , B(%rbp)
149
150 jmp 10f
151
152 2:
153 literal_double 0e0.0 , %xmm0
154 movsd %xmm0 , B(%rbp)
155
156 jmp 10f
157
158 10:
159 literal_double 0e0.0 , %xmm0
160 ucomisd Y(%rbp) , %xmm0
161 ja 1f
162 jmp 2f
163
164 1:
165 literal_double 0e365.25 , %xmm0
166 movsd Y(%rbp) , %xmm1
167 mulsd %xmm1 , %xmm0
168
169 literal_double 0e0.75 , %xmm1
170 subsd %xmm1 , %xmm0
171
172 call trunc
173
174 movsd %xmm0 , C(%rbp)
175
176 jmp 10f
177
178 2:
179 literal_double 0e365.25 , %xmm0
180 movsd Y(%rbp) , %xmm1
181 mulsd %xmm1 , %xmm0
182
183 call trunc
184
185 movsd %xmm0 , C(%rbp)
186
187 jmp 10f
188
189 10:
190 movsd M(%rbp) , %xmm0
191 literal_double 0e1.0 , %xmm1
192 addsd %xmm1 , %xmm0
193 literal_double 30.6001 , %xmm1
194 mulsd %xmm1 , %xmm0
195 call trunc
196 movsd %xmm0 , E(%rbp)
197
198
199 movsd B(%rbp) , %xmm0
200
201 movsd C(%rbp) , %xmm1 ; addsd %xmm1 , %xmm0
202 movsd E(%rbp) , %xmm1 ; addsd %xmm1 , %xmm0
203 movsd D(%rbp) , %xmm1 ; addsd %xmm1 , %xmm0
204
205 literal_double 0e1720994.5 %xmm1 ; addsd %xmm1 , %xmm0
206
207 movsd %xmm0 , RES(%rbp)
208
209 print_double RES(%rbp)
210 print_nl
211
212 mov $0 , %rax
213
214 leave
215 ret
a5b0fff @dharmatech minor tweaks
authored
216 ```
4dcecaa @dharmatech README and more md
authored
217
218 # Assemble and run
219
220 $ gcc julian-date.s -lm && ./a.out 2011 7 23
221 2455765.500000
Something went wrong with that request. Please try again.