summaryrefslogtreecommitdiffstats
path: root/Lib/test/decimaltestdata/ddMax.decTest
blob: 515f79a5b038d943f018b7c17641d45f6b898efc (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
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
------------------------------------------------------------------------
-- ddMax.decTest -- decDouble maxnum                                  --
-- Copyright (c) IBM Corporation, 1981, 2007.  All rights reserved.   --
------------------------------------------------------------------------
-- Please see the document "General Decimal Arithmetic Testcases"     --
-- at http://www2.hursley.ibm.com/decimal for the description of      --
-- these testcases.                                                   --
--                                                                    --
-- These testcases are experimental ('beta' versions), and they       --
-- may contain errors.  They are offered on an as-is basis.  In       --
-- particular, achieving the same results as the tests here is not    --
-- a guarantee that an implementation complies with any Standard      --
-- or specification.  The tests are not exhaustive.                   --
--                                                                    --
-- Please send comments, suggestions, and corrections to the author:  --
--   Mike Cowlishaw, IBM Fellow                                       --
--   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         --
--   mfc@uk.ibm.com                                                   --
------------------------------------------------------------------------
version: 2.57

-- we assume that base comparison is tested in compare.decTest, so
-- these mainly cover special cases and rounding
precision:   16
maxExponent: 384
minExponent: -383
extended:    1
clamp:       1
rounding:    half_even

-- sanity checks
ddmax001 max  -2  -2  -> -2
ddmax002 max  -2  -1  -> -1
ddmax003 max  -2   0  ->  0
ddmax004 max  -2   1  ->  1
ddmax005 max  -2   2  ->  2
ddmax006 max  -1  -2  -> -1
ddmax007 max  -1  -1  -> -1
ddmax008 max  -1   0  ->  0
ddmax009 max  -1   1  ->  1
ddmax010 max  -1   2  ->  2
ddmax011 max   0  -2  ->  0
ddmax012 max   0  -1  ->  0
ddmax013 max   0   0  ->  0
ddmax014 max   0   1  ->  1
ddmax015 max   0   2  ->  2
ddmax016 max   1  -2  ->  1
ddmax017 max   1  -1  ->  1
ddmax018 max   1   0  ->  1
ddmax019 max   1   1  ->  1
ddmax020 max   1   2  ->  2
ddmax021 max   2  -2  ->  2
ddmax022 max   2  -1  ->  2
ddmax023 max   2   0  ->  2
ddmax025 max   2   1  ->  2
ddmax026 max   2   2  ->  2

-- extended zeros
ddmax030 max   0     0   ->  0
ddmax031 max   0    -0   ->  0
ddmax032 max   0    -0.0 ->  0
ddmax033 max   0     0.0 ->  0
ddmax034 max  -0     0   ->  0    -- note: -0 = 0, but 0 chosen
ddmax035 max  -0    -0   -> -0
ddmax036 max  -0    -0.0 -> -0.0
ddmax037 max  -0     0.0 ->  0.0
ddmax038 max   0.0   0   ->  0
ddmax039 max   0.0  -0   ->  0.0
ddmax040 max   0.0  -0.0 ->  0.0
ddmax041 max   0.0   0.0 ->  0.0
ddmax042 max  -0.0   0   ->  0
ddmax043 max  -0.0  -0   -> -0.0
ddmax044 max  -0.0  -0.0 -> -0.0
ddmax045 max  -0.0   0.0 ->  0.0

ddmax050 max  -0E1   0E1 ->  0E+1
ddmax051 max  -0E2   0E2 ->  0E+2
ddmax052 max  -0E2   0E1 ->  0E+1
ddmax053 max  -0E1   0E2 ->  0E+2
ddmax054 max   0E1  -0E1 ->  0E+1
ddmax055 max   0E2  -0E2 ->  0E+2
ddmax056 max   0E2  -0E1 ->  0E+2
ddmax057 max   0E1  -0E2 ->  0E+1

ddmax058 max   0E1   0E1 ->  0E+1
ddmax059 max   0E2   0E2 ->  0E+2
ddmax060 max   0E2   0E1 ->  0E+2
ddmax061 max   0E1   0E2 ->  0E+2
ddmax062 max  -0E1  -0E1 -> -0E+1
ddmax063 max  -0E2  -0E2 -> -0E+2
ddmax064 max  -0E2  -0E1 -> -0E+1
ddmax065 max  -0E1  -0E2 -> -0E+1

-- Specials
ddmax090 max  Inf  -Inf   ->  Infinity
ddmax091 max  Inf  -1000  ->  Infinity
ddmax092 max  Inf  -1     ->  Infinity
ddmax093 max  Inf  -0     ->  Infinity
ddmax094 max  Inf   0     ->  Infinity
ddmax095 max  Inf   1     ->  Infinity
ddmax096 max  Inf   1000  ->  Infinity
ddmax097 max  Inf   Inf   ->  Infinity
ddmax098 max -1000  Inf   ->  Infinity
ddmax099 max -Inf   Inf   ->  Infinity
ddmax100 max -1     Inf   ->  Infinity
ddmax101 max -0     Inf   ->  Infinity
ddmax102 max  0     Inf   ->  Infinity
ddmax103 max  1     Inf   ->  Infinity
ddmax104 max  1000  Inf   ->  Infinity
ddmax105 max  Inf   Inf   ->  Infinity

ddmax120 max -Inf  -Inf   -> -Infinity
ddmax121 max -Inf  -1000  -> -1000
ddmax122 max -Inf  -1     -> -1
ddmax123 max -Inf  -0     -> -0
ddmax124 max -Inf   0     ->  0
ddmax125 max -Inf   1     ->  1
ddmax126 max -Inf   1000  ->  1000
ddmax127 max -Inf   Inf   ->  Infinity
ddmax128 max -Inf  -Inf   ->  -Infinity
ddmax129 max -1000 -Inf   ->  -1000
ddmax130 max -1    -Inf   ->  -1
ddmax131 max -0    -Inf   ->  -0
ddmax132 max  0    -Inf   ->  0
ddmax133 max  1    -Inf   ->  1
ddmax134 max  1000 -Inf   ->  1000
ddmax135 max  Inf  -Inf   ->  Infinity

-- 2004.08.02 754r chooses number over NaN in mixed cases
ddmax141 max  NaN -Inf    -> -Infinity
ddmax142 max  NaN -1000   -> -1000
ddmax143 max  NaN -1      -> -1
ddmax144 max  NaN -0      -> -0
ddmax145 max  NaN  0      ->  0
ddmax146 max  NaN  1      ->  1
ddmax147 max  NaN  1000   ->  1000
ddmax148 max  NaN  Inf    ->  Infinity
ddmax149 max  NaN  NaN    ->  NaN
ddmax150 max -Inf  NaN    -> -Infinity
ddmax151 max -1000 NaN    -> -1000
ddmax152 max -1    NaN    -> -1
ddmax153 max -0    NaN    -> -0
ddmax154 max  0    NaN    ->  0
ddmax155 max  1    NaN    ->  1
ddmax156 max  1000 NaN    ->  1000
ddmax157 max  Inf  NaN    ->  Infinity

ddmax161 max  sNaN -Inf   ->  NaN  Invalid_operation
ddmax162 max  sNaN -1000  ->  NaN  Invalid_operation
ddmax163 max  sNaN -1     ->  NaN  Invalid_operation
ddmax164 max  sNaN -0     ->  NaN  Invalid_operation
ddmax165 max  sNaN  0     ->  NaN  Invalid_operation
ddmax166 max  sNaN  1     ->  NaN  Invalid_operation
ddmax167 max  sNaN  1000  ->  NaN  Invalid_operation
ddmax168 max  sNaN  NaN   ->  NaN  Invalid_operation
ddmax169 max  sNaN sNaN   ->  NaN  Invalid_operation
ddmax170 max  NaN  sNaN   ->  NaN  Invalid_operation
ddmax171 max -Inf  sNaN   ->  NaN  Invalid_operation
ddmax172 max -1000 sNaN   ->  NaN  Invalid_operation
ddmax173 max -1    sNaN   ->  NaN  Invalid_operation
ddmax174 max -0    sNaN   ->  NaN  Invalid_operation
ddmax175 max  0    sNaN   ->  NaN  Invalid_operation
ddmax176 max  1    sNaN   ->  NaN  Invalid_operation
ddmax177 max  1000 sNaN   ->  NaN  Invalid_operation
ddmax178 max  Inf  sNaN   ->  NaN  Invalid_operation
ddmax179 max  NaN  sNaN   ->  NaN  Invalid_operation

-- propagating NaNs
ddmax181 max  NaN9  -Inf   -> -Infinity
ddmax182 max  NaN8     9   ->  9
ddmax183 max -NaN7   Inf   ->  Infinity

ddmax184 max -NaN1   NaN11 -> -NaN1
ddmax185 max  NaN2   NaN12 ->  NaN2
ddmax186 max -NaN13 -NaN7  -> -NaN13
ddmax187 max  NaN14 -NaN5  ->  NaN14

ddmax188 max -Inf    NaN4  -> -Infinity
ddmax189 max -9     -NaN3  -> -9
ddmax190 max  Inf    NaN2  ->  Infinity

ddmax191 max  sNaN99 -Inf    ->  NaN99 Invalid_operation
ddmax192 max  sNaN98 -1      ->  NaN98 Invalid_operation
ddmax193 max -sNaN97  NaN    -> -NaN97 Invalid_operation
ddmax194 max  sNaN96 sNaN94  ->  NaN96 Invalid_operation
ddmax195 max  NaN95  sNaN93  ->  NaN93 Invalid_operation
ddmax196 max -Inf    sNaN92  ->  NaN92 Invalid_operation
ddmax197 max  0      sNaN91  ->  NaN91 Invalid_operation
ddmax198 max  Inf   -sNaN90  -> -NaN90 Invalid_operation
ddmax199 max  NaN    sNaN89  ->  NaN89 Invalid_operation

-- old rounding checks
ddmax221 max 12345678000 1  -> 12345678000
ddmax222 max 1 12345678000  -> 12345678000
ddmax223 max 1234567800  1  -> 1234567800
ddmax224 max 1 1234567800   -> 1234567800
ddmax225 max 1234567890  1  -> 1234567890
ddmax226 max 1 1234567890   -> 1234567890
ddmax227 max 1234567891  1  -> 1234567891
ddmax228 max 1 1234567891   -> 1234567891
ddmax229 max 12345678901 1  -> 12345678901
ddmax230 max 1 12345678901  -> 12345678901
ddmax231 max 1234567896  1  -> 1234567896
ddmax232 max 1 1234567896   -> 1234567896
ddmax233 max -1234567891  1 -> 1
ddmax234 max 1 -1234567891  -> 1
ddmax235 max -12345678901 1 -> 1
ddmax236 max 1 -12345678901 -> 1
ddmax237 max -1234567896  1 -> 1
ddmax238 max 1 -1234567896  -> 1

-- from examples
ddmax280 max '3'   '2'  ->  '3'
ddmax281 max '-10' '3'  ->  '3'
ddmax282 max '1.0' '1'  ->  '1'
ddmax283 max '1' '1.0'  ->  '1'
ddmax284 max '7' 'NaN'  ->  '7'

-- expanded list from min/max 754r purple prose
-- [explicit tests for exponent ordering]
ddmax401 max  Inf    1.1     ->  Infinity
ddmax402 max  1.1    1       ->  1.1
ddmax403 max  1      1.0     ->  1
ddmax404 max  1.0    0.1     ->  1.0
ddmax405 max  0.1    0.10    ->  0.1
ddmax406 max  0.10   0.100   ->  0.10
ddmax407 max  0.10   0       ->  0.10
ddmax408 max  0      0.0     ->  0
ddmax409 max  0.0   -0       ->  0.0
ddmax410 max  0.0   -0.0     ->  0.0
ddmax411 max  0.00  -0.0     ->  0.00
ddmax412 max  0.0   -0.00    ->  0.0
ddmax413 max  0     -0.0     ->  0
ddmax414 max  0     -0       ->  0
ddmax415 max -0.0   -0       -> -0.0
ddmax416 max -0     -0.100   -> -0
ddmax417 max -0.100 -0.10    -> -0.100
ddmax418 max -0.10  -0.1     -> -0.10
ddmax419 max -0.1   -1.0     -> -0.1
ddmax420 max -1.0   -1       -> -1.0
ddmax421 max -1     -1.1     -> -1
ddmax423 max -1.1   -Inf     -> -1.1
-- same with operands reversed
ddmax431 max  1.1    Inf     ->  Infinity
ddmax432 max  1      1.1     ->  1.1
ddmax433 max  1.0    1       ->  1
ddmax434 max  0.1    1.0     ->  1.0
ddmax435 max  0.10   0.1     ->  0.1
ddmax436 max  0.100  0.10    ->  0.10
ddmax437 max  0      0.10    ->  0.10
ddmax438 max  0.0    0       ->  0
ddmax439 max -0      0.0     ->  0.0
ddmax440 max -0.0    0.0     ->  0.0
ddmax441 max -0.0    0.00    ->  0.00
ddmax442 max -0.00   0.0     ->  0.0
ddmax443 max -0.0    0       ->  0
ddmax444 max -0      0       ->  0
ddmax445 max -0     -0.0     -> -0.0
ddmax446 max -0.100 -0       -> -0
ddmax447 max -0.10  -0.100   -> -0.100
ddmax448 max -0.1   -0.10    -> -0.10
ddmax449 max -1.0   -0.1     -> -0.1
ddmax450 max -1     -1.0     -> -1.0
ddmax451 max -1.1   -1       -> -1
ddmax453 max -Inf   -1.1     -> -1.1
-- largies
ddmax460 max  1000   1E+3    ->  1E+3
ddmax461 max  1E+3   1000    ->  1E+3
ddmax462 max  1000  -1E+3    ->  1000
ddmax463 max  1E+3  -1000    ->  1E+3
ddmax464 max -1000   1E+3    ->  1E+3
ddmax465 max -1E+3   1000    ->  1000
ddmax466 max -1000  -1E+3    -> -1000
ddmax467 max -1E+3  -1000    -> -1000

-- misalignment traps for little-endian
ddmax471 max      1.0       0.1  -> 1.0
ddmax472 max      0.1       1.0  -> 1.0
ddmax473 max     10.0       0.1  -> 10.0
ddmax474 max      0.1      10.0  -> 10.0
ddmax475 max      100       1.0  -> 100
ddmax476 max      1.0       100  -> 100
ddmax477 max     1000      10.0  -> 1000
ddmax478 max     10.0      1000  -> 1000
ddmax479 max    10000     100.0  -> 10000
ddmax480 max    100.0     10000  -> 10000
ddmax481 max   100000    1000.0  -> 100000
ddmax482 max   1000.0    100000  -> 100000
ddmax483 max  1000000   10000.0  -> 1000000
ddmax484 max  10000.0   1000000  -> 1000000

-- subnormals
ddmax510 max  1.00E-383       0  ->   1.00E-383
ddmax511 max  0.1E-383        0  ->   1E-384    Subnormal
ddmax512 max  0.10E-383       0  ->   1.0E-384  Subnormal
ddmax513 max  0.100E-383      0  ->   1.00E-384 Subnormal
ddmax514 max  0.01E-383       0  ->   1E-385    Subnormal
ddmax515 max  0.999E-383      0  ->   9.99E-384 Subnormal
ddmax516 max  0.099E-383      0  ->   9.9E-385  Subnormal
ddmax517 max  0.009E-383      0  ->   9E-386    Subnormal
ddmax518 max  0.001E-383      0  ->   1E-386    Subnormal
ddmax519 max  0.0009E-383     0  ->   9E-387    Subnormal
ddmax520 max  0.0001E-383     0  ->   1E-387    Subnormal

ddmax530 max -1.00E-383       0  ->   0
ddmax531 max -0.1E-383        0  ->   0
ddmax532 max -0.10E-383       0  ->   0
ddmax533 max -0.100E-383      0  ->   0
ddmax534 max -0.01E-383       0  ->   0
ddmax535 max -0.999E-383      0  ->   0
ddmax536 max -0.099E-383      0  ->   0
ddmax537 max -0.009E-383      0  ->   0
ddmax538 max -0.001E-383      0  ->   0
ddmax539 max -0.0009E-383     0  ->   0
ddmax540 max -0.0001E-383     0  ->   0

-- Null tests
ddmax900 max 10  #  -> NaN Invalid_operation
ddmax901 max  # 10  -> NaN Invalid_operation



ake Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful.
summaryrefslogtreecommitdiffstats
path: root/generic/tclLiteral.c
blob: 3f1d059845505b93b0bb5095195210c1ab68da9f (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
/*
 * tclLiteral.c --
 *
 *	Implementation of the global and ByteCode-local literal tables used to
 *	manage the Tcl objects created for literal values during compilation
 *	of Tcl scripts. This implementation borrows heavily from the more
 *	general hashtable implementation of Tcl hash tables that appears in
 *	tclHash.c.
 *
 * Copyright (c) 1997-1998 Sun Microsystems, Inc.
 * Copyright (c) 2004 by Kevin B. Kenny.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclLiteral.c,v 1.30 2007/03/21 16:25:28 dgp Exp $
 */

#include "tclInt.h"
#include "tclCompile.h"

/*
 * When there are this many entries per bucket, on average, rebuild a
 * literal's hash table to make it larger.
 */

#define REBUILD_MULTIPLIER	3

/*
 * Function prototypes for static functions in this file:
 */

static int		AddLocalLiteralEntry(CompileEnv *envPtr,
			    LiteralEntry *globalPtr, int localHash);
static void		ExpandLocalLiteralArray(CompileEnv *envPtr);
static unsigned int	HashString(const char *bytes, int length);
static void		RebuildLiteralTable(LiteralTable *tablePtr);

/*
 *----------------------------------------------------------------------
 *
 * TclInitLiteralTable --
 *
 *	This function is called to initialize the fields of a literal table
 *	structure for either an interpreter or a compilation's CompileEnv
 *	structure.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The literal table is made ready for use.
 *
 *----------------------------------------------------------------------
 */

void
TclInitLiteralTable(
    register LiteralTable *tablePtr)
				/* Pointer to table structure, which is
				 * supplied by the caller. */
{
#if (TCL_SMALL_HASH_TABLE != 4)
    Tcl_Panic("TclInitLiteralTable: TCL_SMALL_HASH_TABLE is %d, not 4",
	    TCL_SMALL_HASH_TABLE);
#endif

    tablePtr->buckets = tablePtr->staticBuckets;
    tablePtr->staticBuckets[0] = tablePtr->staticBuckets[1] = 0;
    tablePtr->staticBuckets[2] = tablePtr->staticBuckets[3] = 0;
    tablePtr->numBuckets = TCL_SMALL_HASH_TABLE;
    tablePtr->numEntries = 0;
    tablePtr->rebuildSize = TCL_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
    tablePtr->mask = 3;
}

/*
 *----------------------------------------------------------------------
 *
 * TclCleanupLiteralTable --
 *
 *	This function frees the internal representation of every literal in a
 *	literal table. It is called prior to deleting an interp, so that
 *	variable refs will be cleaned up properly.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Each literal in the table has its internal representation freed.
 *
 *----------------------------------------------------------------------
 */

void
TclCleanupLiteralTable(
    Tcl_Interp *interp,		/* Interpreter containing literals to purge */
    LiteralTable *tablePtr)	/* Points to the literal table being
				 * cleaned. */
{
    int i;
    LiteralEntry* entryPtr;	/* Pointer to the current entry in the hash
				 * table of literals. */
    LiteralEntry* nextPtr;	/* Pointer to the next entry in the bucket. */
    Tcl_Obj* objPtr;		/* Pointer to a literal object whose internal
				 * rep is being freed. */
    const Tcl_ObjType* typePtr;	/* Pointer to the object's type. */
    int didOne;			/* Flag for whether we've removed a literal in
				 * the current bucket. */

#ifdef TCL_COMPILE_DEBUG
    TclVerifyGlobalLiteralTable((Interp *) interp);
#endif /* TCL_COMPILE_DEBUG */

    for (i=0 ; i<tablePtr->numBuckets ; i++) {
	/*
	 * It is tempting simply to walk each hash bucket once and delete the
	 * internal representations of each literal in turn. It's also wrong.
	 * The problem is that freeing a literal's internal representation can
	 * delete other literals to which it refers, making nextPtr invalid.
	 * So each time we free an internal rep, we start its bucket over
	 * again.
	 */

	do {
	    didOne = 0;
	    entryPtr = tablePtr->buckets[i];
	    while (entryPtr != NULL) {
		objPtr = entryPtr->objPtr;
		nextPtr = entryPtr->nextPtr;
		typePtr = objPtr->typePtr;
		if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) {
		    if (objPtr->bytes == NULL) {
			Tcl_Panic( "literal without a string rep" );
		    }
		    objPtr->typePtr = NULL;
		    typePtr->freeIntRepProc(objPtr);
		    didOne = 1;
		} else {
		    entryPtr = nextPtr;
		}
	    }
	} while (didOne);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclDeleteLiteralTable --
 *
 *	This function frees up everything associated with a literal table
 *	except for the table's structure itself. It is called when the
 *	interpreter is deleted.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Each literal in the table is released: i.e., its reference count in
 *	the global literal table is decremented and, if it becomes zero, the
 *	literal is freed. In addition, the table's bucket array is freed.
 *
 *----------------------------------------------------------------------
 */

void
TclDeleteLiteralTable(
    Tcl_Interp *interp,		/* Interpreter containing shared literals
				 * referenced by the table to delete. */
    LiteralTable *tablePtr)	/* Points to the literal table to delete. */
{
    LiteralEntry *entryPtr, *nextPtr;
    Tcl_Obj *objPtr;
    int i;

    /*
     * Release remaining literals in the table. Note that releasing a literal
     * might release other literals, modifying the table, so we restart the
     * search from the bucket chain we last found an entry.
     */

#ifdef TCL_COMPILE_DEBUG
    TclVerifyGlobalLiteralTable((Interp *) interp);
#endif /*TCL_COMPILE_DEBUG*/

    /*
     * We used to call TclReleaseLiteral for each literal in the table, which
     * is rather inefficient as it causes one lookup-by-hash for each
     * reference to the literal. We now rely at interp-deletion on each
     * bytecode object to release its references to the literal Tcl_Obj
     * without requiring that it updates the global table itself, and deal
     * here only with the table.
     */

    for (i=0 ; i<tablePtr->numBuckets ; i++) {
	entryPtr = tablePtr->buckets[i];
	while (entryPtr != NULL) {
	    objPtr = entryPtr->objPtr;
	    TclDecrRefCount(objPtr);
	    nextPtr = entryPtr->nextPtr;
	    ckfree((char *) entryPtr);
	    entryPtr = nextPtr;
	}
    }

    /*
     * Free up the table's bucket array if it was dynamically allocated.
     */

    if (tablePtr->buckets != tablePtr->staticBuckets) {
	ckfree((char *) tablePtr->buckets);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclRegisterLiteral --
 *
 *	Find, or if necessary create, an object in a CompileEnv literal array
 *	that has a string representation matching the argument string.
 *
 * Results:
 *	The index in the CompileEnv's literal array that references a shared
 *	literal matching the string. The object is created if necessary.
 *
 * Side effects:
 *	To maximize sharing, we look up the string in the interpreter's global
 *	literal table. If not found, we create a new shared literal in the
 *	global table. We then add a reference to the shared literal in the
 *	CompileEnv's literal array.
 *
 *	If LITERAL_ON_HEAP is set in flags, this function is given ownership
 *	of the string: if an object is created then its string representation
 *	is set directly from string, otherwise the string is freed. Typically,
 *	a caller sets LITERAL_ON_HEAP if "string" is an already heap-allocated
 *	buffer holding the result of backslash substitutions.
 *
 *----------------------------------------------------------------------
 */

int
TclRegisterLiteral(
    CompileEnv *envPtr,		/* Points to the CompileEnv in whose object
				 * array an object is found or created. */
    register char *bytes,	/* Points to string for which to find or
				 * create an object in CompileEnv's object
				 * array. */
    int length,			/* Number of bytes in the string. If < 0, the
				 * string consists of all bytes up to the
				 * first null character. */
    int flags)			/* If LITERAL_ON_HEAP then the caller already
				 * malloc'd bytes and ownership is passed to
				 * this function. If LITERAL_NS_SCOPE then
				 * the literal shouldnot be shared accross
				 * namespaces. */
{
    Interp *iPtr = envPtr->iPtr;
    LiteralTable *globalTablePtr = &(iPtr->literalTable);
    LiteralTable *localTablePtr = &(envPtr->localLitTable);
    register LiteralEntry *globalPtr, *localPtr;
    register Tcl_Obj *objPtr;
    unsigned int hash;
    int localHash, globalHash, objIndex;
    Namespace *nsPtr;

    if (length < 0) {
	length = (bytes ? strlen(bytes) : 0);
    }
    hash = HashString(bytes, length);

    /*
     * Is the literal already in the CompileEnv's local literal array? If so,
     * just return its index.
     */

    localHash = (hash & localTablePtr->mask);
    for (localPtr=localTablePtr->buckets[localHash] ; localPtr!=NULL;
	    localPtr = localPtr->nextPtr) {
	objPtr = localPtr->objPtr;
	if ((objPtr->length == length) && ((length == 0)
		|| ((objPtr->bytes[0] == bytes[0])
		&& (memcmp(objPtr->bytes, bytes, (unsigned) length) == 0)))) {
	    if (flags & LITERAL_ON_HEAP) {
		ckfree(bytes);
	    }
	    objIndex = (localPtr - envPtr->literalArrayPtr);
#ifdef TCL_COMPILE_DEBUG
	    TclVerifyLocalLiteralTable(envPtr);
#endif /*TCL_COMPILE_DEBUG*/

	    return objIndex;
	}
    }

    /*
     * The literal is new to this CompileEnv. Should it be shared accross
     * namespaces? If it is a fully qualified name, the namespace
     * specification is not needed to avoid sharing.
     */

    if ((flags & LITERAL_NS_SCOPE) && iPtr->varFramePtr
	    && ((length <2) || (bytes[0] != ':') || (bytes[1] != ':'))) {
	nsPtr = iPtr->varFramePtr->nsPtr;
    } else {
	nsPtr = NULL;
    }

    /*
     * Is it in the interpreter's global literal table?
     */

    globalHash = (hash & globalTablePtr->mask);
    for (globalPtr=globalTablePtr->buckets[globalHash] ; globalPtr!=NULL;
	    globalPtr = globalPtr->nextPtr) {
	objPtr = globalPtr->objPtr;
	if ((globalPtr->nsPtr == nsPtr)
		&& (objPtr->length == length) && ((length == 0)
		|| ((objPtr->bytes[0] == bytes[0])
		&& (memcmp(objPtr->bytes, bytes, (unsigned) length) == 0)))) {
	    /*
	     * A global literal was found. Add an entry to the CompileEnv's
	     * local literal array.
	     */

	    if (flags & LITERAL_ON_HEAP) {
		ckfree(bytes);
	    }
	    objIndex = AddLocalLiteralEntry(envPtr, globalPtr, localHash);
#ifdef TCL_COMPILE_DEBUG
	    if (globalPtr->refCount < 1) {
		Tcl_Panic("TclRegisterLiteral: global literal \"%.*s\" had bad refCount %d",
			(length>60? 60 : length), bytes, globalPtr->refCount);
	    }
	    TclVerifyLocalLiteralTable(envPtr);
#endif /*TCL_COMPILE_DEBUG*/
	    return objIndex;
	}
    }

    /*
     * The literal is new to the interpreter. Add it to the global literal
     * table then add an entry to the CompileEnv's local literal array.
     * Convert the object to an integer object if possible.
     */

    TclNewObj(objPtr);
    Tcl_IncrRefCount(objPtr);
    if (flags & LITERAL_ON_HEAP) {
	objPtr->bytes = bytes;
	objPtr->length = length;
    } else {
	TclInitStringRep(objPtr, bytes, length);
    }

#ifdef TCL_COMPILE_DEBUG
    if (TclLookupLiteralEntry((Tcl_Interp *) iPtr, objPtr) != NULL) {
	Tcl_Panic("TclRegisterLiteral: literal \"%.*s\" found globally but shouldn't be",
		(length>60? 60 : length), bytes);
    }
#endif

    globalPtr = (LiteralEntry *) ckalloc((unsigned) sizeof(LiteralEntry));
    globalPtr->objPtr = objPtr;
    globalPtr->refCount = 0;
    globalPtr->nsPtr = nsPtr;
    globalPtr->nextPtr = globalTablePtr->buckets[globalHash];
    globalTablePtr->buckets[globalHash] = globalPtr;
    globalTablePtr->numEntries++;

    /*
     * If the global literal table has exceeded a decent size, rebuild it with
     * more buckets.
     */

    if (globalTablePtr->numEntries >= globalTablePtr->rebuildSize) {
	RebuildLiteralTable(globalTablePtr);
    }
    objIndex = AddLocalLiteralEntry(envPtr, globalPtr, localHash);

#ifdef TCL_COMPILE_DEBUG
    TclVerifyGlobalLiteralTable(iPtr);
    TclVerifyLocalLiteralTable(envPtr);
    {
	LiteralEntry *entryPtr;
	int found, i;

	found = 0;
	for (i=0 ; i<globalTablePtr->numBuckets ; i++) {
	    for (entryPtr=globalTablePtr->buckets[i]; entryPtr!=NULL ;
		    entryPtr=entryPtr->nextPtr) {
		if ((entryPtr == globalPtr) && (entryPtr->objPtr == objPtr)) {
		    found = 1;
		}
	    }
	}
	if (!found) {
	    Tcl_Panic("TclRegisterLiteral: literal \"%.*s\" wasn't global",
		    (length>60? 60 : length), bytes);
	}
    }
#endif /*TCL_COMPILE_DEBUG*/

#ifdef TCL_COMPILE_STATS
    iPtr->stats.numLiteralsCreated++;
    iPtr->stats.totalLitStringBytes += (double) (length + 1);
    iPtr->stats.currentLitStringBytes += (double) (length + 1);
    iPtr->stats.literalCount[TclLog2(length)]++;
#endif /*TCL_COMPILE_STATS*/

    return objIndex;
}

/*
 *----------------------------------------------------------------------
 *
 * TclLookupLiteralEntry --
 *
 *	Finds the LiteralEntry that corresponds to a literal Tcl object
 *	holding a literal.
 *
 * Results:
 *	Returns the matching LiteralEntry if found, otherwise NULL.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

LiteralEntry *
TclLookupLiteralEntry(
    Tcl_Interp *interp,		/* Interpreter for which objPtr was created to
				 * hold a literal. */
    register Tcl_Obj *objPtr)	/* Points to a Tcl object holding a literal
				 * that was previously created by a call to
				 * TclRegisterLiteral. */
{
    Interp *iPtr = (Interp *) interp;
    LiteralTable *globalTablePtr = &(iPtr->literalTable);
    register LiteralEntry *entryPtr;
    char *bytes;
    int length, globalHash;

    bytes = Tcl_GetStringFromObj(objPtr, &length);
    globalHash = (HashString(bytes, length) & globalTablePtr->mask);
    for (entryPtr=globalTablePtr->buckets[globalHash] ; entryPtr!=NULL;
	    entryPtr=entryPtr->nextPtr) {
	if (entryPtr->objPtr == objPtr) {
	    return entryPtr;
	}
    }
    return NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * TclHideLiteral --
 *
 *	Remove a literal entry from the literal hash tables, leaving it in the
 *	literal array so existing references continue to function. This makes
 *	it possible to turn a shared literal into a private literal that
 *	cannot be shared.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Removes the literal from the local hash table and decrements the
 *	global hash entry's reference count.
 *
 *----------------------------------------------------------------------
 */

void
TclHideLiteral(
    Tcl_Interp *interp,		/* Interpreter for which objPtr was created to
				 * hold a literal. */
    register CompileEnv *envPtr,/* Points to CompileEnv whose literal array
				 * contains the entry being hidden. */
    int index)			/* The index of the entry in the literal
				 * array. */
{
    LiteralEntry **nextPtrPtr, *entryPtr, *lPtr;
    LiteralTable *localTablePtr = &(envPtr->localLitTable);
    int localHash, length;
    char *bytes;
    Tcl_Obj *newObjPtr;

    lPtr = &(envPtr->literalArrayPtr[index]);

    /*
     * To avoid unwanted sharing we need to copy the object and remove it from
     * the local and global literal tables. It still has a slot in the literal
     * array so it can be referred to by byte codes, but it will not be
     * matched by literal searches.
     */

    newObjPtr = Tcl_DuplicateObj(lPtr->objPtr);
    Tcl_IncrRefCount(newObjPtr);
    TclReleaseLiteral(interp, lPtr->objPtr);
    lPtr->objPtr = newObjPtr;

    bytes = Tcl_GetStringFromObj(newObjPtr, &length);
    localHash = (HashString(bytes, length) & localTablePtr->mask);
    nextPtrPtr = &localTablePtr->buckets[localHash];

    for (entryPtr=*nextPtrPtr ; entryPtr!=NULL ; entryPtr=*nextPtrPtr) {
	if (entryPtr == lPtr) {
	    *nextPtrPtr = lPtr->nextPtr;
	    lPtr->nextPtr = NULL;
	    localTablePtr->numEntries--;
	    break;
	}
	nextPtrPtr = &entryPtr->nextPtr;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclAddLiteralObj --
 *
 *	Add a single literal object to the literal array. This function does
 *	not add the literal to the local or global literal tables. The caller
 *	is expected to add the entry to whatever tables are appropriate.
 *
 * Results:
 *	The index in the CompileEnv's literal array that references the
 *	literal. Stores the pointer to the new literal entry in the location
 *	referenced by the localPtrPtr argument.
 *
 * Side effects:
 *	Expands the literal array if necessary. Increments the refcount on the
 *	literal object.
 *
 *----------------------------------------------------------------------
 */

int
TclAddLiteralObj(
    register CompileEnv *envPtr,/* Points to CompileEnv in whose literal array
				 * the object is to be inserted. */
    Tcl_Obj *objPtr,		/* The object to insert into the array. */
    LiteralEntry **litPtrPtr)	/* The location where the pointer to the new
				 * literal entry should be stored. May be
				 * NULL. */
{
    register LiteralEntry *lPtr;
    int objIndex;

    if (envPtr->literalArrayNext >= envPtr->literalArrayEnd) {
	ExpandLocalLiteralArray(envPtr);
    }
    objIndex = envPtr->literalArrayNext;
    envPtr->literalArrayNext++;

    lPtr = &(envPtr->literalArrayPtr[objIndex]);
    lPtr->objPtr = objPtr;
    Tcl_IncrRefCount(objPtr);
    lPtr->refCount = -1;	/* i.e., unused */
    lPtr->nextPtr = NULL;

    if (litPtrPtr) {
	*litPtrPtr = lPtr;
    }

    return objIndex;
}

/*
 *----------------------------------------------------------------------
 *
 * AddLocalLiteralEntry --
 *
 *	Insert a new literal into a CompileEnv's local literal array.
 *
 * Results:
 *	The index in the CompileEnv's literal array that references the
 *	literal.
 *
 * Side effects:
 *	Increments the ref count of the global LiteralEntry since the
 *	CompileEnv now refers to the literal. Expands the literal array if
 *	necessary. May rebuild the hash bucket array of the CompileEnv's
 *	literal array if it becomes too large.
 *
 *----------------------------------------------------------------------
 */

static int
AddLocalLiteralEntry(
    register CompileEnv *envPtr,/* Points to CompileEnv in whose literal array
				 * the object is to be inserted. */
    LiteralEntry *globalPtr,	/* Points to the global LiteralEntry for the
				 * literal to add to the CompileEnv. */
    int localHash)		/* Hash value for the literal's string. */
{
    register LiteralTable *localTablePtr = &(envPtr->localLitTable);
    LiteralEntry *localPtr;
    int objIndex;

    objIndex = TclAddLiteralObj(envPtr, globalPtr->objPtr, &localPtr);

    /*
     * Add the literal to the local table.
     */

    localPtr->nextPtr = localTablePtr->buckets[localHash];
    localTablePtr->buckets[localHash] = localPtr;
    localTablePtr->numEntries++;

    globalPtr->refCount++;

    /*
     * If the CompileEnv's local literal table has exceeded a decent size,
     * rebuild it with more buckets.
     */

    if (localTablePtr->numEntries >= localTablePtr->rebuildSize) {
	RebuildLiteralTable(localTablePtr);
    }

#ifdef TCL_COMPILE_DEBUG
    TclVerifyLocalLiteralTable(envPtr);
    {
	char *bytes;
	int length, found, i;

	found = 0;
	for (i=0 ; i<localTablePtr->numBuckets ; i++) {
	    for (localPtr=localTablePtr->buckets[i] ; localPtr!=NULL ;
		    localPtr=localPtr->nextPtr) {
		if (localPtr->objPtr == globalPtr->objPtr) {
		    found = 1;
		}
	    }
	}

	if (!found) {
	    bytes = Tcl_GetStringFromObj(globalPtr->objPtr, &length);
	    Tcl_Panic("AddLocalLiteralEntry: literal \"%.*s\" wasn't found locally",
		    (length>60? 60 : length), bytes);
	}
    }
#endif /*TCL_COMPILE_DEBUG*/

    return objIndex;
}

/*
 *----------------------------------------------------------------------
 *
 * ExpandLocalLiteralArray --
 *
 *	Function that uses malloc to allocate more storage for a CompileEnv's
 *	local literal array.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The literal array in *envPtr is reallocated to a new array of double
 *	the size, and if envPtr->mallocedLiteralArray is non-zero the old
 *	array is freed. Entries are copied from the old array to the new one.
 *	The local literal table is updated to refer to the new entries.
 *
 *----------------------------------------------------------------------
 */

static void
ExpandLocalLiteralArray(
    register CompileEnv *envPtr)/* Points to the CompileEnv whose object array
				 * must be enlarged. */
{
    /*
     * The current allocated local literal entries are stored between elements
     * 0 and (envPtr->literalArrayNext - 1) [inclusive].
     */

    LiteralTable *localTablePtr = &(envPtr->localLitTable);
    int currElems = envPtr->literalArrayNext;
    size_t currBytes = (currElems * sizeof(LiteralEntry));
    LiteralEntry *currArrayPtr = envPtr->literalArrayPtr;
    LiteralEntry *newArrayPtr;
    int i;

    if (envPtr->mallocedLiteralArray) {
	newArrayPtr = (LiteralEntry *) ckrealloc(
		(char *)currArrayPtr, 2 * currBytes);
    } else {
	/*
	 * envPtr->literalArrayPtr isn't a ckalloc'd pointer, so we must
	 * code a ckrealloc equivalent for ourselves
	 */
	newArrayPtr = (LiteralEntry *) ckalloc(2 * currBytes);
	memcpy(newArrayPtr, currArrayPtr, currBytes);
	envPtr->mallocedLiteralArray = 1;
    }

    /*
     * Update the local literal table's bucket array.
     */

    if (currArrayPtr != newArrayPtr) {
	for (i=0 ; i<currElems ; i++) {
	    if (newArrayPtr[i].nextPtr != NULL) {
		newArrayPtr[i].nextPtr = newArrayPtr 
			+ (newArrayPtr[i].nextPtr - currArrayPtr);
	    }
	}
	for (i=0 ; i<localTablePtr->numBuckets ; i++) {
	    if (localTablePtr->buckets[i] != NULL) {
		localTablePtr->buckets[i] = newArrayPtr
			+ (localTablePtr->buckets[i] - currArrayPtr);
	    }
	}
    }

    envPtr->literalArrayPtr = newArrayPtr;
    envPtr->literalArrayEnd = (2 * currElems);
}

/*
 *----------------------------------------------------------------------
 *
 * TclReleaseLiteral --
 *
 *	This function releases a reference to one of the shared Tcl objects
 *	that hold literals. It is called to release the literals referenced by
 *	a ByteCode that is being destroyed, and it is also called by
 *	TclDeleteLiteralTable.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The reference count for the global LiteralTable entry that corresponds
 *	to the literal is decremented. If no other reference to a global
 *	literal object remains, it is freed.
 *
 *----------------------------------------------------------------------
 */

void
TclReleaseLiteral(
    Tcl_Interp *interp,		/* Interpreter for which objPtr was created to
				 * hold a literal. */
    register Tcl_Obj *objPtr)	/* Points to a literal object that was
				 * previously created by a call to
				 * TclRegisterLiteral. */
{
    Interp *iPtr = (Interp *) interp;
    LiteralTable *globalTablePtr = &(iPtr->literalTable);
    register LiteralEntry *entryPtr, *prevPtr;
    char *bytes;
    int length, index;

    bytes = Tcl_GetStringFromObj(objPtr, &length);
    index = (HashString(bytes, length) & globalTablePtr->mask);

    /*
     * Check to see if the object is in the global literal table and remove
     * this reference. The object may not be in the table if it is a hidden
     * local literal.
     */

    for (prevPtr=NULL, entryPtr=globalTablePtr->buckets[index];
	    entryPtr!=NULL ; prevPtr=entryPtr, entryPtr=entryPtr->nextPtr) {
	if (entryPtr->objPtr == objPtr) {
	    entryPtr->refCount--;

	    /*
	     * If the literal is no longer being used by any ByteCode, delete
	     * the entry then remove the reference corresponding to the global
	     * literal table entry (decrement the ref count of the object).
	     */

	    if (entryPtr->refCount == 0) {
		if (prevPtr == NULL) {
		    globalTablePtr->buckets[index] = entryPtr->nextPtr;
		} else {
		    prevPtr->nextPtr = entryPtr->nextPtr;
		}
		ckfree((char *) entryPtr);
		globalTablePtr->numEntries--;

		TclDecrRefCount(objPtr);

#ifdef TCL_COMPILE_STATS
		iPtr->stats.currentLitStringBytes -= (double) (length + 1);
#endif /*TCL_COMPILE_STATS*/
	    }
	    break;
	}
    }

    /*
     * Remove the reference corresponding to the local literal table entry.
     */

    Tcl_DecrRefCount(objPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * HashString --
 *
 *	Compute a one-word summary of a text string, which can be used to
 *	generate a hash index.
 *
 * Results:
 *	The return value is a one-word summary of the information in string.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static unsigned int
HashString(
    register const char *bytes,	/* String for which to compute hash value. */
    int length)			/* Number of bytes in the string. */
{
    register unsigned int result;
    register int i;

    /*
     * I tried a zillion different hash functions and asked many other people
     * for advice. Many people had their own favorite functions, all
     * different, but no-one had much idea why they were good ones. I chose
     * the one below (multiply by 9 and add new character) because of the
     * following reasons:
     *
     * 1. Multiplying by 10 is perfect for keys that are decimal strings, and
     *	  multiplying by 9 is just about as good.
     * 2. Times-9 is (shift-left-3) plus (old). This means that each
     *	  character's bits hang around in the low-order bits of the hash value
     *	  for ever, plus they spread fairly rapidly up to the high-order bits
     *	  to fill out the hash value. This seems works well both for decimal
     *	  and non-decimal strings.
     */

    result = 0;
    for (i=0 ; i<length ; i++) {
	result += (result<<3) + bytes[i];
    }
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * RebuildLiteralTable --
 *
 *	This function is invoked when the ratio of entries to hash buckets
 *	becomes too large in a local or global literal table. It allocates a
 *	larger bucket array and moves the entries into the new buckets.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory gets reallocated and entries get rehashed into new buckets.
 *
 *----------------------------------------------------------------------
 */

static void
RebuildLiteralTable(
    register LiteralTable *tablePtr)
				/* Local or global table to enlarge. */
{
    LiteralEntry **oldBuckets;
    register LiteralEntry **oldChainPtr, **newChainPtr;
    register LiteralEntry *entryPtr;
    LiteralEntry **bucketPtr;
    char *bytes;
    int oldSize, count, index, length;

    oldSize = tablePtr->numBuckets;
    oldBuckets = tablePtr->buckets;

    /*
     * Allocate and initialize the new bucket array, and set up hashing
     * constants for new array size.
     */

    tablePtr->numBuckets *= 4;
    tablePtr->buckets = (LiteralEntry **) ckalloc((unsigned)
	    (tablePtr->numBuckets * sizeof(LiteralEntry *)));
    for (count=tablePtr->numBuckets, newChainPtr=tablePtr->buckets;
	    count>0 ; count--, newChainPtr++) {
	*newChainPtr = NULL;
    }
    tablePtr->rebuildSize *= 4;
    tablePtr->mask = (tablePtr->mask << 2) + 3;

    /*
     * Rehash all of the existing entries into the new bucket array.
     */

    for (oldChainPtr=oldBuckets ; oldSize>0 ; oldSize--,oldChainPtr++) {
	for (entryPtr=*oldChainPtr ; entryPtr!=NULL ; entryPtr=*oldChainPtr) {
	    bytes = Tcl_GetStringFromObj(entryPtr->objPtr, &length);
	    index = (HashString(bytes, length) & tablePtr->mask);

	    *oldChainPtr = entryPtr->nextPtr;
	    bucketPtr = &(tablePtr->buckets[index]);
	    entryPtr->nextPtr = *bucketPtr;
	    *bucketPtr = entryPtr;
	}
    }

    /*
     * Free up the old bucket array, if it was dynamically allocated.
     */

    if (oldBuckets != tablePtr->staticBuckets) {
	ckfree((char *) oldBuckets);
    }
}

#ifdef TCL_COMPILE_STATS
/*
 *----------------------------------------------------------------------
 *
 * TclLiteralStats --
 *
 *	Return statistics describing the layout of the hash table in its hash
 *	buckets.
 *
 * Results:
 *	The return value is a malloc-ed string containing information about
 *	tablePtr. It is the caller's responsibility to free this string.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

char *
TclLiteralStats(
    LiteralTable *tablePtr)	/* Table for which to produce stats. */
{
#define NUM_COUNTERS 10
    int count[NUM_COUNTERS], overflow, i, j;
    double average, tmp;
    register LiteralEntry *entryPtr;
    char *result, *p;

    /*
     * Compute a histogram of bucket usage. For each bucket chain i, j is the
     * number of entries in the chain.
     */

    for (i=0 ; i<NUM_COUNTERS ; i++) {
	count[i] = 0;
    }
    overflow = 0;
    average = 0.0;
    for (i=0 ; i<tablePtr->numBuckets ; i++) {
	j = 0;
	for (entryPtr=tablePtr->buckets[i] ; entryPtr!=NULL;
		entryPtr=entryPtr->nextPtr) {
	    j++;
	}
	if (j < NUM_COUNTERS) {
	    count[j]++;
	} else {
	    overflow++;
	}
	tmp = j;
	average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;
    }

    /*
     * Print out the histogram and a few other pieces of information.
     */

    result = (char *) ckalloc((unsigned) ((NUM_COUNTERS*60) + 300));
    sprintf(result, "%d entries in table, %d buckets\n",
	    tablePtr->numEntries, tablePtr->numBuckets);
    p = result + strlen(result);
    for (i=0 ; i<NUM_COUNTERS ; i++) {
	sprintf(p, "number of buckets with %d entries: %d\n",
		i, count[i]);
	p += strlen(p);
    }
    sprintf(p, "number of buckets with %d or more entries: %d\n",
	    NUM_COUNTERS, overflow);
    p += strlen(p);
    sprintf(p, "average search distance for entry: %.1f", average);
    return result;
}
#endif /*TCL_COMPILE_STATS*/

#ifdef TCL_COMPILE_DEBUG
/*
 *----------------------------------------------------------------------
 *
 * TclVerifyLocalLiteralTable --
 *
 *	Check a CompileEnv's local literal table for consistency.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Tcl_Panic if problems are found.
 *
 *----------------------------------------------------------------------
 */

void
TclVerifyLocalLiteralTable(
    CompileEnv *envPtr)		/* Points to CompileEnv whose literal table is
				 * to be validated. */
{
    register LiteralTable *localTablePtr = &(envPtr->localLitTable);
    register LiteralEntry *localPtr;
    char *bytes;
    register int i;
    int length, count;

    count = 0;
    for (i=0 ; i<localTablePtr->numBuckets ; i++) {
	for (localPtr=localTablePtr->buckets[i] ; localPtr!=NULL;
		localPtr=localPtr->nextPtr) {
	    count++;
	    if (localPtr->refCount != -1) {
		bytes = Tcl_GetStringFromObj(localPtr->objPtr, &length);
		Tcl_Panic("TclVerifyLocalLiteralTable: local literal \"%.*s\" had bad refCount %d",
			(length>60? 60 : length), bytes, localPtr->refCount);
	    }
	    if (TclLookupLiteralEntry((Tcl_Interp *) envPtr->iPtr,
		    localPtr->objPtr) == NULL) {
		bytes = Tcl_GetStringFromObj(localPtr->objPtr, &length);
		Tcl_Panic("TclVerifyLocalLiteralTable: local literal \"%.*s\" is not global",
			(length>60? 60 : length), bytes);
	    }
	    if (localPtr->objPtr->bytes == NULL) {
		Tcl_Panic("TclVerifyLocalLiteralTable: literal has NULL string rep");
	    }
	}
    }
    if (count != localTablePtr->numEntries) {
	Tcl_Panic("TclVerifyLocalLiteralTable: local literal table had %d entries, should be %d",
		count, localTablePtr->numEntries);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclVerifyGlobalLiteralTable --
 *
 *	Check an interpreter's global literal table literal for consistency.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Tcl_Panic if problems are found.
 *
 *----------------------------------------------------------------------
 */

void
TclVerifyGlobalLiteralTable(
    Interp *iPtr)		/* Points to interpreter whose global literal
				 * table is to be validated. */
{
    register LiteralTable *globalTablePtr = &(iPtr->literalTable);
    register LiteralEntry *globalPtr;
    char *bytes;
    register int i;
    int length, count;

    count = 0;
    for (i=0 ; i<globalTablePtr->numBuckets ; i++) {
	for (globalPtr=globalTablePtr->buckets[i] ; globalPtr!=NULL;
		globalPtr=globalPtr->nextPtr) {
	    count++;
	    if (globalPtr->refCount < 1) {
		bytes = Tcl_GetStringFromObj(globalPtr->objPtr, &length);
		Tcl_Panic("TclVerifyGlobalLiteralTable: global literal \"%.*s\" had bad refCount %d",
			(length>60? 60 : length), bytes, globalPtr->refCount);
	    }
	    if (globalPtr->objPtr->bytes == NULL) {
		Tcl_Panic("TclVerifyGlobalLiteralTable: literal has NULL string rep");
	    }
	}
    }
    if (count != globalTablePtr->numEntries) {
	Tcl_Panic("TclVerifyGlobalLiteralTable: global literal table had %d entries, should be %d",
		count, globalTablePtr->numEntries);
    }
}
#endif /*TCL_COMPILE_DEBUG*/

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */