Skip to content

Commit 9fc2b93

Browse files
committedMar 24, 2015
Fix endianness inconsistency with PcgRandom::bytes()
1 parent a423202 commit 9fc2b93

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed
 

Diff for: ‎src/noise.cpp

+10-25
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,20 @@ s32 PcgRandom::range(s32 min, s32 max)
123123

124124
void PcgRandom::bytes(void *out, size_t len)
125125
{
126-
u32 r;
127126
u8 *outb = (u8 *)out;
127+
int bytes_left = 0;
128+
u32 r;
128129

129-
size_t len_alignment = (uintptr_t)out % sizeof(u32);
130-
if (len_alignment) {
131-
len -= len_alignment;
132-
r = next();
133-
while (len_alignment--) {
134-
*outb = r & 0xFF;
135-
outb++;
136-
r >>= 8;
130+
while (len--) {
131+
if (bytes_left == 0) {
132+
bytes_left = sizeof(u32);
133+
r = next();
137134
}
138-
}
139135

140-
size_t len_dwords = len / sizeof(u32);
141-
while (len_dwords--) {
142-
r = next();
143-
*(u32 *)outb = next();
144-
outb += sizeof(u32);
145-
}
146-
147-
size_t len_remaining = len % sizeof(u32);
148-
if (len_remaining) {
149-
r = next();
150-
while (len_remaining--) {
151-
*outb = r & 0xFF;
152-
outb++;
153-
r >>= 8;
154-
}
136+
*outb = r & 0xFF;
137+
outb++;
138+
bytes_left--;
139+
r >>= 8;
155140
}
156141
}
157142

0 commit comments

Comments
 (0)
Please sign in to comment.