/
bits.c
1858 lines (1805 loc) · 64.9 KB
/
bits.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
/*
* CS:APP Data Lab
*
* <Please put your name and userid here>
*
* bits.c - Source file with your solutions to the Lab.
* This is the file you will hand in to your instructor.
*
* WARNING: Do not include the <stdio.h> header; it confuses the dlc
* compiler. You can still use printf for debugging without including
* <stdio.h>, although you might get a compiler warning. In general,
* it's not good practice to ignore compiler warnings, but in this
* case it's OK.
*/
#if 0
/*
* Instructions to Students:
*
* STEP 1: Read the following instructions carefully.
*/
You will provide your solution to the Data Lab by
editing the collection of functions in this source file.
INTEGER CODING RULES:
Replace the "return" statement in each function with one
or more lines of C code that implements the function. Your code
must conform to the following style:
int Funct(arg1, arg2, ...) {
/* brief description of how your implementation works */
int var1 = Expr1;
...
int varM = ExprM;
varJ = ExprJ;
...
varN = ExprN;
return ExprR;
}
Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xffffffff.
2. Function arguments and local variables (no global variables).
3. Unary integer operations ! ~
4. Binary integer operations & ^ | + << >>
Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.
You are expressly forbidden to:
1. Use any control constructs such as if, do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, -, or ?:
6. Use any form of casting.
7. Use any data type other than int. This implies that you
cannot use arrays, structs, or unions.
You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has unpredictable behavior when shifting an integer by more
than the word size.
EXAMPLES OF ACCEPTABLE CODING STYLE:
/*
* pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
*/
int pow2plus1(int x) {
/* exploit ability of shifts to compute powers of 2 */
return (1 << x) + 1;
}
/*
* pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
*/
int pow2plus4(int x) {
/* exploit ability of shifts to compute powers of 2 */
int result = (1 << x);
result += 4;
return result;
}
FLOATING POINT CODING RULES
For the problems that require you to implent floating-point operations,
the coding rules are less strict. You are allowed to use looping and
conditional control. You are allowed to use both ints and unsigneds.
You can use arbitrary integer and unsigned constants.
You are expressly forbidden to:
1. Define or use any macros.
2. Define any additional functions in this file.
3. Call any functions.
4. Use any form of casting.
5. Use any data type other than int or unsigned. This means that you
cannot use arrays, structs, or unions.
6. Use any floating point data types, operations, or constants.
NOTES:
1. Use the dlc (data lab checker) compiler (described in the handout) to
check the legality of your solutions.
2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
that you are allowed to use for your implementation of the function.
The max operator count is checked by dlc. Note that '=' is not
counted; you may use as many of these as you want without penalty.
3. Use the btest test harness to check your functions for correctness.
4. Use the BDD checker to formally verify your functions
5. The maximum number of ops for each function is given in the
header comment for each function. If there are any inconsistencies
between the maximum ops in the writeup and in this file, consider
this file the authoritative source.
/*
* STEP 2: Modify the following functions according the coding rules.
*
* IMPORTANT. TO AVOID GRADING SURPRISES:
* 1. Use the dlc compiler to check that your solutions conform
* to the coding rules.
* 2. Use the BDD checker to formally verify that your solutions produce
* the correct answers.
*/
#endif
/* Copyright (C) 1991-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This header is separate from features.h so that the compiler can
include it implicitly at the start of every compilation. It must
not itself include <features.h> or any other header that includes
<features.h> because the implicit include comes before any feature
test macros that may be defined in a source file before it first
explicitly includes a system header. GCC knows the name of this
header in order to preinclude it. */
/* glibc's intent is to support the IEC 559 math functionality, real
and complex. If the GCC (4.9 and later) predefined macros
specifying compiler intent are available, use them to determine
whether the overall intent is to support these features; otherwise,
presume an older compiler has intent to support these features and
define these macros by default. */
/* wchar_t uses Unicode 10.0.0. Version 10.0 of the Unicode Standard is
synchronized with ISO/IEC 10646:2017, fifth edition, plus
the following additions from Amendment 1 to the fifth edition:
- 56 emoji characters
- 285 hentaigana
- 3 additional Zanabazar Square characters */
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
/* Solution:
Core idea: De Morgan's laws.
*/
return ~(~x|~y);
}
/*
* bitConditional - x ? y : z for each bit respectively
* Example: bitConditional(0b00110011, 0b01010101, 0b00001111) = 0b00011101
* Legal ops: & | ^ ~
* Max ops: 8
* Rating: 1
*/
int bitConditional(int x, int y, int z) {
/* Solution 1:
Core idea: dual mask.
return (y&x)^(z&~x);
*/
/* Solution 2:
Core idea: someone told me it can be conquered with 3 ops, and I enumerate till finding a cure.
*/
return ((y^z)&x)^z;
}
/*
* byteSwap - swaps the nth byte and the mth byte
* Examples: byteSwap(0x12345678, 1, 3) = 0x56341278
* byteSwap(0xDEADBEEF, 0, 2) = 0xDEEFBEAD
* You may assume that 0 <= n <= 3, 0 <= m <= 3
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 2
*/
int byteSwap(int x, int n, int m) {
/* Solution 1 -- works, but been improved.
Core idea: left and right shifts, clear and fill bytes.
int a = x>>(n<<=3)&0xff;
int b = x>>(m<<=3)&0xff;
return (x&~((0xff<<n)|(0xff<<m)))|(a<<m)|(b<<n);
*/
/* Solution 2 (the following code):
Core idea: t = x^y; x^t->y, y^t->x;
*/
int t;
n <<= 3, m <<= 3;
t = ((x>>n)^(x>>m))&0xff;
return x^(t<<m)^(t<<n);
}
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 0 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int logicalShift(int x, int n) {
/* Solution 1 -- acceptable, but not optimal
Core idea: clear sign bit, arithmetic shift, restore sign bit.
int s = x>>31&1;
int x1 = x&~(1<<31);
return (x1>>n)|s<<(31+(~n+1));
*/
/* Solution 2 (the following code):
Core idea: arithmetic shift, then set mask
*/
return ~(1<<31>>n<<1)&x>>n;
}
/*
* cleanConsecutive1 - change any consecutive 1 to zeros in the binary form of x.
* Consecutive 1 means a set of 1 that contains more than one 1.
* Examples cleanConsecutive1(0x10) = 0x10
* cleanConsecutive1(0xF0) = 0x0
* cleanConsecutive1(0xFFFF0001) = 0x1
* cleanConsecutive1(0x4F4F4F4F) = 0x40404040
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 4
*/
int cleanConsecutive1(int x){
/* Solution 1:
Core idea: logical left and right shifts.
return x&~(x<<1)&((1<<31)|~(x>>1));
Solution 2:
An abnormal solution.
*/
int t = x&(x<<1);
return (t|t>>1)^x;
}
/*
* countTrailingZero - return the number of consecutive 0 from the lowest bit of
* the binary form of x.
* YOU MAY USE BIG CONST IN THIS PROBLEM, LIKE 0xFFFF0000
* YOU MAY USE BIG CONST IN THIS PROBLEM, LIKE 0xFFFF0000
* YOU MAY USE BIG CONST IN THIS PROBLEM, LIKE 0xFFFF0000
* Examples countTrailingZero(0x0) = 32, countTrailingZero(0x1) = 0,
* countTrailingZero(0xFFFF0000) = 16,
* countTrailingZero(0xFFFFFFF0) = 8,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 40
* Rating: 4
*/
int countTrailingZero(int x){
/* Solution 1:
Core idea: 1. bisection(binary lifting? divide and conquer? whatever).
2. generate a map (with truth table and observation) when length of x is small.
int cnt = 0;
int m;
m = (!(x&0xffff)<<31>>31)&16;
cnt = m, x >>= m;
m = (!(x&0xff)<<31>>31)&8;
cnt += m, x >>= m;
m = (!(x&0xf)<<31>>31)&4;
cnt += m, x >>= m;
m = (!(x&0x3)<<31>>31)&2;
cnt += m, x >>= m;
cnt += !(x&1)+!x;
return cnt;
Solution 2 (my roommate's idea, record it here for future reference):
Procedure: perform x = x&(-x)-1,
which maps x = ****1000
to 00000111.
Then, calculate how many 1s there are.
(No code)
Solution 3 (my roommate's idea, record it here for future reference):
Ouch, I forget all about it!!! XD
(No code)
Solution 4:
Core idea: exactly sol1, use logically equivalent expressions with less operators.
int cnt = 0;
int m;
m = !(x&0xffff)<<4;
cnt = m, x >>= m;
m = !(x&0xff)<<3;
cnt += m, x >>= m;
m = !(x&0xf)<<2;
cnt += m, x >>= m;
m = !(x&0x3)<<1;
cnt += m, x >>= m;
cnt += !(x&1)+!x;
return cnt;
Solution 5:
Core idea: enlighted by sol2, a similar solution using less ops.
Procedure: perform x = x&(-x),
which maps x = ****1000
to 00001000.
Then, find out the index of the remaining 1.
The thing is, there's only one non-zero bit, which allows us to do some clever
tricks to avoid condition stream.
int y = x&(~x+1), cnt = 0;
cnt = !(y&0x0000ffff)<<4;
cnt += !(y&0x00ff00ff)<<3;
cnt += !(y&0x0f0f0f0f)<<2;
cnt += !(y&0x33333333)<<1;
cnt += !(y&0x55555555);
cnt += !y;
return cnt;
*/
int y = x&(~x+1), cnt = 0;
cnt = !(y&0x0000ffff)<<4;
cnt += !(y&0x00ff00ff)<<3;
cnt += !(y&0x0f0f0f0f)<<2;
cnt += !(y&0x33333333)<<1;
cnt += !(y&0x55555555);
cnt += !y;
return cnt;
}
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
/* Solution:
Core idea: bias b = x < 0? (1<<n)-1: 0, like taught in CSAPP.
And, I figured out a good way to simulate condition stream and calculate (1<<n)-1 all at once.
*/
int b = x>>31;
b = b^(b<<n);
return (x+b)>>n;
}
/*
* oneMoreThan - return 1 if y is one more than x, and 0 otherwise
* Examples oneMoreThan(0, 1) = 1, oneMoreThan(-1, 1) = 0
* Legal ops: ~ & ! ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int oneMoreThan(int x, int y) {
/* Solution:
Core idea: mathematical observation.
Analysis:
y-x = 1 <=> y+(~x)+1 = 1 (is that true?)
<=> y+(~x) = 0
Note that the equation fails when (x, y) = (TMax, TMin), so we need to rid out such case.
There's no other corner cases. I know it because I passed all the tests. I cannot give a mathematical proof.
*/
return !((~x+y)|!(y^1<<31));
}
/*
* satMul3 - multiplies by 3, saturating to Tmin or Tmax if overflow
* Examples: satMul3(0x10000000) = 0x30000000
* satMul3(0x30000000) = 0x7FFFFFFF (Saturate to TMax)
* satMul3(0x70000000) = 0x7FFFFFFF (Saturate to TMax)
* satMul3(0xD0000000) = 0x80000000 (Saturate to TMin)
* satMul3(0xA0000000) = 0x80000000 (Saturate to TMin)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 25
* Rating: 3
*/
int satMul3(int x) {
/* Solution:
Core idea: x*3 overflows iff x+x overflows or (x+x)+x overflows.
CSAPP says: x+y overflows iff x, y have the same signs but x+y has a different one.
TMin won't cause trouble.
Important optimization:
note that (x&y)^(x|z) equals to ~y when x = 111...111, z when x = 0.
This allows us to perform condition stream and bitwise negation at the same time.
*/
int TMin = 1<<31;
int y = x+x, z = y+x;
int sx = x>>31;
int f = ((x^y)|(x^z))>>31; // f = y's or z's sign differs x's? 111...111: 000...000
int M = sx^TMin; // M = TMax when x is negative, TMin otherwise
return (f&M)^(f|z); // bless of god
}
/*
* subOK - Determine if can compute x-y without overflow
* Example: subOK(0x80000000,0x80000000) = 1,
* subOK(0x80000000,0x70000000) = 0,
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 3
*/
int subOK(int x, int y) {
/* Solution:
Core idea: classified discussion.
Analysis:
case 1: x,y of same signs. By all means won't overflow.
case 2: x,y of different signs. Overflows iff (x-y)'s sign differs x's.
TMin won't cause trouble. Not that I proved it, but that I passed it.
*/
int z = x+(~y+1);
return !(((x^y)&(x^z))>>31);
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
/* Solution 1 -- good, but not best
Core idea: classified discussion.
Analysis:
case 1: x,y of difference signs. x <= y iff x < 0 && y >= 0.
case 2: x,y of same signs. x <= y iff y-x >= 0.
Note that you cannot perform y-x in case 1, overflow will catch ya.
return ((x&~y) | ~((x^y)|(~x+y+1)))>>31&1;
*/
/* Solution 2 (the following code):
Core idea: same as sol1, but one step further.
Analysis:
case 1: x,y of difference signs. x <= y iff x < 0 && y >= 0, i.e. x's sign bit is 1.
case 2: x,y of same signs. x <= y iff y-x >= 0.
y-x >= 0 <=> x-y <= 0
<=> x-y < 1
<=> x+(~y)+1 < 1
<=> x+(~y) < 0
<=> (x+(~y))'s sign bit is 1.
So, we want to find t = (x,y of same signs? ~y: 0), so that case 1,2 amounts to (x+t)'s sign bit is 1.
Using sign bit mask trick, we can easily find such t. :)
TMin and overflow won't cause trouble, as in Sol1.
*/
int s = (x^y)>>31;
int t = ~(s|y);
return (x+t)>>31&1;
}
/*
* trueThreeFourths - multiplies by 3/4 rounding toward 0,
* avoiding errors due to overflow
* Examples: trueThreeFourths(11) = 8
* trueThreeFourths(-9) = -6
* trueThreeFourths(1073741824) = 805306368 (no overflow)
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 20
* Rating: 4
*/
int trueThreeFourths(int x)
{
/* Solution 1 -- available, but not optimal
Core idea: Euclidean division.
int t = (x+((x>>31)&3))>>2;
int y = t+t+t;
int r = x+~(t<<2)+1;
int l = r+r+r;
t = (l+((l>>31)&3))>>2;
return y+t;
*/
/* Solution 2:
Core idea: x*(3/4) = x-x/4
But, as LHS rounds towards 0, RHS has to round against 0.
For negative x, that amounts to rounding down.
For positive x which is not a multiple of 4, an additional 1 must be added.
int t = x>>2; // t = x/4, rounding down
int s = x>>31; // sign bit
int e = !(x&0x3); // e == 1 <=> x is a multiple of 4
t += !(s|e); // now t = x/4, rounding against 0
return x+~t+1;
Solution 3:
Core idea: logical equivalence to sol2, saving one more op.
*/
int t = x>>2;
int s = x>>31;
int e = !(x&0x3);
int d = (s|e)&1;
return x+~t+d;
}
/*
* float_twice - Return bit-level equivalent of expression 2*f for
* floating point argument f.
* Both the argument and result are passed as unsigned int's, but
* they are to be interpreted as the bit-level representation of
* single-precision floating point values.
* When argument is NaN, return argument
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_twice(unsigned uf) {
/*
I don't what to comment on this.
I don't know what I was typing either.
*/
// const unsigned m_exp = 0x7f800000; // (1<<30)-(1<<23)
// unsigned m_frac = 0x7fffff; // (1<<23)-1
// unsigned n_frac = 0xff800000; // ~m_frac
// unsigned exp = m_exp&uf, frac = m_frac&uf;
// unsigned res;
// const unsigned sp = 0x7f000000;
// res = uf+0x800000;
// switch (exp){
// case 0x7f800000: return uf;
// case 0: return (uf&n_frac)+(frac<<1);
// case 0x7f000000: res &= n_frac;
// default: return res;
// }
const unsigned m_sexp = 0xff800000; // (1<<30)-(1<<23)
unsigned sexp = m_sexp&uf;
switch (sexp){
case 0x7f800000:
case 0xff800000: return uf;
case 0x00000000:
case 0x80000000: return sexp|(uf<<1);
case 0x7f000000: return 0x7f800000;
case 0xff000000: return 0xff800000;
default: return uf+0x800000;
}
}
/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x) {
/*
I don't what to comment on this.
I don't know what I was typing either.
Damn floating point.
*/
unsigned m = 0x800000; // (1<<23)
unsigned TMin = 0x80000000;
unsigned frac;
unsigned exp = 0x4f000000;
//unsigned s = x&TMin;
unsigned x1;
unsigned mid = 0x40;
int f = 1;
switch (x){
case 0x80000000: return 0xcf000000;
case 0: return 0;
}
//if (s) x = -x;
while (1){
if (x&0x80000000){
if (f){
exp = 0xcf000000;
x = -x;
}
else break;
}
switch (exp){
case 0x4f000000: exp = 0x4e800000; break;
case 0x4e800000: exp = 0x4e000000; break;
case 0x4e000000: exp = 0x4d800000; break;
case 0x4d800000: exp = 0x4d000000; break;
case 0x4d000000: exp = 0x4c800000; break;
case 0x4c800000: exp = 0x4c000000; break;
case 0x4c000000: exp = 0x4b800000; break;
case 0x4b800000: exp = 0x4b000000; break;
case 0x4b000000: exp = 0x4a800000; break;
case 0x4a800000: exp = 0x4a000000; break;
case 0x4a000000: exp = 0x49800000; break;
case 0x49800000: exp = 0x49000000; break;
case 0x49000000: exp = 0x48800000; break;
case 0x48800000: exp = 0x48000000; break;
case 0x48000000: exp = 0x47800000; break;
case 0x47800000: exp = 0x47000000; break;
case 0x47000000: exp = 0x46800000; break;
case 0x46800000: exp = 0x46000000; break;
case 0x46000000: exp = 0x45800000; break;
case 0x45800000: exp = 0x45000000; break;
case 0x45000000: exp = 0x44800000; break;
case 0x44800000: exp = 0x44000000; break;
case 0x44000000: exp = 0x43800000; break;
case 0x43800000: exp = 0x43000000; break;
case 0x43000000: exp = 0x42800000; break;
case 0x42800000: exp = 0x42000000; break;
case 0x42000000: exp = 0x41800000; break;
case 0x41800000: exp = 0x41000000; break;
case 0x41000000: exp = 0x40800000; break;
case 0x40800000: exp = 0x40000000; break;
case 0x40000000: exp = 0x3f800000; break;
case 0x3f800000: exp = 0x3f000000; break;
case 0xcf000000: exp = 0xce800000; break;
case 0xce800000: exp = 0xce000000; break;
case 0xce000000: exp = 0xcd800000; break;
case 0xcd800000: exp = 0xcd000000; break;
case 0xcd000000: exp = 0xcc800000; break;
case 0xcc800000: exp = 0xcc000000; break;
case 0xcc000000: exp = 0xcb800000; break;
case 0xcb800000: exp = 0xcb000000; break;
case 0xcb000000: exp = 0xca800000; break;
case 0xca800000: exp = 0xca000000; break;
case 0xca000000: exp = 0xc9800000; break;
case 0xc9800000: exp = 0xc9000000; break;
case 0xc9000000: exp = 0xc8800000; break;
case 0xc8800000: exp = 0xc8000000; break;
case 0xc8000000: exp = 0xc7800000; break;
case 0xc7800000: exp = 0xc7000000; break;
case 0xc7000000: exp = 0xc6800000; break;
case 0xc6800000: exp = 0xc6000000; break;
case 0xc6000000: exp = 0xc5800000; break;
case 0xc5800000: exp = 0xc5000000; break;
case 0xc5000000: exp = 0xc4800000; break;
case 0xc4800000: exp = 0xc4000000; break;
case 0xc4000000: exp = 0xc3800000; break;
case 0xc3800000: exp = 0xc3000000; break;
case 0xc3000000: exp = 0xc2800000; break;
case 0xc2800000: exp = 0xc2000000; break;
case 0xc2000000: exp = 0xc1800000; break;
case 0xc1800000: exp = 0xc1000000; break;
case 0xc1000000: exp = 0xc0800000; break;
case 0xc0800000: exp = 0xc0000000; break;
case 0xc0000000: exp = 0xbf800000; break;
case 0xbf800000: exp = 0xbf000000; break;
}
x <<= 1;
f = 0;
// --exp;
}
frac = (x^TMin)>>8;
x1 = x&0x1fe;
// x1 = x&0x7f;
// if (x1 > mid) delta = 1;
// else if (x1 == mid) delta = frac&1;
switch (x1){
case 0x1fe:
case 0x1fc:
case 0x1fa:
case 0x1f8:
case 0x1f6:
case 0x1f4:
case 0x1f2:
case 0x1f0:
case 0x1ee:
case 0x1ec:
case 0x1ea:
case 0x1e8:
case 0x1e6:
case 0x1e4:
case 0x1e2:
case 0x1e0:
case 0x1de:
case 0x1dc:
case 0x1da:
case 0x1d8:
case 0x1d6:
case 0x1d4:
case 0x1d2:
case 0x1d0:
case 0x1ce:
case 0x1cc:
case 0x1ca:
case 0x1c8:
case 0x1c6:
case 0x1c4:
case 0x1c2:
case 0x1c0:
case 0x1be:
case 0x1bc:
case 0x1ba:
case 0x1b8:
case 0x1b6:
case 0x1b4:
case 0x1b2:
case 0x1b0:
case 0x1ae:
case 0x1ac:
case 0x1aa:
case 0x1a8:
case 0x1a6:
case 0x1a4:
case 0x1a2:
case 0x1a0:
case 0x19e:
case 0x19c:
case 0x19a:
case 0x198:
case 0x196:
case 0x194:
case 0x192:
case 0x190:
case 0x18e:
case 0x18c:
case 0x18a:
case 0x188:
case 0x186:
case 0x184:
case 0x182:
case 0x180:
case 0xfe:
case 0xfc:
case 0xfa:
case 0xf8:
case 0xf6:
case 0xf4:
case 0xf2:
case 0xf0:
case 0xee:
case 0xec:
case 0xea:
case 0xe8:
case 0xe6:
case 0xe4:
case 0xe2:
case 0xe0:
case 0xde:
case 0xdc:
case 0xda:
case 0xd8:
case 0xd6:
case 0xd4:
case 0xd2:
case 0xd0:
case 0xce:
case 0xcc:
case 0xca:
case 0xc8:
case 0xc6:
case 0xc4:
case 0xc2:
case 0xc0:
case 0xbe:
case 0xbc:
case 0xba:
case 0xb8:
case 0xb6:
case 0xb4:
case 0xb2:
case 0xb0:
case 0xae:
case 0xac:
case 0xaa:
case 0xa8:
case 0xa6:
case 0xa4:
case 0xa2:
case 0xa0:
case 0x9e:
case 0x9c:
case 0x9a:
case 0x98:
case 0x96:
case 0x94:
case 0x92:
case 0x90:
case 0x8e:
case 0x8c:
case 0x8a:
case 0x88:
case 0x86:
case 0x84:
case 0x82:
switch (exp){
case 0x4e800000: exp = 0x4e800001; break;
case 0x4e000000: exp = 0x4e000001; break;
case 0x4d800000: exp = 0x4d800001; break;
case 0x4d000000: exp = 0x4d000001; break;
case 0x4c800000: exp = 0x4c800001; break;
case 0x4c000000: exp = 0x4c000001; break;
case 0x4b800000: exp = 0x4b800001; break;
case 0x4b000000: exp = 0x4b000001; break;
case 0x4a800000: exp = 0x4a800001; break;
case 0x4a000000: exp = 0x4a000001; break;
case 0x49800000: exp = 0x49800001; break;
case 0x49000000: exp = 0x49000001; break;
case 0x48800000: exp = 0x48800001; break;
case 0x48000000: exp = 0x48000001; break;
case 0x47800000: exp = 0x47800001; break;
case 0x47000000: exp = 0x47000001; break;
case 0x46800000: exp = 0x46800001; break;
case 0x46000000: exp = 0x46000001; break;
case 0x45800000: exp = 0x45800001; break;
case 0x45000000: exp = 0x45000001; break;
case 0x44800000: exp = 0x44800001; break;
case 0x44000000: exp = 0x44000001; break;
case 0x43800000: exp = 0x43800001; break;
case 0x43000000: exp = 0x43000001; break;
case 0x42800000: exp = 0x42800001; break;
case 0x42000000: exp = 0x42000001; break;
case 0x41800000: exp = 0x41800001; break;
case 0x41000000: exp = 0x41000001; break;
case 0x40800000: exp = 0x40800001; break;
case 0x40000000: exp = 0x40000001; break;
case 0x3f800000: exp = 0x3f800001; break;
case 0x3f000000: exp = 0x3f000001; break;
case 0xce800000: exp = 0xce800001; break;
case 0xce000000: exp = 0xce000001; break;
case 0xcd800000: exp = 0xcd800001; break;
case 0xcd000000: exp = 0xcd000001; break;
case 0xcc800000: exp = 0xcc800001; break;
case 0xcc000000: exp = 0xcc000001; break;
case 0xcb800000: exp = 0xcb800001; break;
case 0xcb000000: exp = 0xcb000001; break;
case 0xca800000: exp = 0xca800001; break;
case 0xca000000: exp = 0xca000001; break;
case 0xc9800000: exp = 0xc9800001; break;
case 0xc9000000: exp = 0xc9000001; break;
case 0xc8800000: exp = 0xc8800001; break;
case 0xc8000000: exp = 0xc8000001; break;
case 0xc7800000: exp = 0xc7800001; break;
case 0xc7000000: exp = 0xc7000001; break;
case 0xc6800000: exp = 0xc6800001; break;
case 0xc6000000: exp = 0xc6000001; break;
case 0xc5800000: exp = 0xc5800001; break;
case 0xc5000000: exp = 0xc5000001; break;
case 0xc4800000: exp = 0xc4800001; break;
case 0xc4000000: exp = 0xc4000001; break;
case 0xc3800000: exp = 0xc3800001; break;
case 0xc3000000: exp = 0xc3000001; break;
case 0xc2800000: exp = 0xc2800001; break;
case 0xc2000000: exp = 0xc2000001; break;
case 0xc1800000: exp = 0xc1800001; break;
case 0xc1000000: exp = 0xc1000001; break;
case 0xc0800000: exp = 0xc0800001; break;
case 0xc0000000: exp = 0xc0000001; break;
case 0xbf800000: exp = 0xbf800001; break;
case 0xbf000000: exp = 0xbf000001; break;
}
}
return exp+frac;
}
/*
* float_f2i - Return bit-level equivalent of expression (int) f
* for floating point argument f.
* Argument is passed as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point value.
* Anything out of range (including NaN and infinity) should return
* 0x80000000u.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
int float_f2i(unsigned uf) {
/*
I don't what to comment on this.
I don't know what I was typing either.
Damn floating point.
*/
unsigned TMin = 0x80000000;
unsigned TMax = 0x7fffffff;
unsigned MEXP = 0xff800000;
unsigned frac = (uf<<8)|TMin;
unsigned exp; //= (uf&TMax)>>23;
unsigned s = 0;
int res;
//if (uf == 0xcf000000) return 0x80000000;
// exp = 158-((uf&TMax)>>23);
switch(uf&MEXP){
case 0xff800000: exp = 0xffffff9f; s = 1; break;
case 0xff000000: exp = 0xffffffa0; s = 1; break;
case 0xfe800000: exp = 0xffffffa1; s = 1; break;
case 0xfe000000: exp = 0xffffffa2; s = 1; break;
case 0xfd800000: exp = 0xffffffa3; s = 1; break;
case 0xfd000000: exp = 0xffffffa4; s = 1; break;
case 0xfc800000: exp = 0xffffffa5; s = 1; break;
case 0xfc000000: exp = 0xffffffa6; s = 1; break;
case 0xfb800000: exp = 0xffffffa7; s = 1; break;
case 0xfb000000: exp = 0xffffffa8; s = 1; break;
case 0xfa800000: exp = 0xffffffa9; s = 1; break;
case 0xfa000000: exp = 0xffffffaa; s = 1; break;
case 0xf9800000: exp = 0xffffffab; s = 1; break;
case 0xf9000000: exp = 0xffffffac; s = 1; break;
case 0xf8800000: exp = 0xffffffad; s = 1; break;
case 0xf8000000: exp = 0xffffffae; s = 1; break;
case 0xf7800000: exp = 0xffffffaf; s = 1; break;
case 0xf7000000: exp = 0xffffffb0; s = 1; break;
case 0xf6800000: exp = 0xffffffb1; s = 1; break;
case 0xf6000000: exp = 0xffffffb2; s = 1; break;
case 0xf5800000: exp = 0xffffffb3; s = 1; break;
case 0xf5000000: exp = 0xffffffb4; s = 1; break;
case 0xf4800000: exp = 0xffffffb5; s = 1; break;
case 0xf4000000: exp = 0xffffffb6; s = 1; break;
case 0xf3800000: exp = 0xffffffb7; s = 1; break;
case 0xf3000000: exp = 0xffffffb8; s = 1; break;
case 0xf2800000: exp = 0xffffffb9; s = 1; break;
case 0xf2000000: exp = 0xffffffba; s = 1; break;
case 0xf1800000: exp = 0xffffffbb; s = 1; break;
case 0xf1000000: exp = 0xffffffbc; s = 1; break;
case 0xf0800000: exp = 0xffffffbd; s = 1; break;
case 0xf0000000: exp = 0xffffffbe; s = 1; break;
case 0xef800000: exp = 0xffffffbf; s = 1; break;
case 0xef000000: exp = 0xffffffc0; s = 1; break;
case 0xee800000: exp = 0xffffffc1; s = 1; break;
case 0xee000000: exp = 0xffffffc2; s = 1; break;
case 0xed800000: exp = 0xffffffc3; s = 1; break;
case 0xed000000: exp = 0xffffffc4; s = 1; break;
case 0xec800000: exp = 0xffffffc5; s = 1; break;
case 0xec000000: exp = 0xffffffc6; s = 1; break;
case 0xeb800000: exp = 0xffffffc7; s = 1; break;
case 0xeb000000: exp = 0xffffffc8; s = 1; break;
case 0xea800000: exp = 0xffffffc9; s = 1; break;
case 0xea000000: exp = 0xffffffca; s = 1; break;
case 0xe9800000: exp = 0xffffffcb; s = 1; break;
case 0xe9000000: exp = 0xffffffcc; s = 1; break;
case 0xe8800000: exp = 0xffffffcd; s = 1; break;
case 0xe8000000: exp = 0xffffffce; s = 1; break;
case 0xe7800000: exp = 0xffffffcf; s = 1; break;
case 0xe7000000: exp = 0xffffffd0; s = 1; break;
case 0xe6800000: exp = 0xffffffd1; s = 1; break;
case 0xe6000000: exp = 0xffffffd2; s = 1; break;
case 0xe5800000: exp = 0xffffffd3; s = 1; break;
case 0xe5000000: exp = 0xffffffd4; s = 1; break;
case 0xe4800000: exp = 0xffffffd5; s = 1; break;
case 0xe4000000: exp = 0xffffffd6; s = 1; break;
case 0xe3800000: exp = 0xffffffd7; s = 1; break;
case 0xe3000000: exp = 0xffffffd8; s = 1; break;
case 0xe2800000: exp = 0xffffffd9; s = 1; break;
case 0xe2000000: exp = 0xffffffda; s = 1; break;
case 0xe1800000: exp = 0xffffffdb; s = 1; break;
case 0xe1000000: exp = 0xffffffdc; s = 1; break;
case 0xe0800000: exp = 0xffffffdd; s = 1; break;
case 0xe0000000: exp = 0xffffffde; s = 1; break;
case 0xdf800000: exp = 0xffffffdf; s = 1; break;
case 0xdf000000: exp = 0xffffffe0; s = 1; break;
case 0xde800000: exp = 0xffffffe1; s = 1; break;
case 0xde000000: exp = 0xffffffe2; s = 1; break;
case 0xdd800000: exp = 0xffffffe3; s = 1; break;
case 0xdd000000: exp = 0xffffffe4; s = 1; break;
case 0xdc800000: exp = 0xffffffe5; s = 1; break;
case 0xdc000000: exp = 0xffffffe6; s = 1; break;
case 0xdb800000: exp = 0xffffffe7; s = 1; break;
case 0xdb000000: exp = 0xffffffe8; s = 1; break;
case 0xda800000: exp = 0xffffffe9; s = 1; break;
case 0xda000000: exp = 0xffffffea; s = 1; break;
case 0xd9800000: exp = 0xffffffeb; s = 1; break;
case 0xd9000000: exp = 0xffffffec; s = 1; break;
case 0xd8800000: exp = 0xffffffed; s = 1; break;
case 0xd8000000: exp = 0xffffffee; s = 1; break;
case 0xd7800000: exp = 0xffffffef; s = 1; break;
case 0xd7000000: exp = 0xfffffff0; s = 1; break;
case 0xd6800000: exp = 0xfffffff1; s = 1; break;
case 0xd6000000: exp = 0xfffffff2; s = 1; break;
case 0xd5800000: exp = 0xfffffff3; s = 1; break;
case 0xd5000000: exp = 0xfffffff4; s = 1; break;
case 0xd4800000: exp = 0xfffffff5; s = 1; break;
case 0xd4000000: exp = 0xfffffff6; s = 1; break;
case 0xd3800000: exp = 0xfffffff7; s = 1; break;
case 0xd3000000: exp = 0xfffffff8; s = 1; break;
case 0xd2800000: exp = 0xfffffff9; s = 1; break;
case 0xd2000000: exp = 0xfffffffa; s = 1; break;
case 0xd1800000: exp = 0xfffffffb; s = 1; break;
case 0xd1000000: exp = 0xfffffffc; s = 1; break;
case 0xd0800000: exp = 0xfffffffd; s = 1; break;
case 0xd0000000: exp = 0xfffffffe; s = 1; break;
case 0xcf800000: exp = 0xffffffff; s = 1; break;
case 0xcf000000: exp = 0x0; s = 1; break;
case 0xce800000: exp = 0x1; s = 1; break;
case 0xce000000: exp = 0x2; s = 1; break;
case 0xcd800000: exp = 0x3; s = 1; break;