-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
4947 lines (4415 loc) · 127 KB
/
time.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
time.c -
$Author$
created at: Tue Dec 28 14:31:59 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "ruby/ruby.h"
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include "ruby/encoding.h"
#include "internal.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <float.h>
#include <math.h>
#include "timev.h"
static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den, id_offset;
static ID id_eq, id_ne, id_quo, id_div, id_cmp, id_lshift;
#define NDIV(x,y) (-(-((x)+1)/(y))-1)
#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1)
#define DIV(n,d) ((n)<0 ? NDIV((n),(d)) : (n)/(d))
#define MOD(n,d) ((n)<0 ? NMOD((n),(d)) : (n)%(d))
static int
eq(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
return x == y;
}
return RTEST(rb_funcall(x, id_eq, 1, y));
}
static int
cmp(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
if ((long)x < (long)y)
return -1;
if ((long)x > (long)y)
return 1;
return 0;
}
return rb_cmpint(rb_funcall(x, id_cmp, 1, y), x, y);
}
#define ne(x,y) (!eq((x),(y)))
#define lt(x,y) (cmp((x),(y)) < 0)
#define gt(x,y) (cmp((x),(y)) > 0)
#define le(x,y) (cmp((x),(y)) <= 0)
#define ge(x,y) (cmp((x),(y)) >= 0)
static VALUE
add(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
long l = FIX2LONG(x) + FIX2LONG(y);
if (FIXABLE(l)) return LONG2FIX(l);
return LONG2NUM(l);
}
if (TYPE(x) == T_BIGNUM) return rb_big_plus(x, y);
return rb_funcall(x, '+', 1, y);
}
static VALUE
sub(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
long l = FIX2LONG(x) - FIX2LONG(y);
if (FIXABLE(l)) return LONG2FIX(l);
return LONG2NUM(l);
}
if (TYPE(x) == T_BIGNUM) return rb_big_minus(x, y);
return rb_funcall(x, '-', 1, y);
}
#if !(HAVE_LONG_LONG && SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG)
static int
long_mul(long x, long y, long *z)
{
unsigned long a, b, c;
int s;
if (x == 0 || y == 0) {
*z = 0;
return 1;
}
if (x < 0) {
s = -1;
a = (unsigned long)-x;
}
else {
s = 1;
a = (unsigned long)x;
}
if (y < 0) {
s = -s;
b = (unsigned long)-y;
}
else {
b = (unsigned long)y;
}
if (a <= ULONG_MAX / b) {
c = a * b;
if (s < 0) {
if (c <= (unsigned long)LONG_MAX + 1) {
*z = -(long)c;
return 1;
}
}
else {
if (c <= (unsigned long)LONG_MAX) {
*z = (long)c;
return 1;
}
}
}
return 0;
}
#endif
static VALUE
mul(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y)) {
#if HAVE_LONG_LONG && SIZEOF_LONG * 2 <= SIZEOF_LONG_LONG
LONG_LONG ll = (LONG_LONG)FIX2LONG(x) * FIX2LONG(y);
if (FIXABLE(ll))
return LONG2FIX(ll);
return LL2NUM(ll);
#else
long z;
if (long_mul(FIX2LONG(x), FIX2LONG(y), &z))
return LONG2NUM(z);
#endif
}
if (TYPE(x) == T_BIGNUM)
return rb_big_mul(x, y);
return rb_funcall(x, '*', 1, y);
}
#define div(x,y) (rb_funcall((x), id_div, 1, (y)))
static VALUE
mod(VALUE x, VALUE y)
{
switch (TYPE(x)) {
case T_BIGNUM: return rb_big_modulo(x, y);
default: return rb_funcall(x, '%', 1, y);
}
}
#define neg(x) (sub(INT2FIX(0), (x)))
#define lshift(x,y) (rb_funcall((x), id_lshift, 1, (y)))
static VALUE
quo(VALUE x, VALUE y)
{
VALUE ret;
if (FIXNUM_P(x) && FIXNUM_P(y)) {
long a, b, c;
a = FIX2LONG(x);
b = FIX2LONG(y);
if (b == 0) rb_num_zerodiv();
c = a / b;
if (c * b == a) {
return LONG2NUM(c);
}
}
ret = rb_funcall(x, id_quo, 1, y);
if (TYPE(ret) == T_RATIONAL &&
RRATIONAL(ret)->den == INT2FIX(1)) {
ret = RRATIONAL(ret)->num;
}
return ret;
}
#define mulquo(x,y,z) (((y) == (z)) ? (x) : quo(mul((x),(y)),(z)))
static void
divmodv(VALUE n, VALUE d, VALUE *q, VALUE *r)
{
VALUE tmp, ary;
tmp = rb_funcall(n, id_divmod, 1, d);
ary = rb_check_array_type(tmp);
if (NIL_P(ary)) {
rb_raise(rb_eTypeError, "unexpected divmod result: into %s",
rb_obj_classname(tmp));
}
*q = rb_ary_entry(ary, 0);
*r = rb_ary_entry(ary, 1);
}
#if SIZEOF_LONG == 8
# define INT64toNUM(x) LONG2NUM(x)
# define UINT64toNUM(x) ULONG2NUM(x)
#elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
# define INT64toNUM(x) LL2NUM(x)
# define UINT64toNUM(x) ULL2NUM(x)
#endif
#if defined(HAVE_UINT64_T) && SIZEOF_LONG*2 <= SIZEOF_UINT64_T
typedef uint64_t uwideint_t;
typedef int64_t wideint_t;
typedef uint64_t WIDEVALUE;
typedef int64_t SIGNED_WIDEVALUE;
# define WIDEVALUE_IS_WIDER 1
# define UWIDEINT_MAX UINT64_MAX
# define WIDEINT_MAX INT64_MAX
# define WIDEINT_MIN INT64_MIN
# define FIXWINT_P(tv) ((tv) & 1)
# define FIXWVtoINT64(tv) RSHIFT((SIGNED_WIDEVALUE)(tv), 1)
# define INT64toFIXWV(wi) ((WIDEVALUE)((SIGNED_WIDEVALUE)(wi) << 1 | FIXNUM_FLAG))
# define FIXWV_MAX (((int64_t)1 << 62) - 1)
# define FIXWV_MIN (-((int64_t)1 << 62))
# define FIXWVABLE(wi) (POSFIXWVABLE(wi) && NEGFIXWVABLE(wi))
# define WINT2FIXWV(i) WIDEVAL_WRAP(INT64toFIXWV(i))
# define FIXWV2WINT(w) FIXWVtoINT64(WIDEVAL_GET(w))
#else
typedef unsigned long uwideint_t;
typedef long wideint_t;
typedef VALUE WIDEVALUE;
typedef SIGNED_VALUE SIGNED_WIDEVALUE;
# define WIDEVALUE_IS_WIDER 0
# define UWIDEINT_MAX ULONG_MAX
# define WIDEINT_MAX LONG_MAX
# define WIDEINT_MIN LONG_MIN
# define FIXWINT_P(v) FIXNUM_P(v)
# define FIXWV_MAX FIXNUM_MAX
# define FIXWV_MIN FIXNUM_MIN
# define FIXWVABLE(i) FIXABLE(i)
# define WINT2FIXWV(i) WIDEVAL_WRAP(LONG2FIX(i))
# define FIXWV2WINT(w) FIX2LONG(WIDEVAL_GET(w))
#endif
#define POSFIXWVABLE(wi) ((wi) < FIXWV_MAX+1)
#define NEGFIXWVABLE(wi) ((wi) >= FIXWV_MIN)
#define FIXWV_P(w) FIXWINT_P(WIDEVAL_GET(w))
/* #define STRUCT_WIDEVAL */
#ifdef STRUCT_WIDEVAL
/* for type checking */
typedef struct {
WIDEVALUE value;
} wideval_t;
static inline wideval_t WIDEVAL_WRAP(WIDEVALUE v) { wideval_t w = { v }; return w; }
# define WIDEVAL_GET(w) ((w).value)
#else
typedef WIDEVALUE wideval_t;
# define WIDEVAL_WRAP(v) (v)
# define WIDEVAL_GET(w) (w)
#endif
#if WIDEVALUE_IS_WIDER
static inline wideval_t
wint2wv(wideint_t wi)
{
if (FIXWVABLE(wi))
return WINT2FIXWV(wi);
else
return WIDEVAL_WRAP(INT64toNUM(wi));
}
# define WINT2WV(wi) wint2wv(wi)
#else
# define WINT2WV(wi) WIDEVAL_WRAP(LONG2NUM(wi))
#endif
static inline VALUE
w2v(wideval_t w)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w))
return INT64toNUM(FIXWV2WINT(w));
return (VALUE)WIDEVAL_GET(w);
#else
return WIDEVAL_GET(w);
#endif
}
#if WIDEVALUE_IS_WIDER
static int
bdigit_find_maxbit(BDIGIT d)
{
int res = 0;
if (d & ~(BDIGIT)0xffff) {
d >>= 16;
res += 16;
}
if (d & ~(BDIGIT)0xff) {
d >>= 8;
res += 8;
}
if (d & ~(BDIGIT)0xf) {
d >>= 4;
res += 4;
}
if (d & ~(BDIGIT)0x3) {
d >>= 2;
res += 2;
}
if (d & ~(BDIGIT)0x1) {
d >>= 1;
res += 1;
}
return res;
}
static VALUE
rb_big_abs_find_maxbit(VALUE big)
{
BDIGIT *ds = RBIGNUM_DIGITS(big);
BDIGIT d;
long len = RBIGNUM_LEN(big);
VALUE res;
while (0 < len && ds[len-1] == 0)
len--;
if (len == 0)
return Qnil;
res = mul(LONG2NUM(len-1), INT2FIX(SIZEOF_BDIGITS * CHAR_BIT));
d = ds[len-1];
res = add(res, LONG2FIX(bdigit_find_maxbit(d)));
return res;
}
static VALUE
rb_big_abs_find_minbit(VALUE big)
{
BDIGIT *ds = RBIGNUM_DIGITS(big);
BDIGIT d;
long len = RBIGNUM_LEN(big);
long i;
VALUE res;
for (i = 0; i < len; i++)
if (ds[i])
break;
if (i == len)
return Qnil;
res = mul(LONG2NUM(i), INT2FIX(SIZEOF_BDIGITS * CHAR_BIT));
d = ds[i];
res = add(res, LONG2FIX(ffs(d)-1));
return res;
}
static wideval_t
v2w_bignum(VALUE v)
{
long len = RBIGNUM_LEN(v);
BDIGIT *ds;
wideval_t w;
VALUE maxbit;
ds = RBIGNUM_DIGITS(v);
w = WIDEVAL_WRAP(v);
maxbit = rb_big_abs_find_maxbit(v);
if (NIL_P(maxbit))
return WINT2FIXWV(0);
if (lt(maxbit, INT2FIX(sizeof(wideint_t) * CHAR_BIT - 2)) ||
(eq(maxbit, INT2FIX(sizeof(wideint_t) * CHAR_BIT - 2)) &&
RBIGNUM_NEGATIVE_P(v) &&
eq(rb_big_abs_find_minbit(v), INT2FIX(sizeof(wideint_t) * CHAR_BIT - 2)))) {
wideint_t i;
i = 0;
while (len)
i = (i << sizeof(BDIGIT)*CHAR_BIT) | ds[--len];
if (RBIGNUM_NEGATIVE_P(v)) {
i = -i;
}
w = WINT2FIXWV(i);
}
return w;
}
#endif
static inline wideval_t
v2w(VALUE v)
{
#if WIDEVALUE_IS_WIDER
if (FIXNUM_P(v)) {
return WIDEVAL_WRAP((WIDEVALUE)(SIGNED_WIDEVALUE)(long)v);
}
else if (TYPE(v) == T_BIGNUM &&
RBIGNUM_LEN(v) * sizeof(BDIGIT) <= sizeof(WIDEVALUE)) {
return v2w_bignum(v);
}
#endif
return WIDEVAL_WRAP(v);
}
static int
weq(wideval_t wx, wideval_t wy)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
return WIDEVAL_GET(wx) == WIDEVAL_GET(wy);
}
return RTEST(rb_funcall(w2v(wx), id_eq, 1, w2v(wy)));
#else
return eq(WIDEVAL_GET(wx), WIDEVAL_GET(wy));
#endif
}
static int
wcmp(wideval_t wx, wideval_t wy)
{
VALUE x, y;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t a, b;
a = FIXWV2WINT(wx);
b = FIXWV2WINT(wy);
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
#endif
x = w2v(wx);
y = w2v(wy);
return rb_cmpint(rb_funcall(x, id_cmp, 1, y), x, y);
}
#define wne(x,y) (!weq((x),(y)))
#define wlt(x,y) (wcmp((x),(y)) < 0)
#define wgt(x,y) (wcmp((x),(y)) > 0)
#define wle(x,y) (wcmp((x),(y)) <= 0)
#define wge(x,y) (wcmp((x),(y)) >= 0)
static wideval_t
wadd(wideval_t wx, wideval_t wy)
{
VALUE x;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t r = FIXWV2WINT(wx) + FIXWV2WINT(wy);
return WINT2WV(r);
}
else
#endif
x = w2v(wx);
if (TYPE(x) == T_BIGNUM) return v2w(rb_big_plus(x, w2v(wy)));
return v2w(rb_funcall(x, '+', 1, w2v(wy)));
}
static wideval_t
wsub(wideval_t wx, wideval_t wy)
{
VALUE x;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t r = FIXWV2WINT(wx) - FIXWV2WINT(wy);
return WINT2WV(r);
}
else
#endif
x = w2v(wx);
if (TYPE(x) == T_BIGNUM) return v2w(rb_big_minus(x, w2v(wy)));
return v2w(rb_funcall(x, '-', 1, w2v(wy)));
}
static int
wi_mul(wideint_t x, wideint_t y, wideint_t *z)
{
uwideint_t a, b, c;
int s;
if (x == 0 || y == 0) {
*z = 0;
return 1;
}
if (x < 0) {
s = -1;
a = (uwideint_t)-x;
}
else {
s = 1;
a = (uwideint_t)x;
}
if (y < 0) {
s = -s;
b = (uwideint_t)-y;
}
else {
b = (uwideint_t)y;
}
if (a <= UWIDEINT_MAX / b) {
c = a * b;
if (s < 0) {
if (c <= (uwideint_t)WIDEINT_MAX + 1) {
*z = -(wideint_t)c;
return 1;
}
}
else {
if (c <= (uwideint_t)WIDEINT_MAX) {
*z = (wideint_t)c;
return 1;
}
}
}
return 0;
}
static wideval_t
wmul(wideval_t wx, wideval_t wy)
{
VALUE x, z;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t z;
if (wi_mul(FIXWV2WINT(wx), FIXWV2WINT(wy), &z))
return WINT2WV(z);
}
#endif
x = w2v(wx);
if (TYPE(x) == T_BIGNUM) return v2w(rb_big_mul(x, w2v(wy)));
z = rb_funcall(x, '*', 1, w2v(wy));
if (TYPE(z) == T_RATIONAL && RRATIONAL(z)->den == INT2FIX(1)) {
z = RRATIONAL(z)->num;
}
return v2w(z);
}
static wideval_t
wquo(wideval_t wx, wideval_t wy)
{
VALUE x, y, ret;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wx) && FIXWV_P(wy)) {
wideint_t a, b, c;
a = FIXWV2WINT(wx);
b = FIXWV2WINT(wy);
if (b == 0) rb_num_zerodiv();
c = a / b;
if (c * b == a) {
return WINT2WV(c);
}
}
#endif
x = w2v(wx);
y = w2v(wy);
ret = rb_funcall(x, id_quo, 1, y);
if (TYPE(ret) == T_RATIONAL &&
RRATIONAL(ret)->den == INT2FIX(1)) {
ret = RRATIONAL(ret)->num;
}
return v2w(ret);
}
#define wmulquo(x,y,z) ((WIDEVAL_GET(y) == WIDEVAL_GET(z)) ? (x) : wquo(wmul((x),(y)),(z)))
#define wmulquoll(x,y,z) (((y) == (z)) ? (x) : wquo(wmul((x),WINT2WV(y)),WINT2WV(z)))
static void
wdivmod(wideval_t wn, wideval_t wd, wideval_t *wq, wideval_t *wr)
{
VALUE tmp, ary;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(wn) && FIXWV_P(wd)) {
wideint_t n, d, q, r;
d = FIXWV2WINT(wd);
if (d == 0) rb_num_zerodiv();
if (d == 1) {
*wq = wn;
*wr = WINT2FIXWV(0);
return;
}
if (d == -1) {
wideint_t xneg = -FIXWV2WINT(wn);
*wq = WINT2WV(xneg);
*wr = WINT2FIXWV(0);
return;
}
n = FIXWV2WINT(wn);
if (n == 0) {
*wq = WINT2FIXWV(0);
*wr = WINT2FIXWV(0);
return;
}
if (d < 0) {
if (n < 0) {
q = ((-n) / (-d));
r = ((-n) % (-d));
if (r != 0) {
q -= 1;
r += d;
}
}
else { /* 0 < n */
q = -(n / (-d));
r = -(n % (-d));
}
}
else { /* 0 < d */
if (n < 0) {
q = -((-n) / d);
r = -((-n) % d);
if (r != 0) {
q -= 1;
r += d;
}
}
else { /* 0 < n */
q = n / d;
r = n % d;
}
}
*wq = WINT2FIXWV(q);
*wr = WINT2FIXWV(r);
return;
}
#endif
tmp = rb_funcall(w2v(wn), id_divmod, 1, w2v(wd));
ary = rb_check_array_type(tmp);
if (NIL_P(ary)) {
rb_raise(rb_eTypeError, "unexpected divmod result: into %s",
rb_obj_classname(tmp));
}
*wq = v2w(rb_ary_entry(ary, 0));
*wr = v2w(rb_ary_entry(ary, 1));
}
static void
wmuldivmod(wideval_t wx, wideval_t wy, wideval_t wz, wideval_t *wq, wideval_t *wr)
{
if (WIDEVAL_GET(wy) == WIDEVAL_GET(wz)) {
*wq = wx;
*wr = WINT2FIXWV(0);
return;
}
wdivmod(wmul(wx,wy), wz, wq, wr);
}
static wideval_t
wdiv(wideval_t wx, wideval_t wy)
{
wideval_t q, r;
wdivmod(wx, wy, &q, &r);
return q;
}
static wideval_t
wmod(wideval_t wx, wideval_t wy)
{
wideval_t q, r;
wdivmod(wx, wy, &q, &r);
return r;
}
static VALUE
num_exact(VALUE v)
{
VALUE tmp;
int t;
t = TYPE(v);
switch (t) {
case T_FIXNUM:
case T_BIGNUM:
return v;
case T_RATIONAL:
break;
case T_STRING:
case T_NIL:
goto typeerror;
default:
if ((tmp = rb_check_funcall(v, rb_intern("to_r"), 0, NULL)) != Qundef) {
if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
v = tmp;
break;
}
if (!NIL_P(tmp = rb_check_to_integer(v, "to_int"))) {
v = tmp;
break;
}
goto typeerror;
}
t = TYPE(v);
switch (t) {
case T_FIXNUM:
case T_BIGNUM:
return v;
case T_RATIONAL:
if (RRATIONAL(v)->den == INT2FIX(1))
v = RRATIONAL(v)->num;
break;
default:
typeerror:
rb_raise(rb_eTypeError, "can't convert %s into an exact number",
NIL_P(v) ? "nil" : rb_obj_classname(v));
}
return v;
}
/* time_t */
#ifndef TYPEOF_TIMEVAL_TV_SEC
# define TYPEOF_TIMEVAL_TV_SEC time_t
#endif
#ifndef TYPEOF_TIMEVAL_TV_USEC
# if INT_MAX >= 1000000
# define TYPEOF_TIMEVAL_TV_USEC int
# else
# define TYPEOF_TIMEVAL_TV_USEC long
# endif
#endif
#if SIZEOF_TIME_T == SIZEOF_LONG
typedef unsigned long unsigned_time_t;
#elif SIZEOF_TIME_T == SIZEOF_INT
typedef unsigned int unsigned_time_t;
#elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
typedef unsigned LONG_LONG unsigned_time_t;
#else
# error cannot find integer type which size is same as time_t.
#endif
#define TIMET_MAX (~(time_t)0 <= 0 ? (time_t)((~(unsigned_time_t)0) >> 1) : (time_t)(~(unsigned_time_t)0))
#define TIMET_MIN (~(time_t)0 <= 0 ? (time_t)(((unsigned_time_t)1) << (sizeof(time_t) * CHAR_BIT - 1)) : (time_t)0)
static wideval_t
rb_time_magnify(wideval_t w)
{
if (FIXWV_P(w)) {
wideint_t z;
if (wi_mul(FIXWV2WINT(w), TIME_SCALE, &z))
return WINT2WV(z);
}
return wmul(w, WINT2FIXWV(TIME_SCALE));
}
static wideval_t
rb_time_unmagnify(wideval_t w)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w)) {
wideint_t a, b, c;
a = FIXWV2WINT(w);
b = TIME_SCALE;
c = a / b;
if (c * b == a) {
return WINT2FIXWV(c);
}
}
#endif
return wquo(w, WINT2FIXWV(TIME_SCALE));
}
static VALUE
rb_time_unmagnify_to_float(wideval_t w)
{
VALUE v;
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w)) {
wideint_t a, b, c;
a = FIXWV2WINT(w);
b = TIME_SCALE;
c = a / b;
if (c * b == a) {
return DBL2NUM((double)c);
}
v = DBL2NUM((double)FIXWV2WINT(w));
return quo(v, DBL2NUM(TIME_SCALE));
}
#endif
v = w2v(w);
return quo(v, DBL2NUM(TIME_SCALE));
}
static void
split_second(wideval_t timew, wideval_t *timew_p, VALUE *subsecx_p)
{
wideval_t q, r;
wdivmod(timew, WINT2FIXWV(TIME_SCALE), &q, &r);
*timew_p = q;
*subsecx_p = w2v(r);
}
static wideval_t
timet2wv(time_t t)
{
#if WIDEVALUE_IS_WIDER
if (TIMET_MIN == 0) {
uwideint_t wi = (uwideint_t)t;
if (wi <= FIXWV_MAX) {
return WINT2FIXWV(wi);
}
}
else {
wideint_t wi = (wideint_t)t;
if (FIXWV_MIN <= wi && wi <= FIXWV_MAX) {
return WINT2FIXWV(wi);
}
}
#endif
return v2w(TIMET2NUM(t));
}
#define TIMET2WV(t) timet2wv(t)
static time_t
wv2timet(wideval_t w)
{
#if WIDEVALUE_IS_WIDER
if (FIXWV_P(w)) {
wideint_t wi = FIXWV2WINT(w);
if (TIMET_MIN == 0) {
if (wi < 0)
rb_raise(rb_eRangeError, "negative value to convert into `time_t'");
if (TIMET_MAX < (uwideint_t)wi)
rb_raise(rb_eRangeError, "too big to convert into `time_t'");
}
else {
if (wi < TIMET_MIN || TIMET_MAX < wi)
rb_raise(rb_eRangeError, "too big to convert into `time_t'");
}
return (time_t)wi;
}
#endif
return NUM2TIMET(w2v(w));
}
#define WV2TIMET(t) wv2timet(t)
VALUE rb_cTime;
static VALUE time_utc_offset _((VALUE));
static int obj2int(VALUE obj);
static VALUE obj2vint(VALUE obj);
static int month_arg(VALUE arg);
static void validate_utc_offset(VALUE utc_offset);
static void validate_vtm(struct vtm *vtm);
static VALUE time_gmtime(VALUE);
static VALUE time_localtime(VALUE);
static VALUE time_fixoff(VALUE);
static time_t timegm_noleapsecond(struct tm *tm);
static int tmcmp(struct tm *a, struct tm *b);
static int vtmcmp(struct vtm *a, struct vtm *b);
static const char *find_time_t(struct tm *tptr, int utc_p, time_t *tp);
static struct vtm *localtimew(wideval_t timew, struct vtm *result);
static int leap_year_p(long y);
#define leap_year_v_p(y) leap_year_p(NUM2LONG(mod((y), INT2FIX(400))))
#ifdef HAVE_GMTIME_R
#define rb_gmtime_r(t, tm) gmtime_r((t), (tm))
#define rb_localtime_r(t, tm) localtime_r((t), (tm))
#else
static inline struct tm *
rb_gmtime_r(const time_t *tp, struct tm *result)
{
struct tm *t = gmtime(tp);
if (t) *result = *t;
return t;
}
static inline struct tm *
rb_localtime_r(const time_t *tp, struct tm *result)
{
struct tm *t = localtime(tp);
if (t) *result = *t;
return t;
}
#endif
static struct tm *
rb_localtime_r2(const time_t *t, struct tm *result)
{
#if defined __APPLE__ && defined __LP64__
if (*t != (time_t)(int)*t) return NULL;
#endif
result = rb_localtime_r(t, result);
#if defined(HAVE_MKTIME) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result) {
long gmtoff1 = 0;
long gmtoff2 = 0;
struct tm tmp = *result;
time_t t2;
# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
gmtoff1 = result->tm_gmtoff;
# endif
t2 = mktime(&tmp);
# if defined(HAVE_STRUCT_TM_TM_GMTOFF)
gmtoff2 = tmp.tm_gmtoff;
# endif
if (*t + gmtoff1 != t2 + gmtoff2)
result = NULL;
}
#endif
return result;
}
#define LOCALTIME(tm, result) (tzset(),rb_localtime_r2((tm), &(result)))
#if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
static struct tm *
rb_gmtime_r2(const time_t *t, struct tm *result)
{
result = rb_gmtime_r(t, result);
#if defined(HAVE_TIMEGM) && defined(LOCALTIME_OVERFLOW_PROBLEM)
if (result) {
struct tm tmp = *result;
time_t t2 = timegm(&tmp);
if (*t != t2)
result = NULL;
}
#endif
return result;
}
# define GMTIME(tm, result) rb_gmtime_r2((tm), &(result))
#endif
static const int common_year_yday_offset[] = {
-1,
-1 + 31,
-1 + 31 + 28,
-1 + 31 + 28 + 31,
-1 + 31 + 28 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
-1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
/* 1 2 3 4 5 6 7 8 9 10 11 */
};
static const int leap_year_yday_offset[] = {
-1,
-1 + 31,
-1 + 31 + 29,
-1 + 31 + 29 + 31,
-1 + 31 + 29 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
-1 + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
/* 1 2 3 4 5 6 7 8 9 10 11 */
};
static const int common_year_days_in_month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const int leap_year_days_in_month[] = {
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static int
calc_tm_yday(long tm_year, int tm_mon, int tm_mday)
{
int tm_year_mod400 = (int)MOD(tm_year, 400);
int tm_yday = tm_mday;
if (leap_year_p(tm_year_mod400 + 1900))
tm_yday += leap_year_yday_offset[tm_mon];
else
tm_yday += common_year_yday_offset[tm_mon];
return tm_yday;
}
static wideval_t
timegmw_noleapsecond(struct vtm *vtm)
{
VALUE year1900;
VALUE q400, r400;
int year_mod400;
int yday;
long days_in400;
VALUE vdays, ret;
wideval_t wret;
year1900 = sub(vtm->year, INT2FIX(1900));
divmodv(year1900, INT2FIX(400), &q400, &r400);
year_mod400 = NUM2INT(r400);
yday = calc_tm_yday(year_mod400, vtm->mon-1, vtm->mday);
/*
* `Seconds Since the Epoch' in SUSv3:
* tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
* (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
* ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400