Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(27)

Delta Between Two Patch Sets: src/cmd/gc/bv.c

Issue 9223046: code review 9223046: cmd/5l, cmd/6l, cmd/8l, cmd/gc, runtime: generate and u... (Closed)
Left Patch Set: diff -r bbe324079abe https://code.google.com/p/go/ Created 11 years, 11 months ago
Right Patch Set: diff -r 81ccdb178fd7 https://code.google.com/p/go/ Created 11 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/cmd/8l/optab.c ('k') | src/cmd/gc/go.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2013 The Go Authors. All rights reserved. 1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 #include <u.h> 5 #include <u.h>
6 #include <libc.h> 6 #include <libc.h>
7 #include "go.h" 7 #include "go.h"
8 8
9 enum { 9 enum {
10 WORDSIZE = sizeof(uint32), 10 WORDSIZE = sizeof(uint32),
11 » WORDSIZELOG2 = 2, 11 » WORDBITS = 32,
12 » WORDBITS = WORDSIZE << WORDSIZELOG2,
13 }; 12 };
14 13
15 static uintptr 14 uintptr
16 size(uintptr n) 15 bvsize(uintptr n)
17 { 16 {
18 return ((n + WORDBITS - 1) / WORDBITS) * WORDSIZE; 17 return ((n + WORDBITS - 1) / WORDBITS) * WORDSIZE;
19 } 18 }
20 19
21 Bvec* 20 Bvec*
22 bvalloc(int32 n) 21 bvalloc(int32 n)
23 { 22 {
24 Bvec *bv; 23 Bvec *bv;
25 uintptr nbytes; 24 uintptr nbytes;
26 25
27 if(n < 0) 26 if(n < 0)
28 » » fatal("initial size is negative\n"); 27 » » fatal("bvalloc: initial size is negative\n");
29 » nbytes = sizeof(Bvec) + size(n); 28 » nbytes = sizeof(Bvec) + bvsize(n);
30 bv = malloc(nbytes); 29 bv = malloc(nbytes);
31 if(bv == nil) 30 if(bv == nil)
32 » » fatal("malloc failed\n"); 31 » » fatal("bvalloc: malloc failed\n");
33 memset(bv, 0, nbytes); 32 memset(bv, 0, nbytes);
34 bv->n = n; 33 bv->n = n;
35 return bv; 34 return bv;
36 } 35 }
37 36
38 void 37 void
39 bvset(Bvec *bv, int32 i) 38 bvset(Bvec *bv, int32 i)
40 { 39 {
41 uint32 mask; 40 uint32 mask;
42 41
43 if(i < 0 || i >= bv->n) 42 if(i < 0 || i >= bv->n)
44 » » fatal("index %d is out of bounds with length %d\n", i, bv->n); 43 » » fatal("bvset: index %d is out of bounds with length %d\n", i, bv ->n);
45 mask = 1 << (i % WORDBITS); 44 mask = 1 << (i % WORDBITS);
46 bv->b[i / WORDBITS] |= mask; 45 bv->b[i / WORDBITS] |= mask;
47 } 46 }
48 47
49 void 48 void
50 bvres(Bvec *bv, int32 i) 49 bvres(Bvec *bv, int32 i)
51 { 50 {
52 uint32 mask; 51 uint32 mask;
53 52
54 if(i < 0 || i >= bv->n) 53 if(i < 0 || i >= bv->n)
55 » » fatal("index %d is out of bounds with length %d\n", i, bv->n); 54 » » fatal("bvres: index %d is out of bounds with length %d\n", i, bv ->n);
56 mask = ~(1 << (i % WORDBITS)); 55 mask = ~(1 << (i % WORDBITS));
57 bv->b[i / WORDBITS] &= mask; 56 bv->b[i / WORDBITS] &= mask;
58 } 57 }
59 58
60 int 59 int
61 bvget(Bvec *bv, int32 i) 60 bvget(Bvec *bv, int32 i)
62 { 61 {
63 uint32 mask, word; 62 uint32 mask, word;
64 63
65 if(i < 0 || i >= bv->n) 64 if(i < 0 || i >= bv->n)
66 » » fatal("index %d is out of bounds with length %d\n", i, bv->n); 65 » » fatal("bvget: index %d is out of bounds with length %d\n", i, bv ->n);
67 mask = 1 << (i % WORDBITS); 66 mask = 1 << (i % WORDBITS);
68 word = bv->b[i / WORDBITS] & mask; 67 word = bv->b[i / WORDBITS] & mask;
69 » return (word ? 1 : 0); 68 » return word ? 1 : 0;
70 } 69 }
71 70
72 static int32 71 int
73 popcnt(uint32 x) 72 bvisempty(Bvec *bv)
74 { 73 {
75 » x = x - ((x >> 1) & 0x55555555); 74 » int32 i;
76 » x = (x & 0x33333333) + ((x >> 2) & 0x33333333); 75
77 » x = (x + (x >> 4)) & 0x0f0f0f0f; 76 » for(i = 0; i < bv->n; i += WORDBITS)
78 » x = x + (x >> 8); 77 » » if(bv->b[i / WORDBITS] != 0)
79 » x = x + (x >> 16); 78 » » » return 0;
80 » return x & 0x0000003f; 79 » return 1;
81 } 80 }
82 81
83 int32 82 int bvcmp(Bvec *bv1, Bvec *bv2)
84 bvpopcnt(Bvec *bv)
85 { 83 {
86 » int32 i, sum; 84 » int32 i;
87 85
88 » sum = 0; 86 » if(bv1->n != bv2->n) {
89 » for(i = 0; i < bv->n; i += WORDBITS) 87 » » fatal("bvcmp: size %d != %d\n", bv1->n, bv2->n);
90 » » sum += popcnt(bv->b[i / WORDBITS]); 88 » }
91 » return sum; 89 » for(i = 0; i < bv1->n; i += WORDBITS) {
90 » » if(bv1->b[i / WORDBITS] != bv2->b[i / WORDBITS]) {
91 » » » fatal("bvcmp: element %x != %x @ %d\n", bv1->b[i/WORDBIT S], bv2->b[i/WORDBITS], i/WORDBITS);
92 » » }
93 » }
94 » return 0;
92 } 95 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b