Merge remote-tracking branch 'origin/master' into llvm7

This commit is contained in:
Andrew Kelley 2018-06-18 14:51:23 -04:00
commit c757984879
110 changed files with 1816 additions and 886 deletions

View File

@ -30,7 +30,7 @@ if(GIT_EXE)
message("WARNING: Tag does not match configured Zig version")
endif()
else()
set(ZIG_VERSION "${ZIG_VERSION_MAJOR}.${ZIG_VERSION_MINOR}.${ZIG_VERSION_PATCH}.${ZIG_GIT_REV}")
set(ZIG_VERSION "${ZIG_VERSION}+${ZIG_GIT_REV}")
endif()
endif()
message("Configuring zig version ${ZIG_VERSION}")
@ -438,6 +438,7 @@ set(ZIG_STD_FILES
"debug/failing_allocator.zig"
"debug/index.zig"
"dwarf.zig"
"dynamic_library.zig"
"elf.zig"
"empty.zig"
"event.zig"

View File

@ -370,17 +370,17 @@ pub fn main() void {
<tr>
<td><code>f32</code></td>
<td><code>float</code></td>
<td>32-bit floating point (23-bit mantissa)</td>
<td>32-bit floating point (23-bit mantissa) IEEE-754-2008 binary32</td>
</tr>
<tr>
<td><code>f64</code></td>
<td><code>double</code></td>
<td>64-bit floating point (52-bit mantissa)</td>
<td>64-bit floating point (52-bit mantissa) IEEE-754-2008 binary64</td>
</tr>
<tr>
<td><code>f128</code></td>
<td>(none)</td>
<td>128-bit floating point (112-bit mantissa)</td>
<td>128-bit floating point (112-bit mantissa) IEEE-754-2008 binary128</td>
</tr>
<tr>
<td><code>bool</code></td>
@ -407,6 +407,16 @@ pub fn main() void {
<td>(none)</td>
<td>an error code</td>
</tr>
<tr>
<td><code>comptime_int</code></td>
<td>(none)</td>
<td>Only allowed for {#link|comptime#}-known values. The type of integer literals.</td>
</tr>
<tr>
<td><code>comptime_float</code></td>
<td>(none)</td>
<td>Only allowed for {#link|comptime#}-known values. The type of float literals.</td>
</tr>
</table>
</div>
{#see_also|Integers|Floats|void|Errors#}
@ -642,7 +652,18 @@ fn divide(a: i32, b: i32) i32 {
{#header_close#}
{#header_close#}
{#header_open|Floats#}
<p>Zig has the following floating point types:</p>
<ul>
<li><code>f32</code> - IEEE-754-2008 binary32</li>
<li><code>f64</code> - IEEE-754-2008 binary64</li>
<li><code>f128</code> - IEEE-754-2008 binary128</li>
<li><code>c_longdouble</code> - matches <code>long double</code> for the target C ABI</li>
</ul>
{#header_open|Float Literals#}
<p>
Float literals have type <code>comptime_float</code> which is guaranteed to hold at least all possible values
that the largest other floating point type can hold. Float literals implicitly cast to any other type.
</p>
{#code_begin|syntax#}
const floating_point = 123.0E+77;
const another_float = 123.0;
@ -1334,7 +1355,7 @@ var some_integers: [100]i32 = undefined;
test "modify an array" {
for (some_integers) |*item, i| {
item.* = i32(i);
item.* = @intCast(i32, i);
}
assert(some_integers[10] == 10);
assert(some_integers[99] == 99);
@ -1376,8 +1397,8 @@ var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (initial_value) |*pt, i| {
pt.* = Point{
.x = i32(i),
.y = i32(i) * 2,
.x = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
};
}
break :init initial_value;
@ -1630,7 +1651,7 @@ fn foo(bytes: []u8) u32 {
<pre><code class="zig">@ptrCast(*u32, f32(12.34)).*</code></pre>
<p>Instead, use {#link|@bitCast#}:
<pre><code class="zig">@bitCast(u32, f32(12.34))</code></pre>
<p>As an added benefit, the <code>@bitcast</code> version works at compile-time.</p>
<p>As an added benefit, the <code>@bitCast</code> version works at compile-time.</p>
{#see_also|Slices|Memory#}
{#header_close#}
{#header_close#}
@ -2389,7 +2410,7 @@ test "for basics" {
var sum2: i32 = 0;
for (items) |value, i| {
assert(@typeOf(i) == usize);
sum2 += i32(i);
sum2 += @intCast(i32, i);
}
assert(sum2 == 10);
}
@ -2797,39 +2818,30 @@ fn foo() void { }
{#code_end#}
{#header_open|Pass-by-value Parameters#}
<p>
In Zig, structs, unions, and enums with payloads cannot be passed by value
to a function.
</p>
{#code_begin|test_err|not copyable; cannot pass by value#}
const Foo = struct {
x: i32,
};
fn bar(foo: Foo) void {}
test "pass aggregate type by value to function" {
bar(Foo {.x = 12,});
}
{#code_end#}
<p>
Instead, one must use <code>*const</code>. Zig allows implicitly casting something
to a const pointer to it:
In Zig, structs, unions, and enums with payloads can be passed directly to a function:
</p>
{#code_begin|test#}
const Foo = struct {
const Point = struct {
x: i32,
y: i32,
};
fn bar(foo: *const Foo) void {}
fn foo(point: Point) i32 {
return point.x + point.y;
}
test "implicitly cast to const pointer" {
bar(Foo {.x = 12,});
const assert = @import("std").debug.assert;
test "pass aggregate type by non-copy value to function" {
assert(foo(Point{ .x = 1, .y = 2 }) == 3);
}
{#code_end#}
<p>
However,
the C ABI does allow passing structs and unions by value. So functions which
use the C calling convention may pass structs and unions by value.
In this case, the value may be passed by reference, or by value, whichever way
Zig decides will be faster.
</p>
<p>
For extern functions, Zig follows the C ABI for passing structs and unions by value.
</p>
{#header_close#}
{#header_open|Function Reflection#}
@ -3539,13 +3551,91 @@ const optional_value: ?i32 = null;
<p>TODO: ptrcast builtin</p>
<p>TODO: explain number literals vs concrete types</p>
{#header_close#}
{#header_open|void#}
<p>TODO: assigning void has no codegen</p>
<p>TODO: hashmap with void becomes a set</p>
<p>TODO: difference between c_void and void</p>
<p>TODO: void is the default return value of functions</p>
<p>TODO: functions require assigning the return value</p>
<p>
<code>void</code> represents a type that has no value. Code that makes use of void values is
not included in the final generated code:
</p>
{#code_begin|syntax#}
export fn entry() void {
var x: void = {};
var y: void = {};
x = y;
}
{#code_end#}
<p>When this turns into LLVM IR, there is no code generated in the body of <code>entry</code>,
even in debug mode. For example, on x86_64:</p>
<pre><code>0000000000000010 &lt;entry&gt;:
10: 55 push %rbp
11: 48 89 e5 mov %rsp,%rbp
14: 5d pop %rbp
15: c3 retq </code></pre>
<p>These assembly instructions do not have any code associated with the void values -
they only perform the function call prologue and epilog.</p>
<p>
<code>void</code> can be useful for instantiating generic types. For example, given a
<code>Map(Key, Value)</code>, one can pass <code>void</code> for the <code>Value</code>
type to make it into a <code>Set</code>:
</p>
{#code_begin|test#}
const std = @import("std");
const assert = std.debug.assert;
test "turn HashMap into a set with void" {
var map = std.HashMap(i32, void, hash_i32, eql_i32).init(std.debug.global_allocator);
defer map.deinit();
_ = try map.put(1, {});
_ = try map.put(2, {});
assert(map.contains(2));
assert(!map.contains(3));
_ = map.remove(2);
assert(!map.contains(2));
}
fn hash_i32(x: i32) u32 {
return @bitCast(u32, x);
}
fn eql_i32(a: i32, b: i32) bool {
return a == b;
}
{#code_end#}
<p>Note that this is different than using a dummy value for the hash map value.
By using <code>void</code> as the type of the value, the hash map entry type has no value field, and
thus the hash map takes up less space. Further, all the code that deals with storing and loading the
value is deleted, as seen above.
</p>
<p>
<code>void</code> is distinct from <code>c_void</code>, which is defined like this:
<code>pub const c_void = @OpaqueType();</code>.
<code>void</code> has a known size of 0 bytes, and <code>c_void</code> has an unknown, but non-zero, size.
</p>
<p>
Expressions of type <code>void</code> are the only ones whose value can be ignored. For example:
</p>
{#code_begin|test_err|expression value is ignored#}
test "ignoring expression value" {
foo();
}
fn foo() i32 {
return 1234;
}
{#code_end#}
<p>However, if the expression has type <code>void</code>:</p>
{#code_begin|test#}
test "ignoring expression value" {
foo();
}
fn foo() void {}
{#code_end#}
{#header_close#}
{#header_open|this#}
<p>TODO: example of this referring to Self struct</p>
<p>TODO: example of this referring to recursion function</p>
@ -4548,6 +4638,19 @@ comptime {
</p>
{#see_also|Alignment#}
{#header_close#}
{#header_open|@boolToInt#}
<pre><code class="zig">@boolToInt(value: bool) u1</code></pre>
<p>
Converts <code>true</code> to <code>u1(1)</code> and <code>false</code> to
<code>u1(0)</code>.
</p>
<p>
If the value is known at compile-time, the return type is <code>comptime_int</code>
instead of <code>u1</code>.
</p>
{#header_close#}
{#header_open|@cDefine#}
<pre><code class="zig">@cDefine(comptime name: []u8, value)</code></pre>
<p>
@ -4822,21 +4925,6 @@ test "main" {
Creates a symbol in the output object file.
</p>
{#header_close#}
{#header_open|@tagName#}
<pre><code class="zig">@tagName(value: var) []const u8</code></pre>
<p>
Converts an enum value or union value to a slice of bytes representing the name.
</p>
{#header_close#}
{#header_open|@TagType#}
<pre><code class="zig">@TagType(T: type) type</code></pre>
<p>
For an enum, returns the integer type that is used to store the enumeration value.
</p>
<p>
For a union, returns the enum type that is used to store the tag value.
</p>
{#header_close#}
{#header_open|@errorName#}
<pre><code class="zig">@errorName(err: error) []u8</code></pre>
<p>
@ -4871,6 +4959,12 @@ test "main" {
</p>
{#see_also|Compile Variables#}
{#header_close#}
{#header_open|@field#}
<pre><code class="zig">@field(lhs: var, comptime field_name: []const u8) (field)</code></pre>
<p>Preforms field access equivalent to <code>lhs.-&gtfield_name-&lt</code>.</p>
{#header_close#}
{#header_open|@fieldParentPtr#}
<pre><code class="zig">@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8,
field_ptr: *T) *ParentType</code></pre>
@ -4878,6 +4972,23 @@ test "main" {
Given a pointer to a field, returns the base pointer of a struct.
</p>
{#header_close#}
{#header_open|@floatCast#}
<pre><code class="zig">@floatCast(comptime DestType: type, value: var) DestType</code></pre>
<p>
Convert from one float type to another. This cast is safe, but may cause the
numeric value to lose precision.
</p>
{#header_close#}
{#header_open|@floatToInt#}
<pre><code class="zig">@floatToInt(comptime DestType: type, float: var) DestType</code></pre>
<p>
Converts the integer part of a floating point number to the destination type.
To convert the other way, use {#link|@intToFloat#}. This cast is always safe.
</p>
{#header_close#}
{#header_open|@frameAddress#}
<pre><code class="zig">@frameAddress()</code></pre>
<p>
@ -4932,12 +5043,30 @@ fn add(a: i32, b: i32) i32 { return a + b; }
</p>
{#see_also|@noInlineCall#}
{#header_close#}
{#header_open|@intCast#}
<pre><code class="zig">@intCast(comptime DestType: type, int: var) DestType</code></pre>
<p>
Converts an integer to another integer while keeping the same numerical value.
Attempting to convert a number which is out of range of the destination type results in
{#link|Undefined Behavior#}.
</p>
{#header_close#}
{#header_open|@intToFloat#}
<pre><code class="zig">@intToFloat(comptime DestType: type, int: var) DestType</code></pre>
<p>
Converts an integer to the closest floating point representation. To convert the other way, use {#link|@floatToInt#}. This cast is always safe.
</p>
{#header_close#}
{#header_open|@intToPtr#}
<pre><code class="zig">@intToPtr(comptime DestType: type, int: usize) DestType</code></pre>
<p>
Converts an integer to a pointer. To convert the other way, use {#link|@ptrToInt#}.
</p>
{#header_close#}
{#header_open|@IntType#}
<pre><code class="zig">@IntType(comptime is_signed: bool, comptime bit_count: u8) type</code></pre>
<p>
@ -4975,10 +5104,6 @@ fn add(a: i32, b: i32) i32 { return a + b; }
It does not include functions, variables, or constants.
</p>
{#header_close#}
{#header_open|@field#}
<pre><code class="zig">@field(lhs: var, comptime field_name: []const u8) (field)</code></pre>
<p>Preforms field access equivalent to <code>lhs.-&gtfield_name-&lt</code>.</p>
{#header_close#}
{#header_open|@memberType#}
<pre><code class="zig">@memberType(comptime T: type, comptime index: usize) type</code></pre>
<p>Returns the field type of a struct or union.</p>
@ -5358,6 +5483,21 @@ pub const FloatMode = enum {
If no overflow or underflow occurs, returns <code>false</code>.
</p>
{#header_close#}
{#header_open|@tagName#}
<pre><code class="zig">@tagName(value: var) []const u8</code></pre>
<p>
Converts an enum value or union value to a slice of bytes representing the name.
</p>
{#header_close#}
{#header_open|@TagType#}
<pre><code class="zig">@TagType(T: type) type</code></pre>
<p>
For an enum, returns the integer type that is used to store the enumeration value.
</p>
<p>
For a union, returns the enum type that is used to store the tag value.
</p>
{#header_close#}
{#header_open|@truncate#}
<pre><code class="zig">@truncate(comptime T: type, integer) T</code></pre>
<p>
@ -5718,7 +5858,7 @@ comptime {
{#code_begin|test_err|attempt to cast negative value to unsigned integer#}
comptime {
const value: i32 = -1;
const unsigned = u32(value);
const unsigned = @intCast(u32, value);
}
{#code_end#}
<p>At runtime crashes with the message <code>attempt to cast negative value to unsigned integer</code> and a stack trace.</p>
@ -5732,7 +5872,7 @@ comptime {
{#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#}
comptime {
const spartan_count: u16 = 300;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
}
{#code_end#}
<p>At runtime crashes with the message <code>integer cast truncated bits</code> and a stack trace.</p>
@ -6653,7 +6793,7 @@ hljs.registerLanguage("zig", function(t) {
a = t.IR + "\\s*\\(",
c = {
keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong resume cancel await async orelse",
built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall",
built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall",
literal: "true false null undefined"
},
n = [e, t.CLCM, t.CBCM, s, r];

View File

@ -8,7 +8,7 @@ const c = @cImport({
const msg = c"Hello, world!\n";
export fn main(argc: c_int, argv: **u8) c_int {
if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;
return 0;
}

View File

@ -700,6 +700,36 @@ const args_fmt_spec = []Flag{
}),
};
const Fmt = struct {
seen: std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8),
queue: std.LinkedList([]const u8),
any_error: bool,
// file_path must outlive Fmt
fn addToQueue(self: *Fmt, file_path: []const u8) !void {
const new_node = try self.seen.allocator.construct(std.LinkedList([]const u8).Node{
.prev = undefined,
.next = undefined,
.data = file_path,
});
if (try self.seen.put(file_path, {})) |_| return;
self.queue.append(new_node);
}
fn addDirToQueue(self: *Fmt, file_path: []const u8) !void {
var dir = try std.os.Dir.open(self.seen.allocator, file_path);
defer dir.close();
while (try dir.next()) |entry| {
if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
const full_path = try os.path.join(self.seen.allocator, file_path, entry.name);
try self.addToQueue(full_path);
}
}
}
};
fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
var flags = try Args.parse(allocator, args_fmt_spec, args);
defer flags.deinit();
@ -728,21 +758,38 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
}
};
var fmt_errors = false;
var fmt = Fmt{
.seen = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator),
.queue = std.LinkedList([]const u8).init(),
.any_error = false,
};
for (flags.positionals.toSliceConst()) |file_path| {
try fmt.addToQueue(file_path);
}
while (fmt.queue.popFirst()) |node| {
const file_path = node.data;
var file = try os.File.openRead(allocator, file_path);
defer file.close();
const source_code = io.readFileAlloc(allocator, file_path) catch |err| {
try stderr.print("unable to open '{}': {}\n", file_path, err);
fmt_errors = true;
continue;
const source_code = io.readFileAlloc(allocator, file_path) catch |err| switch (err) {
error.IsDir => {
try fmt.addDirToQueue(file_path);
continue;
},
else => {
try stderr.print("unable to open '{}': {}\n", file_path, err);
fmt.any_error = true;
continue;
},
};
defer allocator.free(source_code);
var tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing file '{}': {}\n", file_path, err);
fmt_errors = true;
fmt.any_error = true;
continue;
};
defer tree.deinit();
@ -755,7 +802,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
try errmsg.printToFile(&stderr_file, msg, color);
}
if (tree.errors.len != 0) {
fmt_errors = true;
fmt.any_error = true;
continue;
}
@ -769,7 +816,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
}
}
if (fmt_errors) {
if (fmt.any_error) {
os.exit(1);
}
}

View File

@ -1357,6 +1357,11 @@ enum BuiltinFnId {
BuiltinFnIdMod,
BuiltinFnIdSqrt,
BuiltinFnIdTruncate,
BuiltinFnIdIntCast,
BuiltinFnIdFloatCast,
BuiltinFnIdIntToFloat,
BuiltinFnIdFloatToInt,
BuiltinFnIdBoolToInt,
BuiltinFnIdIntType,
BuiltinFnIdSetCold,
BuiltinFnIdSetRuntimeSafety,
@ -2038,6 +2043,11 @@ enum IrInstructionId {
IrInstructionIdCmpxchg,
IrInstructionIdFence,
IrInstructionIdTruncate,
IrInstructionIdIntCast,
IrInstructionIdFloatCast,
IrInstructionIdIntToFloat,
IrInstructionIdFloatToInt,
IrInstructionIdBoolToInt,
IrInstructionIdIntType,
IrInstructionIdBoolNot,
IrInstructionIdMemset,
@ -2630,6 +2640,40 @@ struct IrInstructionTruncate {
IrInstruction *target;
};
struct IrInstructionIntCast {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionFloatCast {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionIntToFloat {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionFloatToInt {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionBoolToInt {
IrInstruction base;
IrInstruction *target;
};
struct IrInstructionIntType {
IrInstruction base;

View File

@ -1022,6 +1022,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
ensure_complete_type(g, fn_type_id->return_type);
if (type_is_invalid(fn_type_id->return_type))
return g->builtin_types.entry_invalid;
assert(fn_type_id->return_type->id != TypeTableEntryIdOpaque);
} else {
zig_panic("TODO implement inferred return types https://github.com/ziglang/zig/issues/447");
}
@ -1135,7 +1136,10 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
gen_param_info->src_index = i;
gen_param_info->gen_index = SIZE_MAX;
type_ensure_zero_bits_known(g, type_entry);
ensure_complete_type(g, type_entry);
if (type_is_invalid(type_entry))
return g->builtin_types.entry_invalid;
if (type_has_bits(type_entry)) {
TypeTableEntry *gen_type;
if (handle_is_ptr(type_entry)) {
@ -1546,12 +1550,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
case TypeTableEntryIdPromise:
ensure_complete_type(g, type_entry);
if (calling_convention_allows_zig_types(fn_type_id.cc) && !type_is_copyable(g, type_entry)) {
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("type '%s' is not copyable; cannot pass by value", buf_ptr(&type_entry->name)));
return g->builtin_types.entry_invalid;
}
break;
}
FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];

View File

@ -326,17 +326,6 @@ static void addLLVMArgAttr(LLVMValueRef fn_val, unsigned param_index, const char
return addLLVMAttr(fn_val, param_index + 1, attr_name);
}
//static void addLLVMArgAttrInt(LLVMValueRef fn_val, unsigned param_index, const char *attr_name, uint64_t attr_val) {
// return addLLVMAttrInt(fn_val, param_index + 1, attr_name, attr_val);
//}
static void addLLVMCallsiteAttr(LLVMValueRef call_instr, unsigned param_index, const char *attr_name) {
unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name, strlen(attr_name));
assert(kind_id != 0);
LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(LLVMGetGlobalContext(), kind_id, 0);
LLVMAddCallSiteAttribute(call_instr, param_index + 1, llvm_attr);
}
static bool is_symbol_available(CodeGen *g, Buf *name) {
return g->exported_symbol_names.maybe_get(name) == nullptr && g->external_prototypes.maybe_get(name) == nullptr;
}
@ -585,11 +574,6 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) {
if (param_type->id == TypeTableEntryIdPointer) {
addLLVMArgAttr(fn_table_entry->llvm_value, (unsigned)gen_index, "nonnull");
}
// Note: byval is disabled on windows due to an LLVM bug:
// https://github.com/ziglang/zig/issues/536
if (is_byval && g->zig_target.os != OsWindows) {
addLLVMArgAttr(fn_table_entry->llvm_value, (unsigned)gen_index, "byval");
}
}
uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, fn_table_entry);
@ -3053,15 +3037,6 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
}
for (size_t param_i = 0; param_i < fn_type_id->param_count; param_i += 1) {
FnGenParamInfo *gen_info = &fn_type->data.fn.gen_param_info[param_i];
// Note: byval is disabled on windows due to an LLVM bug:
// https://github.com/ziglang/zig/issues/536
if (gen_info->is_byval && g->zig_target.os != OsWindows) {
addLLVMCallsiteAttr(result, (unsigned)gen_info->gen_index, "byval");
}
}
if (instruction->is_async) {
LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
LLVMBuildStore(g->builder, result, payload_ptr);
@ -4658,6 +4633,11 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdPromiseResultType:
case IrInstructionIdAwaitBookkeeping:
case IrInstructionIdAddImplicitReturnType:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
case IrInstructionIdBoolToInt:
zig_unreachable();
case IrInstructionIdReturn:
@ -6246,6 +6226,11 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6);
create_builtin_fn(g, BuiltinFnIdFence, "fence", 1);
create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2);
create_builtin_fn(g, BuiltinFnIdIntCast, "intCast", 2);
create_builtin_fn(g, BuiltinFnIdFloatCast, "floatCast", 2);
create_builtin_fn(g, BuiltinFnIdIntToFloat, "intToFloat", 2);
create_builtin_fn(g, BuiltinFnIdFloatToInt, "floatToInt", 2);
create_builtin_fn(g, BuiltinFnIdBoolToInt, "boolToInt", 1);
create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1);
create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
@ -6683,7 +6668,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
int err;
Buf *abs_full_path = buf_alloc();
if ((err = os_path_real(builtin_zig_path, abs_full_path))) {
fprintf(stderr, "unable to open '%s': %s", buf_ptr(builtin_zig_path), err_str(err));
fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
exit(1);
}
@ -6851,11 +6836,11 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
Buf *abs_full_path = buf_alloc();
int err;
if ((err = os_path_real(&path_to_code_src, abs_full_path))) {
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
}
Buf *import_code = buf_alloc();
if ((err = os_fetch_file_path(abs_full_path, import_code, false))) {
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
}
return add_source_file(g, package, abs_full_path, import_code);
@ -6939,13 +6924,13 @@ static void gen_root_source(CodeGen *g) {
Buf *abs_full_path = buf_alloc();
int err;
if ((err = os_path_real(rel_full_path, abs_full_path))) {
fprintf(stderr, "unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err));
fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));
exit(1);
}
Buf *source_code = buf_alloc();
if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {
fprintf(stderr, "unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err));
fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));
exit(1);
}
@ -7289,7 +7274,7 @@ static void gen_h_file(CodeGen *g) {
FILE *out_h = fopen(buf_ptr(g->out_h_path), "wb");
if (!out_h)
zig_panic("unable to open %s: %s", buf_ptr(g->out_h_path), strerror(errno));
zig_panic("unable to open %s: %s\n", buf_ptr(g->out_h_path), strerror(errno));
Buf *export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
buf_upcase(export_macro);

View File

@ -460,6 +460,26 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTruncate *) {
return IrInstructionIdTruncate;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntCast *) {
return IrInstructionIdIntCast;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatCast *) {
return IrInstructionIdFloatCast;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntToFloat *) {
return IrInstructionIdIntToFloat;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatToInt *) {
return IrInstructionIdFloatToInt;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionBoolToInt *) {
return IrInstructionIdBoolToInt;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntType *) {
return IrInstructionIdIntType;
}
@ -1899,10 +1919,57 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s
return &instruction->base;
}
static IrInstruction *ir_build_truncate_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *dest_type, IrInstruction *target) {
IrInstruction *new_instruction = ir_build_truncate(irb, old_instruction->scope, old_instruction->source_node, dest_type, target);
ir_link_new_instruction(new_instruction, old_instruction);
return new_instruction;
static IrInstruction *ir_build_int_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionIntCast *instruction = ir_build_instruction<IrInstructionIntCast>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_float_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionFloatCast *instruction = ir_build_instruction<IrInstructionFloatCast>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_int_to_float(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionIntToFloat *instruction = ir_build_instruction<IrInstructionIntToFloat>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_float_to_int(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionFloatToInt *instruction = ir_build_instruction<IrInstructionFloatToInt>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_bool_to_int(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target) {
IrInstructionBoolToInt *instruction = ir_build_instruction<IrInstructionBoolToInt>(irb, scope, source_node);
instruction->target = target;
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_int_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_signed, IrInstruction *bit_count) {
@ -3957,6 +4024,76 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, truncate, lval);
}
case BuiltinFnIdIntCast:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_int_cast(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdFloatCast:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_float_cast(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdIntToFloat:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_int_to_float(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdFloatToInt:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_float_to_int(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdBoolToInt:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
IrInstruction *result = ir_build_bool_to_int(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdIntType:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
@ -9941,22 +10078,32 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
}
// explicit cast from bool to int
// explicit widening conversion
if (wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdBool)
{
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);
}
// explicit widening or shortening cast
if ((wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdInt) ||
(wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdFloat))
actual_type->id == TypeTableEntryIdInt &&
wanted_type->data.integral.is_signed == actual_type->data.integral.is_signed &&
wanted_type->data.integral.bit_count >= actual_type->data.integral.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// small enough unsigned ints can get casted to large enough signed ints
if (wanted_type->id == TypeTableEntryIdInt && wanted_type->data.integral.is_signed &&
actual_type->id == TypeTableEntryIdInt && !actual_type->data.integral.is_signed &&
wanted_type->data.integral.bit_count > actual_type->data.integral.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// explicit float widening conversion
if (wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdFloat &&
wanted_type->data.floating.bit_count >= actual_type->data.floating.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// explicit error set cast
if (wanted_type->id == TypeTableEntryIdErrorSet &&
actual_type->id == TypeTableEntryIdErrorSet)
@ -9964,20 +10111,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ir_analyze_err_set_cast(ira, source_instr, value, wanted_type);
}
// explicit cast from int to float
if (wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdInt)
{
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpIntToFloat, false);
}
// explicit cast from float to int
if (wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdFloat)
{
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpFloatToInt, false);
}
// explicit cast from [N]T to []const T
if (is_slice(wanted_type) && actual_type->id == TypeTableEntryIdArray) {
TypeTableEntry *ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;
@ -10463,13 +10596,6 @@ static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, Typ
zig_unreachable();
}
static IrInstruction *ir_implicit_byval_const_ref_cast(IrAnalyze *ira, IrInstruction *inst) {
if (type_is_copyable(ira->codegen, inst->value.type))
return inst;
TypeTableEntry *const_ref_type = get_pointer_to_type(ira->codegen, inst->value.type, true);
return ir_implicit_cast(ira, inst, const_ref_type);
}
static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *ptr) {
TypeTableEntry *type_entry = ptr->value.type;
if (type_is_invalid(type_entry)) {
@ -12283,7 +12409,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
IrInstruction *casted_arg;
if (is_var_args) {
arg_part_of_generic_id = true;
casted_arg = ir_implicit_byval_const_ref_cast(ira, arg);
casted_arg = arg;
} else {
if (param_decl_node->data.param_decl.var_token == nullptr) {
AstNode *param_type_node = param_decl_node->data.param_decl.type;
@ -12296,7 +12422,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
return false;
} else {
arg_part_of_generic_id = true;
casted_arg = ir_implicit_byval_const_ref_cast(ira, arg);
casted_arg = arg;
}
}
@ -12515,9 +12641,18 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
size_t next_proto_i = 0;
if (first_arg_ptr) {
IrInstruction *first_arg;
assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
bool first_arg_known_bare = false;
if (fn_type_id->next_param_index >= 1) {
TypeTableEntry *param_type = fn_type_id->param_info[next_proto_i].type;
if (type_is_invalid(param_type))
return ira->codegen->builtin_types.entry_invalid;
first_arg_known_bare = param_type->id != TypeTableEntryIdPointer;
}
IrInstruction *first_arg;
if (!first_arg_known_bare && handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
first_arg = first_arg_ptr;
} else {
first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
@ -12667,9 +12802,18 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
size_t next_proto_i = 0;
if (first_arg_ptr) {
IrInstruction *first_arg;
assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
bool first_arg_known_bare = false;
if (fn_type_id->next_param_index >= 1) {
TypeTableEntry *param_type = fn_type_id->param_info[next_proto_i].type;
if (type_is_invalid(param_type))
return ira->codegen->builtin_types.entry_invalid;
first_arg_known_bare = param_type->id != TypeTableEntryIdPointer;
}
IrInstruction *first_arg;
if (!first_arg_known_bare && handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
first_arg = first_arg_ptr;
} else {
first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
@ -12802,10 +12946,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
return ira->codegen->builtin_types.entry_invalid;
}
if (inst_fn_type_id.async_allocator_type == nullptr) {
IrInstruction *casted_inst = ir_implicit_byval_const_ref_cast(ira, uncasted_async_allocator_inst);
if (type_is_invalid(casted_inst->value.type))
return ira->codegen->builtin_types.entry_invalid;
inst_fn_type_id.async_allocator_type = casted_inst->value.type;
inst_fn_type_id.async_allocator_type = uncasted_async_allocator_inst->value.type;
}
async_allocator_inst = ir_implicit_cast(ira, uncasted_async_allocator_inst, inst_fn_type_id.async_allocator_type);
if (type_is_invalid(async_allocator_inst->value.type))
@ -12866,9 +13007,16 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
IrInstruction **casted_args = allocate<IrInstruction *>(call_param_count);
size_t next_arg_index = 0;
if (first_arg_ptr) {
IrInstruction *first_arg;
assert(first_arg_ptr->value.type->id == TypeTableEntryIdPointer);
if (handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type)) {
TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type;
if (type_is_invalid(param_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *first_arg;
if (param_type->id == TypeTableEntryIdPointer &&
handle_is_ptr(first_arg_ptr->value.type->data.pointer.child_type))
{
first_arg = first_arg_ptr;
} else {
first_arg = ir_get_deref(ira, first_arg_ptr, first_arg_ptr);
@ -12876,10 +13024,6 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
return ira->codegen->builtin_types.entry_invalid;
}
TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type;
if (type_is_invalid(param_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *casted_arg = ir_implicit_cast(ira, first_arg, param_type);
if (type_is_invalid(casted_arg->value.type))
return ira->codegen->builtin_types.entry_invalid;
@ -17354,10 +17498,162 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
return dest_type;
}
ir_build_truncate_from(&ira->new_irb, &instruction->base, dest_type_value, target);
IrInstruction *new_instruction = ir_build_truncate(&ira->new_irb, instruction->base.scope,
instruction->base.source_node, dest_type_value, target);
ir_link_new_instruction(new_instruction, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstructionIntCast *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
if (dest_type->id != TypeTableEntryIdInt) {
ir_add_error(ira, instruction->dest_type, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id == TypeTableEntryIdComptimeInt) {
if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type,
CastOpNumLitToConcrete, false);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
} else {
return ira->codegen->builtin_types.entry_invalid;
}
}
if (target->value.type->id != TypeTableEntryIdInt) {
ir_add_error(ira, instruction->target, buf_sprintf("expected integer type, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstructionFloatCast *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
if (dest_type->id != TypeTableEntryIdFloat) {
ir_add_error(ira, instruction->dest_type,
buf_sprintf("expected float type, found '%s'", buf_ptr(&dest_type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id == TypeTableEntryIdComptimeInt ||
target->value.type->id == TypeTableEntryIdComptimeFloat)
{
if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
CastOp op;
if (target->value.type->id == TypeTableEntryIdComptimeInt) {
op = CastOpIntToFloat;
} else {
op = CastOpNumLitToConcrete;
}
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, op, false);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
} else {
return ira->codegen->builtin_types.entry_invalid;
}
}
if (target->value.type->id != TypeTableEntryIdFloat) {
ir_add_error(ira, instruction->target, buf_sprintf("expected float type, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInstructionIntToFloat *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id != TypeTableEntryIdInt && target->value.type->id != TypeTableEntryIdComptimeInt) {
ir_add_error(ira, instruction->target, buf_sprintf("expected int type, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstructionFloatToInt *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpFloatToInt, false);
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_bool_to_int(IrAnalyze *ira, IrInstructionBoolToInt *instruction) {
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id != TypeTableEntryIdBool) {
ir_add_error(ira, instruction->target, buf_sprintf("expected bool, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
if (instr_is_comptime(target)) {
bool is_true;
if (!ir_resolve_bool(ira, target, &is_true))
return ira->codegen->builtin_types.entry_invalid;
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
bigint_init_unsigned(&out_val->data.x_bigint, is_true ? 1 : 0);
return ira->codegen->builtin_types.entry_num_lit_int;
}
TypeTableEntry *u1_type = get_int_type(ira->codegen, false, 1);
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, u1_type, CastOpBoolToInt, false);
ir_link_new_instruction(result, &instruction->base);
return u1_type;
}
static TypeTableEntry *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstructionIntType *instruction) {
IrInstruction *is_signed_value = instruction->is_signed->other;
bool is_signed;
@ -18380,6 +18676,11 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc
fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
if (type_is_invalid(fn_type_id.return_type))
return ira->codegen->builtin_types.entry_invalid;
if (fn_type_id.return_type->id == TypeTableEntryIdOpaque) {
ir_add_error(ira, instruction->return_type,
buf_sprintf("return type cannot be opaque"));
return ira->codegen->builtin_types.entry_invalid;
}
if (fn_type_id.cc == CallingConventionAsync) {
if (instruction->async_allocator_type_value == nullptr) {
@ -19888,6 +20189,16 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_fence(ira, (IrInstructionFence *)instruction);
case IrInstructionIdTruncate:
return ir_analyze_instruction_truncate(ira, (IrInstructionTruncate *)instruction);
case IrInstructionIdIntCast:
return ir_analyze_instruction_int_cast(ira, (IrInstructionIntCast *)instruction);
case IrInstructionIdFloatCast:
return ir_analyze_instruction_float_cast(ira, (IrInstructionFloatCast *)instruction);
case IrInstructionIdIntToFloat:
return ir_analyze_instruction_int_to_float(ira, (IrInstructionIntToFloat *)instruction);
case IrInstructionIdFloatToInt:
return ir_analyze_instruction_float_to_int(ira, (IrInstructionFloatToInt *)instruction);
case IrInstructionIdBoolToInt:
return ir_analyze_instruction_bool_to_int(ira, (IrInstructionBoolToInt *)instruction);
case IrInstructionIdIntType:
return ir_analyze_instruction_int_type(ira, (IrInstructionIntType *)instruction);
case IrInstructionIdBoolNot:
@ -20231,6 +20542,11 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdPromiseResultType:
case IrInstructionIdSqrt:
case IrInstructionIdAtomicLoad:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
case IrInstructionIdBoolToInt:
return false;
case IrInstructionIdAsm:

View File

@ -648,6 +648,44 @@ static void ir_print_truncate(IrPrint *irp, IrInstructionTruncate *instruction)
fprintf(irp->f, ")");
}
static void ir_print_int_cast(IrPrint *irp, IrInstructionIntCast *instruction) {
fprintf(irp->f, "@intCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_cast(IrPrint *irp, IrInstructionFloatCast *instruction) {
fprintf(irp->f, "@floatCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_to_float(IrPrint *irp, IrInstructionIntToFloat *instruction) {
fprintf(irp->f, "@intToFloat(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_to_int(IrPrint *irp, IrInstructionFloatToInt *instruction) {
fprintf(irp->f, "@floatToInt(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_bool_to_int(IrPrint *irp, IrInstructionBoolToInt *instruction) {
fprintf(irp->f, "@boolToInt(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_type(IrPrint *irp, IrInstructionIntType *instruction) {
fprintf(irp->f, "@IntType(");
ir_print_other_instruction(irp, instruction->is_signed);
@ -1417,6 +1455,21 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdTruncate:
ir_print_truncate(irp, (IrInstructionTruncate *)instruction);
break;
case IrInstructionIdIntCast:
ir_print_int_cast(irp, (IrInstructionIntCast *)instruction);
break;
case IrInstructionIdFloatCast:
ir_print_float_cast(irp, (IrInstructionFloatCast *)instruction);
break;
case IrInstructionIdIntToFloat:
ir_print_int_to_float(irp, (IrInstructionIntToFloat *)instruction);
break;
case IrInstructionIdFloatToInt:
ir_print_float_to_int(irp, (IrInstructionFloatToInt *)instruction);
break;
case IrInstructionIdBoolToInt:
ir_print_bool_to_int(irp, (IrInstructionBoolToInt *)instruction);
break;
case IrInstructionIdIntType:
ir_print_int_type(irp, (IrInstructionIntType *)instruction);
break;

View File

@ -208,7 +208,7 @@ static Buf *get_dynamic_linker_path(CodeGen *g) {
static void construct_linker_job_elf(LinkJob *lj) {
CodeGen *g = lj->codegen;
if (lj->link_in_crt) {
if (g->libc_link_lib != nullptr) {
find_libc_lib_path(g);
}
@ -432,7 +432,7 @@ static bool zig_lld_link(ZigLLVM_ObjectFormatType oformat, const char **args, si
static void construct_linker_job_coff(LinkJob *lj) {
CodeGen *g = lj->codegen;
if (lj->link_in_crt) {
if (g->libc_link_lib != nullptr) {
find_libc_lib_path(g);
}

View File

@ -34,7 +34,7 @@ static int usage(const char *arg0) {
" --assembly [source] add assembly file to build\n"
" --cache-dir [path] override the cache directory\n"
" --color [auto|off|on] enable or disable colored error messages\n"
" --emit [filetype] emit a specific file format as compilation output\n"
" --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
" --enable-timing-info print timing diagnostics\n"
" --libc-include-dir [path] directory where libc stdlib.h resides\n"
" --name [name] override output name\n"

View File

@ -989,12 +989,29 @@ int os_self_exe_path(Buf *out_path) {
}
#elif defined(ZIG_OS_DARWIN)
// How long is the executable's path?
uint32_t u32_len = 0;
int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
assert(ret1 != 0);
buf_resize(out_path, u32_len);
int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);
Buf *tmp = buf_alloc_fixed(u32_len);
// Fill the executable path.
int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len);
assert(ret2 == 0);
// According to libuv project, PATH_MAX*2 works around a libc bug where
// the resolved path is sometimes bigger than PATH_MAX.
buf_resize(out_path, PATH_MAX*2);
char *real_path = realpath(buf_ptr(tmp), buf_ptr(out_path));
if (!real_path) {
buf_init_from_buf(out_path, tmp);
return 0;
}
// Resize out_path for the correct length.
buf_resize(out_path, strlen(buf_ptr(out_path)));
return 0;
#elif defined(ZIG_OS_LINUX)
buf_resize(out_path, 256);

View File

@ -685,21 +685,41 @@ static int get_arch_pointer_bit_width(ZigLLVM_ArchType arch) {
uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
switch (target->os) {
case OsFreestanding:
switch (id) {
case CIntTypeShort:
case CIntTypeUShort:
return 16;
case CIntTypeInt:
case CIntTypeUInt:
return 32;
case CIntTypeLong:
case CIntTypeULong:
return get_arch_pointer_bit_width(target->arch.arch);
case CIntTypeLongLong:
case CIntTypeULongLong:
return 64;
case CIntTypeCount:
zig_unreachable();
switch (target->arch.arch) {
case ZigLLVM_msp430:
switch (id) {
case CIntTypeShort:
case CIntTypeUShort:
return 16;
case CIntTypeInt:
case CIntTypeUInt:
return 16;
case CIntTypeLong:
case CIntTypeULong:
return 32;
case CIntTypeLongLong:
case CIntTypeULongLong:
return 64;
case CIntTypeCount:
zig_unreachable();
}
default:
switch (id) {
case CIntTypeShort:
case CIntTypeUShort:
return 16;
case CIntTypeInt:
case CIntTypeUInt:
return 32;
case CIntTypeLong:
case CIntTypeULong:
return get_arch_pointer_bit_width(target->arch.arch);
case CIntTypeLongLong:
case CIntTypeULongLong:
return 64;
case CIntTypeCount:
zig_unreachable();
}
}
case OsLinux:
case OsMacOSX:

View File

@ -29,36 +29,36 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
};
}
pub fn deinit(self: *const Self) void {
pub fn deinit(self: Self) void {
self.allocator.free(self.items);
}
pub fn toSlice(self: *const Self) []align(A) T {
pub fn toSlice(self: Self) []align(A) T {
return self.items[0..self.len];
}
pub fn toSliceConst(self: *const Self) []align(A) const T {
pub fn toSliceConst(self: Self) []align(A) const T {
return self.items[0..self.len];
}
pub fn at(self: *const Self, n: usize) T {
pub fn at(self: Self, n: usize) T {
return self.toSliceConst()[n];
}
/// Sets the value at index `i`, or returns `error.OutOfBounds` if
/// the index is not in range.
pub fn setOrError(self: *const Self, i: usize, item: *const T) !void {
pub fn setOrError(self: Self, i: usize, item: T) !void {
if (i >= self.len) return error.OutOfBounds;
self.items[i] = item.*;
self.items[i] = item;
}
/// Sets the value at index `i`, asserting that the value is in range.
pub fn set(self: *const Self, i: usize, item: *const T) void {
pub fn set(self: *Self, i: usize, item: T) void {
assert(i < self.len);
self.items[i] = item.*;
self.items[i] = item;
}
pub fn count(self: *const Self) usize {
pub fn count(self: Self) usize {
return self.len;
}
@ -81,12 +81,12 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
return result;
}
pub fn insert(self: *Self, n: usize, item: *const T) !void {
pub fn insert(self: *Self, n: usize, item: T) !void {
try self.ensureCapacity(self.len + 1);
self.len += 1;
mem.copy(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]);
self.items[n] = item.*;
self.items[n] = item;
}
pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void {
@ -97,9 +97,9 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
mem.copy(T, self.items[n .. n + items.len], items);
}
pub fn append(self: *Self, item: *const T) !void {
pub fn append(self: *Self, item: T) !void {
const new_item_ptr = try self.addOne();
new_item_ptr.* = item.*;
new_item_ptr.* = item;
}
pub fn appendSlice(self: *Self, items: []align(A) const T) !void {
@ -185,23 +185,23 @@ test "basic ArrayList test" {
{
var i: usize = 0;
while (i < 10) : (i += 1) {
list.append(i32(i + 1)) catch unreachable;
list.append(@intCast(i32, i + 1)) catch unreachable;
}
}
{
var i: usize = 0;
while (i < 10) : (i += 1) {
assert(list.items[i] == i32(i + 1));
assert(list.items[i] == @intCast(i32, i + 1));
}
}
for (list.toSlice()) |v, i| {
assert(v == i32(i + 1));
assert(v == @intCast(i32, i + 1));
}
for (list.toSliceConst()) |v, i| {
assert(v == i32(i + 1));
assert(v == @intCast(i32, i + 1));
}
assert(list.pop() == 10);

View File

@ -99,7 +99,7 @@ pub const Base64Decoder = struct {
assert(!result.char_in_alphabet[c]);
assert(c != pad_char);
result.char_to_index[c] = u8(i);
result.char_to_index[c] = @intCast(u8, i);
result.char_in_alphabet[c] = true;
}
@ -284,7 +284,7 @@ pub const Base64DecoderUnsafe = struct {
};
for (alphabet_chars) |c, i| {
assert(c != pad_char);
result.char_to_index[c] = u8(i);
result.char_to_index[c] = @intCast(u8, i);
}
return result;
}

View File

@ -234,7 +234,7 @@ pub const Builder = struct {
defer wanted_steps.deinit();
if (step_names.len == 0) {
try wanted_steps.append(&self.default_step);
try wanted_steps.append(self.default_step);
} else {
for (step_names) |step_name| {
const s = try self.getTopLevelStepByName(step_name);

View File

@ -79,7 +79,7 @@ fn Blake2s(comptime out_len: usize) type {
mem.copy(u32, d.h[0..], iv[0..]);
// No key plus default parameters
d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
d.h[0] ^= 0x01010000 ^ @intCast(u32, out_len >> 3);
d.t = 0;
d.buf_len = 0;
}
@ -110,7 +110,7 @@ fn Blake2s(comptime out_len: usize) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
}
pub fn final(d: *Self, out: []u8) void {
@ -144,7 +144,7 @@ fn Blake2s(comptime out_len: usize) type {
}
v[12] ^= @truncate(u32, d.t);
v[13] ^= u32(d.t >> 32);
v[13] ^= @intCast(u32, d.t >> 32);
if (last) v[14] = ~v[14];
const rounds = comptime []RoundParam{
@ -345,7 +345,7 @@ fn Blake2b(comptime out_len: usize) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
}
pub fn final(d: *Self, out: []u8) void {
@ -377,7 +377,7 @@ fn Blake2b(comptime out_len: usize) type {
}
v[12] ^= @truncate(u64, d.t);
v[13] ^= u64(d.t >> 64);
v[13] ^= @intCast(u64, d.t >> 64);
if (last) v[14] = ~v[14];
const rounds = comptime []RoundParam{

View File

@ -78,7 +78,7 @@ pub const Md5 = struct {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
// Md5 uses the bottom 64-bits for length padding
d.total_len +%= b.len;
@ -103,9 +103,9 @@ pub const Md5 = struct {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[56] = u8(d.total_len & 0x1f) << 3;
d.buf[56] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[56 + i] = u8(len & 0xff);
d.buf[56 + i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -78,7 +78,7 @@ pub const Sha1 = struct {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -102,9 +102,9 @@ pub const Sha1 = struct {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[63] = u8(d.total_len & 0x1f) << 3;
d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[63 - i] = u8(len & 0xff);
d.buf[63 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -131,7 +131,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -155,9 +155,9 @@ fn Sha2_32(comptime params: Sha2Params32) type {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[63] = u8(d.total_len & 0x1f) << 3;
d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[63 - i] = u8(len & 0xff);
d.buf[63 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}
@ -472,7 +472,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -496,9 +496,9 @@ fn Sha2_64(comptime params: Sha2Params64) type {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[127] = u8(d.total_len & 0x1f) << 3;
d.buf[127] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 16) : (i += 1) {
d.buf[127 - i] = u8(len & 0xff);
d.buf[127 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -554,7 +554,7 @@ const LineNumberProgram = struct {
const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
errdefer self.file_entries.allocator.free(file_name);
return LineInfo{
.line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
.line = if (self.prev_line >= 0) @intCast(usize, self.prev_line) else 0,
.column = self.prev_column,
.file_name = file_name,
.allocator = self.file_entries.allocator,
@ -639,6 +639,7 @@ const ParseFormValueError = error{
Unexpected,
InvalidDebugInfo,
EndOfFile,
IsDir,
OutOfMemory,
};
@ -1069,7 +1070,7 @@ fn readULeb128(in_stream: var) !u64 {
var operand: u64 = undefined;
if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
if (@shlWithOverflow(u64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
result |= operand;
@ -1088,13 +1089,13 @@ fn readILeb128(in_stream: var) !i64 {
var operand: i64 = undefined;
if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
if (@shlWithOverflow(i64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
result |= operand;
shift += 7;
if ((byte & 0b10000000) == 0) {
if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << u6(shift));
if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << @intCast(u6, shift));
return result;
}
}

156
std/dynamic_library.zig Normal file
View File

@ -0,0 +1,156 @@
const std = @import("index.zig");
const mem = std.mem;
const elf = std.elf;
const cstr = std.cstr;
const linux = std.os.linux;
pub const DynLib = struct {
allocator: *mem.Allocator,
elf_lib: ElfLib,
fd: i32,
map_addr: usize,
map_size: usize,
/// Trusts the file
pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib {
const fd = try std.os.posixOpen(allocator, path, 0, linux.O_RDONLY | linux.O_CLOEXEC);
errdefer std.os.close(fd);
const size = @intCast(usize, (try std.os.posixFStat(fd)).size);
const addr = linux.mmap(
null,
size,
linux.PROT_READ | linux.PROT_EXEC,
linux.MAP_PRIVATE | linux.MAP_LOCKED,
fd,
0,
);
errdefer _ = linux.munmap(addr, size);
const bytes = @intToPtr([*]align(std.os.page_size) u8, addr)[0..size];
return DynLib{
.allocator = allocator,
.elf_lib = try ElfLib.init(bytes),
.fd = fd,
.map_addr = addr,
.map_size = size,
};
}
pub fn close(self: *DynLib) void {
_ = linux.munmap(self.map_addr, self.map_size);
std.os.close(self.fd);
self.* = undefined;
}
pub fn lookup(self: *DynLib, name: []const u8) ?usize {
return self.elf_lib.lookup("", name);
}
};
pub const ElfLib = struct {
strings: [*]u8,
syms: [*]elf.Sym,
hashtab: [*]linux.Elf_Symndx,
versym: ?[*]u16,
verdef: ?*elf.Verdef,
base: usize,
// Trusts the memory
pub fn init(bytes: []align(@alignOf(elf.Ehdr)) u8) !ElfLib {
const eh = @ptrCast(*elf.Ehdr, bytes.ptr);
if (!mem.eql(u8, eh.e_ident[0..4], "\x7fELF")) return error.NotElfFile;
if (eh.e_type != elf.ET_DYN) return error.NotDynamicLibrary;
const elf_addr = @ptrToInt(bytes.ptr);
var ph_addr: usize = elf_addr + eh.e_phoff;
var base: usize = @maxValue(usize);
var maybe_dynv: ?[*]usize = null;
{
var i: usize = 0;
while (i < eh.e_phnum) : ({
i += 1;
ph_addr += eh.e_phentsize;
}) {
const ph = @intToPtr(*elf.Phdr, ph_addr);
switch (ph.p_type) {
elf.PT_LOAD => base = elf_addr + ph.p_offset - ph.p_vaddr,
elf.PT_DYNAMIC => maybe_dynv = @intToPtr([*]usize, elf_addr + ph.p_offset),
else => {},
}
}
}
const dynv = maybe_dynv orelse return error.MissingDynamicLinkingInformation;
if (base == @maxValue(usize)) return error.BaseNotFound;
var maybe_strings: ?[*]u8 = null;
var maybe_syms: ?[*]elf.Sym = null;
var maybe_hashtab: ?[*]linux.Elf_Symndx = null;
var maybe_versym: ?[*]u16 = null;
var maybe_verdef: ?*elf.Verdef = null;
{
var i: usize = 0;
while (dynv[i] != 0) : (i += 2) {
const p = base + dynv[i + 1];
switch (dynv[i]) {
elf.DT_STRTAB => maybe_strings = @intToPtr([*]u8, p),
elf.DT_SYMTAB => maybe_syms = @intToPtr([*]elf.Sym, p),
elf.DT_HASH => maybe_hashtab = @intToPtr([*]linux.Elf_Symndx, p),
elf.DT_VERSYM => maybe_versym = @intToPtr([*]u16, p),
elf.DT_VERDEF => maybe_verdef = @intToPtr(*elf.Verdef, p),
else => {},
}
}
}
return ElfLib{
.base = base,
.strings = maybe_strings orelse return error.ElfStringSectionNotFound,
.syms = maybe_syms orelse return error.ElfSymSectionNotFound,
.hashtab = maybe_hashtab orelse return error.ElfHashTableNotFound,
.versym = maybe_versym,
.verdef = maybe_verdef,
};
}
/// Returns the address of the symbol
pub fn lookup(self: *const ElfLib, vername: []const u8, name: []const u8) ?usize {
const maybe_versym = if (self.verdef == null) null else self.versym;
const OK_TYPES = (1 << elf.STT_NOTYPE | 1 << elf.STT_OBJECT | 1 << elf.STT_FUNC | 1 << elf.STT_COMMON);
const OK_BINDS = (1 << elf.STB_GLOBAL | 1 << elf.STB_WEAK | 1 << elf.STB_GNU_UNIQUE);
var i: usize = 0;
while (i < self.hashtab[1]) : (i += 1) {
if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == self.syms[i].st_shndx) continue;
if (!mem.eql(u8, name, cstr.toSliceConst(self.strings + self.syms[i].st_name))) continue;
if (maybe_versym) |versym| {
if (!checkver(self.verdef.?, versym[i], vername, self.strings))
continue;
}
return self.base + self.syms[i].st_value;
}
return null;
}
};
fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [*]u8) bool {
var def = def_arg;
const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
while (true) {
if (0 == (def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
break;
if (def.vd_next == 0)
return false;
def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next);
}
const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux);
return mem.eql(u8, vername, cstr.toSliceConst(strings + aux.vda_name));
}

View File

@ -305,6 +305,21 @@ pub const STT_ARM_16BIT = STT_HIPROC;
pub const VER_FLG_BASE = 0x1;
pub const VER_FLG_WEAK = 0x2;
/// An unknown type.
pub const ET_NONE = 0;
/// A relocatable file.
pub const ET_REL = 1;
/// An executable file.
pub const ET_EXEC = 2;
/// A shared object.
pub const ET_DYN = 3;
/// A core file.
pub const ET_CORE = 4;
pub const FileType = enum {
Relocatable,
Executable,

View File

@ -29,11 +29,11 @@ pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: Ro
switch (mode) {
RoundMode.Decimal => {
if (float_decimal.exp >= 0) {
round_digit = precision + usize(float_decimal.exp);
round_digit = precision + @intCast(usize, float_decimal.exp);
} else {
// if a small negative exp, then adjust we need to offset by the number
// of leading zeros that will occur.
const min_exp_required = usize(-float_decimal.exp);
const min_exp_required = @intCast(usize, -float_decimal.exp);
if (precision > min_exp_required) {
round_digit = precision - min_exp_required;
}
@ -107,16 +107,16 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
// normalize the midpoint
const e = math.frexp(val).exponent;
var exp = i16(math.floor(307 + f64(e) * 0.30103));
var exp = @floatToInt(i16, math.floor(307 + @intToFloat(f64, e) * 0.30103));
if (exp < 20) {
exp = 20;
} else if (usize(exp) >= lookup_table.len) {
exp = i16(lookup_table.len - 1);
} else if (@intCast(usize, exp) >= lookup_table.len) {
exp = @intCast(i16, lookup_table.len - 1);
}
var mid = lookup_table[usize(exp)];
var mid = lookup_table[@intCast(usize, exp)];
mid = hpProd(mid, val);
const lten = lookup_table[usize(exp)].val;
const lten = lookup_table[@intCast(usize, exp)].val;
exp -= 307;
@ -168,25 +168,25 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
// the 0-index for this extra digit.
var buf_index: usize = 1;
while (true) {
var hdig = u8(math.floor(high.val));
if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
var hdig = @floatToInt(u8, math.floor(high.val));
if ((high.val == @intToFloat(f64, hdig)) and (high.off < 0)) hdig -= 1;
var ldig = u8(math.floor(low.val));
if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
var ldig = @floatToInt(u8, math.floor(low.val));
if ((low.val == @intToFloat(f64, ldig)) and (low.off < 0)) ldig -= 1;
if (ldig != hdig) break;
buffer[buf_index] = hdig + '0';
buf_index += 1;
high.val -= f64(hdig);
low.val -= f64(ldig);
high.val -= @intToFloat(f64, hdig);
low.val -= @intToFloat(f64, ldig);
hpMul10(&high);
hpMul10(&low);
}
const tmp = (high.val + low.val) / 2.0;
var mdig = u8(math.floor(tmp + 0.5));
if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
var mdig = @floatToInt(u8, math.floor(tmp + 0.5));
if ((@intToFloat(f64, mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
buffer[buf_index] = mdig + '0';
buf_index += 1;
@ -304,7 +304,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
assert((val > 9.007199254740992e15) and val < (3.40282366920938e38));
var mid = u128(val);
var mid = @floatToInt(u128, val);
var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);
var high: u128 = mid + fpeint((val - fpprev(val)) / 2.0);
@ -314,11 +314,11 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
low -= 1;
}
var l64 = u64(low % pow19);
const lf = u64((low / pow19) % pow19);
var l64 = @intCast(u64, low % pow19);
const lf = @intCast(u64, (low / pow19) % pow19);
var h64 = u64(high % pow19);
const hf = u64((high / pow19) % pow19);
var h64 = @intCast(u64, high % pow19);
const hf = @intCast(u64, (high / pow19) % pow19);
if (lf != hf) {
l64 = lf;
@ -329,7 +329,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
var mi: i32 = mismatch10(l64, h64);
var x: u64 = 1;
{
var i = i32(lf == hf);
var i: i32 = @boolToInt(lf == hf);
while (i < mi) : (i += 1) {
x *= 10;
}
@ -341,14 +341,14 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
var buf_index = u64toa(m64, buffer) - 1;
if (mi != 0) {
buffer[buf_index - 1] += u8(buffer[buf_index] >= '5');
buffer[buf_index - 1] += @boolToInt(buffer[buf_index] >= '5');
} else {
buf_index += 1;
}
return FloatDecimal{
.digits = buffer[0..buf_index],
.exp = i32(buf_index) + mi,
.exp = @intCast(i32, buf_index) + mi,
};
}
@ -359,33 +359,33 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
assert((val >= 16.0) and (val < 9.007199254740992e15));
const u = u64(val);
const n = f64(u);
const u = @floatToInt(u64, val);
const n = @intToFloat(f64, u);
var mid = val - n;
var lo = ((fpprev(val) - n) + mid) / 2.0;
var hi = ((fpnext(val) - n) + mid) / 2.0;
var buf_index = u64toa(u, buffer);
var exp = i32(buf_index);
var exp = @intCast(i32, buf_index);
var j = buf_index;
buffer[j] = 0;
if (mid != 0.0) {
while (mid != 0.0) {
lo *= 10.0;
const ldig = i32(lo);
lo -= f64(ldig);
const ldig = @floatToInt(i32, lo);
lo -= @intToFloat(f64, ldig);
mid *= 10.0;
const mdig = i32(mid);
mid -= f64(mdig);
const mdig = @floatToInt(i32, mid);
mid -= @intToFloat(f64, mdig);
hi *= 10.0;
const hdig = i32(hi);
hi -= f64(hdig);
const hdig = @floatToInt(i32, hi);
hi -= @intToFloat(f64, hdig);
buffer[j] = u8(mdig + '0');
buffer[j] = @intCast(u8, mdig + '0');
j += 1;
if (hdig != ldig or j > 50) break;
@ -452,7 +452,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
var buf_index: usize = 0;
if (value < kTen8) {
const v = u32(value);
const v = @intCast(u32, value);
if (v < 10000) {
const d1: u32 = (v / 100) << 1;
const d2: u32 = (v % 100) << 1;
@ -507,8 +507,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buf_index += 1;
}
} else if (value < kTen16) {
const v0: u32 = u32(value / kTen8);
const v1: u32 = u32(value % kTen8);
const v0: u32 = @intCast(u32, value / kTen8);
const v1: u32 = @intCast(u32, value % kTen8);
const b0: u32 = v0 / 10000;
const c0: u32 = v0 % 10000;
@ -578,11 +578,11 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buffer[buf_index] = c_digits_lut[d8 + 1];
buf_index += 1;
} else {
const a = u32(value / kTen16); // 1 to 1844
const a = @intCast(u32, value / kTen16); // 1 to 1844
value %= kTen16;
if (a < 10) {
buffer[buf_index] = '0' + u8(a);
buffer[buf_index] = '0' + @intCast(u8, a);
buf_index += 1;
} else if (a < 100) {
const i: u32 = a << 1;
@ -591,7 +591,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buffer[buf_index] = c_digits_lut[i + 1];
buf_index += 1;
} else if (a < 1000) {
buffer[buf_index] = '0' + u8(a / 100);
buffer[buf_index] = '0' + @intCast(u8, a / 100);
buf_index += 1;
const i: u32 = (a % 100) << 1;
@ -612,8 +612,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buf_index += 1;
}
const v0 = u32(value / kTen8);
const v1 = u32(value % kTen8);
const v0 = @intCast(u32, value / kTen8);
const v1 = @intCast(u32, value % kTen8);
const b0: u32 = v0 / 10000;
const c0: u32 = v0 % 10000;

View File

@ -5,6 +5,7 @@ const assert = debug.assert;
const mem = std.mem;
const builtin = @import("builtin");
const errol = @import("errol/index.zig");
const lossyCast = std.math.lossyCast;
const max_int_digits = 65;
@ -162,8 +163,6 @@ pub fn formatType(
},
builtin.TypeInfo.Pointer.Size.Many => {
if (ptr_info.child == u8) {
//This is a bit of a hack, but it made more sense to
// do this check here than have formatText do it
if (fmt[0] == 's') {
const len = std.cstr.len(value);
return formatText(value[0..len], fmt, context, Errors, output);
@ -176,6 +175,12 @@ pub fn formatType(
return output(context, casted_value);
},
},
builtin.TypeId.Array => |info| {
if (info.child == u8) {
return formatText(value, fmt, context, Errors, output);
}
return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(&value));
},
else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
}
}
@ -459,7 +464,7 @@ pub fn formatFloatDecimal(
errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);
// exp < 0 means the leading is always 0 as errol result is normalized.
var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
@ -488,7 +493,7 @@ pub fn formatFloatDecimal(
// Zero-fill until we reach significant digits or run out of precision.
if (float_decimal.exp <= 0) {
const zero_digit_count = usize(-float_decimal.exp);
const zero_digit_count = @intCast(usize, -float_decimal.exp);
const zeros_to_print = math.min(zero_digit_count, precision);
var i: usize = 0;
@ -517,7 +522,7 @@ pub fn formatFloatDecimal(
}
} else {
// exp < 0 means the leading is always 0 as errol result is normalized.
var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
@ -543,7 +548,7 @@ pub fn formatFloatDecimal(
// Zero-fill until we reach significant digits or run out of precision.
if (float_decimal.exp < 0) {
const zero_digit_count = usize(-float_decimal.exp);
const zero_digit_count = @intCast(usize, -float_decimal.exp);
var i: usize = 0;
while (i < zero_digit_count) : (i += 1) {
@ -574,7 +579,7 @@ pub fn formatBytes(
1024 => math.min(math.log2(value) / 10, mags_iec.len - 1),
else => unreachable,
};
const new_value = f64(value) / math.pow(f64, f64(radix), f64(magnitude));
const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));
const suffix = switch (radix) {
1000 => mags_si[magnitude],
1024 => mags_iec[magnitude],
@ -624,15 +629,15 @@ fn formatIntSigned(
if (value < 0) {
const minus_sign: u8 = '-';
try output(context, (*[1]u8)(&minus_sign)[0..]);
const new_value = uint(-(value + 1)) + 1;
const new_value = @intCast(uint, -(value + 1)) + 1;
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
} else if (width == 0) {
return formatIntUnsigned(uint(value), base, uppercase, width, context, Errors, output);
return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
} else {
const plus_sign: u8 = '+';
try output(context, (*[1]u8)(&plus_sign)[0..]);
const new_value = uint(value);
const new_value = @intCast(uint, value);
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
}
@ -656,7 +661,7 @@ fn formatIntUnsigned(
while (true) {
const digit = a % base;
index -= 1;
buf[index] = digitToChar(u8(digit), uppercase);
buf[index] = digitToChar(@intCast(u8, digit), uppercase);
a /= base;
if (a == 0) break;
}

View File

@ -26,7 +26,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
var tables: [8][256]u32 = undefined;
for (tables[0]) |*e, i| {
var crc = u32(i);
var crc = @intCast(u32, i);
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
@ -122,7 +122,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
var table: [16]u32 = undefined;
for (table) |*e, i| {
var crc = u32(i * 16);
var crc = @intCast(u32, i * 16);
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {

View File

@ -81,7 +81,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
// Remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.msg_len +%= @truncate(u8, b.len);
}
@ -233,7 +233,7 @@ test "siphash64-2-4 sanity" {
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
buffer[i] = u8(i);
buffer[i] = @intCast(u8, i);
const expected = mem.readInt(vector, u64, Endian.Little);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
@ -312,7 +312,7 @@ test "siphash128-2-4 sanity" {
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
buffer[i] = u8(i);
buffer[i] = @intCast(u8, i);
const expected = mem.readInt(vector, u128, Endian.Little);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);

View File

@ -408,7 +408,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
for (slice) |*item, i| {
item.* = try allocator.create(i32);
item.*.* = i32(i);
item.*.* = @intCast(i32, i);
}
for (slice) |item, i| {

View File

@ -8,6 +8,7 @@ pub const HashMap = @import("hash_map.zig").HashMap;
pub const LinkedList = @import("linked_list.zig").LinkedList;
pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList;
pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
pub const DynLib = @import("dynamic_library.zig").DynLib;
pub const atomic = @import("atomic/index.zig");
pub const base64 = @import("base64.zig");

View File

@ -242,11 +242,16 @@ pub fn writeFile(allocator: *mem.Allocator, path: []const u8, data: []const u8)
/// On success, caller owns returned buffer.
pub fn readFileAlloc(allocator: *mem.Allocator, path: []const u8) ![]u8 {
return readFileAllocAligned(allocator, path, @alignOf(u8));
}
/// On success, caller owns returned buffer.
pub fn readFileAllocAligned(allocator: *mem.Allocator, path: []const u8, comptime A: u29) ![]align(A) u8 {
var file = try File.openRead(allocator, path);
defer file.close();
const size = try file.getEndPos();
const buf = try allocator.alloc(u8, size);
const buf = try allocator.alignedAlloc(u8, A, size);
errdefer allocator.free(buf);
var adapter = FileInStream.init(&file);

View File

@ -180,7 +180,7 @@ pub const StreamingParser = struct {
pub fn fromInt(x: var) State {
debug.assert(x == 0 or x == 1);
const T = @TagType(State);
return State(T(x));
return State(@intCast(T, x));
}
};
@ -1326,7 +1326,7 @@ pub const Parser = struct {
},
// Array Parent -> [ ..., <array>, value ]
Value.Array => |*array| {
try array.append(value);
try array.append(value.*);
p.state = State.ArrayValue;
},
else => {

View File

@ -95,12 +95,12 @@ fn acos64(x: f64) f64 {
const pio2_lo: f64 = 6.12323399573676603587e-17;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32);
const hx = @intCast(u32, ux >> 32);
const ix = hx & 0x7FFFFFFF;
// |x| >= 1 or nan
if (ix >= 0x3FF00000) {
const lx = u32(ux & 0xFFFFFFFF);
const lx = @intCast(u32, ux & 0xFFFFFFFF);
// acos(1) = 0, acos(-1) = pi
if ((ix - 0x3FF00000) | lx == 0) {

View File

@ -87,12 +87,12 @@ fn asin64(x: f64) f64 {
const pio2_lo: f64 = 6.12323399573676603587e-17;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32);
const hx = @intCast(u32, ux >> 32);
const ix = hx & 0x7FFFFFFF;
// |x| >= 1 or nan
if (ix >= 0x3FF00000) {
const lx = u32(ux & 0xFFFFFFFF);
const lx = @intCast(u32, ux & 0xFFFFFFFF);
// asin(1) = +-pi/2 with inexact
if ((ix - 0x3FF00000) | lx == 0) {

View File

@ -138,7 +138,7 @@ fn atan64(x_: f64) f64 {
var x = x_;
var ux = @bitCast(u64, x);
var ix = u32(ux >> 32);
var ix = @intCast(u32, ux >> 32);
const sign = ix >> 31;
ix &= 0x7FFFFFFF;
@ -159,7 +159,7 @@ fn atan64(x_: f64) f64 {
// |x| < 2^(-27)
if (ix < 0x3E400000) {
if (ix < 0x00100000) {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
}
return x;
}

View File

@ -124,12 +124,12 @@ fn atan2_64(y: f64, x: f64) f64 {
}
var ux = @bitCast(u64, x);
var ix = u32(ux >> 32);
var lx = u32(ux & 0xFFFFFFFF);
var ix = @intCast(u32, ux >> 32);
var lx = @intCast(u32, ux & 0xFFFFFFFF);
var uy = @bitCast(u64, y);
var iy = u32(uy >> 32);
var ly = u32(uy & 0xFFFFFFFF);
var iy = @intCast(u32, uy >> 32);
var ly = @intCast(u32, uy & 0xFFFFFFFF);
// x = 1.0
if ((ix -% 0x3FF00000) | lx == 0) {

View File

@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 {
if (e < 0x3FF - 32) {
// underflow
if (e == 0) {
math.forceEval(f32(y));
math.forceEval(@floatCast(f32, y));
}
}
// |x| < 0.5

File diff suppressed because it is too large Load Diff

View File

@ -54,7 +54,7 @@ fn cbrt32(x: f32) f32 {
r = t * t * t;
t = t * (f64(x) + x + r) / (x + r + r);
return f32(t);
return @floatCast(f32, t);
}
fn cbrt64(x: f64) f64 {
@ -69,7 +69,7 @@ fn cbrt64(x: f64) f64 {
const P4: f64 = 0.145996192886612446982;
var u = @bitCast(u64, x);
var hx = u32(u >> 32) & 0x7FFFFFFF;
var hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
// cbrt(nan, inf) = itself
if (hx >= 0x7FF00000) {
@ -79,7 +79,7 @@ fn cbrt64(x: f64) f64 {
// cbrt to ~5bits
if (hx < 0x00100000) {
u = @bitCast(u64, x * 0x1.0p54);
hx = u32(u >> 32) & 0x7FFFFFFF;
hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
// cbrt(0) is itself
if (hx == 0) {

View File

@ -20,7 +20,7 @@ pub fn ceil(x: var) @typeOf(x) {
fn ceil32(x: f32) f32 {
var u = @bitCast(u32, x);
var e = i32((u >> 23) & 0xFF) - 0x7F;
var e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
var m: u32 = undefined;
// TODO: Shouldn't need this explicit check.
@ -31,7 +31,7 @@ fn ceil32(x: f32) f32 {
if (e >= 23) {
return x;
} else if (e >= 0) {
m = u32(0x007FFFFF) >> u5(e);
m = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & m == 0) {
return x;
}

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn atan(z: var) Complex(@typeOf(z.re)) {
pub fn atan(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => atan32(z),
@ -25,11 +25,11 @@ fn redupif32(x: f32) f32 {
t -= 0.5;
}
const u = f32(i32(t));
const u = @intToFloat(f32, @floatToInt(i32, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
fn atan32(z: *const Complex(f32)) Complex(f32) {
fn atan32(z: Complex(f32)) Complex(f32) {
const maxnum = 1.0e38;
const x = z.re;
@ -74,11 +74,11 @@ fn redupif64(x: f64) f64 {
t -= 0.5;
}
const u = f64(i64(t));
const u = @intToFloat(f64, @floatToInt(i64, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
fn atan64(z: *const Complex(f64)) Complex(f64) {
fn atan64(z: Complex(f64)) Complex(f64) {
const maxnum = 1.0e308;
const x = z.re;

View File

@ -83,12 +83,12 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) {
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
const hy = u32(fy >> 32);
const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;

View File

@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
pub fn exp(z: var) Complex(@typeOf(z.re)) {
pub fn exp(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
};
}
fn exp32(z: *const Complex(f32)) Complex(f32) {
fn exp32(z: Complex(f32)) Complex(f32) {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
@ -63,7 +63,7 @@ fn exp32(z: *const Complex(f32)) Complex(f32) {
}
}
fn exp64(z: *const Complex(f64)) Complex(f64) {
fn exp64(z: Complex(f64)) Complex(f64) {
const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2

View File

@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn add(self: *const Self, other: *const Self) Self {
pub fn add(self: Self, other: Self) Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
pub fn sub(self: *const Self, other: *const Self) Self {
pub fn sub(self: Self, other: Self) Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
pub fn mul(self: *const Self, other: *const Self) Self {
pub fn mul(self: Self, other: Self) Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
}
pub fn div(self: *const Self, other: *const Self) Self {
pub fn div(self: Self, other: Self) Self {
const re_num = self.re * other.re + self.im * other.im;
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn conjugate(self: *const Self) Self {
pub fn conjugate(self: Self) Self {
return Self{
.re = self.re,
.im = -self.im,
};
}
pub fn reciprocal(self: *const Self) Self {
pub fn reciprocal(self: Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self{
.re = self.re / m,
@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn magnitude(self: *const Self) T {
pub fn magnitude(self: Self) T {
return math.sqrt(self.re * self.re + self.im * self.im);
}
};

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
pub fn ldexp_cexp(z: var, expt: i32) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@ -20,11 +20,12 @@ fn frexp_exp32(x: f32, expt: *i32) f32 {
const exp_x = math.exp(x - kln2);
const hx = @bitCast(u32, exp_x);
expt.* = i32(hx >> 23) - (0x7f + 127) + k;
// TODO zig should allow this cast implicitly because it should know the value is in range
expt.* = @intCast(i32, hx >> 23) - (0x7f + 127) + k;
return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
}
fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) {
fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp32(z.re, &ex_expt);
const exptf = expt + ex_expt;
@ -45,16 +46,16 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
const exp_x = math.exp(x - kln2);
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k;
const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
return @bitCast(f64, (u64(high_word) << 32) | lx);
}
fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) {
fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp64(z.re, &ex_expt);
const exptf = i64(expt + ex_expt);

View File

@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
pub fn sinh(z: var) Complex(@typeOf(z.re)) {
pub fn sinh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sinh32(z),
@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) {
};
}
fn sinh32(z: *const Complex(f32)) Complex(f32) {
fn sinh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -78,17 +78,17 @@ fn sinh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
}
fn sinh64(z: *const Complex(f64)) Complex(f64) {
fn sinh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
const hy = u32(fy >> 32);
const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;

View File

@ -4,18 +4,17 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
pub fn sqrt(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sqrt32(z),
f64 => sqrt64(z),
else => @compileError("sqrt not implemented for " ++ @typeName(z)),
else => @compileError("sqrt not implemented for " ++ @typeName(T)),
};
}
fn sqrt32(z: *const Complex(f32)) Complex(f32) {
fn sqrt32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -50,14 +49,20 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
if (dx >= 0) {
const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(f32(t), f32(dy / (2.0 * t)));
return Complex(f32).new(
@floatCast(f32, t),
@floatCast(f32, dy / (2.0 * t)),
);
} else {
const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y)));
return Complex(f32).new(
@floatCast(f32, math.fabs(y) / (2.0 * t)),
@floatCast(f32, math.copysign(f64, t, y)),
);
}
}
fn sqrt64(z: *const Complex(f64)) Complex(f64) {
fn sqrt64(z: Complex(f64)) Complex(f64) {
// may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
const threshold = 0x1.a827999fcef32p+1022;

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn tanh(z: var) Complex(@typeOf(z.re)) {
pub fn tanh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => tanh32(z),
@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) {
};
}
fn tanh32(z: *const Complex(f32)) Complex(f32) {
fn tanh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -51,12 +51,14 @@ fn tanh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((beta * rho * s) / den, t / den);
}
fn tanh64(z: *const Complex(f64)) Complex(f64) {
fn tanh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
// TODO: zig should allow this conversion implicitly because it can notice that the value necessarily
// fits in range.
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;

View File

@ -55,7 +55,7 @@ fn cos32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -106,7 +106,7 @@ fn cos64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -49,7 +49,7 @@ fn cosh32(x: f32) f32 {
fn cosh64(x: f64) f64 {
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
// TODO: Shouldn't need this explicit check.

View File

@ -29,7 +29,7 @@ fn exp32(x_: f32) f32 {
var x = x_;
var hx = @bitCast(u32, x);
const sign = i32(hx >> 31);
const sign = @intCast(i32, hx >> 31);
hx &= 0x7FFFFFFF;
if (math.isNan(x)) {
@ -63,12 +63,12 @@ fn exp32(x_: f32) f32 {
if (hx > 0x3EB17218) {
// |x| > 1.5 * ln2
if (hx > 0x3F851592) {
k = i32(invln2 * x + half[usize(sign)]);
k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
} else {
k = 1 - sign - sign;
}
const fk = f32(k);
const fk = @intToFloat(f32, k);
hi = x - fk * ln2hi;
lo = fk * ln2lo;
x = hi - lo;
@ -110,7 +110,7 @@ fn exp64(x_: f64) f64 {
var x = x_;
var ux = @bitCast(u64, x);
var hx = ux >> 32;
const sign = i32(hx >> 31);
const sign = @intCast(i32, hx >> 31);
hx &= 0x7FFFFFFF;
if (math.isNan(x)) {
@ -148,12 +148,12 @@ fn exp64(x_: f64) f64 {
if (hx > 0x3EB17218) {
// |x| >= 1.5 * ln2
if (hx > 0x3FF0A2B2) {
k = i32(invln2 * x + half[usize(sign)]);
k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
} else {
k = 1 - sign - sign;
}
const dk = f64(k);
const dk = @intToFloat(f64, k);
hi = x - dk * ln2hi;
lo = dk * ln2lo;
x = hi - lo;

View File

@ -38,8 +38,8 @@ const exp2ft = []const f64{
fn exp2_32(x: f32) f32 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const tblsiz = u32(exp2ft.len);
const redux: f32 = 0x1.8p23 / f32(tblsiz);
const tblsiz = @intCast(u32, exp2ft.len);
const redux: f32 = 0x1.8p23 / @intToFloat(f32, tblsiz);
const P1: f32 = 0x1.62e430p-1;
const P2: f32 = 0x1.ebfbe0p-3;
const P3: f32 = 0x1.c6b348p-5;
@ -89,7 +89,7 @@ fn exp2_32(x: f32) f32 {
var r: f64 = exp2ft[i0];
const t: f64 = r * z;
r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
return f32(r * uk);
return @floatCast(f32, r * uk);
}
const exp2dt = []f64{
@ -355,8 +355,8 @@ const exp2dt = []f64{
fn exp2_64(x: f64) f64 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const tblsiz = u32(exp2dt.len / 2);
const redux: f64 = 0x1.8p52 / f64(tblsiz);
const tblsiz = @intCast(u32, exp2dt.len / 2);
const redux: f64 = 0x1.8p52 / @intToFloat(f64, tblsiz);
const P1: f64 = 0x1.62e42fefa39efp-1;
const P2: f64 = 0x1.ebfbdff82c575p-3;
const P3: f64 = 0x1.c6b08d704a0a6p-5;
@ -364,7 +364,7 @@ fn exp2_64(x: f64) f64 {
const P5: f64 = 0x1.5d88003875c74p-10;
const ux = @bitCast(u64, x);
const ix = u32(ux >> 32) & 0x7FFFFFFF;
const ix = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
// TODO: This should be handled beneath.
if (math.isNan(x)) {
@ -386,7 +386,7 @@ fn exp2_64(x: f64) f64 {
if (ux >> 63 != 0) {
// underflow
if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
math.forceEval(f32(-0x1.0p-149 / x));
math.forceEval(@floatCast(f32, -0x1.0p-149 / x));
}
if (x <= -1075) {
return 0;

View File

@ -78,8 +78,8 @@ fn expm1_32(x_: f32) f32 {
kf += 0.5;
}
k = i32(kf);
const t = f32(k);
k = @floatToInt(i32, kf);
const t = @intToFloat(f32, k);
hi = x - t * ln2_hi;
lo = t * ln2_lo;
}
@ -123,7 +123,7 @@ fn expm1_32(x_: f32) f32 {
}
}
const twopk = @bitCast(f32, u32((0x7F +% k) << 23));
const twopk = @bitCast(f32, @intCast(u32, (0x7F +% k) << 23));
if (k < 0 or k > 56) {
var y = x - e + 1.0;
@ -136,7 +136,7 @@ fn expm1_32(x_: f32) f32 {
return y - 1.0;
}
const uf = @bitCast(f32, u32(0x7F -% k) << 23);
const uf = @bitCast(f32, @intCast(u32, 0x7F -% k) << 23);
if (k < 23) {
return (x - e + (1 - uf)) * twopk;
} else {
@ -158,7 +158,7 @@ fn expm1_64(x_: f64) f64 {
var x = x_;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32) & 0x7FFFFFFF;
const hx = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
const sign = ux >> 63;
if (math.isNegativeInf(x)) {
@ -207,8 +207,8 @@ fn expm1_64(x_: f64) f64 {
kf += 0.5;
}
k = i32(kf);
const t = f64(k);
k = @floatToInt(i32, kf);
const t = @intToFloat(f64, k);
hi = x - t * ln2_hi;
lo = t * ln2_lo;
}
@ -219,7 +219,7 @@ fn expm1_64(x_: f64) f64 {
// |x| < 2^(-54)
else if (hx < 0x3C900000) {
if (hx < 0x00100000) {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
}
return x;
} else {
@ -252,7 +252,7 @@ fn expm1_64(x_: f64) f64 {
}
}
const twopk = @bitCast(f64, u64(0x3FF +% k) << 52);
const twopk = @bitCast(f64, @intCast(u64, 0x3FF +% k) << 52);
if (k < 0 or k > 56) {
var y = x - e + 1.0;
@ -265,7 +265,7 @@ fn expm1_64(x_: f64) f64 {
return y - 1.0;
}
const uf = @bitCast(f64, u64(0x3FF -% k) << 52);
const uf = @bitCast(f64, @intCast(u64, 0x3FF -% k) << 52);
if (k < 20) {
return (x - e + (1 - uf)) * twopk;
} else {

View File

@ -20,7 +20,7 @@ pub fn floor(x: var) @typeOf(x) {
fn floor32(x: f32) f32 {
var u = @bitCast(u32, x);
const e = i32((u >> 23) & 0xFF) - 0x7F;
const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
var m: u32 = undefined;
// TODO: Shouldn't need this explicit check.
@ -33,7 +33,7 @@ fn floor32(x: f32) f32 {
}
if (e >= 0) {
m = u32(0x007FFFFF) >> u5(e);
m = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & m == 0) {
return x;
}

View File

@ -17,10 +17,10 @@ fn fma32(x: f32, y: f32, z: f32) f32 {
const e = (u >> 52) & 0x7FF;
if ((u & 0x1FFFFFFF) != 0x10000000 or e == 0x7FF or xy_z - xy == z) {
return f32(xy_z);
return @floatCast(f32, xy_z);
} else {
// TODO: Handle inexact case with double-rounding
return f32(xy_z);
return @floatCast(f32, xy_z);
}
}
@ -124,7 +124,7 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 {
var sum = dd_add(a, b);
if (sum.lo != 0) {
var uhii = @bitCast(u64, sum.hi);
const bits_lost = -i32((uhii >> 52) & 0x7FF) - scale + 1;
const bits_lost = -@intCast(i32, (uhii >> 52) & 0x7FF) - scale + 1;
if ((bits_lost != 1) == (uhii & 1 != 0)) {
const uloi = @bitCast(u64, sum.lo);
uhii += 1 - (((uhii ^ uloi) >> 62) & 2);

View File

@ -30,7 +30,7 @@ fn frexp32(x: f32) frexp32_result {
var result: frexp32_result = undefined;
var y = @bitCast(u32, x);
const e = i32(y >> 23) & 0xFF;
const e = @intCast(i32, y >> 23) & 0xFF;
if (e == 0) {
if (x != 0) {
@ -67,7 +67,7 @@ fn frexp64(x: f64) frexp64_result {
var result: frexp64_result = undefined;
var y = @bitCast(u64, x);
const e = i32(y >> 52) & 0x7FF;
const e = @intCast(i32, y >> 52) & 0x7FF;
if (e == 0) {
if (x != 0) {

View File

@ -49,7 +49,7 @@ fn hypot32(x: f32, y: f32) f32 {
yy *= 0x1.0p-90;
}
return z * math.sqrt(f32(f64(x) * x + f64(y) * y));
return z * math.sqrt(@floatCast(f32, f64(x) * x + f64(y) * y));
}
fn sq(hi: *f64, lo: *f64, x: f64) void {

View File

@ -23,7 +23,7 @@ const fp_ilogb0 = fp_ilogbnan;
fn ilogb32(x: f32) i32 {
var u = @bitCast(u32, x);
var e = i32((u >> 23) & 0xFF);
var e = @intCast(i32, (u >> 23) & 0xFF);
// TODO: We should be able to merge this with the lower check.
if (math.isNan(x)) {
@ -59,7 +59,7 @@ fn ilogb32(x: f32) i32 {
fn ilogb64(x: f64) i32 {
var u = @bitCast(u64, x);
var e = i32((u >> 52) & 0x7FF);
var e = @intCast(i32, (u >> 52) & 0x7FF);
if (math.isNan(x)) {
return @maxValue(i32);

View File

@ -227,7 +227,7 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
/// A negative shift amount results in a right shift.
pub fn shl(comptime T: type, a: T, shift_amt: var) T {
const abs_shift_amt = absCast(shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
if (@typeOf(shift_amt).is_signed) {
if (shift_amt >= 0) {
@ -251,7 +251,7 @@ test "math.shl" {
/// A negative shift amount results in a lefft shift.
pub fn shr(comptime T: type, a: T, shift_amt: var) T {
const abs_shift_amt = absCast(shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
if (@typeOf(shift_amt).is_signed) {
if (shift_amt >= 0) {
@ -473,9 +473,9 @@ fn testRem() void {
/// Result is an unsigned integer.
pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
const uint = @IntType(false, @typeOf(x).bit_count);
if (x >= 0) return uint(x);
if (x >= 0) return @intCast(uint, x);
return uint(-(x + 1)) + 1;
return @intCast(uint, -(x + 1)) + 1;
}
test "math.absCast" {
@ -499,7 +499,7 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
if (x == -@minValue(int)) return @minValue(int);
return -int(x);
return -@intCast(int, x);
}
test "math.negateCast" {
@ -522,7 +522,7 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
} else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
return error.Overflow;
} else {
return T(x);
return @intCast(T, x);
}
}
@ -536,6 +536,17 @@ test "math.cast" {
assert(@typeOf(try cast(u8, u32(255))) == u8);
}
pub const AlignCastError = error{UnalignedMemory};
/// Align cast a pointer but return an error if it's the wrong alignment
pub fn alignCast(comptime alignment: u29, ptr: var) AlignCastError!@typeOf(@alignCast(alignment, ptr)) {
const addr = @ptrToInt(ptr);
if (addr % alignment != 0) {
return error.UnalignedMemory;
}
return @alignCast(alignment, ptr);
}
pub fn floorPowerOfTwo(comptime T: type, value: T) T {
var x = value;
@ -554,7 +565,7 @@ test "math.floorPowerOfTwo" {
pub fn log2_int(comptime T: type, x: T) Log2Int(T) {
assert(x != 0);
return Log2Int(T)(T.bit_count - 1 - @clz(x));
return @intCast(Log2Int(T), T.bit_count - 1 - @clz(x));
}
pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
@ -586,3 +597,14 @@ fn testFloorPowerOfTwo() void {
assert(floorPowerOfTwo(u4, 8) == 8);
assert(floorPowerOfTwo(u4, 9) == 8);
}
pub fn lossyCast(comptime T: type, value: var) T {
switch (@typeInfo(@typeOf(value))) {
builtin.TypeId.Int => return @intToFloat(T, value),
builtin.TypeId.Float => return @floatCast(T, value),
builtin.TypeId.ComptimeInt => return T(value),
builtin.TypeId.ComptimeFloat => return T(value),
else => @compileError("bad type"),
}
}

View File

@ -71,7 +71,7 @@ pub fn ln_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -83,7 +83,7 @@ pub fn ln_32(x_: f32) f32 {
const t2 = z * (Lg1 + w * Lg3);
const R = t2 + t1;
const hfsq = 0.5 * f * f;
const dk = f32(k);
const dk = @intToFloat(f32, k);
return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
}
@ -103,7 +103,7 @@ pub fn ln_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -119,7 +119,7 @@ pub fn ln_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, ix) >> 32);
hx = @intCast(u32, @bitCast(u64, ix) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -128,7 +128,7 @@ pub fn ln_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -141,7 +141,7 @@ pub fn ln_64(x_: f64) f64 {
const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
const R = t2 + t1;
const dk = f64(k);
const dk = @intToFloat(f64, k);
return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
}

View File

@ -13,22 +13,23 @@ pub fn log(comptime T: type, base: T, x: T) T {
return math.ln(x);
}
const float_base = math.lossyCast(f64, base);
switch (@typeId(T)) {
TypeId.ComptimeFloat => {
return @typeOf(1.0)(math.ln(f64(x)) / math.ln(f64(base)));
return @typeOf(1.0)(math.ln(f64(x)) / math.ln(float_base));
},
TypeId.ComptimeInt => {
return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(float_base)));
},
builtin.TypeId.Int => {
// TODO implement integer log without using float math
return T(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
},
builtin.TypeId.Float => {
switch (T) {
f32 => return f32(math.ln(f64(x)) / math.ln(f64(base))),
f64 => return math.ln(x) / math.ln(f64(base)),
f32 => return @floatCast(f32, math.ln(f64(x)) / math.ln(float_base)),
f64 => return math.ln(x) / math.ln(float_base),
else => @compileError("log not implemented for " ++ @typeName(T)),
}
},

View File

@ -28,7 +28,7 @@ pub fn log10(x: var) @typeOf(x) {
return @typeOf(1)(math.floor(log10_64(f64(x))));
},
TypeId.Int => {
return T(math.floor(log10_64(f64(x))));
return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
},
else => @compileError("log10 not implemented for " ++ @typeName(T)),
}
@ -71,7 +71,7 @@ pub fn log10_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -89,7 +89,7 @@ pub fn log10_32(x_: f32) f32 {
u &= 0xFFFFF000;
hi = @bitCast(f32, u);
const lo = f - hi - hfsq + s * (hfsq + R);
const dk = f32(k);
const dk = @intToFloat(f32, k);
return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi;
}
@ -109,7 +109,7 @@ pub fn log10_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -125,7 +125,7 @@ pub fn log10_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, x) >> 32);
hx = @intCast(u32, @bitCast(u64, x) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -134,7 +134,7 @@ pub fn log10_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -157,7 +157,7 @@ pub fn log10_64(x_: f64) f64 {
// val_hi + val_lo ~ log10(1 + f) + k * log10(2)
var val_hi = hi * ivln10hi;
const dk = f64(k);
const dk = @intToFloat(f64, k);
const y = dk * log10_2hi;
var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;

View File

@ -71,7 +71,7 @@ fn log1p_32(x: f32) f32 {
const uf = 1 + x;
var iu = @bitCast(u32, uf);
iu += 0x3F800000 - 0x3F3504F3;
k = i32(iu >> 23) - 0x7F;
k = @intCast(i32, iu >> 23) - 0x7F;
// correction to avoid underflow in c / u
if (k < 25) {
@ -93,7 +93,7 @@ fn log1p_32(x: f32) f32 {
const t2 = z * (Lg1 + w * Lg3);
const R = t2 + t1;
const hfsq = 0.5 * f * f;
const dk = f32(k);
const dk = @intToFloat(f32, k);
return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}
@ -112,7 +112,7 @@ fn log1p_64(x: f64) f64 {
const Lg7: f64 = 1.479819860511658591e-01;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 1;
var c: f64 = undefined;
var f: f64 = undefined;
@ -150,9 +150,9 @@ fn log1p_64(x: f64) f64 {
if (k != 0) {
const uf = 1 + x;
const hu = @bitCast(u64, uf);
var iu = u32(hu >> 32);
var iu = @intCast(u32, hu >> 32);
iu += 0x3FF00000 - 0x3FE6A09E;
k = i32(iu >> 20) - 0x3FF;
k = @intCast(i32, iu >> 20) - 0x3FF;
// correction to avoid underflow in c / u
if (k < 54) {
@ -175,7 +175,7 @@ fn log1p_64(x: f64) f64 {
const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
const R = t2 + t1;
const dk = f64(k);
const dk = @intToFloat(f64, k);
return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}

View File

@ -75,7 +75,7 @@ pub fn log2_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -93,7 +93,7 @@ pub fn log2_32(x_: f32) f32 {
u &= 0xFFFFF000;
hi = @bitCast(f32, u);
const lo = f - hi - hfsq + s * (hfsq + R);
return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + f32(k);
return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + @intToFloat(f32, k);
}
pub fn log2_64(x_: f64) f64 {
@ -109,7 +109,7 @@ pub fn log2_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -125,7 +125,7 @@ pub fn log2_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, x) >> 32);
hx = @intCast(u32, @bitCast(u64, x) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -134,7 +134,7 @@ pub fn log2_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -159,7 +159,7 @@ pub fn log2_64(x_: f64) f64 {
var val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
// spadd(val_hi, val_lo, y)
const y = f64(k);
const y = @intToFloat(f64, k);
const ww = y + val_hi;
val_lo += (y - ww) + val_hi;
val_hi = ww;

View File

@ -29,7 +29,7 @@ fn modf32(x: f32) modf32_result {
var result: modf32_result = undefined;
const u = @bitCast(u32, x);
const e = i32((u >> 23) & 0xFF) - 0x7F;
const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
const us = u & 0x80000000;
// TODO: Shouldn't need this.
@ -57,7 +57,7 @@ fn modf32(x: f32) modf32_result {
return result;
}
const mask = u32(0x007FFFFF) >> u5(e);
const mask = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & mask == 0) {
result.ipart = x;
result.fpart = @bitCast(f32, us);
@ -74,7 +74,7 @@ fn modf64(x: f64) modf64_result {
var result: modf64_result = undefined;
const u = @bitCast(u64, x);
const e = i32((u >> 52) & 0x7FF) - 0x3FF;
const e = @intCast(i32, (u >> 52) & 0x7FF) - 0x3FF;
const us = u & (1 << 63);
if (math.isInf(x)) {
@ -101,7 +101,7 @@ fn modf64(x: f64) modf64_result {
return result;
}
const mask = u64(@maxValue(u64) >> 12) >> u6(e);
const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e);
if (u & mask == 0) {
result.ipart = x;
result.fpart = @bitCast(f64, us);

View File

@ -146,7 +146,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
var xe = r2.exponent;
var x1 = r2.significand;
var i = i32(yi);
var i = @floatToInt(i32, yi);
while (i != 0) : (i >>= 1) {
if (i & 1 == 1) {
a1 *= x1;
@ -171,7 +171,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
fn isOddInteger(x: f64) bool {
const r = math.modf(x);
return r.fpart == 0.0 and i64(r.ipart) & 1 == 1;
return r.fpart == 0.0 and @floatToInt(i64, r.ipart) & 1 == 1;
}
test "math.pow" {

View File

@ -37,7 +37,7 @@ fn scalbn32(x: f32, n_: i32) f32 {
}
}
const u = u32(n +% 0x7F) << 23;
const u = @intCast(u32, n +% 0x7F) << 23;
return y * @bitCast(f32, u);
}
@ -67,7 +67,7 @@ fn scalbn64(x: f64, n_: i32) f64 {
}
}
const u = u64(n +% 0x3FF) << 52;
const u = @intCast(u64, n +% 0x3FF) << 52;
return y * @bitCast(f64, u);
}

View File

@ -60,7 +60,7 @@ fn sin32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -112,7 +112,7 @@ fn sin64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -57,7 +57,7 @@ fn sinh64(x: f64) f64 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
if (x == 0.0 or math.isNan(x)) {

View File

@ -99,7 +99,7 @@ fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) {
}
const ResultType = @IntType(false, T.bit_count / 2);
return ResultType(res);
return @intCast(ResultType, res);
}
test "math.sqrt_int" {

View File

@ -53,7 +53,7 @@ fn tan32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -102,7 +102,7 @@ fn tan64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -68,7 +68,7 @@ fn tanh32(x: f32) f32 {
fn tanh64(x: f64) f64 {
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
var t: f64 = undefined;
@ -100,7 +100,7 @@ fn tanh64(x: f64) f64 {
}
// |x| is subnormal
else {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
t = x;
}

View File

@ -19,7 +19,7 @@ pub fn trunc(x: var) @typeOf(x) {
fn trunc32(x: f32) f32 {
const u = @bitCast(u32, x);
var e = i32(((u >> 23) & 0xFF)) - 0x7F + 9;
var e = @intCast(i32, ((u >> 23) & 0xFF)) - 0x7F + 9;
var m: u32 = undefined;
if (e >= 23 + 9) {
@ -29,7 +29,7 @@ fn trunc32(x: f32) f32 {
e = 1;
}
m = u32(@maxValue(u32)) >> u5(e);
m = u32(@maxValue(u32)) >> @intCast(u5, e);
if (u & m == 0) {
return x;
} else {
@ -40,7 +40,7 @@ fn trunc32(x: f32) f32 {
fn trunc64(x: f64) f64 {
const u = @bitCast(u64, x);
var e = i32(((u >> 52) & 0x7FF)) - 0x3FF + 12;
var e = @intCast(i32, ((u >> 52) & 0x7FF)) - 0x3FF + 12;
var m: u64 = undefined;
if (e >= 52 + 12) {
@ -50,7 +50,7 @@ fn trunc64(x: f64) f64 {
e = 1;
}
m = u64(@maxValue(u64)) >> u6(e);
m = u64(@maxValue(u64)) >> @intCast(u6, e);
if (u & m == 0) {
return x;
} else {

View File

@ -40,16 +40,12 @@ pub const Allocator = struct {
/// Call destroy with the result
/// TODO once #733 is solved, this will replace create
pub fn construct(self: *Allocator, init: var) t: {
// TODO this is a workaround for type getting parsed as Error!&const T
const T = @typeOf(init).Child;
break :t Error!*T;
} {
const T = @typeOf(init).Child;
pub fn construct(self: *Allocator, init: var) Error!*@typeOf(init) {
const T = @typeOf(init);
if (@sizeOf(T) == 0) return &{};
const slice = try self.alloc(T, 1);
const ptr = &slice[0];
ptr.* = init.*;
ptr.* = init;
return ptr;
}
@ -338,7 +334,7 @@ pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) T {
builtin.Endian.Little => {
const ShiftType = math.Log2Int(T);
for (bytes) |b, index| {
result = result | (T(b) << ShiftType(index * 8));
result = result | (T(b) << @intCast(ShiftType, index * 8));
}
},
}

View File

@ -413,7 +413,7 @@ pub const ChildProcess = struct {
}
// we are the parent
const pid = i32(pid_result);
const pid = @intCast(i32, pid_result);
if (self.stdin_behavior == StdIo.Pipe) {
self.stdin = os.File.openHandle(stdin_pipe[1]);
} else {

View File

@ -290,7 +290,7 @@ pub fn WIFSIGNALED(x: i32) bool {
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}
pub fn close(fd: i32) usize {
@ -339,7 +339,14 @@ pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
}
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
const ptr_result = c.mmap(
@ptrCast(*c_void, address),
length,
@bitCast(c_int, @intCast(c_uint, prot)),
@bitCast(c_int, c_uint(flags)),
fd,
offset,
);
const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
return errnoWrap(isize_result);
}

View File

@ -265,17 +265,8 @@ pub const File = struct {
pub fn getEndPos(self: *File) !usize {
if (is_posix) {
var stat: posix.Stat = undefined;
const err = posix.getErrno(posix.fstat(self.handle, &stat));
if (err > 0) {
return switch (err) {
posix.EBADF => error.BadFd,
posix.ENOMEM => error.SystemResources,
else => os.unexpectedErrorPosix(err),
};
}
return usize(stat.size);
const stat = try os.posixFStat(self.handle);
return @intCast(usize, stat.size);
} else if (is_windows) {
var file_size: windows.LARGE_INTEGER = undefined;
if (windows.GetFileSizeEx(self.handle, &file_size) == 0) {
@ -286,7 +277,7 @@ pub const File = struct {
}
if (file_size < 0)
return error.Overflow;
return math.cast(usize, u64(file_size));
return math.cast(usize, @intCast(u64, file_size));
} else {
@compileError("TODO support getEndPos on this OS");
}
@ -320,9 +311,15 @@ pub const File = struct {
}
}
pub const ReadError = error{};
pub const ReadError = error{
BadFd,
Io,
IsDir,
pub fn read(self: *File, buffer: []u8) !usize {
Unexpected,
};
pub fn read(self: *File, buffer: []u8) ReadError!usize {
if (is_posix) {
var index: usize = 0;
while (index < buffer.len) {
@ -335,6 +332,7 @@ pub const File = struct {
posix.EFAULT => unreachable,
posix.EBADF => return error.BadFd,
posix.EIO => return error.Io,
posix.EISDIR => return error.IsDir,
else => return os.unexpectedErrorPosix(read_err),
}
}
@ -345,7 +343,7 @@ pub const File = struct {
} else if (is_windows) {
var index: usize = 0;
while (index < buffer.len) {
const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
const want_read_count = @intCast(windows.DWORD, math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
var amt_read: windows.DWORD = undefined;
if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
const err = windows.GetLastError();

View File

@ -126,7 +126,7 @@ pub fn getRandomBytes(buf: []u8) !void {
}
defer _ = windows.CryptReleaseContext(hCryptProv, 0);
if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) {
if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) {
const err = windows.GetLastError();
return switch (err) {
else => unexpectedErrorWindows(err),
@ -343,7 +343,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 {
else => return unexpectedErrorPosix(err),
}
}
return i32(result);
return @intCast(i32, result);
}
}
@ -586,7 +586,7 @@ pub fn getCwd(allocator: *Allocator) ![]u8 {
errdefer allocator.free(buf);
while (true) {
const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
const result = windows.GetCurrentDirectoryA(@intCast(windows.WORD, buf.len), buf.ptr);
if (result == 0) {
const err = windows.GetLastError();
@ -2019,7 +2019,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
const rc = posix.socket(domain, socket_type, protocol);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
posix.EACCES => return PosixSocketError.PermissionDenied,
posix.EAFNOSUPPORT => return PosixSocketError.AddressFamilyNotSupported,
posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable,
@ -2183,7 +2183,7 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!
const rc = posix.accept4(fd, addr, &sockaddr_size, flags);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
posix.EINTR => continue,
else => return unexpectedErrorPosix(err),
@ -2226,7 +2226,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
const rc = posix.epoll_create1(flags);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
else => return unexpectedErrorPosix(err),
posix.EINVAL => return LinuxEpollCreateError.InvalidSyscall,
@ -2296,7 +2296,7 @@ pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) Lin
pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
while (true) {
const rc = posix.epoll_wait(epfd, events.ptr, u32(events.len), timeout);
const rc = posix.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout);
const err = posix.getErrno(rc);
switch (err) {
0 => return rc,
@ -2661,7 +2661,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
posix.EAGAIN => return SpawnThreadError.SystemResources,
posix.EPERM => unreachable,
posix.EINVAL => unreachable,
else => return unexpectedErrorPosix(usize(err)),
else => return unexpectedErrorPosix(@intCast(usize, err)),
}
} else if (builtin.os == builtin.Os.linux) {
// use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly
@ -2697,3 +2697,17 @@ pub fn posixWait(pid: i32) i32 {
}
}
}
pub fn posixFStat(fd: i32) !posix.Stat {
var stat: posix.Stat = undefined;
const err = posix.getErrno(posix.fstat(fd, &stat));
if (err > 0) {
return switch (err) {
posix.EBADF => error.BadFd,
posix.ENOMEM => error.SystemResources,
else => os.unexpectedErrorPosix(err),
};
}
return stat;
}

View File

@ -642,7 +642,7 @@ pub fn WIFEXITED(s: i32) bool {
return WTERMSIG(s) == 0;
}
pub fn WIFSTOPPED(s: i32) bool {
return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
}
pub fn WIFSIGNALED(s: i32) bool {
return (unsigned(s) & 0xffff) -% 1 < 0xff;
@ -658,11 +658,11 @@ pub const winsize = extern struct {
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}
pub fn dup2(old: i32, new: i32) usize {
return syscall2(SYS_dup2, usize(old), usize(new));
return syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -693,12 +693,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
}
pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
return syscall3(SYS_getdents, usize(fd), @ptrToInt(dirp), count);
return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
}
pub fn isatty(fd: i32) bool {
var wsz: winsize = undefined;
return syscall3(SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
return syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
}
// TODO https://github.com/ziglang/zig/issues/265
@ -727,7 +727,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
}
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), @bitCast(usize, offset));
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
}
pub fn munmap(address: usize, length: usize) usize {
@ -735,7 +735,7 @@ pub fn munmap(address: usize, length: usize) usize {
}
pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
return syscall3(SYS_read, usize(fd), @ptrToInt(buf), count);
return syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -749,7 +749,7 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
}
pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
return syscall4(SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -766,11 +766,11 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {
}
pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
return syscall3(SYS_write, usize(fd), @ptrToInt(buf), count);
return syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count);
}
pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
return syscall4(SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
return syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -790,7 +790,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
// TODO https://github.com/ziglang/zig/issues/265
pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
return syscall4(SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
}
/// See also `clone` (from the arch-specific include)
@ -804,11 +804,11 @@ pub fn clone2(flags: usize, child_stack_ptr: usize) usize {
}
pub fn close(fd: i32) usize {
return syscall1(SYS_close, usize(fd));
return syscall1(SYS_close, @intCast(usize, fd));
}
pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
return syscall3(SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos);
return syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos);
}
pub fn exit(status: i32) noreturn {
@ -817,11 +817,11 @@ pub fn exit(status: i32) noreturn {
}
pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
return syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));
return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
}
pub fn kill(pid: i32, sig: i32) usize {
return syscall2(SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @intCast(usize, sig));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -999,8 +999,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len;
pub fn raise(sig: i32) usize {
var set: sigset_t = undefined;
blockAppSignals(&set);
const tid = i32(syscall0(SYS_gettid));
const ret = syscall2(SYS_tkill, usize(tid), usize(sig));
const tid = @intCast(i32, syscall0(SYS_gettid));
const ret = syscall2(SYS_tkill, @intCast(usize, tid), @intCast(usize, sig));
restoreSignals(&set);
return ret;
}
@ -1019,12 +1019,12 @@ fn restoreSignals(set: *sigset_t) void {
pub fn sigaddset(set: *sigset_t, sig: u6) void {
const s = sig - 1;
(set.*)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
(set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
}
pub fn sigismember(set: *const sigset_t, sig: u6) bool {
const s = sig - 1;
return ((set.*)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
}
pub const in_port_t = u16;
@ -1057,11 +1057,11 @@ pub const iovec = extern struct {
};
pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
return syscall3(SYS_getsockname, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
}
pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
return syscall3(SYS_getpeername, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
}
pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
@ -1069,47 +1069,47 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
}
pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
return syscall5(SYS_setsockopt, usize(fd), level, optname, usize(optval), @ptrToInt(optlen));
return syscall5(SYS_setsockopt, @intCast(usize, fd), level, optname, @intCast(usize, optval), @ptrToInt(optlen));
}
pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
return syscall5(SYS_getsockopt, usize(fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
return syscall5(SYS_getsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
}
pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize {
return syscall3(SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
return syscall3(SYS_sendmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
}
pub fn connect(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
return syscall3(SYS_connect, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
}
pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
return syscall3(SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
return syscall3(SYS_recvmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
}
pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
return syscall6(SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
return syscall6(SYS_recvfrom, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
}
pub fn shutdown(fd: i32, how: i32) usize {
return syscall2(SYS_shutdown, usize(fd), usize(how));
return syscall2(SYS_shutdown, @intCast(usize, fd), @intCast(usize, how));
}
pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
return syscall3(SYS_bind, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
}
pub fn listen(fd: i32, backlog: u32) usize {
return syscall2(SYS_listen, usize(fd), backlog);
return syscall2(SYS_listen, @intCast(usize, fd), backlog);
}
pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
return syscall6(SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
return syscall6(SYS_sendto, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
}
pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
return syscall4(SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(*fd[0]));
return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(*fd[0]));
}
pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
@ -1117,11 +1117,11 @@ pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
}
pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
return syscall4(SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
return syscall4(SYS_accept4, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len), flags);
}
pub fn fstat(fd: i32, stat_buf: *Stat) usize {
return syscall2(SYS_fstat, usize(fd), @ptrToInt(stat_buf));
return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -1214,15 +1214,15 @@ pub fn epoll_create1(flags: usize) usize {
}
pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
return syscall4(SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
return syscall4(SYS_epoll_ctl, @intCast(usize, epoll_fd), @intCast(usize, op), @intCast(usize, fd), @ptrToInt(ev));
}
pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
return syscall4(SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
}
pub fn timerfd_create(clockid: i32, flags: u32) usize {
return syscall2(SYS_timerfd_create, usize(clockid), usize(flags));
return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
}
pub const itimerspec = extern struct {
@ -1231,11 +1231,11 @@ pub const itimerspec = extern struct {
};
pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
return syscall2(SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
return syscall2(SYS_timerfd_gettime, @intCast(usize, fd), @ptrToInt(curr_value));
}
pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
return syscall4(SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
return syscall4(SYS_timerfd_settime, @intCast(usize, fd), @intCast(usize, flags), @ptrToInt(new_value), @ptrToInt(old_value));
}
pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
@ -1345,7 +1345,7 @@ pub const cap_user_data_t = extern struct {
};
pub fn unshare(flags: usize) usize {
return syscall1(SYS_unshare, usize(flags));
return syscall1(SYS_unshare, @intCast(usize, flags));
}
pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {

View File

@ -21,7 +21,7 @@ test "timer" {
.it_value = time_interval,
};
err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
err = linux.timerfd_settime(@intCast(i32, timer_fd), 0, &new_time, null);
assert(err == 0);
var event = linux.epoll_event{
@ -29,12 +29,12 @@ test "timer" {
.data = linux.epoll_data{ .ptr = 0 },
};
err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);
err = linux.epoll_ctl(@intCast(i32, epoll_fd), linux.EPOLL_CTL_ADD, @intCast(i32, timer_fd), &event);
assert(err == 0);
const events_one: linux.epoll_event = undefined;
var events = []linux.epoll_event{events_one} ** 8;
// TODO implicit cast from *[N]T to [*]T
err = linux.epoll_wait(i32(epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
err = linux.epoll_wait(@intCast(i32, epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
}

View File

@ -62,8 +62,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
var i: usize = 0;
while (i < hashtab[1]) : (i += 1) {
if (0 == (u32(1) << u5(syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << u5(syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == (u32(1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == syms[i].st_shndx) continue;
if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue;
if (maybe_versym) |versym| {

View File

@ -14,12 +14,12 @@ pub const epoch = @import("epoch.zig");
pub fn sleep(seconds: usize, nanoseconds: usize) void {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
posixSleep(u63(seconds), u63(nanoseconds));
posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds));
},
Os.windows => {
const ns_per_ms = ns_per_s / ms_per_s;
const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
windows.Sleep(windows.DWORD(milliseconds));
windows.Sleep(@intCast(windows.DWORD, milliseconds));
},
else => @compileError("Unsupported OS"),
}
@ -83,8 +83,8 @@ fn milliTimestampDarwin() u64 {
var tv: darwin.timeval = undefined;
var err = darwin.gettimeofday(&tv, null);
debug.assert(err == 0);
const sec_ms = u64(tv.tv_sec) * ms_per_s;
const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
const sec_ms = @intCast(u64, tv.tv_sec) * ms_per_s;
const usec_ms = @divFloor(@intCast(u64, tv.tv_usec), us_per_s / ms_per_s);
return u64(sec_ms) + u64(usec_ms);
}
@ -95,8 +95,8 @@ fn milliTimestampPosix() u64 {
var ts: posix.timespec = undefined;
const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
debug.assert(err == 0);
const sec_ms = u64(ts.tv_sec) * ms_per_s;
const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s);
const sec_ms = @intCast(u64, ts.tv_sec) * ms_per_s;
const nsec_ms = @divFloor(@intCast(u64, ts.tv_nsec), ns_per_s / ms_per_s);
return sec_ms + nsec_ms;
}
@ -162,13 +162,13 @@ pub const Timer = struct {
var freq: i64 = undefined;
var err = windows.QueryPerformanceFrequency(&freq);
if (err == windows.FALSE) return error.TimerUnsupported;
self.frequency = u64(freq);
self.frequency = @intCast(u64, freq);
self.resolution = @divFloor(ns_per_s, self.frequency);
var start_time: i64 = undefined;
err = windows.QueryPerformanceCounter(&start_time);
debug.assert(err != windows.FALSE);
self.start_time = u64(start_time);
self.start_time = @intCast(u64, start_time);
},
Os.linux => {
//On Linux, seccomp can do arbitrary things to our ability to call
@ -184,12 +184,12 @@ pub const Timer = struct {
posix.EINVAL => return error.TimerUnsupported,
else => return std.os.unexpectedErrorPosix(errno),
}
self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
self.resolution = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
result = posix.clock_gettime(monotonic_clock_id, &ts);
errno = posix.getErrno(result);
if (errno != 0) return std.os.unexpectedErrorPosix(errno);
self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
self.start_time = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
},
Os.macosx, Os.ios => {
darwin.mach_timebase_info(&self.frequency);
@ -236,7 +236,7 @@ pub const Timer = struct {
var result: i64 = undefined;
var err = windows.QueryPerformanceCounter(&result);
debug.assert(err != windows.FALSE);
return u64(result);
return @intCast(u64, result);
}
fn clockDarwin() u64 {
@ -247,7 +247,7 @@ pub const Timer = struct {
var ts: posix.timespec = undefined;
var result = posix.clock_gettime(monotonic_clock_id, &ts);
debug.assert(posix.getErrno(result) == 0);
return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
return @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
}
};

View File

@ -42,7 +42,7 @@ pub const WriteError = error{
};
pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void {
if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), @intCast(u32, bytes.len), null, null) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources,
@ -68,7 +68,12 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
const size = @sizeOf(windows.FILE_NAME_INFO);
var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH);
if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
if (windows.GetFileInformationByHandleEx(
handle,
windows.FileNameInfo,
@ptrCast(*c_void, &name_info_bytes[0]),
@intCast(u32, name_info_bytes.len),
) == 0) {
return true;
}

View File

@ -55,16 +55,16 @@ pub const Random = struct {
if (T.is_signed) {
const uint = @IntType(false, T.bit_count);
if (start >= 0 and end >= 0) {
return T(r.range(uint, uint(start), uint(end)));
return @intCast(T, r.range(uint, @intCast(uint, start), @intCast(uint, end)));
} else if (start < 0 and end < 0) {
// Can't overflow because the range is over signed ints
return math.negateCast(r.range(uint, math.absCast(end), math.absCast(start)) + 1) catch unreachable;
} else if (start < 0 and end >= 0) {
const end_uint = uint(end);
const end_uint = @intCast(uint, end);
const total_range = math.absCast(start) + end_uint;
const value = r.range(uint, 0, total_range);
const result = if (value < end_uint) x: {
break :x T(value);
break :x @intCast(T, value);
} else if (value == end_uint) x: {
break :x start;
} else x: {
@ -213,9 +213,9 @@ pub const Pcg = struct {
self.s = l *% default_multiplier +% (self.i | 1);
const xor_s = @truncate(u32, ((l >> 18) ^ l) >> 27);
const rot = u32(l >> 59);
const rot = @intCast(u32, l >> 59);
return (xor_s >> u5(rot)) | (xor_s << u5((0 -% rot) & 31));
return (xor_s >> @intCast(u5, rot)) | (xor_s << @intCast(u5, (0 -% rot) & 31));
}
fn seed(self: *Pcg, init_s: u64) void {
@ -322,7 +322,7 @@ pub const Xoroshiro128 = struct {
inline for (table) |entry| {
var b: usize = 0;
while (b < 64) : (b += 1) {
if ((entry & (u64(1) << u6(b))) != 0) {
if ((entry & (u64(1) << @intCast(u6, b))) != 0) {
s0 ^= self.s[0];
s1 ^= self.s[1];
}
@ -667,13 +667,13 @@ test "Random range" {
}
fn testRange(r: *Random, start: i32, end: i32) void {
const count = usize(end - start);
const count = @intCast(usize, end - start);
var values_buffer = []bool{false} ** 20;
const values = values_buffer[0..count];
var i: usize = 0;
while (i < count) {
const value = r.range(i32, start, end);
const index = usize(value - start);
const index = @intCast(usize, value - start);
if (!values[index]) {
i += 1;
values[index] = true;

View File

@ -104,7 +104,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
pub fn deinit(self: *Self) void {
self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
self.freeShelves(@intCast(ShelfIndex, self.dynamic_segments.len), 0);
self.allocator.free(self.dynamic_segments);
self.* = undefined;
}
@ -158,7 +158,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
/// Only grows capacity, or retains current capacity
pub fn growCapacity(self: *Self, new_capacity: usize) !void {
const new_cap_shelf_count = shelfCount(new_capacity);
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
if (new_cap_shelf_count > old_shelf_count) {
self.dynamic_segments = try self.allocator.realloc([*]T, self.dynamic_segments, new_cap_shelf_count);
var i = old_shelf_count;
@ -175,7 +175,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
/// Only shrinks capacity or retains current capacity
pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
if (new_capacity <= prealloc_item_count) {
const len = ShelfIndex(self.dynamic_segments.len);
const len = @intCast(ShelfIndex, self.dynamic_segments.len);
self.freeShelves(len, 0);
self.allocator.free(self.dynamic_segments);
self.dynamic_segments = [][*]T{};
@ -183,7 +183,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
const new_cap_shelf_count = shelfCount(new_capacity);
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
assert(new_cap_shelf_count <= old_shelf_count);
if (new_cap_shelf_count == old_shelf_count) {
return;
@ -338,7 +338,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
{
var i: usize = 0;
while (i < 100) : (i += 1) {
try list.push(i32(i + 1));
try list.push(@intCast(i32, i + 1));
assert(list.len == i + 1);
}
}
@ -346,7 +346,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
{
var i: usize = 0;
while (i < 100) : (i += 1) {
assert(list.at(i).* == i32(i + 1));
assert(list.at(i).* == @intCast(i32, i + 1));
}
}

View File

@ -80,7 +80,7 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
return callMainWithArgs(usize(c_argc), c_argv, envp);
return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
}
fn callMain() u8 {

View File

@ -135,9 +135,9 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
const mask = if (T == f32) 0xff else 0x7ff;
var ux = @bitCast(uint, x);
var uy = @bitCast(uint, y);
var ex = i32((ux >> digits) & mask);
var ey = i32((uy >> digits) & mask);
const sx = if (T == f32) u32(ux & 0x80000000) else i32(ux >> bits_minus_1);
var ex = @intCast(i32, (ux >> digits) & mask);
var ey = @intCast(i32, (uy >> digits) & mask);
const sx = if (T == f32) @intCast(u32, ux & 0x80000000) else @intCast(i32, ux >> bits_minus_1);
var i: uint = undefined;
if (uy << 1 == 0 or isNan(uint, uy) or ex == mask)
@ -156,7 +156,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
ex -= 1;
i <<= 1;
}) {}
ux <<= log2uint(@bitCast(u32, -ex + 1));
ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1));
} else {
ux &= @maxValue(uint) >> exp_bits;
ux |= 1 << digits;
@ -167,7 +167,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
ey -= 1;
i <<= 1;
}) {}
uy <<= log2uint(@bitCast(u32, -ey + 1));
uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1));
} else {
uy &= @maxValue(uint) >> exp_bits;
uy |= 1 << digits;
@ -199,12 +199,12 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
ux -%= 1 << digits;
ux |= uint(@bitCast(u32, ex)) << digits;
} else {
ux >>= log2uint(@bitCast(u32, -ex + 1));
ux >>= @intCast(log2uint, @bitCast(u32, -ex + 1));
}
if (T == f32) {
ux |= sx;
} else {
ux |= uint(sx) << bits_minus_1;
ux |= @intCast(uint, sx) << bits_minus_1;
}
return @bitCast(T, ux);
}
@ -229,8 +229,8 @@ export fn sqrt(x: f64) f64 {
const sign: u32 = 0x80000000;
const u = @bitCast(u64, x);
var ix0 = u32(u >> 32);
var ix1 = u32(u & 0xFFFFFFFF);
var ix0 = @intCast(u32, u >> 32);
var ix1 = @intCast(u32, u & 0xFFFFFFFF);
// sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan
if (ix0 & 0x7FF00000 == 0x7FF00000) {
@ -247,7 +247,7 @@ export fn sqrt(x: f64) f64 {
}
// normalize x
var m = i32(ix0 >> 20);
var m = @intCast(i32, ix0 >> 20);
if (m == 0) {
// subnormal
while (ix0 == 0) {
@ -261,9 +261,9 @@ export fn sqrt(x: f64) f64 {
while (ix0 & 0x00100000 == 0) : (i += 1) {
ix0 <<= 1;
}
m -= i32(i) - 1;
ix0 |= ix1 >> u5(32 - i);
ix1 <<= u5(i);
m -= @intCast(i32, i) - 1;
ix0 |= ix1 >> @intCast(u5, 32 - i);
ix1 <<= @intCast(u5, i);
}
// unbias exponent
@ -347,10 +347,10 @@ export fn sqrt(x: f64) f64 {
// NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same
// behaviour at least.
var iix0 = i32(ix0);
var iix0 = @intCast(i32, ix0);
iix0 = iix0 +% (m << 20);
const uz = (u64(iix0) << 32) | ix1;
const uz = (@intCast(u64, iix0) << 32) | ix1;
return @bitCast(f64, uz);
}

View File

@ -91,5 +91,5 @@ pub extern fn __unordtf2(a: f128, b: f128) c_int {
const aAbs = @bitCast(rep_t, a) & absMask;
const bAbs = @bitCast(rep_t, b) & absMask;
return c_int(aAbs > infRep or bAbs > infRep);
return @boolToInt(aAbs > infRep or bAbs > infRep);
}

View File

@ -13,7 +13,7 @@ pub extern fn __divti3(a: i128, b: i128) i128 {
const r = udivmod(u128, @bitCast(u128, an), @bitCast(u128, bn), null);
const s = s_a ^ s_b;
return (i128(r) ^ s) -% s;
return (@bitCast(i128, r) ^ s) -% s;
}
pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {

View File

@ -32,14 +32,14 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
const aAbs: rep_t = aRep & absMask;
const sign = if ((aRep & signBit) != 0) i32(-1) else i32(1);
const exponent = i32(aAbs >> significandBits) - exponentBias;
const exponent = @intCast(i32, aAbs >> significandBits) - exponentBias;
const significand: rep_t = (aAbs & significandMask) | implicitBit;
// If either the value or the exponent is negative, the result is zero.
if (sign == -1 or exponent < 0) return 0;
// If the value is too large for the integer type, saturate.
if (c_uint(exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
if (@intCast(c_uint, exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
// If 0 <= exponent < significandBits, right shift to get the result.
// Otherwise, shift left.
@ -47,11 +47,11 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
// TODO this is a workaround for the mysterious "integer cast truncated bits"
// happening on the next line
@setRuntimeSafety(false);
return fixuint_t(significand >> Log2Int(rep_t)(significandBits - exponent));
return @intCast(fixuint_t, significand >> @intCast(Log2Int(rep_t), significandBits - exponent));
} else {
// TODO this is a workaround for the mysterious "integer cast truncated bits"
// happening on the next line
@setRuntimeSafety(false);
return fixuint_t(significand) << Log2Int(fixuint_t)(exponent - significandBits);
return @intCast(fixuint_t, significand) << @intCast(Log2Int(fixuint_t), exponent - significandBits);
}
}

View File

@ -292,7 +292,7 @@ extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 {
@setRuntimeSafety(is_test);
const d = __udivsi3(a, b);
rem.* = u32(i32(a) -% (i32(d) * i32(b)));
rem.* = @bitCast(u32, @bitCast(i32, a) -% (@bitCast(i32, d) * @bitCast(i32, b)));
return d;
}
@ -316,12 +316,12 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
sr += 1;
// 1 <= sr <= n_uword_bits - 1
// Not a special case
var q: u32 = n << u5(n_uword_bits - sr);
var r: u32 = n >> u5(sr);
var q: u32 = n << @intCast(u5, n_uword_bits - sr);
var r: u32 = n >> @intCast(u5, sr);
var carry: u32 = 0;
while (sr > 0) : (sr -= 1) {
// r:q = ((r:q) << 1) | carry
r = (r << 1) | (q >> u5(n_uword_bits - 1));
r = (r << 1) | (q >> @intCast(u5, n_uword_bits - 1));
q = (q << 1) | carry;
// carry = 0;
// if (r.all >= d.all)
@ -329,8 +329,8 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
// r.all -= d.all;
// carry = 1;
// }
const s = i32(d -% r -% 1) >> u5(n_uword_bits - 1);
carry = u32(s & 1);
const s = @intCast(i32, d -% r -% 1) >> @intCast(u5, n_uword_bits - 1);
carry = @intCast(u32, s & 1);
r -= d & @bitCast(u32, s);
}
q = (q << 1) | carry;

View File

@ -71,7 +71,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
r[high] = n[high] & (d[high] - 1);
rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
}
return n[high] >> Log2SingleInt(@ctz(d[high]));
return n[high] >> @intCast(Log2SingleInt, @ctz(d[high]));
}
// K K
// ---
@ -88,10 +88,10 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// 1 <= sr <= SingleInt.bit_count - 1
// q.all = a << (DoubleInt.bit_count - sr);
q[low] = 0;
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
// r.all = a >> sr;
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
} else {
// d[low] != 0
if (d[high] == 0) {
@ -107,8 +107,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
return a;
}
sr = @ctz(d[low]);
q[high] = n[high] >> Log2SingleInt(sr);
q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[high] >> @intCast(Log2SingleInt, sr);
q[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
}
// K X
@ -126,15 +126,15 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
} else if (sr < SingleInt.bit_count) {
// 2 <= sr <= SingleInt.bit_count - 1
q[low] = 0;
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
} else {
// SingleInt.bit_count + 1 <= sr <= DoubleInt.bit_count - 1
q[low] = n[low] << Log2SingleInt(DoubleInt.bit_count - sr);
q[high] = (n[high] << Log2SingleInt(DoubleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr - SingleInt.bit_count));
q[low] = n[low] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr);
q[high] = (n[high] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count));
r[high] = 0;
r[low] = n[high] >> Log2SingleInt(sr - SingleInt.bit_count);
r[low] = n[high] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count);
}
} else {
// K X
@ -158,9 +158,9 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
r[high] = 0;
r[low] = n[high];
} else {
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
}
}
}
@ -184,8 +184,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// carry = 1;
// }
r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
carry = u32(s & 1);
const s: SignedDoubleInt = @intCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
carry = @intCast(u32, s & 1);
r_all -= b & @bitCast(DoubleInt, s);
r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
}

View File

@ -35,22 +35,22 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
// - Increasing the initial shift by 6 each time
// - Each time after the first shorten the shifted
// value to a max of 0b111111 (63)
1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range
1 => out[0] = @intCast(u8, c), // Can just do 0 + codepoint for initial range
2 => {
out[0] = u8(0b11000000 | (c >> 6));
out[1] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11000000 | (c >> 6));
out[1] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
3 => {
if (0xd800 <= c and c <= 0xdfff) return error.Utf8CannotEncodeSurrogateHalf;
out[0] = u8(0b11100000 | (c >> 12));
out[1] = u8(0b10000000 | ((c >> 6) & 0b111111));
out[2] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11100000 | (c >> 12));
out[1] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
out[2] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
4 => {
out[0] = u8(0b11110000 | (c >> 18));
out[1] = u8(0b10000000 | ((c >> 12) & 0b111111));
out[2] = u8(0b10000000 | ((c >> 6) & 0b111111));
out[3] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11110000 | (c >> 18));
out[1] = @intCast(u8, 0b10000000 | ((c >> 12) & 0b111111));
out[2] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
out[3] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
else => unreachable,
}

View File

@ -1128,7 +1128,7 @@ pub const Tokenizer = struct {
// check utf8-encoded character.
const length = std.unicode.utf8ByteSequenceLength(c0) catch return 1;
if (self.index + length > self.buffer.len) {
return u3(self.buffer.len - self.index);
return @intCast(u3, self.buffer.len - self.index);
}
const bytes = self.buffer[self.index .. self.index + length];
switch (length) {

View File

@ -13,6 +13,7 @@ comptime {
_ = @import("cases/bugs/656.zig");
_ = @import("cases/bugs/828.zig");
_ = @import("cases/bugs/920.zig");
_ = @import("cases/byval_arg_var.zig");
_ = @import("cases/cast.zig");
_ = @import("cases/const_slice_child.zig");
_ = @import("cases/coroutines.zig");

View File

@ -13,9 +13,19 @@ pub fn addCases(cases: *tests.BuildExamplesContext) void {
cases.addBuildFile("example/shared_library/build.zig");
cases.addBuildFile("example/mix_o_files/build.zig");
}
cases.addBuildFile("test/standalone/issue_339/build.zig");
if (builtin.os != builtin.Os.macosx) {
// TODO https://github.com/ziglang/zig/issues/1126
cases.addBuildFile("test/standalone/issue_339/build.zig");
}
cases.addBuildFile("test/standalone/issue_794/build.zig");
cases.addBuildFile("test/standalone/pkg_import/build.zig");
cases.addBuildFile("test/standalone/use_alias/build.zig");
cases.addBuildFile("test/standalone/brace_expansion/build.zig");
if (false) {
// TODO this test is disabled because it is failing on the CI server's linux. when this is fixed
// enable it for at least linux
// TODO hook up the DynLib API for windows using LoadLibraryA
// TODO figure out how to make this work on darwin - probably libSystem has dlopen/dlsym in it
cases.addBuildFile("test/standalone/load_dynamic_library/build.zig");
}
}

View File

@ -8,14 +8,14 @@ test "bool literals" {
test "cast bool to int" {
const t = true;
const f = false;
assert(i32(t) == i32(1));
assert(i32(f) == i32(0));
assert(@boolToInt(t) == u32(1));
assert(@boolToInt(f) == u32(0));
nonConstCastBoolToInt(t, f);
}
fn nonConstCastBoolToInt(t: bool, f: bool) void {
assert(i32(t) == i32(1));
assert(i32(f) == i32(0));
assert(@boolToInt(t) == u32(1));
assert(@boolToInt(f) == u32(0));
}
test "bool cmp" {

View File

@ -0,0 +1,27 @@
const std = @import("std");
var result: []const u8 = "wrong";
test "aoeu" {
start();
blowUpStack(10);
std.debug.assert(std.mem.eql(u8, result, "string literal"));
}
fn start() void {
foo("string literal");
}
fn foo(x: var) void {
bar(x);
}
fn bar(x: var) void {
result = x;
}
fn blowUpStack(x: u32) void {
if (x == 0) return;
blowUpStack(x - 1);
}

View File

@ -318,14 +318,6 @@ fn testCastConstArrayRefToConstSlice() void {
assert(mem.eql(u8, slice, "aoeu"));
}
test "var args implicitly casts by value arg to const ref" {
foo("hello");
}
fn foo(args: ...) void {
assert(@typeOf(args[0]) == *const [5]u8);
}
test "peer type resolution: error and [N]T" {
// TODO: implicit error!T to error!U where T can implicitly cast to U
//assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
@ -351,7 +343,7 @@ fn testPeerErrorAndArray2(x: u8) error![]const u8 {
test "explicit cast float number literal to integer if no fraction component" {
const x = i32(1e4);
assert(x == 10000);
const y = i32(f32(1e4));
const y = @floatToInt(i32, f32(1e4));
assert(y == 10000);
}
@ -406,3 +398,25 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
const x: [*]const ?[*]const u8 = &window_name;
assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name"));
}
test "@intCast comptime_int" {
const result = @intCast(i32, 1234);
assert(@typeOf(result) == i32);
assert(result == 1234);
}
test "@floatCast comptime_int and comptime_float" {
const result = @floatCast(f32, 1234);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
const result2 = @floatCast(f32, 1234.0);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
}
test "comptime_int @intToFloat" {
const result = @intToFloat(f32, 1234);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
}

View File

@ -99,7 +99,7 @@ test "int to enum" {
testIntToEnumEval(3);
}
fn testIntToEnumEval(x: i32) void {
assert(IntToEnumNumber(u3(x)) == IntToEnumNumber.Three);
assert(IntToEnumNumber(@intCast(u3, x)) == IntToEnumNumber.Three);
}
const IntToEnumNumber = enum {
Zero,

View File

@ -5,7 +5,7 @@ const builtin = @import("builtin");
test "compile time recursion" {
assert(some_data.len == 21);
}
var some_data: [usize(fibonacci(7))]u8 = undefined;
var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
fn fibonacci(x: i32) i32 {
if (x <= 1) return 1;
return fibonacci(x - 1) + fibonacci(x - 2);
@ -356,7 +356,7 @@ const global_array = x: {
test "compile-time downcast when the bits fit" {
comptime {
const spartan_count: u16 = 255;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
assert(byte == 255);
}
}
@ -440,7 +440,7 @@ test "binary math operator in partially inlined function" {
var b: [16]u8 = undefined;
for (b) |*r, i|
r.* = u8(i + 1);
r.* = @intCast(u8, i + 1);
copyWithPartialInline(s[0..], b[0..]);
assert(s[0] == 0x1020304);
@ -480,7 +480,7 @@ fn generateTable(comptime T: type) [1010]T {
var res: [1010]T = undefined;
var i: usize = 0;
while (i < 1010) : (i += 1) {
res[i] = T(i);
res[i] = @intCast(T, i);
}
return res;
}

Some files were not shown because too many files have changed in this diff Show More