mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
refactor std to use for loop
This commit is contained in:
parent
fbbef14013
commit
4c50606b9d
@ -52,7 +52,7 @@ compromises backward compatibility.
|
|||||||
|
|
||||||
* Have a look in the examples/ folder to see some code examples.
|
* Have a look in the examples/ folder to see some code examples.
|
||||||
* Basic language features available such as loops, inline assembly,
|
* Basic language features available such as loops, inline assembly,
|
||||||
expressions, literals, functions, importing, structs, enums.
|
expressions, literals, functions, importing, structs, tagged unions.
|
||||||
* Linux x86_64 is supported.
|
* Linux x86_64 is supported.
|
||||||
* Building for the native target is supported.
|
* Building for the native target is supported.
|
||||||
* Optimized machine code that Zig produces is indistinguishable from
|
* Optimized machine code that Zig produces is indistinguishable from
|
||||||
|
|||||||
@ -1790,7 +1790,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
|
|||||||
LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
|
LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
|
||||||
LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");
|
LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");
|
||||||
|
|
||||||
LLVMValueRef array_val = gen_expr(g, node->data.for_expr.array_expr);
|
LLVMValueRef array_val = gen_array_base_ptr(g, node->data.for_expr.array_expr);
|
||||||
add_debug_source_node(g, node);
|
add_debug_source_node(g, node);
|
||||||
LLVMBuildStore(g->builder, LLVMConstNull(index_var->type->type_ref), index_ptr);
|
LLVMBuildStore(g->builder, LLVMConstNull(index_var->type->type_ref), index_ptr);
|
||||||
LLVMValueRef len_val;
|
LLVMValueRef len_val;
|
||||||
|
|||||||
@ -11,9 +11,10 @@ pub struct Rand {
|
|||||||
r.index = 0;
|
r.index = 0;
|
||||||
r.array[0] = seed;
|
r.array[0] = seed;
|
||||||
var i : @typeof(ARRAY_SIZE) = 1;
|
var i : @typeof(ARRAY_SIZE) = 1;
|
||||||
|
var prev_value: u64 = seed;
|
||||||
while (i < ARRAY_SIZE) {
|
while (i < ARRAY_SIZE) {
|
||||||
const prev_value : u64 = r.array[i - 1];
|
|
||||||
r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i);
|
r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i);
|
||||||
|
prev_value = r.array[i];
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,9 +68,8 @@ pub struct Rand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_numbers(r: &Rand) => {
|
fn generate_numbers(r: &Rand) => {
|
||||||
var i : @typeof(ARRAY_SIZE) = 0;
|
for (item, r.array, i) {
|
||||||
while (i < ARRAY_SIZE) {
|
const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
|
||||||
const y : u32 = (r.array[i] & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
|
|
||||||
const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
|
const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
|
||||||
r.array[i] = if ((y % 2) == 0) {
|
r.array[i] = if ((y % 2) == 0) {
|
||||||
untempered
|
untempered
|
||||||
@ -77,7 +77,6 @@ pub struct Rand {
|
|||||||
// y is odd
|
// y is odd
|
||||||
untempered ^ 0x9908b0df
|
untempered ^ 0x9908b0df
|
||||||
};
|
};
|
||||||
i += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -51,9 +51,7 @@ pub fn readline(buf: []u8, out_len: &usize) bool => {
|
|||||||
pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => {
|
pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => {
|
||||||
var x : u64 = 0;
|
var x : u64 = 0;
|
||||||
|
|
||||||
var i : @typeof(buf.len) = 0;
|
for (c, buf) {
|
||||||
while (i < buf.len) {
|
|
||||||
const c = buf[i];
|
|
||||||
const digit = char_to_digit(c);
|
const digit = char_to_digit(c);
|
||||||
|
|
||||||
if (digit > radix) {
|
if (digit > radix) {
|
||||||
@ -69,8 +67,6 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) bool => {
|
|||||||
if (@add_with_overflow(u64, x, digit, &x)) {
|
if (@add_with_overflow(u64, x, digit, &x)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = x;
|
*result = x;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user