authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-15 07:59:46+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-15 07:59:46+03:00
log97946e2a41a6a44ed376b57b32a19ce9d85f0bc2
tree5c7de4068bbb50e11ebe414bfc61540bc85b79ce
parentac42503266f27af2a4528af3ff0f1ef2cfbfe766
parent6dbdac4b605fb56b4180b6c66154275c22954a8d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9091 from Vexu/translate-c

Translate-c: move utility functions to a separate namespace

11 files changed, 999 insertions(+), 976 deletions(-)

lib/std/c.zig-1
......@@ -10,7 +10,6 @@ const page_size = std.mem.page_size;
1010pub const tokenizer = @import("c/tokenizer.zig");
1111pub const Token = tokenizer.Token;
1212pub const Tokenizer = tokenizer.Tokenizer;
13pub const builtins = @import("c/builtins.zig");
1413
1514test {
1615 _ = tokenizer;
lib/std/c/builtins.zig deleted-196
......@@ -1,196 +0,0 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6
7const std = @import("std");
8
9pub inline fn __builtin_bswap16(val: u16) u16 {
10 return @byteSwap(u16, val);
11}
12pub inline fn __builtin_bswap32(val: u32) u32 {
13 return @byteSwap(u32, val);
14}
15pub inline fn __builtin_bswap64(val: u64) u64 {
16 return @byteSwap(u64, val);
17}
18
19pub inline fn __builtin_signbit(val: f64) c_int {
20 return @boolToInt(std.math.signbit(val));
21}
22pub inline fn __builtin_signbitf(val: f32) c_int {
23 return @boolToInt(std.math.signbit(val));
24}
25
26pub inline fn __builtin_popcount(val: c_uint) c_int {
27 // popcount of a c_uint will never exceed the capacity of a c_int
28 @setRuntimeSafety(false);
29 return @bitCast(c_int, @as(c_uint, @popCount(c_uint, val)));
30}
31pub inline fn __builtin_ctz(val: c_uint) c_int {
32 // Returns the number of trailing 0-bits in val, starting at the least significant bit position.
33 // In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
34 @setRuntimeSafety(false);
35 return @bitCast(c_int, @as(c_uint, @ctz(c_uint, val)));
36}
37pub inline fn __builtin_clz(val: c_uint) c_int {
38 // Returns the number of leading 0-bits in x, starting at the most significant bit position.
39 // In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
40 @setRuntimeSafety(false);
41 return @bitCast(c_int, @as(c_uint, @clz(c_uint, val)));
42}
43
44pub inline fn __builtin_sqrt(val: f64) f64 {
45 return @sqrt(val);
46}
47pub inline fn __builtin_sqrtf(val: f32) f32 {
48 return @sqrt(val);
49}
50
51pub inline fn __builtin_sin(val: f64) f64 {
52 return @sin(val);
53}
54pub inline fn __builtin_sinf(val: f32) f32 {
55 return @sin(val);
56}
57pub inline fn __builtin_cos(val: f64) f64 {
58 return @cos(val);
59}
60pub inline fn __builtin_cosf(val: f32) f32 {
61 return @cos(val);
62}
63
64pub inline fn __builtin_exp(val: f64) f64 {
65 return @exp(val);
66}
67pub inline fn __builtin_expf(val: f32) f32 {
68 return @exp(val);
69}
70pub inline fn __builtin_exp2(val: f64) f64 {
71 return @exp2(val);
72}
73pub inline fn __builtin_exp2f(val: f32) f32 {
74 return @exp2(val);
75}
76pub inline fn __builtin_log(val: f64) f64 {
77 return @log(val);
78}
79pub inline fn __builtin_logf(val: f32) f32 {
80 return @log(val);
81}
82pub inline fn __builtin_log2(val: f64) f64 {
83 return @log2(val);
84}
85pub inline fn __builtin_log2f(val: f32) f32 {
86 return @log2(val);
87}
88pub inline fn __builtin_log10(val: f64) f64 {
89 return @log10(val);
90}
91pub inline fn __builtin_log10f(val: f32) f32 {
92 return @log10(val);
93}
94
95// Standard C Library bug: The absolute value of the most negative integer remains negative.
96pub inline fn __builtin_abs(val: c_int) c_int {
97 return std.math.absInt(val) catch std.math.minInt(c_int);
98}
99pub inline fn __builtin_fabs(val: f64) f64 {
100 return @fabs(val);
101}
102pub inline fn __builtin_fabsf(val: f32) f32 {
103 return @fabs(val);
104}
105
106pub inline fn __builtin_floor(val: f64) f64 {
107 return @floor(val);
108}
109pub inline fn __builtin_floorf(val: f32) f32 {
110 return @floor(val);
111}
112pub inline fn __builtin_ceil(val: f64) f64 {
113 return @ceil(val);
114}
115pub inline fn __builtin_ceilf(val: f32) f32 {
116 return @ceil(val);
117}
118pub inline fn __builtin_trunc(val: f64) f64 {
119 return @trunc(val);
120}
121pub inline fn __builtin_truncf(val: f32) f32 {
122 return @trunc(val);
123}
124pub inline fn __builtin_round(val: f64) f64 {
125 return @round(val);
126}
127pub inline fn __builtin_roundf(val: f32) f32 {
128 return @round(val);
129}
130
131pub inline fn __builtin_strlen(s: [*c]const u8) usize {
132 return std.mem.lenZ(s);
133}
134pub inline fn __builtin_strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int {
135 return @as(c_int, std.cstr.cmp(s1, s2));
136}
137
138pub inline fn __builtin_object_size(ptr: ?*const c_void, ty: c_int) usize {
139 // clang semantics match gcc's: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
140 // If it is not possible to determine which objects ptr points to at compile time,
141 // __builtin_object_size should return (size_t) -1 for type 0 or 1 and (size_t) 0
142 // for type 2 or 3.
143 if (ty == 0 or ty == 1) return @bitCast(usize, -@as(isize, 1));
144 if (ty == 2 or ty == 3) return 0;
145 unreachable;
146}
147
148pub inline fn __builtin___memset_chk(
149 dst: ?*c_void,
150 val: c_int,
151 len: usize,
152 remaining: usize,
153) ?*c_void {
154 if (len > remaining) @panic("std.c.builtins.memset_chk called with len > remaining");
155 return __builtin_memset(dst, val, len);
156}
157
158pub inline fn __builtin_memset(dst: ?*c_void, val: c_int, len: usize) ?*c_void {
159 const dst_cast = @ptrCast([*c]u8, dst);
160 @memset(dst_cast, @bitCast(u8, @truncate(i8, val)), len);
161 return dst;
162}
163
164pub inline fn __builtin___memcpy_chk(
165 noalias dst: ?*c_void,
166 noalias src: ?*const c_void,
167 len: usize,
168 remaining: usize,
169) ?*c_void {
170 if (len > remaining) @panic("std.c.builtins.memcpy_chk called with len > remaining");
171 return __builtin_memcpy(dst, src, len);
172}
173
174pub inline fn __builtin_memcpy(
175 noalias dst: ?*c_void,
176 noalias src: ?*const c_void,
177 len: usize,
178) ?*c_void {
179 const dst_cast = @ptrCast([*c]u8, dst);
180 const src_cast = @ptrCast([*c]const u8, src);
181
182 @memcpy(dst_cast, src_cast, len);
183 return dst;
184}
185
186/// The return value of __builtin_expect is `expr`. `c` is the expected value
187/// of `expr` and is used as a hint to the compiler in C. Here it is unused.
188pub inline fn __builtin_expect(expr: c_long, c: c_long) c_long {
189 return expr;
190}
191
192// __builtin_alloca_with_align is not currently implemented.
193// It is used in a run-translated-c test and a test-translate-c test to ensure that non-implemented
194// builtins are correctly demoted. If you implement __builtin_alloca_with_align, please update the
195// run-translated-c test and the test-translate-c test to use a different non-implemented builtin.
196// pub fn __builtin_alloca_with_align(size: usize, alignment: usize) callconv(.Inline) *c_void {}
lib/std/crypto/pcurves/p256/p256_64.zig+158-159
......@@ -18,7 +18,6 @@
1818// if x1 & (2^256-1) < 2^255 then x1 & (2^256-1) else (x1 & (2^256-1)) - 2^256
1919
2020const std = @import("std");
21const cast = std.meta.cast;
2221const mode = std.builtin.mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels
2322
2423// The type MontgomeryDomainFieldElement is a field element in the Montgomery domain.
......@@ -148,7 +147,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
148147 var x17: u64 = undefined;
149148 var x18: u1 = undefined;
150149 addcarryxU64(&x17, &x18, x16, x8, x5);
151 const x19 = (cast(u64, x18) + x6);
150 const x19 = (@as(u64, x18) + x6);
152151 var x20: u64 = undefined;
153152 var x21: u64 = undefined;
154153 mulxU64(&x20, &x21, x11, 0xffffffff00000001);
......@@ -161,7 +160,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
161160 var x26: u64 = undefined;
162161 var x27: u1 = undefined;
163162 addcarryxU64(&x26, &x27, 0x0, x25, x22);
164 const x28 = (cast(u64, x27) + x23);
163 const x28 = (@as(u64, x27) + x23);
165164 var x29: u64 = undefined;
166165 var x30: u1 = undefined;
167166 addcarryxU64(&x29, &x30, 0x0, x11, x24);
......@@ -198,7 +197,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
198197 var x51: u64 = undefined;
199198 var x52: u1 = undefined;
200199 addcarryxU64(&x51, &x52, x50, x42, x39);
201 const x53 = (cast(u64, x52) + x40);
200 const x53 = (@as(u64, x52) + x40);
202201 var x54: u64 = undefined;
203202 var x55: u1 = undefined;
204203 addcarryxU64(&x54, &x55, 0x0, x31, x45);
......@@ -213,7 +212,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
213212 addcarryxU64(&x60, &x61, x59, x37, x51);
214213 var x62: u64 = undefined;
215214 var x63: u1 = undefined;
216 addcarryxU64(&x62, &x63, x61, cast(u64, x38), x53);
215 addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53);
217216 var x64: u64 = undefined;
218217 var x65: u64 = undefined;
219218 mulxU64(&x64, &x65, x54, 0xffffffff00000001);
......@@ -226,7 +225,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
226225 var x70: u64 = undefined;
227226 var x71: u1 = undefined;
228227 addcarryxU64(&x70, &x71, 0x0, x69, x66);
229 const x72 = (cast(u64, x71) + x67);
228 const x72 = (@as(u64, x71) + x67);
230229 var x73: u64 = undefined;
231230 var x74: u1 = undefined;
232231 addcarryxU64(&x73, &x74, 0x0, x54, x68);
......@@ -242,7 +241,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
242241 var x81: u64 = undefined;
243242 var x82: u1 = undefined;
244243 addcarryxU64(&x81, &x82, x80, x62, x65);
245 const x83 = (cast(u64, x82) + cast(u64, x63));
244 const x83 = (@as(u64, x82) + @as(u64, x63));
246245 var x84: u64 = undefined;
247246 var x85: u64 = undefined;
248247 mulxU64(&x84, &x85, x2, (arg2[3]));
......@@ -264,7 +263,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
264263 var x96: u64 = undefined;
265264 var x97: u1 = undefined;
266265 addcarryxU64(&x96, &x97, x95, x87, x84);
267 const x98 = (cast(u64, x97) + x85);
266 const x98 = (@as(u64, x97) + x85);
268267 var x99: u64 = undefined;
269268 var x100: u1 = undefined;
270269 addcarryxU64(&x99, &x100, 0x0, x75, x90);
......@@ -292,7 +291,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
292291 var x115: u64 = undefined;
293292 var x116: u1 = undefined;
294293 addcarryxU64(&x115, &x116, 0x0, x114, x111);
295 const x117 = (cast(u64, x116) + x112);
294 const x117 = (@as(u64, x116) + x112);
296295 var x118: u64 = undefined;
297296 var x119: u1 = undefined;
298297 addcarryxU64(&x118, &x119, 0x0, x99, x113);
......@@ -308,7 +307,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
308307 var x126: u64 = undefined;
309308 var x127: u1 = undefined;
310309 addcarryxU64(&x126, &x127, x125, x107, x110);
311 const x128 = (cast(u64, x127) + cast(u64, x108));
310 const x128 = (@as(u64, x127) + @as(u64, x108));
312311 var x129: u64 = undefined;
313312 var x130: u64 = undefined;
314313 mulxU64(&x129, &x130, x3, (arg2[3]));
......@@ -330,7 +329,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
330329 var x141: u64 = undefined;
331330 var x142: u1 = undefined;
332331 addcarryxU64(&x141, &x142, x140, x132, x129);
333 const x143 = (cast(u64, x142) + x130);
332 const x143 = (@as(u64, x142) + x130);
334333 var x144: u64 = undefined;
335334 var x145: u1 = undefined;
336335 addcarryxU64(&x144, &x145, 0x0, x120, x135);
......@@ -358,7 +357,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
358357 var x160: u64 = undefined;
359358 var x161: u1 = undefined;
360359 addcarryxU64(&x160, &x161, 0x0, x159, x156);
361 const x162 = (cast(u64, x161) + x157);
360 const x162 = (@as(u64, x161) + x157);
362361 var x163: u64 = undefined;
363362 var x164: u1 = undefined;
364363 addcarryxU64(&x163, &x164, 0x0, x144, x158);
......@@ -374,7 +373,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
374373 var x171: u64 = undefined;
375374 var x172: u1 = undefined;
376375 addcarryxU64(&x171, &x172, x170, x152, x155);
377 const x173 = (cast(u64, x172) + cast(u64, x153));
376 const x173 = (@as(u64, x172) + @as(u64, x153));
378377 var x174: u64 = undefined;
379378 var x175: u1 = undefined;
380379 subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff);
......@@ -383,13 +382,13 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
383382 subborrowxU64(&x176, &x177, x175, x167, 0xffffffff);
384383 var x178: u64 = undefined;
385384 var x179: u1 = undefined;
386 subborrowxU64(&x178, &x179, x177, x169, cast(u64, 0x0));
385 subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0));
387386 var x180: u64 = undefined;
388387 var x181: u1 = undefined;
389388 subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001);
390389 var x182: u64 = undefined;
391390 var x183: u1 = undefined;
392 subborrowxU64(&x182, &x183, x181, x173, cast(u64, 0x0));
391 subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0));
393392 var x184: u64 = undefined;
394393 cmovznzU64(&x184, x183, x174, x165);
395394 var x185: u64 = undefined;
......@@ -440,7 +439,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
440439 var x17: u64 = undefined;
441440 var x18: u1 = undefined;
442441 addcarryxU64(&x17, &x18, x16, x8, x5);
443 const x19 = (cast(u64, x18) + x6);
442 const x19 = (@as(u64, x18) + x6);
444443 var x20: u64 = undefined;
445444 var x21: u64 = undefined;
446445 mulxU64(&x20, &x21, x11, 0xffffffff00000001);
......@@ -453,7 +452,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
453452 var x26: u64 = undefined;
454453 var x27: u1 = undefined;
455454 addcarryxU64(&x26, &x27, 0x0, x25, x22);
456 const x28 = (cast(u64, x27) + x23);
455 const x28 = (@as(u64, x27) + x23);
457456 var x29: u64 = undefined;
458457 var x30: u1 = undefined;
459458 addcarryxU64(&x29, &x30, 0x0, x11, x24);
......@@ -490,7 +489,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
490489 var x51: u64 = undefined;
491490 var x52: u1 = undefined;
492491 addcarryxU64(&x51, &x52, x50, x42, x39);
493 const x53 = (cast(u64, x52) + x40);
492 const x53 = (@as(u64, x52) + x40);
494493 var x54: u64 = undefined;
495494 var x55: u1 = undefined;
496495 addcarryxU64(&x54, &x55, 0x0, x31, x45);
......@@ -505,7 +504,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
505504 addcarryxU64(&x60, &x61, x59, x37, x51);
506505 var x62: u64 = undefined;
507506 var x63: u1 = undefined;
508 addcarryxU64(&x62, &x63, x61, cast(u64, x38), x53);
507 addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53);
509508 var x64: u64 = undefined;
510509 var x65: u64 = undefined;
511510 mulxU64(&x64, &x65, x54, 0xffffffff00000001);
......@@ -518,7 +517,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
518517 var x70: u64 = undefined;
519518 var x71: u1 = undefined;
520519 addcarryxU64(&x70, &x71, 0x0, x69, x66);
521 const x72 = (cast(u64, x71) + x67);
520 const x72 = (@as(u64, x71) + x67);
522521 var x73: u64 = undefined;
523522 var x74: u1 = undefined;
524523 addcarryxU64(&x73, &x74, 0x0, x54, x68);
......@@ -534,7 +533,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
534533 var x81: u64 = undefined;
535534 var x82: u1 = undefined;
536535 addcarryxU64(&x81, &x82, x80, x62, x65);
537 const x83 = (cast(u64, x82) + cast(u64, x63));
536 const x83 = (@as(u64, x82) + @as(u64, x63));
538537 var x84: u64 = undefined;
539538 var x85: u64 = undefined;
540539 mulxU64(&x84, &x85, x2, (arg1[3]));
......@@ -556,7 +555,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
556555 var x96: u64 = undefined;
557556 var x97: u1 = undefined;
558557 addcarryxU64(&x96, &x97, x95, x87, x84);
559 const x98 = (cast(u64, x97) + x85);
558 const x98 = (@as(u64, x97) + x85);
560559 var x99: u64 = undefined;
561560 var x100: u1 = undefined;
562561 addcarryxU64(&x99, &x100, 0x0, x75, x90);
......@@ -584,7 +583,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
584583 var x115: u64 = undefined;
585584 var x116: u1 = undefined;
586585 addcarryxU64(&x115, &x116, 0x0, x114, x111);
587 const x117 = (cast(u64, x116) + x112);
586 const x117 = (@as(u64, x116) + x112);
588587 var x118: u64 = undefined;
589588 var x119: u1 = undefined;
590589 addcarryxU64(&x118, &x119, 0x0, x99, x113);
......@@ -600,7 +599,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
600599 var x126: u64 = undefined;
601600 var x127: u1 = undefined;
602601 addcarryxU64(&x126, &x127, x125, x107, x110);
603 const x128 = (cast(u64, x127) + cast(u64, x108));
602 const x128 = (@as(u64, x127) + @as(u64, x108));
604603 var x129: u64 = undefined;
605604 var x130: u64 = undefined;
606605 mulxU64(&x129, &x130, x3, (arg1[3]));
......@@ -622,7 +621,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
622621 var x141: u64 = undefined;
623622 var x142: u1 = undefined;
624623 addcarryxU64(&x141, &x142, x140, x132, x129);
625 const x143 = (cast(u64, x142) + x130);
624 const x143 = (@as(u64, x142) + x130);
626625 var x144: u64 = undefined;
627626 var x145: u1 = undefined;
628627 addcarryxU64(&x144, &x145, 0x0, x120, x135);
......@@ -650,7 +649,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
650649 var x160: u64 = undefined;
651650 var x161: u1 = undefined;
652651 addcarryxU64(&x160, &x161, 0x0, x159, x156);
653 const x162 = (cast(u64, x161) + x157);
652 const x162 = (@as(u64, x161) + x157);
654653 var x163: u64 = undefined;
655654 var x164: u1 = undefined;
656655 addcarryxU64(&x163, &x164, 0x0, x144, x158);
......@@ -666,7 +665,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
666665 var x171: u64 = undefined;
667666 var x172: u1 = undefined;
668667 addcarryxU64(&x171, &x172, x170, x152, x155);
669 const x173 = (cast(u64, x172) + cast(u64, x153));
668 const x173 = (@as(u64, x172) + @as(u64, x153));
670669 var x174: u64 = undefined;
671670 var x175: u1 = undefined;
672671 subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff);
......@@ -675,13 +674,13 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
675674 subborrowxU64(&x176, &x177, x175, x167, 0xffffffff);
676675 var x178: u64 = undefined;
677676 var x179: u1 = undefined;
678 subborrowxU64(&x178, &x179, x177, x169, cast(u64, 0x0));
677 subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0));
679678 var x180: u64 = undefined;
680679 var x181: u1 = undefined;
681680 subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001);
682681 var x182: u64 = undefined;
683682 var x183: u1 = undefined;
684 subborrowxU64(&x182, &x183, x181, x173, cast(u64, 0x0));
683 subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0));
685684 var x184: u64 = undefined;
686685 cmovznzU64(&x184, x183, x174, x165);
687686 var x185: u64 = undefined;
......@@ -728,13 +727,13 @@ pub fn add(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
728727 subborrowxU64(&x11, &x12, x10, x3, 0xffffffff);
729728 var x13: u64 = undefined;
730729 var x14: u1 = undefined;
731 subborrowxU64(&x13, &x14, x12, x5, cast(u64, 0x0));
730 subborrowxU64(&x13, &x14, x12, x5, @as(u64, 0x0));
732731 var x15: u64 = undefined;
733732 var x16: u1 = undefined;
734733 subborrowxU64(&x15, &x16, x14, x7, 0xffffffff00000001);
735734 var x17: u64 = undefined;
736735 var x18: u1 = undefined;
737 subborrowxU64(&x17, &x18, x16, cast(u64, x8), cast(u64, 0x0));
736 subborrowxU64(&x17, &x18, x16, @as(u64, x8), @as(u64, 0x0));
738737 var x19: u64 = undefined;
739738 cmovznzU64(&x19, x18, x9, x1);
740739 var x20: u64 = undefined;
......@@ -774,7 +773,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
774773 var x8: u1 = undefined;
775774 subborrowxU64(&x7, &x8, x6, (arg1[3]), (arg2[3]));
776775 var x9: u64 = undefined;
777 cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff);
776 cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
778777 var x10: u64 = undefined;
779778 var x11: u1 = undefined;
780779 addcarryxU64(&x10, &x11, 0x0, x1, x9);
......@@ -783,7 +782,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
783782 addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff));
784783 var x14: u64 = undefined;
785784 var x15: u1 = undefined;
786 addcarryxU64(&x14, &x15, x13, x5, cast(u64, 0x0));
785 addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0));
787786 var x16: u64 = undefined;
788787 var x17: u1 = undefined;
789788 addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001));
......@@ -806,18 +805,18 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
806805
807806 var x1: u64 = undefined;
808807 var x2: u1 = undefined;
809 subborrowxU64(&x1, &x2, 0x0, cast(u64, 0x0), (arg1[0]));
808 subborrowxU64(&x1, &x2, 0x0, @as(u64, 0x0), (arg1[0]));
810809 var x3: u64 = undefined;
811810 var x4: u1 = undefined;
812 subborrowxU64(&x3, &x4, x2, cast(u64, 0x0), (arg1[1]));
811 subborrowxU64(&x3, &x4, x2, @as(u64, 0x0), (arg1[1]));
813812 var x5: u64 = undefined;
814813 var x6: u1 = undefined;
815 subborrowxU64(&x5, &x6, x4, cast(u64, 0x0), (arg1[2]));
814 subborrowxU64(&x5, &x6, x4, @as(u64, 0x0), (arg1[2]));
816815 var x7: u64 = undefined;
817816 var x8: u1 = undefined;
818 subborrowxU64(&x7, &x8, x6, cast(u64, 0x0), (arg1[3]));
817 subborrowxU64(&x7, &x8, x6, @as(u64, 0x0), (arg1[3]));
819818 var x9: u64 = undefined;
820 cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff);
819 cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
821820 var x10: u64 = undefined;
822821 var x11: u1 = undefined;
823822 addcarryxU64(&x10, &x11, 0x0, x1, x9);
......@@ -826,7 +825,7 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
826825 addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff));
827826 var x14: u64 = undefined;
828827 var x15: u1 = undefined;
829 addcarryxU64(&x14, &x15, x13, x5, cast(u64, 0x0));
828 addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0));
830829 var x16: u64 = undefined;
831830 var x17: u1 = undefined;
832831 addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001));
......@@ -865,7 +864,7 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
865864 addcarryxU64(&x10, &x11, 0x0, x1, x6);
866865 var x12: u64 = undefined;
867866 var x13: u1 = undefined;
868 addcarryxU64(&x12, &x13, x11, cast(u64, 0x0), x8);
867 addcarryxU64(&x12, &x13, x11, @as(u64, 0x0), x8);
869868 var x14: u64 = undefined;
870869 var x15: u1 = undefined;
871870 addcarryxU64(&x14, &x15, 0x0, x12, (arg1[1]));
......@@ -886,10 +885,10 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
886885 addcarryxU64(&x24, &x25, 0x0, x14, x20);
887886 var x26: u64 = undefined;
888887 var x27: u1 = undefined;
889 addcarryxU64(&x26, &x27, x25, (cast(u64, x15) + (cast(u64, x13) + (cast(u64, x9) + x5))), x22);
888 addcarryxU64(&x26, &x27, x25, (@as(u64, x15) + (@as(u64, x13) + (@as(u64, x9) + x5))), x22);
890889 var x28: u64 = undefined;
891890 var x29: u1 = undefined;
892 addcarryxU64(&x28, &x29, x27, x2, (cast(u64, x23) + x19));
891 addcarryxU64(&x28, &x29, x27, x2, (@as(u64, x23) + x19));
893892 var x30: u64 = undefined;
894893 var x31: u1 = undefined;
895894 addcarryxU64(&x30, &x31, x29, x3, x16);
......@@ -898,10 +897,10 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
898897 addcarryxU64(&x32, &x33, 0x0, x26, (arg1[2]));
899898 var x34: u64 = undefined;
900899 var x35: u1 = undefined;
901 addcarryxU64(&x34, &x35, x33, x28, cast(u64, 0x0));
900 addcarryxU64(&x34, &x35, x33, x28, @as(u64, 0x0));
902901 var x36: u64 = undefined;
903902 var x37: u1 = undefined;
904 addcarryxU64(&x36, &x37, x35, x30, cast(u64, 0x0));
903 addcarryxU64(&x36, &x37, x35, x30, @as(u64, 0x0));
905904 var x38: u64 = undefined;
906905 var x39: u64 = undefined;
907906 mulxU64(&x38, &x39, x32, 0xffffffff00000001);
......@@ -922,19 +921,19 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
922921 addcarryxU64(&x48, &x49, x47, x34, x44);
923922 var x50: u64 = undefined;
924923 var x51: u1 = undefined;
925 addcarryxU64(&x50, &x51, x49, x36, (cast(u64, x45) + x41));
924 addcarryxU64(&x50, &x51, x49, x36, (@as(u64, x45) + x41));
926925 var x52: u64 = undefined;
927926 var x53: u1 = undefined;
928 addcarryxU64(&x52, &x53, x51, (cast(u64, x37) + (cast(u64, x31) + x17)), x38);
927 addcarryxU64(&x52, &x53, x51, (@as(u64, x37) + (@as(u64, x31) + x17)), x38);
929928 var x54: u64 = undefined;
930929 var x55: u1 = undefined;
931930 addcarryxU64(&x54, &x55, 0x0, x48, (arg1[3]));
932931 var x56: u64 = undefined;
933932 var x57: u1 = undefined;
934 addcarryxU64(&x56, &x57, x55, x50, cast(u64, 0x0));
933 addcarryxU64(&x56, &x57, x55, x50, @as(u64, 0x0));
935934 var x58: u64 = undefined;
936935 var x59: u1 = undefined;
937 addcarryxU64(&x58, &x59, x57, x52, cast(u64, 0x0));
936 addcarryxU64(&x58, &x59, x57, x52, @as(u64, 0x0));
938937 var x60: u64 = undefined;
939938 var x61: u64 = undefined;
940939 mulxU64(&x60, &x61, x54, 0xffffffff00000001);
......@@ -955,11 +954,11 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
955954 addcarryxU64(&x70, &x71, x69, x56, x66);
956955 var x72: u64 = undefined;
957956 var x73: u1 = undefined;
958 addcarryxU64(&x72, &x73, x71, x58, (cast(u64, x67) + x63));
957 addcarryxU64(&x72, &x73, x71, x58, (@as(u64, x67) + x63));
959958 var x74: u64 = undefined;
960959 var x75: u1 = undefined;
961 addcarryxU64(&x74, &x75, x73, (cast(u64, x59) + (cast(u64, x53) + x39)), x60);
962 const x76 = (cast(u64, x75) + x61);
960 addcarryxU64(&x74, &x75, x73, (@as(u64, x59) + (@as(u64, x53) + x39)), x60);
961 const x76 = (@as(u64, x75) + x61);
963962 var x77: u64 = undefined;
964963 var x78: u1 = undefined;
965964 subborrowxU64(&x77, &x78, 0x0, x70, 0xffffffffffffffff);
......@@ -968,13 +967,13 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
968967 subborrowxU64(&x79, &x80, x78, x72, 0xffffffff);
969968 var x81: u64 = undefined;
970969 var x82: u1 = undefined;
971 subborrowxU64(&x81, &x82, x80, x74, cast(u64, 0x0));
970 subborrowxU64(&x81, &x82, x80, x74, @as(u64, 0x0));
972971 var x83: u64 = undefined;
973972 var x84: u1 = undefined;
974973 subborrowxU64(&x83, &x84, x82, x76, 0xffffffff00000001);
975974 var x85: u64 = undefined;
976975 var x86: u1 = undefined;
977 subborrowxU64(&x85, &x86, x84, cast(u64, 0x0), cast(u64, 0x0));
976 subborrowxU64(&x85, &x86, x84, @as(u64, 0x0), @as(u64, 0x0));
978977 var x87: u64 = undefined;
979978 cmovznzU64(&x87, x86, x77, x70);
980979 var x88: u64 = undefined;
......@@ -1045,13 +1044,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
10451044 addcarryxU64(&x29, &x30, x28, x13, x25);
10461045 var x31: u64 = undefined;
10471046 var x32: u1 = undefined;
1048 addcarryxU64(&x31, &x32, x30, x15, (cast(u64, x26) + x22));
1047 addcarryxU64(&x31, &x32, x30, x15, (@as(u64, x26) + x22));
10491048 var x33: u64 = undefined;
10501049 var x34: u1 = undefined;
10511050 addcarryxU64(&x33, &x34, x32, x17, x19);
10521051 var x35: u64 = undefined;
10531052 var x36: u1 = undefined;
1054 addcarryxU64(&x35, &x36, x34, (cast(u64, x18) + x6), x20);
1053 addcarryxU64(&x35, &x36, x34, (@as(u64, x18) + x6), x20);
10551054 var x37: u64 = undefined;
10561055 var x38: u64 = undefined;
10571056 mulxU64(&x37, &x38, x1, 0x4fffffffd);
......@@ -1105,13 +1104,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
11051104 addcarryxU64(&x69, &x70, x68, x53, x65);
11061105 var x71: u64 = undefined;
11071106 var x72: u1 = undefined;
1108 addcarryxU64(&x71, &x72, x70, x55, (cast(u64, x66) + x62));
1107 addcarryxU64(&x71, &x72, x70, x55, (@as(u64, x66) + x62));
11091108 var x73: u64 = undefined;
11101109 var x74: u1 = undefined;
11111110 addcarryxU64(&x73, &x74, x72, x57, x59);
11121111 var x75: u64 = undefined;
11131112 var x76: u1 = undefined;
1114 addcarryxU64(&x75, &x76, x74, ((cast(u64, x58) + cast(u64, x36)) + (cast(u64, x50) + x38)), x60);
1113 addcarryxU64(&x75, &x76, x74, ((@as(u64, x58) + @as(u64, x36)) + (@as(u64, x50) + x38)), x60);
11151114 var x77: u64 = undefined;
11161115 var x78: u64 = undefined;
11171116 mulxU64(&x77, &x78, x2, 0x4fffffffd);
......@@ -1165,13 +1164,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
11651164 addcarryxU64(&x109, &x110, x108, x93, x105);
11661165 var x111: u64 = undefined;
11671166 var x112: u1 = undefined;
1168 addcarryxU64(&x111, &x112, x110, x95, (cast(u64, x106) + x102));
1167 addcarryxU64(&x111, &x112, x110, x95, (@as(u64, x106) + x102));
11691168 var x113: u64 = undefined;
11701169 var x114: u1 = undefined;
11711170 addcarryxU64(&x113, &x114, x112, x97, x99);
11721171 var x115: u64 = undefined;
11731172 var x116: u1 = undefined;
1174 addcarryxU64(&x115, &x116, x114, ((cast(u64, x98) + cast(u64, x76)) + (cast(u64, x90) + x78)), x100);
1173 addcarryxU64(&x115, &x116, x114, ((@as(u64, x98) + @as(u64, x76)) + (@as(u64, x90) + x78)), x100);
11751174 var x117: u64 = undefined;
11761175 var x118: u64 = undefined;
11771176 mulxU64(&x117, &x118, x3, 0x4fffffffd);
......@@ -1225,13 +1224,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
12251224 addcarryxU64(&x149, &x150, x148, x133, x145);
12261225 var x151: u64 = undefined;
12271226 var x152: u1 = undefined;
1228 addcarryxU64(&x151, &x152, x150, x135, (cast(u64, x146) + x142));
1227 addcarryxU64(&x151, &x152, x150, x135, (@as(u64, x146) + x142));
12291228 var x153: u64 = undefined;
12301229 var x154: u1 = undefined;
12311230 addcarryxU64(&x153, &x154, x152, x137, x139);
12321231 var x155: u64 = undefined;
12331232 var x156: u1 = undefined;
1234 addcarryxU64(&x155, &x156, x154, ((cast(u64, x138) + cast(u64, x116)) + (cast(u64, x130) + x118)), x140);
1233 addcarryxU64(&x155, &x156, x154, ((@as(u64, x138) + @as(u64, x116)) + (@as(u64, x130) + x118)), x140);
12351234 var x157: u64 = undefined;
12361235 var x158: u1 = undefined;
12371236 subborrowxU64(&x157, &x158, 0x0, x149, 0xffffffffffffffff);
......@@ -1240,13 +1239,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
12401239 subborrowxU64(&x159, &x160, x158, x151, 0xffffffff);
12411240 var x161: u64 = undefined;
12421241 var x162: u1 = undefined;
1243 subborrowxU64(&x161, &x162, x160, x153, cast(u64, 0x0));
1242 subborrowxU64(&x161, &x162, x160, x153, @as(u64, 0x0));
12441243 var x163: u64 = undefined;
12451244 var x164: u1 = undefined;
12461245 subborrowxU64(&x163, &x164, x162, x155, 0xffffffff00000001);
12471246 var x165: u64 = undefined;
12481247 var x166: u1 = undefined;
1249 subborrowxU64(&x165, &x166, x164, cast(u64, x156), cast(u64, 0x0));
1248 subborrowxU64(&x165, &x166, x164, @as(u64, x156), @as(u64, 0x0));
12501249 var x167: u64 = undefined;
12511250 cmovznzU64(&x167, x166, x157, x149);
12521251 var x168: u64 = undefined;
......@@ -1325,62 +1324,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void {
13251324 const x2 = (arg1[2]);
13261325 const x3 = (arg1[1]);
13271326 const x4 = (arg1[0]);
1328 const x5 = cast(u8, (x4 & cast(u64, 0xff)));
1327 const x5 = @truncate(u8, (x4 & @as(u64, 0xff)));
13291328 const x6 = (x4 >> 8);
1330 const x7 = cast(u8, (x6 & cast(u64, 0xff)));
1329 const x7 = @truncate(u8, (x6 & @as(u64, 0xff)));
13311330 const x8 = (x6 >> 8);
1332 const x9 = cast(u8, (x8 & cast(u64, 0xff)));
1331 const x9 = @truncate(u8, (x8 & @as(u64, 0xff)));
13331332 const x10 = (x8 >> 8);
1334 const x11 = cast(u8, (x10 & cast(u64, 0xff)));
1333 const x11 = @truncate(u8, (x10 & @as(u64, 0xff)));
13351334 const x12 = (x10 >> 8);
1336 const x13 = cast(u8, (x12 & cast(u64, 0xff)));
1335 const x13 = @truncate(u8, (x12 & @as(u64, 0xff)));
13371336 const x14 = (x12 >> 8);
1338 const x15 = cast(u8, (x14 & cast(u64, 0xff)));
1337 const x15 = @truncate(u8, (x14 & @as(u64, 0xff)));
13391338 const x16 = (x14 >> 8);
1340 const x17 = cast(u8, (x16 & cast(u64, 0xff)));
1341 const x18 = cast(u8, (x16 >> 8));
1342 const x19 = cast(u8, (x3 & cast(u64, 0xff)));
1339 const x17 = @truncate(u8, (x16 & @as(u64, 0xff)));
1340 const x18 = @truncate(u8, (x16 >> 8));
1341 const x19 = @truncate(u8, (x3 & @as(u64, 0xff)));
13431342 const x20 = (x3 >> 8);
1344 const x21 = cast(u8, (x20 & cast(u64, 0xff)));
1343 const x21 = @truncate(u8, (x20 & @as(u64, 0xff)));
13451344 const x22 = (x20 >> 8);
1346 const x23 = cast(u8, (x22 & cast(u64, 0xff)));
1345 const x23 = @truncate(u8, (x22 & @as(u64, 0xff)));
13471346 const x24 = (x22 >> 8);
1348 const x25 = cast(u8, (x24 & cast(u64, 0xff)));
1347 const x25 = @truncate(u8, (x24 & @as(u64, 0xff)));
13491348 const x26 = (x24 >> 8);
1350 const x27 = cast(u8, (x26 & cast(u64, 0xff)));
1349 const x27 = @truncate(u8, (x26 & @as(u64, 0xff)));
13511350 const x28 = (x26 >> 8);
1352 const x29 = cast(u8, (x28 & cast(u64, 0xff)));
1351 const x29 = @truncate(u8, (x28 & @as(u64, 0xff)));
13531352 const x30 = (x28 >> 8);
1354 const x31 = cast(u8, (x30 & cast(u64, 0xff)));
1355 const x32 = cast(u8, (x30 >> 8));
1356 const x33 = cast(u8, (x2 & cast(u64, 0xff)));
1353 const x31 = @truncate(u8, (x30 & @as(u64, 0xff)));
1354 const x32 = @truncate(u8, (x30 >> 8));
1355 const x33 = @truncate(u8, (x2 & @as(u64, 0xff)));
13571356 const x34 = (x2 >> 8);
1358 const x35 = cast(u8, (x34 & cast(u64, 0xff)));
1357 const x35 = @truncate(u8, (x34 & @as(u64, 0xff)));
13591358 const x36 = (x34 >> 8);
1360 const x37 = cast(u8, (x36 & cast(u64, 0xff)));
1359 const x37 = @truncate(u8, (x36 & @as(u64, 0xff)));
13611360 const x38 = (x36 >> 8);
1362 const x39 = cast(u8, (x38 & cast(u64, 0xff)));
1361 const x39 = @truncate(u8, (x38 & @as(u64, 0xff)));
13631362 const x40 = (x38 >> 8);
1364 const x41 = cast(u8, (x40 & cast(u64, 0xff)));
1363 const x41 = @truncate(u8, (x40 & @as(u64, 0xff)));
13651364 const x42 = (x40 >> 8);
1366 const x43 = cast(u8, (x42 & cast(u64, 0xff)));
1365 const x43 = @truncate(u8, (x42 & @as(u64, 0xff)));
13671366 const x44 = (x42 >> 8);
1368 const x45 = cast(u8, (x44 & cast(u64, 0xff)));
1369 const x46 = cast(u8, (x44 >> 8));
1370 const x47 = cast(u8, (x1 & cast(u64, 0xff)));
1367 const x45 = @truncate(u8, (x44 & @as(u64, 0xff)));
1368 const x46 = @truncate(u8, (x44 >> 8));
1369 const x47 = @truncate(u8, (x1 & @as(u64, 0xff)));
13711370 const x48 = (x1 >> 8);
1372 const x49 = cast(u8, (x48 & cast(u64, 0xff)));
1371 const x49 = @truncate(u8, (x48 & @as(u64, 0xff)));
13731372 const x50 = (x48 >> 8);
1374 const x51 = cast(u8, (x50 & cast(u64, 0xff)));
1373 const x51 = @truncate(u8, (x50 & @as(u64, 0xff)));
13751374 const x52 = (x50 >> 8);
1376 const x53 = cast(u8, (x52 & cast(u64, 0xff)));
1375 const x53 = @truncate(u8, (x52 & @as(u64, 0xff)));
13771376 const x54 = (x52 >> 8);
1378 const x55 = cast(u8, (x54 & cast(u64, 0xff)));
1377 const x55 = @truncate(u8, (x54 & @as(u64, 0xff)));
13791378 const x56 = (x54 >> 8);
1380 const x57 = cast(u8, (x56 & cast(u64, 0xff)));
1379 const x57 = @truncate(u8, (x56 & @as(u64, 0xff)));
13811380 const x58 = (x56 >> 8);
1382 const x59 = cast(u8, (x58 & cast(u64, 0xff)));
1383 const x60 = cast(u8, (x58 >> 8));
1381 const x59 = @truncate(u8, (x58 & @as(u64, 0xff)));
1382 const x60 = @truncate(u8, (x58 >> 8));
13841383 out1[0] = x5;
13851384 out1[1] = x7;
13861385 out1[2] = x9;
......@@ -1430,60 +1429,60 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void {
14301429pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void {
14311430 @setRuntimeSafety(mode == .Debug);
14321431
1433 const x1 = (cast(u64, (arg1[31])) << 56);
1434 const x2 = (cast(u64, (arg1[30])) << 48);
1435 const x3 = (cast(u64, (arg1[29])) << 40);
1436 const x4 = (cast(u64, (arg1[28])) << 32);
1437 const x5 = (cast(u64, (arg1[27])) << 24);
1438 const x6 = (cast(u64, (arg1[26])) << 16);
1439 const x7 = (cast(u64, (arg1[25])) << 8);
1432 const x1 = (@as(u64, (arg1[31])) << 56);
1433 const x2 = (@as(u64, (arg1[30])) << 48);
1434 const x3 = (@as(u64, (arg1[29])) << 40);
1435 const x4 = (@as(u64, (arg1[28])) << 32);
1436 const x5 = (@as(u64, (arg1[27])) << 24);
1437 const x6 = (@as(u64, (arg1[26])) << 16);
1438 const x7 = (@as(u64, (arg1[25])) << 8);
14401439 const x8 = (arg1[24]);
1441 const x9 = (cast(u64, (arg1[23])) << 56);
1442 const x10 = (cast(u64, (arg1[22])) << 48);
1443 const x11 = (cast(u64, (arg1[21])) << 40);
1444 const x12 = (cast(u64, (arg1[20])) << 32);
1445 const x13 = (cast(u64, (arg1[19])) << 24);
1446 const x14 = (cast(u64, (arg1[18])) << 16);
1447 const x15 = (cast(u64, (arg1[17])) << 8);
1440 const x9 = (@as(u64, (arg1[23])) << 56);
1441 const x10 = (@as(u64, (arg1[22])) << 48);
1442 const x11 = (@as(u64, (arg1[21])) << 40);
1443 const x12 = (@as(u64, (arg1[20])) << 32);
1444 const x13 = (@as(u64, (arg1[19])) << 24);
1445 const x14 = (@as(u64, (arg1[18])) << 16);
1446 const x15 = (@as(u64, (arg1[17])) << 8);
14481447 const x16 = (arg1[16]);
1449 const x17 = (cast(u64, (arg1[15])) << 56);
1450 const x18 = (cast(u64, (arg1[14])) << 48);
1451 const x19 = (cast(u64, (arg1[13])) << 40);
1452 const x20 = (cast(u64, (arg1[12])) << 32);
1453 const x21 = (cast(u64, (arg1[11])) << 24);
1454 const x22 = (cast(u64, (arg1[10])) << 16);
1455 const x23 = (cast(u64, (arg1[9])) << 8);
1448 const x17 = (@as(u64, (arg1[15])) << 56);
1449 const x18 = (@as(u64, (arg1[14])) << 48);
1450 const x19 = (@as(u64, (arg1[13])) << 40);
1451 const x20 = (@as(u64, (arg1[12])) << 32);
1452 const x21 = (@as(u64, (arg1[11])) << 24);
1453 const x22 = (@as(u64, (arg1[10])) << 16);
1454 const x23 = (@as(u64, (arg1[9])) << 8);
14561455 const x24 = (arg1[8]);
1457 const x25 = (cast(u64, (arg1[7])) << 56);
1458 const x26 = (cast(u64, (arg1[6])) << 48);
1459 const x27 = (cast(u64, (arg1[5])) << 40);
1460 const x28 = (cast(u64, (arg1[4])) << 32);
1461 const x29 = (cast(u64, (arg1[3])) << 24);
1462 const x30 = (cast(u64, (arg1[2])) << 16);
1463 const x31 = (cast(u64, (arg1[1])) << 8);
1456 const x25 = (@as(u64, (arg1[7])) << 56);
1457 const x26 = (@as(u64, (arg1[6])) << 48);
1458 const x27 = (@as(u64, (arg1[5])) << 40);
1459 const x28 = (@as(u64, (arg1[4])) << 32);
1460 const x29 = (@as(u64, (arg1[3])) << 24);
1461 const x30 = (@as(u64, (arg1[2])) << 16);
1462 const x31 = (@as(u64, (arg1[1])) << 8);
14641463 const x32 = (arg1[0]);
1465 const x33 = (x31 + cast(u64, x32));
1464 const x33 = (x31 + @as(u64, x32));
14661465 const x34 = (x30 + x33);
14671466 const x35 = (x29 + x34);
14681467 const x36 = (x28 + x35);
14691468 const x37 = (x27 + x36);
14701469 const x38 = (x26 + x37);
14711470 const x39 = (x25 + x38);
1472 const x40 = (x23 + cast(u64, x24));
1471 const x40 = (x23 + @as(u64, x24));
14731472 const x41 = (x22 + x40);
14741473 const x42 = (x21 + x41);
14751474 const x43 = (x20 + x42);
14761475 const x44 = (x19 + x43);
14771476 const x45 = (x18 + x44);
14781477 const x46 = (x17 + x45);
1479 const x47 = (x15 + cast(u64, x16));
1478 const x47 = (x15 + @as(u64, x16));
14801479 const x48 = (x14 + x47);
14811480 const x49 = (x13 + x48);
14821481 const x50 = (x12 + x49);
14831482 const x51 = (x11 + x50);
14841483 const x52 = (x10 + x51);
14851484 const x53 = (x9 + x52);
1486 const x54 = (x7 + cast(u64, x8));
1485 const x54 = (x7 + @as(u64, x8));
14871486 const x55 = (x6 + x54);
14881487 const x56 = (x5 + x55);
14891488 const x57 = (x4 + x56);
......@@ -1505,7 +1504,7 @@ pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void {
15051504pub fn setOne(out1: *MontgomeryDomainFieldElement) void {
15061505 @setRuntimeSafety(mode == .Debug);
15071506
1508 out1[0] = cast(u64, 0x1);
1507 out1[0] = @as(u64, 0x1);
15091508 out1[1] = 0xffffffff00000000;
15101509 out1[2] = 0xffffffffffffffff;
15111510 out1[3] = 0xfffffffe;
......@@ -1524,9 +1523,9 @@ pub fn msat(out1: *[5]u64) void {
15241523
15251524 out1[0] = 0xffffffffffffffff;
15261525 out1[1] = 0xffffffff;
1527 out1[2] = cast(u64, 0x0);
1526 out1[2] = @as(u64, 0x0);
15281527 out1[3] = 0xffffffff00000001;
1529 out1[4] = cast(u64, 0x0);
1528 out1[4] = @as(u64, 0x0);
15301529}
15311530
15321531/// The function divstep computes a divstep.
......@@ -1562,11 +1561,11 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
15621561
15631562 var x1: u64 = undefined;
15641563 var x2: u1 = undefined;
1565 addcarryxU64(&x1, &x2, 0x0, (~arg1), cast(u64, 0x1));
1566 const x3 = (cast(u1, (x1 >> 63)) & cast(u1, ((arg3[0]) & cast(u64, 0x1))));
1564 addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1));
1565 const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & @as(u64, 0x1))));
15671566 var x4: u64 = undefined;
15681567 var x5: u1 = undefined;
1569 addcarryxU64(&x4, &x5, 0x0, (~arg1), cast(u64, 0x1));
1568 addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1));
15701569 var x6: u64 = undefined;
15711570 cmovznzU64(&x6, x3, arg1, x4);
15721571 var x7: u64 = undefined;
......@@ -1581,19 +1580,19 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
15811580 cmovznzU64(&x11, x3, (arg2[4]), (arg3[4]));
15821581 var x12: u64 = undefined;
15831582 var x13: u1 = undefined;
1584 addcarryxU64(&x12, &x13, 0x0, cast(u64, 0x1), (~(arg2[0])));
1583 addcarryxU64(&x12, &x13, 0x0, @as(u64, 0x1), (~(arg2[0])));
15851584 var x14: u64 = undefined;
15861585 var x15: u1 = undefined;
1587 addcarryxU64(&x14, &x15, x13, cast(u64, 0x0), (~(arg2[1])));
1586 addcarryxU64(&x14, &x15, x13, @as(u64, 0x0), (~(arg2[1])));
15881587 var x16: u64 = undefined;
15891588 var x17: u1 = undefined;
1590 addcarryxU64(&x16, &x17, x15, cast(u64, 0x0), (~(arg2[2])));
1589 addcarryxU64(&x16, &x17, x15, @as(u64, 0x0), (~(arg2[2])));
15911590 var x18: u64 = undefined;
15921591 var x19: u1 = undefined;
1593 addcarryxU64(&x18, &x19, x17, cast(u64, 0x0), (~(arg2[3])));
1592 addcarryxU64(&x18, &x19, x17, @as(u64, 0x0), (~(arg2[3])));
15941593 var x20: u64 = undefined;
15951594 var x21: u1 = undefined;
1596 addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), (~(arg2[4])));
1595 addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), (~(arg2[4])));
15971596 var x22: u64 = undefined;
15981597 cmovznzU64(&x22, x3, (arg3[0]), x12);
15991598 var x23: u64 = undefined;
......@@ -1632,31 +1631,31 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
16321631 subborrowxU64(&x41, &x42, x40, x33, 0xffffffff);
16331632 var x43: u64 = undefined;
16341633 var x44: u1 = undefined;
1635 subborrowxU64(&x43, &x44, x42, x35, cast(u64, 0x0));
1634 subborrowxU64(&x43, &x44, x42, x35, @as(u64, 0x0));
16361635 var x45: u64 = undefined;
16371636 var x46: u1 = undefined;
16381637 subborrowxU64(&x45, &x46, x44, x37, 0xffffffff00000001);
16391638 var x47: u64 = undefined;
16401639 var x48: u1 = undefined;
1641 subborrowxU64(&x47, &x48, x46, cast(u64, x38), cast(u64, 0x0));
1640 subborrowxU64(&x47, &x48, x46, @as(u64, x38), @as(u64, 0x0));
16421641 const x49 = (arg4[3]);
16431642 const x50 = (arg4[2]);
16441643 const x51 = (arg4[1]);
16451644 const x52 = (arg4[0]);
16461645 var x53: u64 = undefined;
16471646 var x54: u1 = undefined;
1648 subborrowxU64(&x53, &x54, 0x0, cast(u64, 0x0), x52);
1647 subborrowxU64(&x53, &x54, 0x0, @as(u64, 0x0), x52);
16491648 var x55: u64 = undefined;
16501649 var x56: u1 = undefined;
1651 subborrowxU64(&x55, &x56, x54, cast(u64, 0x0), x51);
1650 subborrowxU64(&x55, &x56, x54, @as(u64, 0x0), x51);
16521651 var x57: u64 = undefined;
16531652 var x58: u1 = undefined;
1654 subborrowxU64(&x57, &x58, x56, cast(u64, 0x0), x50);
1653 subborrowxU64(&x57, &x58, x56, @as(u64, 0x0), x50);
16551654 var x59: u64 = undefined;
16561655 var x60: u1 = undefined;
1657 subborrowxU64(&x59, &x60, x58, cast(u64, 0x0), x49);
1656 subborrowxU64(&x59, &x60, x58, @as(u64, 0x0), x49);
16581657 var x61: u64 = undefined;
1659 cmovznzU64(&x61, x60, cast(u64, 0x0), 0xffffffffffffffff);
1658 cmovznzU64(&x61, x60, @as(u64, 0x0), 0xffffffffffffffff);
16601659 var x62: u64 = undefined;
16611660 var x63: u1 = undefined;
16621661 addcarryxU64(&x62, &x63, 0x0, x53, x61);
......@@ -1665,7 +1664,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
16651664 addcarryxU64(&x64, &x65, x63, x55, (x61 & 0xffffffff));
16661665 var x66: u64 = undefined;
16671666 var x67: u1 = undefined;
1668 addcarryxU64(&x66, &x67, x65, x57, cast(u64, 0x0));
1667 addcarryxU64(&x66, &x67, x65, x57, @as(u64, 0x0));
16691668 var x68: u64 = undefined;
16701669 var x69: u1 = undefined;
16711670 addcarryxU64(&x68, &x69, x67, x59, (x61 & 0xffffffff00000001));
......@@ -1677,17 +1676,17 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
16771676 cmovznzU64(&x72, x3, (arg5[2]), x66);
16781677 var x73: u64 = undefined;
16791678 cmovznzU64(&x73, x3, (arg5[3]), x68);
1680 const x74 = cast(u1, (x22 & cast(u64, 0x1)));
1679 const x74 = @truncate(u1, (x22 & @as(u64, 0x1)));
16811680 var x75: u64 = undefined;
1682 cmovznzU64(&x75, x74, cast(u64, 0x0), x7);
1681 cmovznzU64(&x75, x74, @as(u64, 0x0), x7);
16831682 var x76: u64 = undefined;
1684 cmovznzU64(&x76, x74, cast(u64, 0x0), x8);
1683 cmovznzU64(&x76, x74, @as(u64, 0x0), x8);
16851684 var x77: u64 = undefined;
1686 cmovznzU64(&x77, x74, cast(u64, 0x0), x9);
1685 cmovznzU64(&x77, x74, @as(u64, 0x0), x9);
16871686 var x78: u64 = undefined;
1688 cmovznzU64(&x78, x74, cast(u64, 0x0), x10);
1687 cmovznzU64(&x78, x74, @as(u64, 0x0), x10);
16891688 var x79: u64 = undefined;
1690 cmovznzU64(&x79, x74, cast(u64, 0x0), x11);
1689 cmovznzU64(&x79, x74, @as(u64, 0x0), x11);
16911690 var x80: u64 = undefined;
16921691 var x81: u1 = undefined;
16931692 addcarryxU64(&x80, &x81, 0x0, x22, x75);
......@@ -1704,13 +1703,13 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
17041703 var x89: u1 = undefined;
17051704 addcarryxU64(&x88, &x89, x87, x26, x79);
17061705 var x90: u64 = undefined;
1707 cmovznzU64(&x90, x74, cast(u64, 0x0), x27);
1706 cmovznzU64(&x90, x74, @as(u64, 0x0), x27);
17081707 var x91: u64 = undefined;
1709 cmovznzU64(&x91, x74, cast(u64, 0x0), x28);
1708 cmovznzU64(&x91, x74, @as(u64, 0x0), x28);
17101709 var x92: u64 = undefined;
1711 cmovznzU64(&x92, x74, cast(u64, 0x0), x29);
1710 cmovznzU64(&x92, x74, @as(u64, 0x0), x29);
17121711 var x93: u64 = undefined;
1713 cmovznzU64(&x93, x74, cast(u64, 0x0), x30);
1712 cmovznzU64(&x93, x74, @as(u64, 0x0), x30);
17141713 var x94: u64 = undefined;
17151714 var x95: u1 = undefined;
17161715 addcarryxU64(&x94, &x95, 0x0, x70, x90);
......@@ -1731,16 +1730,16 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
17311730 subborrowxU64(&x104, &x105, x103, x96, 0xffffffff);
17321731 var x106: u64 = undefined;
17331732 var x107: u1 = undefined;
1734 subborrowxU64(&x106, &x107, x105, x98, cast(u64, 0x0));
1733 subborrowxU64(&x106, &x107, x105, x98, @as(u64, 0x0));
17351734 var x108: u64 = undefined;
17361735 var x109: u1 = undefined;
17371736 subborrowxU64(&x108, &x109, x107, x100, 0xffffffff00000001);
17381737 var x110: u64 = undefined;
17391738 var x111: u1 = undefined;
1740 subborrowxU64(&x110, &x111, x109, cast(u64, x101), cast(u64, 0x0));
1739 subborrowxU64(&x110, &x111, x109, @as(u64, x101), @as(u64, 0x0));
17411740 var x112: u64 = undefined;
17421741 var x113: u1 = undefined;
1743 addcarryxU64(&x112, &x113, 0x0, x6, cast(u64, 0x1));
1742 addcarryxU64(&x112, &x113, 0x0, x6, @as(u64, 0x1));
17441743 const x114 = ((x80 >> 1) | ((x82 << 63) & 0xffffffffffffffff));
17451744 const x115 = ((x82 >> 1) | ((x84 << 63) & 0xffffffffffffffff));
17461745 const x116 = ((x84 >> 1) | ((x86 << 63) & 0xffffffffffffffff));
lib/std/crypto/pcurves/p256/p256_scalar_64.zig+144-145
......@@ -18,7 +18,6 @@
1818// if x1 & (2^256-1) < 2^255 then x1 & (2^256-1) else (x1 & (2^256-1)) - 2^256
1919
2020const std = @import("std");
21const cast = std.meta.cast;
2221const mode = std.builtin.mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels
2322
2423// The type MontgomeryDomainFieldElement is a field element in the Montgomery domain.
......@@ -148,7 +147,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
148147 var x17: u64 = undefined;
149148 var x18: u1 = undefined;
150149 addcarryxU64(&x17, &x18, x16, x8, x5);
151 const x19 = (cast(u64, x18) + x6);
150 const x19 = (@as(u64, x18) + x6);
152151 var x20: u64 = undefined;
153152 var x21: u64 = undefined;
154153 mulxU64(&x20, &x21, x11, 0xccd1c8aaee00bc4f);
......@@ -173,7 +172,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
173172 var x34: u64 = undefined;
174173 var x35: u1 = undefined;
175174 addcarryxU64(&x34, &x35, x33, x25, x22);
176 const x36 = (cast(u64, x35) + x23);
175 const x36 = (@as(u64, x35) + x23);
177176 var x37: u64 = undefined;
178177 var x38: u1 = undefined;
179178 addcarryxU64(&x37, &x38, 0x0, x11, x28);
......@@ -210,7 +209,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
210209 var x59: u64 = undefined;
211210 var x60: u1 = undefined;
212211 addcarryxU64(&x59, &x60, x58, x50, x47);
213 const x61 = (cast(u64, x60) + x48);
212 const x61 = (@as(u64, x60) + x48);
214213 var x62: u64 = undefined;
215214 var x63: u1 = undefined;
216215 addcarryxU64(&x62, &x63, 0x0, x39, x53);
......@@ -225,7 +224,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
225224 addcarryxU64(&x68, &x69, x67, x45, x59);
226225 var x70: u64 = undefined;
227226 var x71: u1 = undefined;
228 addcarryxU64(&x70, &x71, x69, cast(u64, x46), x61);
227 addcarryxU64(&x70, &x71, x69, @as(u64, x46), x61);
229228 var x72: u64 = undefined;
230229 var x73: u64 = undefined;
231230 mulxU64(&x72, &x73, x62, 0xccd1c8aaee00bc4f);
......@@ -250,7 +249,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
250249 var x86: u64 = undefined;
251250 var x87: u1 = undefined;
252251 addcarryxU64(&x86, &x87, x85, x77, x74);
253 const x88 = (cast(u64, x87) + x75);
252 const x88 = (@as(u64, x87) + x75);
254253 var x89: u64 = undefined;
255254 var x90: u1 = undefined;
256255 addcarryxU64(&x89, &x90, 0x0, x62, x80);
......@@ -266,7 +265,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
266265 var x97: u64 = undefined;
267266 var x98: u1 = undefined;
268267 addcarryxU64(&x97, &x98, x96, x70, x88);
269 const x99 = (cast(u64, x98) + cast(u64, x71));
268 const x99 = (@as(u64, x98) + @as(u64, x71));
270269 var x100: u64 = undefined;
271270 var x101: u64 = undefined;
272271 mulxU64(&x100, &x101, x2, (arg2[3]));
......@@ -288,7 +287,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
288287 var x112: u64 = undefined;
289288 var x113: u1 = undefined;
290289 addcarryxU64(&x112, &x113, x111, x103, x100);
291 const x114 = (cast(u64, x113) + x101);
290 const x114 = (@as(u64, x113) + x101);
292291 var x115: u64 = undefined;
293292 var x116: u1 = undefined;
294293 addcarryxU64(&x115, &x116, 0x0, x91, x106);
......@@ -328,7 +327,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
328327 var x139: u64 = undefined;
329328 var x140: u1 = undefined;
330329 addcarryxU64(&x139, &x140, x138, x130, x127);
331 const x141 = (cast(u64, x140) + x128);
330 const x141 = (@as(u64, x140) + x128);
332331 var x142: u64 = undefined;
333332 var x143: u1 = undefined;
334333 addcarryxU64(&x142, &x143, 0x0, x115, x133);
......@@ -344,7 +343,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
344343 var x150: u64 = undefined;
345344 var x151: u1 = undefined;
346345 addcarryxU64(&x150, &x151, x149, x123, x141);
347 const x152 = (cast(u64, x151) + cast(u64, x124));
346 const x152 = (@as(u64, x151) + @as(u64, x124));
348347 var x153: u64 = undefined;
349348 var x154: u64 = undefined;
350349 mulxU64(&x153, &x154, x3, (arg2[3]));
......@@ -366,7 +365,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
366365 var x165: u64 = undefined;
367366 var x166: u1 = undefined;
368367 addcarryxU64(&x165, &x166, x164, x156, x153);
369 const x167 = (cast(u64, x166) + x154);
368 const x167 = (@as(u64, x166) + x154);
370369 var x168: u64 = undefined;
371370 var x169: u1 = undefined;
372371 addcarryxU64(&x168, &x169, 0x0, x144, x159);
......@@ -406,7 +405,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
406405 var x192: u64 = undefined;
407406 var x193: u1 = undefined;
408407 addcarryxU64(&x192, &x193, x191, x183, x180);
409 const x194 = (cast(u64, x193) + x181);
408 const x194 = (@as(u64, x193) + x181);
410409 var x195: u64 = undefined;
411410 var x196: u1 = undefined;
412411 addcarryxU64(&x195, &x196, 0x0, x168, x186);
......@@ -422,7 +421,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
422421 var x203: u64 = undefined;
423422 var x204: u1 = undefined;
424423 addcarryxU64(&x203, &x204, x202, x176, x194);
425 const x205 = (cast(u64, x204) + cast(u64, x177));
424 const x205 = (@as(u64, x204) + @as(u64, x177));
426425 var x206: u64 = undefined;
427426 var x207: u1 = undefined;
428427 subborrowxU64(&x206, &x207, 0x0, x197, 0xf3b9cac2fc632551);
......@@ -437,7 +436,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
437436 subborrowxU64(&x212, &x213, x211, x203, 0xffffffff00000000);
438437 var x214: u64 = undefined;
439438 var x215: u1 = undefined;
440 subborrowxU64(&x214, &x215, x213, x205, cast(u64, 0x0));
439 subborrowxU64(&x214, &x215, x213, x205, @as(u64, 0x0));
441440 var x216: u64 = undefined;
442441 cmovznzU64(&x216, x215, x206, x197);
443442 var x217: u64 = undefined;
......@@ -488,7 +487,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
488487 var x17: u64 = undefined;
489488 var x18: u1 = undefined;
490489 addcarryxU64(&x17, &x18, x16, x8, x5);
491 const x19 = (cast(u64, x18) + x6);
490 const x19 = (@as(u64, x18) + x6);
492491 var x20: u64 = undefined;
493492 var x21: u64 = undefined;
494493 mulxU64(&x20, &x21, x11, 0xccd1c8aaee00bc4f);
......@@ -513,7 +512,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
513512 var x34: u64 = undefined;
514513 var x35: u1 = undefined;
515514 addcarryxU64(&x34, &x35, x33, x25, x22);
516 const x36 = (cast(u64, x35) + x23);
515 const x36 = (@as(u64, x35) + x23);
517516 var x37: u64 = undefined;
518517 var x38: u1 = undefined;
519518 addcarryxU64(&x37, &x38, 0x0, x11, x28);
......@@ -550,7 +549,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
550549 var x59: u64 = undefined;
551550 var x60: u1 = undefined;
552551 addcarryxU64(&x59, &x60, x58, x50, x47);
553 const x61 = (cast(u64, x60) + x48);
552 const x61 = (@as(u64, x60) + x48);
554553 var x62: u64 = undefined;
555554 var x63: u1 = undefined;
556555 addcarryxU64(&x62, &x63, 0x0, x39, x53);
......@@ -565,7 +564,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
565564 addcarryxU64(&x68, &x69, x67, x45, x59);
566565 var x70: u64 = undefined;
567566 var x71: u1 = undefined;
568 addcarryxU64(&x70, &x71, x69, cast(u64, x46), x61);
567 addcarryxU64(&x70, &x71, x69, @as(u64, x46), x61);
569568 var x72: u64 = undefined;
570569 var x73: u64 = undefined;
571570 mulxU64(&x72, &x73, x62, 0xccd1c8aaee00bc4f);
......@@ -590,7 +589,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
590589 var x86: u64 = undefined;
591590 var x87: u1 = undefined;
592591 addcarryxU64(&x86, &x87, x85, x77, x74);
593 const x88 = (cast(u64, x87) + x75);
592 const x88 = (@as(u64, x87) + x75);
594593 var x89: u64 = undefined;
595594 var x90: u1 = undefined;
596595 addcarryxU64(&x89, &x90, 0x0, x62, x80);
......@@ -606,7 +605,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
606605 var x97: u64 = undefined;
607606 var x98: u1 = undefined;
608607 addcarryxU64(&x97, &x98, x96, x70, x88);
609 const x99 = (cast(u64, x98) + cast(u64, x71));
608 const x99 = (@as(u64, x98) + @as(u64, x71));
610609 var x100: u64 = undefined;
611610 var x101: u64 = undefined;
612611 mulxU64(&x100, &x101, x2, (arg1[3]));
......@@ -628,7 +627,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
628627 var x112: u64 = undefined;
629628 var x113: u1 = undefined;
630629 addcarryxU64(&x112, &x113, x111, x103, x100);
631 const x114 = (cast(u64, x113) + x101);
630 const x114 = (@as(u64, x113) + x101);
632631 var x115: u64 = undefined;
633632 var x116: u1 = undefined;
634633 addcarryxU64(&x115, &x116, 0x0, x91, x106);
......@@ -668,7 +667,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
668667 var x139: u64 = undefined;
669668 var x140: u1 = undefined;
670669 addcarryxU64(&x139, &x140, x138, x130, x127);
671 const x141 = (cast(u64, x140) + x128);
670 const x141 = (@as(u64, x140) + x128);
672671 var x142: u64 = undefined;
673672 var x143: u1 = undefined;
674673 addcarryxU64(&x142, &x143, 0x0, x115, x133);
......@@ -684,7 +683,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
684683 var x150: u64 = undefined;
685684 var x151: u1 = undefined;
686685 addcarryxU64(&x150, &x151, x149, x123, x141);
687 const x152 = (cast(u64, x151) + cast(u64, x124));
686 const x152 = (@as(u64, x151) + @as(u64, x124));
688687 var x153: u64 = undefined;
689688 var x154: u64 = undefined;
690689 mulxU64(&x153, &x154, x3, (arg1[3]));
......@@ -706,7 +705,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
706705 var x165: u64 = undefined;
707706 var x166: u1 = undefined;
708707 addcarryxU64(&x165, &x166, x164, x156, x153);
709 const x167 = (cast(u64, x166) + x154);
708 const x167 = (@as(u64, x166) + x154);
710709 var x168: u64 = undefined;
711710 var x169: u1 = undefined;
712711 addcarryxU64(&x168, &x169, 0x0, x144, x159);
......@@ -746,7 +745,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
746745 var x192: u64 = undefined;
747746 var x193: u1 = undefined;
748747 addcarryxU64(&x192, &x193, x191, x183, x180);
749 const x194 = (cast(u64, x193) + x181);
748 const x194 = (@as(u64, x193) + x181);
750749 var x195: u64 = undefined;
751750 var x196: u1 = undefined;
752751 addcarryxU64(&x195, &x196, 0x0, x168, x186);
......@@ -762,7 +761,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
762761 var x203: u64 = undefined;
763762 var x204: u1 = undefined;
764763 addcarryxU64(&x203, &x204, x202, x176, x194);
765 const x205 = (cast(u64, x204) + cast(u64, x177));
764 const x205 = (@as(u64, x204) + @as(u64, x177));
766765 var x206: u64 = undefined;
767766 var x207: u1 = undefined;
768767 subborrowxU64(&x206, &x207, 0x0, x197, 0xf3b9cac2fc632551);
......@@ -777,7 +776,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl
777776 subborrowxU64(&x212, &x213, x211, x203, 0xffffffff00000000);
778777 var x214: u64 = undefined;
779778 var x215: u1 = undefined;
780 subborrowxU64(&x214, &x215, x213, x205, cast(u64, 0x0));
779 subborrowxU64(&x214, &x215, x213, x205, @as(u64, 0x0));
781780 var x216: u64 = undefined;
782781 cmovznzU64(&x216, x215, x206, x197);
783782 var x217: u64 = undefined;
......@@ -830,7 +829,7 @@ pub fn add(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
830829 subborrowxU64(&x15, &x16, x14, x7, 0xffffffff00000000);
831830 var x17: u64 = undefined;
832831 var x18: u1 = undefined;
833 subborrowxU64(&x17, &x18, x16, cast(u64, x8), cast(u64, 0x0));
832 subborrowxU64(&x17, &x18, x16, @as(u64, x8), @as(u64, 0x0));
834833 var x19: u64 = undefined;
835834 cmovznzU64(&x19, x18, x9, x1);
836835 var x20: u64 = undefined;
......@@ -870,7 +869,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
870869 var x8: u1 = undefined;
871870 subborrowxU64(&x7, &x8, x6, (arg1[3]), (arg2[3]));
872871 var x9: u64 = undefined;
873 cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff);
872 cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
874873 var x10: u64 = undefined;
875874 var x11: u1 = undefined;
876875 addcarryxU64(&x10, &x11, 0x0, x1, (x9 & 0xf3b9cac2fc632551));
......@@ -902,18 +901,18 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme
902901
903902 var x1: u64 = undefined;
904903 var x2: u1 = undefined;
905 subborrowxU64(&x1, &x2, 0x0, cast(u64, 0x0), (arg1[0]));
904 subborrowxU64(&x1, &x2, 0x0, @as(u64, 0x0), (arg1[0]));
906905 var x3: u64 = undefined;
907906 var x4: u1 = undefined;
908 subborrowxU64(&x3, &x4, x2, cast(u64, 0x0), (arg1[1]));
907 subborrowxU64(&x3, &x4, x2, @as(u64, 0x0), (arg1[1]));
909908 var x5: u64 = undefined;
910909 var x6: u1 = undefined;
911 subborrowxU64(&x5, &x6, x4, cast(u64, 0x0), (arg1[2]));
910 subborrowxU64(&x5, &x6, x4, @as(u64, 0x0), (arg1[2]));
912911 var x7: u64 = undefined;
913912 var x8: u1 = undefined;
914 subborrowxU64(&x7, &x8, x6, cast(u64, 0x0), (arg1[3]));
913 subborrowxU64(&x7, &x8, x6, @as(u64, 0x0), (arg1[3]));
915914 var x9: u64 = undefined;
916 cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff);
915 cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff);
917916 var x10: u64 = undefined;
918917 var x11: u1 = undefined;
919918 addcarryxU64(&x10, &x11, 0x0, x1, (x9 & 0xf3b9cac2fc632551));
......@@ -973,22 +972,22 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
973972 addcarryxU64(&x18, &x19, 0x0, x1, x10);
974973 var x20: u64 = undefined;
975974 var x21: u1 = undefined;
976 addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), x12);
975 addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), x12);
977976 var x22: u64 = undefined;
978977 var x23: u1 = undefined;
979 addcarryxU64(&x22, &x23, x21, cast(u64, 0x0), x14);
978 addcarryxU64(&x22, &x23, x21, @as(u64, 0x0), x14);
980979 var x24: u64 = undefined;
981980 var x25: u1 = undefined;
982 addcarryxU64(&x24, &x25, x23, cast(u64, 0x0), x16);
981 addcarryxU64(&x24, &x25, x23, @as(u64, 0x0), x16);
983982 var x26: u64 = undefined;
984983 var x27: u1 = undefined;
985984 addcarryxU64(&x26, &x27, 0x0, x20, (arg1[1]));
986985 var x28: u64 = undefined;
987986 var x29: u1 = undefined;
988 addcarryxU64(&x28, &x29, x27, x22, cast(u64, 0x0));
987 addcarryxU64(&x28, &x29, x27, x22, @as(u64, 0x0));
989988 var x30: u64 = undefined;
990989 var x31: u1 = undefined;
991 addcarryxU64(&x30, &x31, x29, x24, cast(u64, 0x0));
990 addcarryxU64(&x30, &x31, x29, x24, @as(u64, 0x0));
992991 var x32: u64 = undefined;
993992 var x33: u64 = undefined;
994993 mulxU64(&x32, &x33, x26, 0xccd1c8aaee00bc4f);
......@@ -1024,16 +1023,16 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
10241023 addcarryxU64(&x52, &x53, x51, x30, x44);
10251024 var x54: u64 = undefined;
10261025 var x55: u1 = undefined;
1027 addcarryxU64(&x54, &x55, x53, (cast(u64, x31) + (cast(u64, x25) + (cast(u64, x17) + x5))), x46);
1026 addcarryxU64(&x54, &x55, x53, (@as(u64, x31) + (@as(u64, x25) + (@as(u64, x17) + x5))), x46);
10281027 var x56: u64 = undefined;
10291028 var x57: u1 = undefined;
10301029 addcarryxU64(&x56, &x57, 0x0, x50, (arg1[2]));
10311030 var x58: u64 = undefined;
10321031 var x59: u1 = undefined;
1033 addcarryxU64(&x58, &x59, x57, x52, cast(u64, 0x0));
1032 addcarryxU64(&x58, &x59, x57, x52, @as(u64, 0x0));
10341033 var x60: u64 = undefined;
10351034 var x61: u1 = undefined;
1036 addcarryxU64(&x60, &x61, x59, x54, cast(u64, 0x0));
1035 addcarryxU64(&x60, &x61, x59, x54, @as(u64, 0x0));
10371036 var x62: u64 = undefined;
10381037 var x63: u64 = undefined;
10391038 mulxU64(&x62, &x63, x56, 0xccd1c8aaee00bc4f);
......@@ -1069,16 +1068,16 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
10691068 addcarryxU64(&x82, &x83, x81, x60, x74);
10701069 var x84: u64 = undefined;
10711070 var x85: u1 = undefined;
1072 addcarryxU64(&x84, &x85, x83, (cast(u64, x61) + (cast(u64, x55) + (cast(u64, x47) + x35))), x76);
1071 addcarryxU64(&x84, &x85, x83, (@as(u64, x61) + (@as(u64, x55) + (@as(u64, x47) + x35))), x76);
10731072 var x86: u64 = undefined;
10741073 var x87: u1 = undefined;
10751074 addcarryxU64(&x86, &x87, 0x0, x80, (arg1[3]));
10761075 var x88: u64 = undefined;
10771076 var x89: u1 = undefined;
1078 addcarryxU64(&x88, &x89, x87, x82, cast(u64, 0x0));
1077 addcarryxU64(&x88, &x89, x87, x82, @as(u64, 0x0));
10791078 var x90: u64 = undefined;
10801079 var x91: u1 = undefined;
1081 addcarryxU64(&x90, &x91, x89, x84, cast(u64, 0x0));
1080 addcarryxU64(&x90, &x91, x89, x84, @as(u64, 0x0));
10821081 var x92: u64 = undefined;
10831082 var x93: u64 = undefined;
10841083 mulxU64(&x92, &x93, x86, 0xccd1c8aaee00bc4f);
......@@ -1114,8 +1113,8 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
11141113 addcarryxU64(&x112, &x113, x111, x90, x104);
11151114 var x114: u64 = undefined;
11161115 var x115: u1 = undefined;
1117 addcarryxU64(&x114, &x115, x113, (cast(u64, x91) + (cast(u64, x85) + (cast(u64, x77) + x65))), x106);
1118 const x116 = (cast(u64, x115) + (cast(u64, x107) + x95));
1116 addcarryxU64(&x114, &x115, x113, (@as(u64, x91) + (@as(u64, x85) + (@as(u64, x77) + x65))), x106);
1117 const x116 = (@as(u64, x115) + (@as(u64, x107) + x95));
11191118 var x117: u64 = undefined;
11201119 var x118: u1 = undefined;
11211120 subborrowxU64(&x117, &x118, 0x0, x110, 0xf3b9cac2fc632551);
......@@ -1130,7 +1129,7 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo
11301129 subborrowxU64(&x123, &x124, x122, x116, 0xffffffff00000000);
11311130 var x125: u64 = undefined;
11321131 var x126: u1 = undefined;
1133 subborrowxU64(&x125, &x126, x124, cast(u64, 0x0), cast(u64, 0x0));
1132 subborrowxU64(&x125, &x126, x124, @as(u64, 0x0), @as(u64, 0x0));
11341133 var x127: u64 = undefined;
11351134 cmovznzU64(&x127, x126, x117, x110);
11361135 var x128: u64 = undefined;
......@@ -1219,7 +1218,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
12191218 addcarryxU64(&x41, &x42, x40, x17, x33);
12201219 var x43: u64 = undefined;
12211220 var x44: u1 = undefined;
1222 addcarryxU64(&x43, &x44, x42, (cast(u64, x18) + x6), (cast(u64, x34) + x22));
1221 addcarryxU64(&x43, &x44, x42, (@as(u64, x18) + x6), (@as(u64, x34) + x22));
12231222 var x45: u64 = undefined;
12241223 var x46: u64 = undefined;
12251224 mulxU64(&x45, &x46, x1, 0x66e12d94f3d95620);
......@@ -1291,7 +1290,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
12911290 addcarryxU64(&x89, &x90, x88, x65, x81);
12921291 var x91: u64 = undefined;
12931292 var x92: u1 = undefined;
1294 addcarryxU64(&x91, &x92, x90, ((cast(u64, x66) + cast(u64, x44)) + (cast(u64, x58) + x46)), (cast(u64, x82) + x70));
1293 addcarryxU64(&x91, &x92, x90, ((@as(u64, x66) + @as(u64, x44)) + (@as(u64, x58) + x46)), (@as(u64, x82) + x70));
12951294 var x93: u64 = undefined;
12961295 var x94: u64 = undefined;
12971296 mulxU64(&x93, &x94, x2, 0x66e12d94f3d95620);
......@@ -1363,7 +1362,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
13631362 addcarryxU64(&x137, &x138, x136, x113, x129);
13641363 var x139: u64 = undefined;
13651364 var x140: u1 = undefined;
1366 addcarryxU64(&x139, &x140, x138, ((cast(u64, x114) + cast(u64, x92)) + (cast(u64, x106) + x94)), (cast(u64, x130) + x118));
1365 addcarryxU64(&x139, &x140, x138, ((@as(u64, x114) + @as(u64, x92)) + (@as(u64, x106) + x94)), (@as(u64, x130) + x118));
13671366 var x141: u64 = undefined;
13681367 var x142: u64 = undefined;
13691368 mulxU64(&x141, &x142, x3, 0x66e12d94f3d95620);
......@@ -1435,7 +1434,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
14351434 addcarryxU64(&x185, &x186, x184, x161, x177);
14361435 var x187: u64 = undefined;
14371436 var x188: u1 = undefined;
1438 addcarryxU64(&x187, &x188, x186, ((cast(u64, x162) + cast(u64, x140)) + (cast(u64, x154) + x142)), (cast(u64, x178) + x166));
1437 addcarryxU64(&x187, &x188, x186, ((@as(u64, x162) + @as(u64, x140)) + (@as(u64, x154) + x142)), (@as(u64, x178) + x166));
14391438 var x189: u64 = undefined;
14401439 var x190: u1 = undefined;
14411440 subborrowxU64(&x189, &x190, 0x0, x181, 0xf3b9cac2fc632551);
......@@ -1450,7 +1449,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma
14501449 subborrowxU64(&x195, &x196, x194, x187, 0xffffffff00000000);
14511450 var x197: u64 = undefined;
14521451 var x198: u1 = undefined;
1453 subborrowxU64(&x197, &x198, x196, cast(u64, x188), cast(u64, 0x0));
1452 subborrowxU64(&x197, &x198, x196, @as(u64, x188), @as(u64, 0x0));
14541453 var x199: u64 = undefined;
14551454 cmovznzU64(&x199, x198, x189, x181);
14561455 var x200: u64 = undefined;
......@@ -1529,62 +1528,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void {
15291528 const x2 = (arg1[2]);
15301529 const x3 = (arg1[1]);
15311530 const x4 = (arg1[0]);
1532 const x5 = cast(u8, (x4 & cast(u64, 0xff)));
1531 const x5 = @truncate(u8, (x4 & @as(u64, 0xff)));
15331532 const x6 = (x4 >> 8);
1534 const x7 = cast(u8, (x6 & cast(u64, 0xff)));
1533 const x7 = @truncate(u8, (x6 & @as(u64, 0xff)));
15351534 const x8 = (x6 >> 8);
1536 const x9 = cast(u8, (x8 & cast(u64, 0xff)));
1535 const x9 = @truncate(u8, (x8 & @as(u64, 0xff)));
15371536 const x10 = (x8 >> 8);
1538 const x11 = cast(u8, (x10 & cast(u64, 0xff)));
1537 const x11 = @truncate(u8, (x10 & @as(u64, 0xff)));
15391538 const x12 = (x10 >> 8);
1540 const x13 = cast(u8, (x12 & cast(u64, 0xff)));
1539 const x13 = @truncate(u8, (x12 & @as(u64, 0xff)));
15411540 const x14 = (x12 >> 8);
1542 const x15 = cast(u8, (x14 & cast(u64, 0xff)));
1541 const x15 = @truncate(u8, (x14 & @as(u64, 0xff)));
15431542 const x16 = (x14 >> 8);
1544 const x17 = cast(u8, (x16 & cast(u64, 0xff)));
1545 const x18 = cast(u8, (x16 >> 8));
1546 const x19 = cast(u8, (x3 & cast(u64, 0xff)));
1543 const x17 = @truncate(u8, (x16 & @as(u64, 0xff)));
1544 const x18 = @truncate(u8, (x16 >> 8));
1545 const x19 = @truncate(u8, (x3 & @as(u64, 0xff)));
15471546 const x20 = (x3 >> 8);
1548 const x21 = cast(u8, (x20 & cast(u64, 0xff)));
1547 const x21 = @truncate(u8, (x20 & @as(u64, 0xff)));
15491548 const x22 = (x20 >> 8);
1550 const x23 = cast(u8, (x22 & cast(u64, 0xff)));
1549 const x23 = @truncate(u8, (x22 & @as(u64, 0xff)));
15511550 const x24 = (x22 >> 8);
1552 const x25 = cast(u8, (x24 & cast(u64, 0xff)));
1551 const x25 = @truncate(u8, (x24 & @as(u64, 0xff)));
15531552 const x26 = (x24 >> 8);
1554 const x27 = cast(u8, (x26 & cast(u64, 0xff)));
1553 const x27 = @truncate(u8, (x26 & @as(u64, 0xff)));
15551554 const x28 = (x26 >> 8);
1556 const x29 = cast(u8, (x28 & cast(u64, 0xff)));
1555 const x29 = @truncate(u8, (x28 & @as(u64, 0xff)));
15571556 const x30 = (x28 >> 8);
1558 const x31 = cast(u8, (x30 & cast(u64, 0xff)));
1559 const x32 = cast(u8, (x30 >> 8));
1560 const x33 = cast(u8, (x2 & cast(u64, 0xff)));
1557 const x31 = @truncate(u8, (x30 & @as(u64, 0xff)));
1558 const x32 = @truncate(u8, (x30 >> 8));
1559 const x33 = @truncate(u8, (x2 & @as(u64, 0xff)));
15611560 const x34 = (x2 >> 8);
1562 const x35 = cast(u8, (x34 & cast(u64, 0xff)));
1561 const x35 = @truncate(u8, (x34 & @as(u64, 0xff)));
15631562 const x36 = (x34 >> 8);
1564 const x37 = cast(u8, (x36 & cast(u64, 0xff)));
1563 const x37 = @truncate(u8, (x36 & @as(u64, 0xff)));
15651564 const x38 = (x36 >> 8);
1566 const x39 = cast(u8, (x38 & cast(u64, 0xff)));
1565 const x39 = @truncate(u8, (x38 & @as(u64, 0xff)));
15671566 const x40 = (x38 >> 8);
1568 const x41 = cast(u8, (x40 & cast(u64, 0xff)));
1567 const x41 = @truncate(u8, (x40 & @as(u64, 0xff)));
15691568 const x42 = (x40 >> 8);
1570 const x43 = cast(u8, (x42 & cast(u64, 0xff)));
1569 const x43 = @truncate(u8, (x42 & @as(u64, 0xff)));
15711570 const x44 = (x42 >> 8);
1572 const x45 = cast(u8, (x44 & cast(u64, 0xff)));
1573 const x46 = cast(u8, (x44 >> 8));
1574 const x47 = cast(u8, (x1 & cast(u64, 0xff)));
1571 const x45 = @truncate(u8, (x44 & @as(u64, 0xff)));
1572 const x46 = @truncate(u8, (x44 >> 8));
1573 const x47 = @truncate(u8, (x1 & @as(u64, 0xff)));
15751574 const x48 = (x1 >> 8);
1576 const x49 = cast(u8, (x48 & cast(u64, 0xff)));
1575 const x49 = @truncate(u8, (x48 & @as(u64, 0xff)));
15771576 const x50 = (x48 >> 8);
1578 const x51 = cast(u8, (x50 & cast(u64, 0xff)));
1577 const x51 = @truncate(u8, (x50 & @as(u64, 0xff)));
15791578 const x52 = (x50 >> 8);
1580 const x53 = cast(u8, (x52 & cast(u64, 0xff)));
1579 const x53 = @truncate(u8, (x52 & @as(u64, 0xff)));
15811580 const x54 = (x52 >> 8);
1582 const x55 = cast(u8, (x54 & cast(u64, 0xff)));
1581 const x55 = @truncate(u8, (x54 & @as(u64, 0xff)));
15831582 const x56 = (x54 >> 8);
1584 const x57 = cast(u8, (x56 & cast(u64, 0xff)));
1583 const x57 = @truncate(u8, (x56 & @as(u64, 0xff)));
15851584 const x58 = (x56 >> 8);
1586 const x59 = cast(u8, (x58 & cast(u64, 0xff)));
1587 const x60 = cast(u8, (x58 >> 8));
1585 const x59 = @truncate(u8, (x58 & @as(u64, 0xff)));
1586 const x60 = @truncate(u8, (x58 >> 8));
15881587 out1[0] = x5;
15891588 out1[1] = x7;
15901589 out1[2] = x9;
......@@ -1634,60 +1633,60 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void {
16341633pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void {
16351634 @setRuntimeSafety(mode == .Debug);
16361635
1637 const x1 = (cast(u64, (arg1[31])) << 56);
1638 const x2 = (cast(u64, (arg1[30])) << 48);
1639 const x3 = (cast(u64, (arg1[29])) << 40);
1640 const x4 = (cast(u64, (arg1[28])) << 32);
1641 const x5 = (cast(u64, (arg1[27])) << 24);
1642 const x6 = (cast(u64, (arg1[26])) << 16);
1643 const x7 = (cast(u64, (arg1[25])) << 8);
1636 const x1 = (@as(u64, (arg1[31])) << 56);
1637 const x2 = (@as(u64, (arg1[30])) << 48);
1638 const x3 = (@as(u64, (arg1[29])) << 40);
1639 const x4 = (@as(u64, (arg1[28])) << 32);
1640 const x5 = (@as(u64, (arg1[27])) << 24);
1641 const x6 = (@as(u64, (arg1[26])) << 16);
1642 const x7 = (@as(u64, (arg1[25])) << 8);
16441643 const x8 = (arg1[24]);
1645 const x9 = (cast(u64, (arg1[23])) << 56);
1646 const x10 = (cast(u64, (arg1[22])) << 48);
1647 const x11 = (cast(u64, (arg1[21])) << 40);
1648 const x12 = (cast(u64, (arg1[20])) << 32);
1649 const x13 = (cast(u64, (arg1[19])) << 24);
1650 const x14 = (cast(u64, (arg1[18])) << 16);
1651 const x15 = (cast(u64, (arg1[17])) << 8);
1644 const x9 = (@as(u64, (arg1[23])) << 56);
1645 const x10 = (@as(u64, (arg1[22])) << 48);
1646 const x11 = (@as(u64, (arg1[21])) << 40);
1647 const x12 = (@as(u64, (arg1[20])) << 32);
1648 const x13 = (@as(u64, (arg1[19])) << 24);
1649 const x14 = (@as(u64, (arg1[18])) << 16);
1650 const x15 = (@as(u64, (arg1[17])) << 8);
16521651 const x16 = (arg1[16]);
1653 const x17 = (cast(u64, (arg1[15])) << 56);
1654 const x18 = (cast(u64, (arg1[14])) << 48);
1655 const x19 = (cast(u64, (arg1[13])) << 40);
1656 const x20 = (cast(u64, (arg1[12])) << 32);
1657 const x21 = (cast(u64, (arg1[11])) << 24);
1658 const x22 = (cast(u64, (arg1[10])) << 16);
1659 const x23 = (cast(u64, (arg1[9])) << 8);
1652 const x17 = (@as(u64, (arg1[15])) << 56);
1653 const x18 = (@as(u64, (arg1[14])) << 48);
1654 const x19 = (@as(u64, (arg1[13])) << 40);
1655 const x20 = (@as(u64, (arg1[12])) << 32);
1656 const x21 = (@as(u64, (arg1[11])) << 24);
1657 const x22 = (@as(u64, (arg1[10])) << 16);
1658 const x23 = (@as(u64, (arg1[9])) << 8);
16601659 const x24 = (arg1[8]);
1661 const x25 = (cast(u64, (arg1[7])) << 56);
1662 const x26 = (cast(u64, (arg1[6])) << 48);
1663 const x27 = (cast(u64, (arg1[5])) << 40);
1664 const x28 = (cast(u64, (arg1[4])) << 32);
1665 const x29 = (cast(u64, (arg1[3])) << 24);
1666 const x30 = (cast(u64, (arg1[2])) << 16);
1667 const x31 = (cast(u64, (arg1[1])) << 8);
1660 const x25 = (@as(u64, (arg1[7])) << 56);
1661 const x26 = (@as(u64, (arg1[6])) << 48);
1662 const x27 = (@as(u64, (arg1[5])) << 40);
1663 const x28 = (@as(u64, (arg1[4])) << 32);
1664 const x29 = (@as(u64, (arg1[3])) << 24);
1665 const x30 = (@as(u64, (arg1[2])) << 16);
1666 const x31 = (@as(u64, (arg1[1])) << 8);
16681667 const x32 = (arg1[0]);
1669 const x33 = (x31 + cast(u64, x32));
1668 const x33 = (x31 + @as(u64, x32));
16701669 const x34 = (x30 + x33);
16711670 const x35 = (x29 + x34);
16721671 const x36 = (x28 + x35);
16731672 const x37 = (x27 + x36);
16741673 const x38 = (x26 + x37);
16751674 const x39 = (x25 + x38);
1676 const x40 = (x23 + cast(u64, x24));
1675 const x40 = (x23 + @as(u64, x24));
16771676 const x41 = (x22 + x40);
16781677 const x42 = (x21 + x41);
16791678 const x43 = (x20 + x42);
16801679 const x44 = (x19 + x43);
16811680 const x45 = (x18 + x44);
16821681 const x46 = (x17 + x45);
1683 const x47 = (x15 + cast(u64, x16));
1682 const x47 = (x15 + @as(u64, x16));
16841683 const x48 = (x14 + x47);
16851684 const x49 = (x13 + x48);
16861685 const x50 = (x12 + x49);
16871686 const x51 = (x11 + x50);
16881687 const x52 = (x10 + x51);
16891688 const x53 = (x9 + x52);
1690 const x54 = (x7 + cast(u64, x8));
1689 const x54 = (x7 + @as(u64, x8));
16911690 const x55 = (x6 + x54);
16921691 const x56 = (x5 + x55);
16931692 const x57 = (x4 + x56);
......@@ -1711,7 +1710,7 @@ pub fn setOne(out1: *MontgomeryDomainFieldElement) void {
17111710
17121711 out1[0] = 0xc46353d039cdaaf;
17131712 out1[1] = 0x4319055258e8617b;
1714 out1[2] = cast(u64, 0x0);
1713 out1[2] = @as(u64, 0x0);
17151714 out1[3] = 0xffffffff;
17161715}
17171716
......@@ -1730,7 +1729,7 @@ pub fn msat(out1: *[5]u64) void {
17301729 out1[1] = 0xbce6faada7179e84;
17311730 out1[2] = 0xffffffffffffffff;
17321731 out1[3] = 0xffffffff00000000;
1733 out1[4] = cast(u64, 0x0);
1732 out1[4] = @as(u64, 0x0);
17341733}
17351734
17361735/// The function divstep computes a divstep.
......@@ -1766,11 +1765,11 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
17661765
17671766 var x1: u64 = undefined;
17681767 var x2: u1 = undefined;
1769 addcarryxU64(&x1, &x2, 0x0, (~arg1), cast(u64, 0x1));
1770 const x3 = (cast(u1, (x1 >> 63)) & cast(u1, ((arg3[0]) & cast(u64, 0x1))));
1768 addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1));
1769 const x3 = (@as(u1, (x1 >> 63)) & @as(u1, ((arg3[0]) & @as(u64, 0x1))));
17711770 var x4: u64 = undefined;
17721771 var x5: u1 = undefined;
1773 addcarryxU64(&x4, &x5, 0x0, (~arg1), cast(u64, 0x1));
1772 addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1));
17741773 var x6: u64 = undefined;
17751774 cmovznzU64(&x6, x3, arg1, x4);
17761775 var x7: u64 = undefined;
......@@ -1785,19 +1784,19 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
17851784 cmovznzU64(&x11, x3, (arg2[4]), (arg3[4]));
17861785 var x12: u64 = undefined;
17871786 var x13: u1 = undefined;
1788 addcarryxU64(&x12, &x13, 0x0, cast(u64, 0x1), (~(arg2[0])));
1787 addcarryxU64(&x12, &x13, 0x0, @as(u64, 0x1), (~(arg2[0])));
17891788 var x14: u64 = undefined;
17901789 var x15: u1 = undefined;
1791 addcarryxU64(&x14, &x15, x13, cast(u64, 0x0), (~(arg2[1])));
1790 addcarryxU64(&x14, &x15, x13, @as(u64, 0x0), (~(arg2[1])));
17921791 var x16: u64 = undefined;
17931792 var x17: u1 = undefined;
1794 addcarryxU64(&x16, &x17, x15, cast(u64, 0x0), (~(arg2[2])));
1793 addcarryxU64(&x16, &x17, x15, @as(u64, 0x0), (~(arg2[2])));
17951794 var x18: u64 = undefined;
17961795 var x19: u1 = undefined;
1797 addcarryxU64(&x18, &x19, x17, cast(u64, 0x0), (~(arg2[3])));
1796 addcarryxU64(&x18, &x19, x17, @as(u64, 0x0), (~(arg2[3])));
17981797 var x20: u64 = undefined;
17991798 var x21: u1 = undefined;
1800 addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), (~(arg2[4])));
1799 addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), (~(arg2[4])));
18011800 var x22: u64 = undefined;
18021801 cmovznzU64(&x22, x3, (arg3[0]), x12);
18031802 var x23: u64 = undefined;
......@@ -1842,25 +1841,25 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
18421841 subborrowxU64(&x45, &x46, x44, x37, 0xffffffff00000000);
18431842 var x47: u64 = undefined;
18441843 var x48: u1 = undefined;
1845 subborrowxU64(&x47, &x48, x46, cast(u64, x38), cast(u64, 0x0));
1844 subborrowxU64(&x47, &x48, x46, @as(u64, x38), @as(u64, 0x0));
18461845 const x49 = (arg4[3]);
18471846 const x50 = (arg4[2]);
18481847 const x51 = (arg4[1]);
18491848 const x52 = (arg4[0]);
18501849 var x53: u64 = undefined;
18511850 var x54: u1 = undefined;
1852 subborrowxU64(&x53, &x54, 0x0, cast(u64, 0x0), x52);
1851 subborrowxU64(&x53, &x54, 0x0, @as(u64, 0x0), x52);
18531852 var x55: u64 = undefined;
18541853 var x56: u1 = undefined;
1855 subborrowxU64(&x55, &x56, x54, cast(u64, 0x0), x51);
1854 subborrowxU64(&x55, &x56, x54, @as(u64, 0x0), x51);
18561855 var x57: u64 = undefined;
18571856 var x58: u1 = undefined;
1858 subborrowxU64(&x57, &x58, x56, cast(u64, 0x0), x50);
1857 subborrowxU64(&x57, &x58, x56, @as(u64, 0x0), x50);
18591858 var x59: u64 = undefined;
18601859 var x60: u1 = undefined;
1861 subborrowxU64(&x59, &x60, x58, cast(u64, 0x0), x49);
1860 subborrowxU64(&x59, &x60, x58, @as(u64, 0x0), x49);
18621861 var x61: u64 = undefined;
1863 cmovznzU64(&x61, x60, cast(u64, 0x0), 0xffffffffffffffff);
1862 cmovznzU64(&x61, x60, @as(u64, 0x0), 0xffffffffffffffff);
18641863 var x62: u64 = undefined;
18651864 var x63: u1 = undefined;
18661865 addcarryxU64(&x62, &x63, 0x0, x53, (x61 & 0xf3b9cac2fc632551));
......@@ -1881,17 +1880,17 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
18811880 cmovznzU64(&x72, x3, (arg5[2]), x66);
18821881 var x73: u64 = undefined;
18831882 cmovznzU64(&x73, x3, (arg5[3]), x68);
1884 const x74 = cast(u1, (x22 & cast(u64, 0x1)));
1883 const x74 = @as(u1, (x22 & @as(u64, 0x1)));
18851884 var x75: u64 = undefined;
1886 cmovznzU64(&x75, x74, cast(u64, 0x0), x7);
1885 cmovznzU64(&x75, x74, @as(u64, 0x0), x7);
18871886 var x76: u64 = undefined;
1888 cmovznzU64(&x76, x74, cast(u64, 0x0), x8);
1887 cmovznzU64(&x76, x74, @as(u64, 0x0), x8);
18891888 var x77: u64 = undefined;
1890 cmovznzU64(&x77, x74, cast(u64, 0x0), x9);
1889 cmovznzU64(&x77, x74, @as(u64, 0x0), x9);
18911890 var x78: u64 = undefined;
1892 cmovznzU64(&x78, x74, cast(u64, 0x0), x10);
1891 cmovznzU64(&x78, x74, @as(u64, 0x0), x10);
18931892 var x79: u64 = undefined;
1894 cmovznzU64(&x79, x74, cast(u64, 0x0), x11);
1893 cmovznzU64(&x79, x74, @as(u64, 0x0), x11);
18951894 var x80: u64 = undefined;
18961895 var x81: u1 = undefined;
18971896 addcarryxU64(&x80, &x81, 0x0, x22, x75);
......@@ -1908,13 +1907,13 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
19081907 var x89: u1 = undefined;
19091908 addcarryxU64(&x88, &x89, x87, x26, x79);
19101909 var x90: u64 = undefined;
1911 cmovznzU64(&x90, x74, cast(u64, 0x0), x27);
1910 cmovznzU64(&x90, x74, @as(u64, 0x0), x27);
19121911 var x91: u64 = undefined;
1913 cmovznzU64(&x91, x74, cast(u64, 0x0), x28);
1912 cmovznzU64(&x91, x74, @as(u64, 0x0), x28);
19141913 var x92: u64 = undefined;
1915 cmovznzU64(&x92, x74, cast(u64, 0x0), x29);
1914 cmovznzU64(&x92, x74, @as(u64, 0x0), x29);
19161915 var x93: u64 = undefined;
1917 cmovznzU64(&x93, x74, cast(u64, 0x0), x30);
1916 cmovznzU64(&x93, x74, @as(u64, 0x0), x30);
19181917 var x94: u64 = undefined;
19191918 var x95: u1 = undefined;
19201919 addcarryxU64(&x94, &x95, 0x0, x70, x90);
......@@ -1941,10 +1940,10 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[
19411940 subborrowxU64(&x108, &x109, x107, x100, 0xffffffff00000000);
19421941 var x110: u64 = undefined;
19431942 var x111: u1 = undefined;
1944 subborrowxU64(&x110, &x111, x109, cast(u64, x101), cast(u64, 0x0));
1943 subborrowxU64(&x110, &x111, x109, @as(u64, x101), @as(u64, 0x0));
19451944 var x112: u64 = undefined;
19461945 var x113: u1 = undefined;
1947 addcarryxU64(&x112, &x113, 0x0, x6, cast(u64, 0x1));
1946 addcarryxU64(&x112, &x113, 0x0, x6, @as(u64, 0x1));
19481947 const x114 = ((x80 >> 1) | ((x82 << 63) & 0xffffffffffffffff));
19491948 const x115 = ((x82 >> 1) | ((x84 << 63) & 0xffffffffffffffff));
19501949 const x116 = ((x84 >> 1) | ((x86 << 63) & 0xffffffffffffffff));
lib/std/meta.zig-376
......@@ -884,319 +884,6 @@ pub fn Vector(comptime len: u32, comptime child: type) type {
884884 });
885885}
886886
887/// Given a type and value, cast the value to the type as c would.
888/// This is for translate-c and is not intended for general use.
889pub fn cast(comptime DestType: type, target: anytype) DestType {
890 // this function should behave like transCCast in translate-c, except it's for macros and enums
891 const SourceType = @TypeOf(target);
892 switch (@typeInfo(DestType)) {
893 .Pointer => return castToPtr(DestType, SourceType, target),
894 .Optional => |dest_opt| {
895 if (@typeInfo(dest_opt.child) == .Pointer) {
896 return castToPtr(DestType, SourceType, target);
897 }
898 },
899 .Enum => |enum_type| {
900 if (@typeInfo(SourceType) == .Int or @typeInfo(SourceType) == .ComptimeInt) {
901 const intermediate = cast(enum_type.tag_type, target);
902 return @intToEnum(DestType, intermediate);
903 }
904 },
905 .Int => {
906 switch (@typeInfo(SourceType)) {
907 .Pointer => {
908 return castInt(DestType, @ptrToInt(target));
909 },
910 .Optional => |opt| {
911 if (@typeInfo(opt.child) == .Pointer) {
912 return castInt(DestType, @ptrToInt(target));
913 }
914 },
915 .Enum => {
916 return castInt(DestType, @enumToInt(target));
917 },
918 .Int => {
919 return castInt(DestType, target);
920 },
921 else => {},
922 }
923 },
924 else => {},
925 }
926 return @as(DestType, target);
927}
928
929fn castInt(comptime DestType: type, target: anytype) DestType {
930 const dest = @typeInfo(DestType).Int;
931 const source = @typeInfo(@TypeOf(target)).Int;
932
933 if (dest.bits < source.bits)
934 return @bitCast(DestType, @truncate(Int(source.signedness, dest.bits), target))
935 else
936 return @bitCast(DestType, @as(Int(source.signedness, dest.bits), target));
937}
938
939fn castPtr(comptime DestType: type, target: anytype) DestType {
940 const dest = ptrInfo(DestType);
941 const source = ptrInfo(@TypeOf(target));
942
943 if (source.is_const and !dest.is_const or source.is_volatile and !dest.is_volatile)
944 return @intToPtr(DestType, @ptrToInt(target))
945 else if (@typeInfo(dest.child) == .Opaque)
946 // dest.alignment would error out
947 return @ptrCast(DestType, target)
948 else
949 return @ptrCast(DestType, @alignCast(dest.alignment, target));
950}
951
952fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType {
953 switch (@typeInfo(SourceType)) {
954 .Int => {
955 return @intToPtr(DestType, castInt(usize, target));
956 },
957 .ComptimeInt => {
958 if (target < 0)
959 return @intToPtr(DestType, @bitCast(usize, @intCast(isize, target)))
960 else
961 return @intToPtr(DestType, @intCast(usize, target));
962 },
963 .Pointer => {
964 return castPtr(DestType, target);
965 },
966 .Optional => |target_opt| {
967 if (@typeInfo(target_opt.child) == .Pointer) {
968 return castPtr(DestType, target);
969 }
970 },
971 else => {},
972 }
973 return @as(DestType, target);
974}
975
976fn ptrInfo(comptime PtrType: type) TypeInfo.Pointer {
977 return switch (@typeInfo(PtrType)) {
978 .Optional => |opt_info| @typeInfo(opt_info.child).Pointer,
979 .Pointer => |ptr_info| ptr_info,
980 else => unreachable,
981 };
982}
983
984test "std.meta.cast" {
985 const E = enum(u2) {
986 Zero,
987 One,
988 Two,
989 };
990
991 var i = @as(i64, 10);
992
993 try testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16));
994 try testing.expect(cast(*u64, &i).* == @as(u64, 10));
995 try testing.expect(cast(*i64, @as(?*align(1) i64, &i)) == &i);
996
997 try testing.expect(cast(?*u8, 2) == @intToPtr(*u8, 2));
998 try testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i);
999 try testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i);
1000
1001 try testing.expect(cast(E, 1) == .One);
1002
1003 try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4)));
1004 try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4)));
1005 try testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10)));
1006 try testing.expectEqual(@as(u8, 2), cast(u8, E.Two));
1007
1008 try testing.expectEqual(@bitCast(i32, @as(u32, 0x8000_0000)), cast(i32, @as(u32, 0x8000_0000)));
1009
1010 try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*const u8, 2)));
1011 try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*volatile u8, 2)));
1012
1013 try testing.expectEqual(@intToPtr(?*c_void, 2), cast(?*c_void, @intToPtr(*u8, 2)));
1014
1015 const C_ENUM = enum(c_int) {
1016 A = 0,
1017 B,
1018 C,
1019 _,
1020 };
1021 try testing.expectEqual(cast(C_ENUM, @as(i64, -1)), @intToEnum(C_ENUM, -1));
1022 try testing.expectEqual(cast(C_ENUM, @as(i8, 1)), .B);
1023 try testing.expectEqual(cast(C_ENUM, @as(u64, 1)), .B);
1024 try testing.expectEqual(cast(C_ENUM, @as(u64, 42)), @intToEnum(C_ENUM, 42));
1025
1026 var foo: c_int = -1;
1027 try testing.expect(cast(*c_void, -1) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))));
1028 try testing.expect(cast(*c_void, foo) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))));
1029 try testing.expect(cast(?*c_void, -1) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))));
1030 try testing.expect(cast(?*c_void, foo) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))));
1031}
1032
1033/// Given a value returns its size as C's sizeof operator would.
1034/// This is for translate-c and is not intended for general use.
1035pub fn sizeof(target: anytype) usize {
1036 const T: type = if (@TypeOf(target) == type) target else @TypeOf(target);
1037 switch (@typeInfo(T)) {
1038 .Float, .Int, .Struct, .Union, .Enum, .Array, .Bool, .Vector => return @sizeOf(T),
1039 .Fn => {
1040 // sizeof(main) returns 1, sizeof(&main) returns pointer size.
1041 // We cannot distinguish those types in Zig, so use pointer size.
1042 return @sizeOf(T);
1043 },
1044 .Null => return @sizeOf(*c_void),
1045 .Void => {
1046 // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC.
1047 return 1;
1048 },
1049 .Opaque => {
1050 if (T == c_void) {
1051 // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC.
1052 return 1;
1053 } else {
1054 @compileError("Cannot use C sizeof on opaque type " ++ @typeName(T));
1055 }
1056 },
1057 .Optional => |opt| {
1058 if (@typeInfo(opt.child) == .Pointer) {
1059 return sizeof(opt.child);
1060 } else {
1061 @compileError("Cannot use C sizeof on non-pointer optional " ++ @typeName(T));
1062 }
1063 },
1064 .Pointer => |ptr| {
1065 if (ptr.size == .Slice) {
1066 @compileError("Cannot use C sizeof on slice type " ++ @typeName(T));
1067 }
1068 // for strings, sizeof("a") returns 2.
1069 // normal pointer decay scenarios from C are handled
1070 // in the .Array case above, but strings remain literals
1071 // and are therefore always pointers, so they need to be
1072 // specially handled here.
1073 if (ptr.size == .One and ptr.is_const and @typeInfo(ptr.child) == .Array) {
1074 const array_info = @typeInfo(ptr.child).Array;
1075 if ((array_info.child == u8 or array_info.child == u16) and
1076 array_info.sentinel != null and
1077 array_info.sentinel.? == 0)
1078 {
1079 // length of the string plus one for the null terminator.
1080 return (array_info.len + 1) * @sizeOf(array_info.child);
1081 }
1082 }
1083 // When zero sized pointers are removed, this case will no
1084 // longer be reachable and can be deleted.
1085 if (@sizeOf(T) == 0) {
1086 return @sizeOf(*c_void);
1087 }
1088 return @sizeOf(T);
1089 },
1090 .ComptimeFloat => return @sizeOf(f64), // TODO c_double #3999
1091 .ComptimeInt => {
1092 // TODO to get the correct result we have to translate
1093 // `1073741824 * 4` as `int(1073741824) *% int(4)` since
1094 // sizeof(1073741824 * 4) != sizeof(4294967296).
1095
1096 // TODO test if target fits in int, long or long long
1097 return @sizeOf(c_int);
1098 },
1099 else => @compileError("std.meta.sizeof does not support type " ++ @typeName(T)),
1100 }
1101}
1102
1103test "sizeof" {
1104 const E = enum(c_int) { One, _ };
1105 const S = extern struct { a: u32 };
1106
1107 const ptr_size = @sizeOf(*c_void);
1108
1109 try testing.expect(sizeof(u32) == 4);
1110 try testing.expect(sizeof(@as(u32, 2)) == 4);
1111 try testing.expect(sizeof(2) == @sizeOf(c_int));
1112
1113 try testing.expect(sizeof(2.0) == @sizeOf(f64));
1114
1115 try testing.expect(sizeof(E) == @sizeOf(c_int));
1116 try testing.expect(sizeof(E.One) == @sizeOf(c_int));
1117
1118 try testing.expect(sizeof(S) == 4);
1119
1120 try testing.expect(sizeof([_]u32{ 4, 5, 6 }) == 12);
1121 try testing.expect(sizeof([3]u32) == 12);
1122 try testing.expect(sizeof([3:0]u32) == 16);
1123 try testing.expect(sizeof(&[_]u32{ 4, 5, 6 }) == ptr_size);
1124
1125 try testing.expect(sizeof(*u32) == ptr_size);
1126 try testing.expect(sizeof([*]u32) == ptr_size);
1127 try testing.expect(sizeof([*c]u32) == ptr_size);
1128 try testing.expect(sizeof(?*u32) == ptr_size);
1129 try testing.expect(sizeof(?[*]u32) == ptr_size);
1130 try testing.expect(sizeof(*c_void) == ptr_size);
1131 try testing.expect(sizeof(*void) == ptr_size);
1132 try testing.expect(sizeof(null) == ptr_size);
1133
1134 try testing.expect(sizeof("foobar") == 7);
1135 try testing.expect(sizeof(&[_:0]u16{ 'f', 'o', 'o', 'b', 'a', 'r' }) == 14);
1136 try testing.expect(sizeof(*const [4:0]u8) == 5);
1137 try testing.expect(sizeof(*[4:0]u8) == ptr_size);
1138 try testing.expect(sizeof([*]const [4:0]u8) == ptr_size);
1139 try testing.expect(sizeof(*const *const [4:0]u8) == ptr_size);
1140 try testing.expect(sizeof(*const [4]u8) == ptr_size);
1141
1142 try testing.expect(sizeof(sizeof) == @sizeOf(@TypeOf(sizeof)));
1143
1144 try testing.expect(sizeof(void) == 1);
1145 try testing.expect(sizeof(c_void) == 1);
1146}
1147
1148pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };
1149
1150fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type {
1151 const signed_decimal = [_]type{ c_int, c_long, c_longlong };
1152 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
1153 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
1154
1155 const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned)
1156 &unsigned
1157 else if (radix == .decimal)
1158 &signed_decimal
1159 else
1160 &signed_oct_hex;
1161
1162 var pos = mem.indexOfScalar(type, list, SuffixType).?;
1163
1164 while (pos < list.len) : (pos += 1) {
1165 if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) {
1166 return list[pos];
1167 }
1168 }
1169 @compileError("Integer literal is too large");
1170}
1171
1172/// Promote the type of an integer literal until it fits as C would.
1173/// This is for translate-c and is not intended for general use.
1174pub fn promoteIntLiteral(
1175 comptime SuffixType: type,
1176 comptime number: comptime_int,
1177 comptime radix: CIntLiteralRadix,
1178) PromoteIntLiteralReturnType(SuffixType, number, radix) {
1179 return number;
1180}
1181
1182test "promoteIntLiteral" {
1183 const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal);
1184 try testing.expectEqual(c_uint, @TypeOf(signed_hex));
1185
1186 if (math.maxInt(c_longlong) == math.maxInt(c_int)) return;
1187
1188 const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal);
1189 const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal);
1190
1191 if (math.maxInt(c_long) > math.maxInt(c_int)) {
1192 try testing.expectEqual(c_long, @TypeOf(signed_decimal));
1193 try testing.expectEqual(c_ulong, @TypeOf(unsigned));
1194 } else {
1195 try testing.expectEqual(c_longlong, @TypeOf(signed_decimal));
1196 try testing.expectEqual(c_ulonglong, @TypeOf(unsigned));
1197 }
1198}
1199
1200887/// For a given function type, returns a tuple type which fields will
1201888/// correspond to the argument types.
1202889///
......@@ -1316,38 +1003,6 @@ pub fn globalOption(comptime name: []const u8, comptime T: type) ?T {
13161003 return @as(T, @field(root, name));
13171004}
13181005
1319/// This function is for translate-c and is not intended for general use.
1320/// Convert from clang __builtin_shufflevector index to Zig @shuffle index
1321/// clang requires __builtin_shufflevector index arguments to be integer constants.
1322/// negative values for `this_index` indicate "don't care" so we arbitrarily choose 0
1323/// clang enforces that `this_index` is less than the total number of vector elements
1324/// See https://ziglang.org/documentation/master/#shuffle
1325/// See https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-shufflevector
1326pub fn shuffleVectorIndex(comptime this_index: c_int, comptime source_vector_len: usize) i32 {
1327 if (this_index <= 0) return 0;
1328
1329 const positive_index = @intCast(usize, this_index);
1330 if (positive_index < source_vector_len) return @intCast(i32, this_index);
1331 const b_index = positive_index - source_vector_len;
1332 return ~@intCast(i32, b_index);
1333}
1334
1335test "shuffleVectorIndex" {
1336 const vector_len: usize = 4;
1337
1338 try testing.expect(shuffleVectorIndex(-1, vector_len) == 0);
1339
1340 try testing.expect(shuffleVectorIndex(0, vector_len) == 0);
1341 try testing.expect(shuffleVectorIndex(1, vector_len) == 1);
1342 try testing.expect(shuffleVectorIndex(2, vector_len) == 2);
1343 try testing.expect(shuffleVectorIndex(3, vector_len) == 3);
1344
1345 try testing.expect(shuffleVectorIndex(4, vector_len) == -1);
1346 try testing.expect(shuffleVectorIndex(5, vector_len) == -2);
1347 try testing.expect(shuffleVectorIndex(6, vector_len) == -3);
1348 try testing.expect(shuffleVectorIndex(7, vector_len) == -4);
1349}
1350
13511006/// Returns whether `error_union` contains an error.
13521007pub fn isError(error_union: anytype) bool {
13531008 return if (error_union) |_| false else |_| true;
......@@ -1357,34 +1012,3 @@ test "isError" {
13571012 try std.testing.expect(isError(math.absInt(@as(i8, -128))));
13581013 try std.testing.expect(!isError(math.absInt(@as(i8, -127))));
13591014}
1360
1361/// This function is for translate-c and is not intended for general use.
1362/// Constructs a [*c] pointer with the const and volatile annotations
1363/// from SelfType for pointing to a C flexible array of ElementType.
1364pub fn FlexibleArrayType(comptime SelfType: type, ElementType: type) type {
1365 switch (@typeInfo(SelfType)) {
1366 .Pointer => |ptr| {
1367 return @Type(TypeInfo{ .Pointer = .{
1368 .size = .C,
1369 .is_const = ptr.is_const,
1370 .is_volatile = ptr.is_volatile,
1371 .alignment = @alignOf(ElementType),
1372 .child = ElementType,
1373 .is_allowzero = true,
1374 .sentinel = null,
1375 } });
1376 },
1377 else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)),
1378 }
1379}
1380
1381test "Flexible Array Type" {
1382 const Container = extern struct {
1383 size: usize,
1384 };
1385
1386 try testing.expectEqual(FlexibleArrayType(*Container, c_int), [*c]c_int);
1387 try testing.expectEqual(FlexibleArrayType(*const Container, c_int), [*c]const c_int);
1388 try testing.expectEqual(FlexibleArrayType(*volatile Container, c_int), [*c]volatile c_int);
1389 try testing.expectEqual(FlexibleArrayType(*const volatile Container, c_int), [*c]const volatile c_int);
1390}
lib/std/zig.zig+4
......@@ -16,6 +16,10 @@ pub const ast = @import("zig/ast.zig");
1616pub const system = @import("zig/system.zig");
1717pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget;
1818
19// Files needed by translate-c.
20pub const c_builtins = @import("zig/c_builtins.zig");
21pub const c_translation = @import("zig/c_translation.zig");
22
1923pub const SrcHash = [16]u8;
2024
2125pub fn hashSrc(src: []const u8) SrcHash {
lib/std/zig/c_builtins.zig created+196
......@@ -0,0 +1,196 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6
7const std = @import("std");
8
9pub inline fn __builtin_bswap16(val: u16) u16 {
10 return @byteSwap(u16, val);
11}
12pub inline fn __builtin_bswap32(val: u32) u32 {
13 return @byteSwap(u32, val);
14}
15pub inline fn __builtin_bswap64(val: u64) u64 {
16 return @byteSwap(u64, val);
17}
18
19pub inline fn __builtin_signbit(val: f64) c_int {
20 return @boolToInt(std.math.signbit(val));
21}
22pub inline fn __builtin_signbitf(val: f32) c_int {
23 return @boolToInt(std.math.signbit(val));
24}
25
26pub inline fn __builtin_popcount(val: c_uint) c_int {
27 // popcount of a c_uint will never exceed the capacity of a c_int
28 @setRuntimeSafety(false);
29 return @bitCast(c_int, @as(c_uint, @popCount(c_uint, val)));
30}
31pub inline fn __builtin_ctz(val: c_uint) c_int {
32 // Returns the number of trailing 0-bits in val, starting at the least significant bit position.
33 // In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
34 @setRuntimeSafety(false);
35 return @bitCast(c_int, @as(c_uint, @ctz(c_uint, val)));
36}
37pub inline fn __builtin_clz(val: c_uint) c_int {
38 // Returns the number of leading 0-bits in x, starting at the most significant bit position.
39 // In C if `val` is 0, the result is undefined; in zig it's the number of bits in a c_uint
40 @setRuntimeSafety(false);
41 return @bitCast(c_int, @as(c_uint, @clz(c_uint, val)));
42}
43
44pub inline fn __builtin_sqrt(val: f64) f64 {
45 return @sqrt(val);
46}
47pub inline fn __builtin_sqrtf(val: f32) f32 {
48 return @sqrt(val);
49}
50
51pub inline fn __builtin_sin(val: f64) f64 {
52 return @sin(val);
53}
54pub inline fn __builtin_sinf(val: f32) f32 {
55 return @sin(val);
56}
57pub inline fn __builtin_cos(val: f64) f64 {
58 return @cos(val);
59}
60pub inline fn __builtin_cosf(val: f32) f32 {
61 return @cos(val);
62}
63
64pub inline fn __builtin_exp(val: f64) f64 {
65 return @exp(val);
66}
67pub inline fn __builtin_expf(val: f32) f32 {
68 return @exp(val);
69}
70pub inline fn __builtin_exp2(val: f64) f64 {
71 return @exp2(val);
72}
73pub inline fn __builtin_exp2f(val: f32) f32 {
74 return @exp2(val);
75}
76pub inline fn __builtin_log(val: f64) f64 {
77 return @log(val);
78}
79pub inline fn __builtin_logf(val: f32) f32 {
80 return @log(val);
81}
82pub inline fn __builtin_log2(val: f64) f64 {
83 return @log2(val);
84}
85pub inline fn __builtin_log2f(val: f32) f32 {
86 return @log2(val);
87}
88pub inline fn __builtin_log10(val: f64) f64 {
89 return @log10(val);
90}
91pub inline fn __builtin_log10f(val: f32) f32 {
92 return @log10(val);
93}
94
95// Standard C Library bug: The absolute value of the most negative integer remains negative.
96pub inline fn __builtin_abs(val: c_int) c_int {
97 return std.math.absInt(val) catch std.math.minInt(c_int);
98}
99pub inline fn __builtin_fabs(val: f64) f64 {
100 return @fabs(val);
101}
102pub inline fn __builtin_fabsf(val: f32) f32 {
103 return @fabs(val);
104}
105
106pub inline fn __builtin_floor(val: f64) f64 {
107 return @floor(val);
108}
109pub inline fn __builtin_floorf(val: f32) f32 {
110 return @floor(val);
111}
112pub inline fn __builtin_ceil(val: f64) f64 {
113 return @ceil(val);
114}
115pub inline fn __builtin_ceilf(val: f32) f32 {
116 return @ceil(val);
117}
118pub inline fn __builtin_trunc(val: f64) f64 {
119 return @trunc(val);
120}
121pub inline fn __builtin_truncf(val: f32) f32 {
122 return @trunc(val);
123}
124pub inline fn __builtin_round(val: f64) f64 {
125 return @round(val);
126}
127pub inline fn __builtin_roundf(val: f32) f32 {
128 return @round(val);
129}
130
131pub inline fn __builtin_strlen(s: [*c]const u8) usize {
132 return std.mem.lenZ(s);
133}
134pub inline fn __builtin_strcmp(s1: [*c]const u8, s2: [*c]const u8) c_int {
135 return @as(c_int, std.cstr.cmp(s1, s2));
136}
137
138pub inline fn __builtin_object_size(ptr: ?*const c_void, ty: c_int) usize {
139 // clang semantics match gcc's: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
140 // If it is not possible to determine which objects ptr points to at compile time,
141 // __builtin_object_size should return (size_t) -1 for type 0 or 1 and (size_t) 0
142 // for type 2 or 3.
143 if (ty == 0 or ty == 1) return @bitCast(usize, -@as(isize, 1));
144 if (ty == 2 or ty == 3) return 0;
145 unreachable;
146}
147
148pub inline fn __builtin___memset_chk(
149 dst: ?*c_void,
150 val: c_int,
151 len: usize,
152 remaining: usize,
153) ?*c_void {
154 if (len > remaining) @panic("std.c.builtins.memset_chk called with len > remaining");
155 return __builtin_memset(dst, val, len);
156}
157
158pub inline fn __builtin_memset(dst: ?*c_void, val: c_int, len: usize) ?*c_void {
159 const dst_cast = @ptrCast([*c]u8, dst);
160 @memset(dst_cast, @bitCast(u8, @truncate(i8, val)), len);
161 return dst;
162}
163
164pub inline fn __builtin___memcpy_chk(
165 noalias dst: ?*c_void,
166 noalias src: ?*const c_void,
167 len: usize,
168 remaining: usize,
169) ?*c_void {
170 if (len > remaining) @panic("std.c.builtins.memcpy_chk called with len > remaining");
171 return __builtin_memcpy(dst, src, len);
172}
173
174pub inline fn __builtin_memcpy(
175 noalias dst: ?*c_void,
176 noalias src: ?*const c_void,
177 len: usize,
178) ?*c_void {
179 const dst_cast = @ptrCast([*c]u8, dst);
180 const src_cast = @ptrCast([*c]const u8, src);
181
182 @memcpy(dst_cast, src_cast, len);
183 return dst;
184}
185
186/// The return value of __builtin_expect is `expr`. `c` is the expected value
187/// of `expr` and is used as a hint to the compiler in C. Here it is unused.
188pub inline fn __builtin_expect(expr: c_long, c: c_long) c_long {
189 return expr;
190}
191
192// __builtin_alloca_with_align is not currently implemented.
193// It is used in a run-translated-c test and a test-translate-c test to ensure that non-implemented
194// builtins are correctly demoted. If you implement __builtin_alloca_with_align, please update the
195// run-translated-c test and the test-translate-c test to use a different non-implemented builtin.
196// pub fn __builtin_alloca_with_align(size: usize, alignment: usize) callconv(.Inline) *c_void {}
lib/std/zig/c_translation.zig created+385
......@@ -0,0 +1,385 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6
7const std = @import("std");
8const testing = std.testing;
9const math = std.math;
10const mem = std.mem;
11
12/// Given a type and value, cast the value to the type as c would.
13pub fn cast(comptime DestType: type, target: anytype) DestType {
14 // this function should behave like transCCast in translate-c, except it's for macros and enums
15 const SourceType = @TypeOf(target);
16 switch (@typeInfo(DestType)) {
17 .Fn, .Pointer => return castToPtr(DestType, SourceType, target),
18 .Optional => |dest_opt| {
19 if (@typeInfo(dest_opt.child) == .Pointer or @typeInfo(dest_opt.child) == .Fn) {
20 return castToPtr(DestType, SourceType, target);
21 }
22 },
23 .Enum => |enum_type| {
24 if (@typeInfo(SourceType) == .Int or @typeInfo(SourceType) == .ComptimeInt) {
25 const intermediate = cast(enum_type.tag_type, target);
26 return @intToEnum(DestType, intermediate);
27 }
28 },
29 .Int => {
30 switch (@typeInfo(SourceType)) {
31 .Pointer => {
32 return castInt(DestType, @ptrToInt(target));
33 },
34 .Optional => |opt| {
35 if (@typeInfo(opt.child) == .Pointer) {
36 return castInt(DestType, @ptrToInt(target));
37 }
38 },
39 .Enum => {
40 return castInt(DestType, @enumToInt(target));
41 },
42 .Int => {
43 return castInt(DestType, target);
44 },
45 else => {},
46 }
47 },
48 else => {},
49 }
50 return @as(DestType, target);
51}
52
53fn castInt(comptime DestType: type, target: anytype) DestType {
54 const dest = @typeInfo(DestType).Int;
55 const source = @typeInfo(@TypeOf(target)).Int;
56
57 if (dest.bits < source.bits)
58 return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target))
59 else
60 return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target));
61}
62
63fn castPtr(comptime DestType: type, target: anytype) DestType {
64 const dest = ptrInfo(DestType);
65 const source = ptrInfo(@TypeOf(target));
66
67 if (source.is_const and !dest.is_const or source.is_volatile and !dest.is_volatile)
68 return @intToPtr(DestType, @ptrToInt(target))
69 else if (@typeInfo(dest.child) == .Opaque)
70 // dest.alignment would error out
71 return @ptrCast(DestType, target)
72 else
73 return @ptrCast(DestType, @alignCast(dest.alignment, target));
74}
75
76fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType {
77 switch (@typeInfo(SourceType)) {
78 .Int => {
79 return @intToPtr(DestType, castInt(usize, target));
80 },
81 .ComptimeInt => {
82 if (target < 0)
83 return @intToPtr(DestType, @bitCast(usize, @intCast(isize, target)))
84 else
85 return @intToPtr(DestType, @intCast(usize, target));
86 },
87 .Pointer => {
88 return castPtr(DestType, target);
89 },
90 .Optional => |target_opt| {
91 if (@typeInfo(target_opt.child) == .Pointer) {
92 return castPtr(DestType, target);
93 }
94 },
95 else => {},
96 }
97 return @as(DestType, target);
98}
99
100fn ptrInfo(comptime PtrType: type) std.builtin.TypeInfo.Pointer {
101 return switch (@typeInfo(PtrType)) {
102 .Optional => |opt_info| @typeInfo(opt_info.child).Pointer,
103 .Pointer => |ptr_info| ptr_info,
104 else => unreachable,
105 };
106}
107
108test "cast" {
109 const E = enum(u2) {
110 Zero,
111 One,
112 Two,
113 };
114
115 var i = @as(i64, 10);
116
117 try testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16));
118 try testing.expect(cast(*u64, &i).* == @as(u64, 10));
119 try testing.expect(cast(*i64, @as(?*align(1) i64, &i)) == &i);
120
121 try testing.expect(cast(?*u8, 2) == @intToPtr(*u8, 2));
122 try testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i);
123 try testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i);
124
125 try testing.expect(cast(E, 1) == .One);
126
127 try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4)));
128 try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4)));
129 try testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10)));
130 try testing.expectEqual(@as(u8, 2), cast(u8, E.Two));
131
132 try testing.expectEqual(@bitCast(i32, @as(u32, 0x8000_0000)), cast(i32, @as(u32, 0x8000_0000)));
133
134 try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*const u8, 2)));
135 try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*volatile u8, 2)));
136
137 try testing.expectEqual(@intToPtr(?*c_void, 2), cast(?*c_void, @intToPtr(*u8, 2)));
138
139 const C_ENUM = enum(c_int) {
140 A = 0,
141 B,
142 C,
143 _,
144 };
145 try testing.expectEqual(cast(C_ENUM, @as(i64, -1)), @intToEnum(C_ENUM, -1));
146 try testing.expectEqual(cast(C_ENUM, @as(i8, 1)), .B);
147 try testing.expectEqual(cast(C_ENUM, @as(u64, 1)), .B);
148 try testing.expectEqual(cast(C_ENUM, @as(u64, 42)), @intToEnum(C_ENUM, 42));
149
150 var foo: c_int = -1;
151 try testing.expect(cast(*c_void, -1) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))));
152 try testing.expect(cast(*c_void, foo) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))));
153 try testing.expect(cast(?*c_void, -1) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))));
154 try testing.expect(cast(?*c_void, foo) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1))));
155
156 const FnPtr = ?fn (*c_void) void;
157 try testing.expect(cast(FnPtr, 0) == @intToPtr(FnPtr, @as(usize, 0)));
158 try testing.expect(cast(FnPtr, foo) == @intToPtr(FnPtr, @bitCast(usize, @as(isize, -1))));
159}
160
161/// Given a value returns its size as C's sizeof operator would.
162pub fn sizeof(target: anytype) usize {
163 const T: type = if (@TypeOf(target) == type) target else @TypeOf(target);
164 switch (@typeInfo(T)) {
165 .Float, .Int, .Struct, .Union, .Enum, .Array, .Bool, .Vector => return @sizeOf(T),
166 .Fn => {
167 // sizeof(main) returns 1, sizeof(&main) returns pointer size.
168 // We cannot distinguish those types in Zig, so use pointer size.
169 return @sizeOf(T);
170 },
171 .Null => return @sizeOf(*c_void),
172 .Void => {
173 // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC.
174 return 1;
175 },
176 .Opaque => {
177 if (T == c_void) {
178 // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC.
179 return 1;
180 } else {
181 @compileError("Cannot use C sizeof on opaque type " ++ @typeName(T));
182 }
183 },
184 .Optional => |opt| {
185 if (@typeInfo(opt.child) == .Pointer) {
186 return sizeof(opt.child);
187 } else {
188 @compileError("Cannot use C sizeof on non-pointer optional " ++ @typeName(T));
189 }
190 },
191 .Pointer => |ptr| {
192 if (ptr.size == .Slice) {
193 @compileError("Cannot use C sizeof on slice type " ++ @typeName(T));
194 }
195 // for strings, sizeof("a") returns 2.
196 // normal pointer decay scenarios from C are handled
197 // in the .Array case above, but strings remain literals
198 // and are therefore always pointers, so they need to be
199 // specially handled here.
200 if (ptr.size == .One and ptr.is_const and @typeInfo(ptr.child) == .Array) {
201 const array_info = @typeInfo(ptr.child).Array;
202 if ((array_info.child == u8 or array_info.child == u16) and
203 array_info.sentinel != null and
204 array_info.sentinel.? == 0)
205 {
206 // length of the string plus one for the null terminator.
207 return (array_info.len + 1) * @sizeOf(array_info.child);
208 }
209 }
210 // When zero sized pointers are removed, this case will no
211 // longer be reachable and can be deleted.
212 if (@sizeOf(T) == 0) {
213 return @sizeOf(*c_void);
214 }
215 return @sizeOf(T);
216 },
217 .ComptimeFloat => return @sizeOf(f64), // TODO c_double #3999
218 .ComptimeInt => {
219 // TODO to get the correct result we have to translate
220 // `1073741824 * 4` as `int(1073741824) *% int(4)` since
221 // sizeof(1073741824 * 4) != sizeof(4294967296).
222
223 // TODO test if target fits in int, long or long long
224 return @sizeOf(c_int);
225 },
226 else => @compileError("std.meta.sizeof does not support type " ++ @typeName(T)),
227 }
228}
229
230test "sizeof" {
231 const E = enum(c_int) { One, _ };
232 const S = extern struct { a: u32 };
233
234 const ptr_size = @sizeOf(*c_void);
235
236 try testing.expect(sizeof(u32) == 4);
237 try testing.expect(sizeof(@as(u32, 2)) == 4);
238 try testing.expect(sizeof(2) == @sizeOf(c_int));
239
240 try testing.expect(sizeof(2.0) == @sizeOf(f64));
241
242 try testing.expect(sizeof(E) == @sizeOf(c_int));
243 try testing.expect(sizeof(E.One) == @sizeOf(c_int));
244
245 try testing.expect(sizeof(S) == 4);
246
247 try testing.expect(sizeof([_]u32{ 4, 5, 6 }) == 12);
248 try testing.expect(sizeof([3]u32) == 12);
249 try testing.expect(sizeof([3:0]u32) == 16);
250 try testing.expect(sizeof(&[_]u32{ 4, 5, 6 }) == ptr_size);
251
252 try testing.expect(sizeof(*u32) == ptr_size);
253 try testing.expect(sizeof([*]u32) == ptr_size);
254 try testing.expect(sizeof([*c]u32) == ptr_size);
255 try testing.expect(sizeof(?*u32) == ptr_size);
256 try testing.expect(sizeof(?[*]u32) == ptr_size);
257 try testing.expect(sizeof(*c_void) == ptr_size);
258 try testing.expect(sizeof(*void) == ptr_size);
259 try testing.expect(sizeof(null) == ptr_size);
260
261 try testing.expect(sizeof("foobar") == 7);
262 try testing.expect(sizeof(&[_:0]u16{ 'f', 'o', 'o', 'b', 'a', 'r' }) == 14);
263 try testing.expect(sizeof(*const [4:0]u8) == 5);
264 try testing.expect(sizeof(*[4:0]u8) == ptr_size);
265 try testing.expect(sizeof([*]const [4:0]u8) == ptr_size);
266 try testing.expect(sizeof(*const *const [4:0]u8) == ptr_size);
267 try testing.expect(sizeof(*const [4]u8) == ptr_size);
268
269 try testing.expect(sizeof(sizeof) == @sizeOf(@TypeOf(sizeof)));
270
271 try testing.expect(sizeof(void) == 1);
272 try testing.expect(sizeof(c_void) == 1);
273}
274
275pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };
276
277fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type {
278 const signed_decimal = [_]type{ c_int, c_long, c_longlong };
279 const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
280 const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
281
282 const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned)
283 &unsigned
284 else if (radix == .decimal)
285 &signed_decimal
286 else
287 &signed_oct_hex;
288
289 var pos = mem.indexOfScalar(type, list, SuffixType).?;
290
291 while (pos < list.len) : (pos += 1) {
292 if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) {
293 return list[pos];
294 }
295 }
296 @compileError("Integer literal is too large");
297}
298
299/// Promote the type of an integer literal until it fits as C would.
300pub fn promoteIntLiteral(
301 comptime SuffixType: type,
302 comptime number: comptime_int,
303 comptime radix: CIntLiteralRadix,
304) PromoteIntLiteralReturnType(SuffixType, number, radix) {
305 return number;
306}
307
308test "promoteIntLiteral" {
309 const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal);
310 try testing.expectEqual(c_uint, @TypeOf(signed_hex));
311
312 if (math.maxInt(c_longlong) == math.maxInt(c_int)) return;
313
314 const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal);
315 const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal);
316
317 if (math.maxInt(c_long) > math.maxInt(c_int)) {
318 try testing.expectEqual(c_long, @TypeOf(signed_decimal));
319 try testing.expectEqual(c_ulong, @TypeOf(unsigned));
320 } else {
321 try testing.expectEqual(c_longlong, @TypeOf(signed_decimal));
322 try testing.expectEqual(c_ulonglong, @TypeOf(unsigned));
323 }
324}
325
326/// Convert from clang __builtin_shufflevector index to Zig @shuffle index
327/// clang requires __builtin_shufflevector index arguments to be integer constants.
328/// negative values for `this_index` indicate "don't care" so we arbitrarily choose 0
329/// clang enforces that `this_index` is less than the total number of vector elements
330/// See https://ziglang.org/documentation/master/#shuffle
331/// See https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-shufflevector
332pub fn shuffleVectorIndex(comptime this_index: c_int, comptime source_vector_len: usize) i32 {
333 if (this_index <= 0) return 0;
334
335 const positive_index = @intCast(usize, this_index);
336 if (positive_index < source_vector_len) return @intCast(i32, this_index);
337 const b_index = positive_index - source_vector_len;
338 return ~@intCast(i32, b_index);
339}
340
341test "shuffleVectorIndex" {
342 const vector_len: usize = 4;
343
344 try testing.expect(shuffleVectorIndex(-1, vector_len) == 0);
345
346 try testing.expect(shuffleVectorIndex(0, vector_len) == 0);
347 try testing.expect(shuffleVectorIndex(1, vector_len) == 1);
348 try testing.expect(shuffleVectorIndex(2, vector_len) == 2);
349 try testing.expect(shuffleVectorIndex(3, vector_len) == 3);
350
351 try testing.expect(shuffleVectorIndex(4, vector_len) == -1);
352 try testing.expect(shuffleVectorIndex(5, vector_len) == -2);
353 try testing.expect(shuffleVectorIndex(6, vector_len) == -3);
354 try testing.expect(shuffleVectorIndex(7, vector_len) == -4);
355}
356
357/// Constructs a [*c] pointer with the const and volatile annotations
358/// from SelfType for pointing to a C flexible array of ElementType.
359pub fn FlexibleArrayType(comptime SelfType: type, ElementType: type) type {
360 switch (@typeInfo(SelfType)) {
361 .Pointer => |ptr| {
362 return @Type(.{ .Pointer = .{
363 .size = .C,
364 .is_const = ptr.is_const,
365 .is_volatile = ptr.is_volatile,
366 .alignment = @alignOf(ElementType),
367 .child = ElementType,
368 .is_allowzero = true,
369 .sentinel = null,
370 } });
371 },
372 else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)),
373 }
374}
375
376test "Flexible Array Type" {
377 const Container = extern struct {
378 size: usize,
379 };
380
381 try testing.expectEqual(FlexibleArrayType(*Container, c_int), [*c]c_int);
382 try testing.expectEqual(FlexibleArrayType(*const Container, c_int), [*c]const c_int);
383 try testing.expectEqual(FlexibleArrayType(*volatile Container, c_int), [*c]volatile c_int);
384 try testing.expectEqual(FlexibleArrayType(*const volatile Container, c_int), [*c]const volatile c_int);
385}
src/translate_c.zig+35-23
......@@ -12,7 +12,6 @@ const meta = std.meta;
1212const ast = @import("translate_c/ast.zig");
1313const Node = ast.Node;
1414const Tag = Node.Tag;
15const c_builtins = std.c.builtins;
1615
1716const CallingConvention = std.builtin.CallingConvention;
1817
......@@ -863,7 +862,7 @@ fn buildFlexibleArrayFn(
863862 defer block_scope.deinit();
864863
865864 const intermediate_type_name = try block_scope.makeMangledName(c, "Intermediate");
866 const intermediate_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type });
865 const intermediate_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type });
867866 const intermediate_type_decl = try Tag.var_simple.create(c.arena, .{
868867 .name = intermediate_type_name,
869868 .init = intermediate_type,
......@@ -872,7 +871,7 @@ fn buildFlexibleArrayFn(
872871 const intermediate_type_ident = try Tag.identifier.create(c.arena, intermediate_type_name);
873872
874873 const return_type_name = try block_scope.makeMangledName(c, "ReturnType");
875 const return_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type });
874 const return_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type });
876875 const return_type_decl = try Tag.var_simple.create(c.arena, .{
877876 .name = return_type_name,
878877 .init = return_type,
......@@ -1290,9 +1289,19 @@ fn transStmt(
12901289 return maybeSuppressResult(c, scope, result_used, shuffle_vec_node);
12911290 },
12921291 // When adding new cases here, see comment for maybeBlockify()
1293 else => {
1294 return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)});
1295 },
1292 .GCCAsmStmtClass,
1293 .GotoStmtClass,
1294 .IndirectGotoStmtClass,
1295 .AttributedStmtClass,
1296 .AddrLabelExprClass,
1297 .AtomicExprClass,
1298 .BlockExprClass,
1299 .UserDefinedLiteralClass,
1300 .BuiltinBitCastExprClass,
1301 .DesignatedInitExprClass,
1302 .LabelStmtClass,
1303 => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}),
1304 else => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "unsupported stmt class {s}", .{@tagName(sc)}),
12961305 }
12971306}
12981307
......@@ -1374,7 +1383,7 @@ fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorE
13741383
13751384 for (init_list) |*init, i| {
13761385 const index_expr = try transExprCoercing(c, scope, expr.getExpr(@intCast(c_uint, i + 2)), .used);
1377 const converted_index = try Tag.std_meta_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len });
1386 const converted_index = try Tag.helpers_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len });
13781387 init.* = converted_index;
13791388 }
13801389
......@@ -1820,13 +1829,10 @@ fn transDeclStmtOne(
18201829 .Function => {
18211830 try visitFnDecl(c, @ptrCast(*const clang.FunctionDecl, decl));
18221831 },
1823 else => |kind| return fail(
1824 c,
1825 error.UnsupportedTranslation,
1826 decl.getLocation(),
1827 "TODO implement translation of DeclStmt kind {s}",
1828 .{@tagName(kind)},
1829 ),
1832 else => {
1833 const decl_name = try c.str(decl.getDeclKindName());
1834 try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name});
1835 },
18301836 }
18311837}
18321838
......@@ -1902,7 +1908,7 @@ fn transImplicitCastExpr(
19021908 const ne = try Tag.not_equal.create(c.arena, .{ .lhs = ptr_to_int, .rhs = Tag.zero_literal.init() });
19031909 return maybeSuppressResult(c, scope, result_used, ne);
19041910 },
1905 .IntegralToBoolean => {
1911 .IntegralToBoolean, .FloatingToBoolean => {
19061912 const sub_expr_node = try transExpr(c, scope, sub_expr, .used);
19071913
19081914 // The expression is already a boolean one, return it as-is
......@@ -1924,14 +1930,14 @@ fn transImplicitCastExpr(
19241930 c,
19251931 error.UnsupportedTranslation,
19261932 @ptrCast(*const clang.Stmt, expr).getBeginLoc(),
1927 "TODO implement translation of CastKind {s}",
1933 "unsupported CastKind {s}",
19281934 .{@tagName(kind)},
19291935 ),
19301936 }
19311937}
19321938
19331939fn isBuiltinDefined(name: []const u8) bool {
1934 inline for (meta.declarations(c_builtins)) |decl| {
1940 inline for (meta.declarations(std.zig.c_builtins)) |decl| {
19351941 if (std.mem.eql(u8, name, decl.name)) return true;
19361942 }
19371943 return false;
......@@ -2358,8 +2364,10 @@ fn transCCast(
23582364 return Tag.float_to_int.create(c.arena, .{ .lhs = dst_node, .rhs = expr });
23592365 }
23602366 if (!cIsFloating(src_type) and cIsFloating(dst_type)) {
2367 var rhs = expr;
2368 if (qualTypeIsBoolean(src_type)) rhs = try Tag.bool_to_int.create(c.arena, expr);
23612369 // @intToFloat(dest_type, val)
2362 return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = expr });
2370 return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = rhs });
23632371 }
23642372 if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) {
23652373 // @boolToInt returns either a comptime_int or a u1
......@@ -2370,7 +2378,7 @@ fn transCCast(
23702378 }
23712379 if (cIsEnum(dst_type)) {
23722380 // import("std").meta.cast(dest_type, val)
2373 return Tag.std_meta_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr });
2381 return Tag.helpers_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr });
23742382 }
23752383 if (cIsEnum(src_type) and !cIsEnum(dst_type)) {
23762384 // @enumToInt(val)
......@@ -4547,6 +4555,10 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan
45474555 .rhs = try transQualType(c, scope, element_qt, source_loc),
45484556 });
45494557 },
4558 .ExtInt, .ExtVector => {
4559 const type_name = c.str(ty.getTypeClassName());
4560 return fail(c, error.UnsupportedType, source_loc, "TODO implement translation of type: '{s}'", .{type_name});
4561 },
45504562 else => {
45514563 const type_name = c.str(ty.getTypeClassName());
45524564 return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name});
......@@ -4982,7 +4994,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
49824994 break :blk br.data.val;
49834995 } else expr;
49844996
4985 const return_type = if (typeof_arg.castTag(.std_meta_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some|
4997 const return_type = if (typeof_arg.castTag(.helpers_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some|
49864998 some.data.lhs
49874999 else if (typeof_arg.castTag(.std_mem_zeroes)) |some|
49885000 some.data
......@@ -5095,7 +5107,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
50955107 if (guaranteed_to_fit) {
50965108 return Tag.as.create(c.arena, .{ .lhs = type_node, .rhs = literal_node });
50975109 } else {
5098 return Tag.std_meta_promoteIntLiteral.create(c.arena, .{
5110 return Tag.helpers_promoteIntLiteral.create(c.arena, .{
50995111 .type = type_node,
51005112 .value = literal_node,
51015113 .radix = try Tag.enum_literal.create(c.arena, radix),
......@@ -5578,7 +5590,7 @@ fn parseCCastExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
55785590 return parseCPostfixExpr(c, m, scope, type_name);
55795591 }
55805592 const node_to_cast = try parseCCastExpr(c, m, scope);
5581 return Tag.std_meta_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast });
5593 return Tag.helpers_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast });
55825594 }
55835595 },
55845596 else => {},
......@@ -5925,7 +5937,7 @@ fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node {
59255937 break :blk inner;
59265938 } else try parseCUnaryExpr(c, m, scope);
59275939
5928 return Tag.std_meta_sizeof.create(c.arena, operand);
5940 return Tag.helpers_sizeof.create(c.arena, operand);
59295941 },
59305942 .Keyword_alignof => {
59315943 // TODO this won't work if using <stdalign.h>'s
src/translate_c/ast.zig+44-43
......@@ -74,7 +74,7 @@ pub const Node = extern union {
7474 tuple,
7575 container_init,
7676 container_init_dot,
77 std_meta_cast,
77 helpers_cast,
7878 /// _ = operand;
7979 discard,
8080
......@@ -124,8 +124,8 @@ pub const Node = extern union {
124124 std_math_Log2Int,
125125 /// @intCast(lhs, rhs)
126126 int_cast,
127 /// @import("std").meta.promoteIntLiteral(value, type, radix)
128 std_meta_promoteIntLiteral,
127 /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix)
128 helpers_promoteIntLiteral,
129129 /// @import("std").meta.alignment(value)
130130 std_meta_alignment,
131131 /// @rem(lhs, rhs)
......@@ -193,12 +193,12 @@ pub const Node = extern union {
193193 array_type,
194194 null_sentinel_array_type,
195195
196 /// @import("std").meta.sizeof(operand)
197 std_meta_sizeof,
198 /// @import("std").meta.FlexibleArrayType(lhs, rhs)
199 std_meta_flexible_array_type,
200 /// @import("std").meta.shuffleVectorIndex(lhs, rhs)
201 std_meta_shuffle_vector_index,
196 /// @import("std").zig.c_translation.sizeof(operand)
197 helpers_sizeof,
198 /// @import("std").zig.c_translation.FlexibleArrayType(lhs, rhs)
199 helpers_flexible_array_type,
200 /// @import("std").zig.c_translation.shuffleVectorIndex(lhs, rhs)
201 helpers_shuffle_vector_index,
202202 /// @import("std").meta.Vector(lhs, rhs)
203203 std_meta_vector,
204204 /// @import("std").mem.zeroes(operand)
......@@ -272,7 +272,7 @@ pub const Node = extern union {
272272 .if_not_break,
273273 .switch_else,
274274 .block_single,
275 .std_meta_sizeof,
275 .helpers_sizeof,
276276 .std_meta_alignment,
277277 .bool_to_int,
278278 .sizeof,
......@@ -332,13 +332,13 @@ pub const Node = extern union {
332332 .align_cast,
333333 .array_access,
334334 .std_mem_zeroinit,
335 .std_meta_flexible_array_type,
336 .std_meta_shuffle_vector_index,
335 .helpers_flexible_array_type,
336 .helpers_shuffle_vector_index,
337337 .std_meta_vector,
338338 .ptr_cast,
339339 .div_exact,
340340 .offset_of,
341 .std_meta_cast,
341 .helpers_cast,
342342 => Payload.BinOp,
343343
344344 .integer_literal,
......@@ -362,7 +362,7 @@ pub const Node = extern union {
362362 .tuple => Payload.TupleInit,
363363 .container_init => Payload.ContainerInit,
364364 .container_init_dot => Payload.ContainerInitDot,
365 .std_meta_promoteIntLiteral => Payload.PromoteIntLiteral,
365 .helpers_promoteIntLiteral => Payload.PromoteIntLiteral,
366366 .block => Payload.Block,
367367 .c_pointer, .single_pointer => Payload.Pointer,
368368 .array_type, .null_sentinel_array_type => Payload.Array,
......@@ -868,7 +868,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
868868 // pub usingnamespace @import("std").c.builtins;
869869 _ = try c.addToken(.keyword_pub, "pub");
870870 const usingnamespace_token = try c.addToken(.keyword_usingnamespace, "usingnamespace");
871 const import_node = try renderStdImport(c, "c", "builtins");
871 const import_node = try renderStdImport(c, &.{ "zig", "c_builtins" });
872872 _ = try c.addToken(.semicolon, ";");
873873
874874 return c.addNode(.{
......@@ -882,52 +882,52 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
882882 },
883883 .std_math_Log2Int => {
884884 const payload = node.castTag(.std_math_Log2Int).?.data;
885 const import_node = try renderStdImport(c, "math", "Log2Int");
885 const import_node = try renderStdImport(c, &.{ "math", "Log2Int" });
886886 return renderCall(c, import_node, &.{payload});
887887 },
888 .std_meta_cast => {
889 const payload = node.castTag(.std_meta_cast).?.data;
890 const import_node = try renderStdImport(c, "meta", "cast");
888 .helpers_cast => {
889 const payload = node.castTag(.helpers_cast).?.data;
890 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "cast" });
891891 return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
892892 },
893 .std_meta_promoteIntLiteral => {
894 const payload = node.castTag(.std_meta_promoteIntLiteral).?.data;
895 const import_node = try renderStdImport(c, "meta", "promoteIntLiteral");
893 .helpers_promoteIntLiteral => {
894 const payload = node.castTag(.helpers_promoteIntLiteral).?.data;
895 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" });
896896 return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix });
897897 },
898898 .std_meta_alignment => {
899899 const payload = node.castTag(.std_meta_alignment).?.data;
900 const import_node = try renderStdImport(c, "meta", "alignment");
900 const import_node = try renderStdImport(c, &.{ "meta", "alignment" });
901901 return renderCall(c, import_node, &.{payload});
902902 },
903 .std_meta_sizeof => {
904 const payload = node.castTag(.std_meta_sizeof).?.data;
905 const import_node = try renderStdImport(c, "meta", "sizeof");
903 .helpers_sizeof => {
904 const payload = node.castTag(.helpers_sizeof).?.data;
905 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "sizeof" });
906906 return renderCall(c, import_node, &.{payload});
907907 },
908908 .std_mem_zeroes => {
909909 const payload = node.castTag(.std_mem_zeroes).?.data;
910 const import_node = try renderStdImport(c, "mem", "zeroes");
910 const import_node = try renderStdImport(c, &.{ "mem", "zeroes" });
911911 return renderCall(c, import_node, &.{payload});
912912 },
913913 .std_mem_zeroinit => {
914914 const payload = node.castTag(.std_mem_zeroinit).?.data;
915 const import_node = try renderStdImport(c, "mem", "zeroInit");
915 const import_node = try renderStdImport(c, &.{ "mem", "zeroInit" });
916916 return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
917917 },
918 .std_meta_flexible_array_type => {
919 const payload = node.castTag(.std_meta_flexible_array_type).?.data;
920 const import_node = try renderStdImport(c, "meta", "FlexibleArrayType");
918 .helpers_flexible_array_type => {
919 const payload = node.castTag(.helpers_flexible_array_type).?.data;
920 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "FlexibleArrayType" });
921921 return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
922922 },
923 .std_meta_shuffle_vector_index => {
924 const payload = node.castTag(.std_meta_shuffle_vector_index).?.data;
925 const import_node = try renderStdImport(c, "meta", "shuffleVectorIndex");
923 .helpers_shuffle_vector_index => {
924 const payload = node.castTag(.helpers_shuffle_vector_index).?.data;
925 const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" });
926926 return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
927927 },
928928 .std_meta_vector => {
929929 const payload = node.castTag(.std_meta_vector).?.data;
930 const import_node = try renderStdImport(c, "meta", "Vector");
930 const import_node = try renderStdImport(c, &.{ "meta", "Vector" });
931931 return renderCall(c, import_node, &.{ payload.lhs, payload.rhs });
932932 },
933933 .call => {
......@@ -2269,13 +2269,13 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
22692269 .alignof,
22702270 .typeof,
22712271 .typeinfo,
2272 .std_meta_sizeof,
22732272 .std_meta_alignment,
2274 .std_meta_cast,
2275 .std_meta_promoteIntLiteral,
22762273 .std_meta_vector,
2277 .std_meta_shuffle_vector_index,
2278 .std_meta_flexible_array_type,
2274 .helpers_sizeof,
2275 .helpers_cast,
2276 .helpers_promoteIntLiteral,
2277 .helpers_shuffle_vector_index,
2278 .helpers_flexible_array_type,
22792279 .std_mem_zeroinit,
22802280 .integer_literal,
22812281 .float_literal,
......@@ -2442,7 +2442,7 @@ fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: Toke
24422442 });
24432443}
24442444
2445fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeIndex {
2445fn renderStdImport(c: *Context, parts: []const []const u8) !NodeIndex {
24462446 const import_tok = try c.addToken(.builtin, "@import");
24472447 _ = try c.addToken(.l_paren, "(");
24482448 const std_tok = try c.addToken(.string_literal, "\"std\"");
......@@ -2463,8 +2463,9 @@ fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeInde
24632463 });
24642464
24652465 var access_chain = import_node;
2466 access_chain = try renderFieldAccess(c, access_chain, first);
2467 access_chain = try renderFieldAccess(c, access_chain, second);
2466 for (parts) |part| {
2467 access_chain = try renderFieldAccess(c, access_chain, part);
2468 }
24682469 return access_chain;
24692470}
24702471
test/translate_c.zig+33-33
......@@ -133,7 +133,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
133133 \\ const A = @enumToInt(enum_Foo.A);
134134 \\ const B = @enumToInt(enum_Foo.B);
135135 \\ const C = @enumToInt(enum_Foo.C);
136 \\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B);
136 \\ var a: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, B);
137137 \\ {
138138 \\ const enum_Foo = extern enum(
139139 ++ default_enum_type ++
......@@ -146,7 +146,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
146146 \\ const A_2 = @enumToInt(enum_Foo.A);
147147 \\ const B_3 = @enumToInt(enum_Foo.B);
148148 \\ const C_4 = @enumToInt(enum_Foo.C);
149 \\ var a_5: enum_Foo = @import("std").meta.cast(enum_Foo, B_3);
149 \\ var a_5: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, B_3);
150150 \\ }
151151 \\}
152152 });
......@@ -242,7 +242,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
242242 \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
243243 , &[_][]const u8{
244244 \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void {
245 \\ return @import("std").meta.cast(?*c_void, @import("std").meta.cast(u32, x) + SYS_BASE_CACHED);
245 \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
246246 \\}
247247 });
248248
......@@ -282,8 +282,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
282282 ,
283283 \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9));
284284 ,
285 \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
286 \\ return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
285 \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
286 \\ return (@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
287287 \\}
288288 });
289289
......@@ -438,17 +438,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
438438 , &[_][]const u8{
439439 \\pub const struct_foo = extern struct {
440440 \\ x: c_int align(4),
441 \\ pub fn y(self: anytype) @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int) {
442 \\ const Intermediate = @import("std").meta.FlexibleArrayType(@TypeOf(self), u8);
443 \\ const ReturnType = @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int);
441 \\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) {
442 \\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8);
443 \\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int);
444444 \\ return @ptrCast(ReturnType, @alignCast(@alignOf(c_int), @ptrCast(Intermediate, self) + 4));
445445 \\ }
446446 \\};
447447 \\pub const struct_bar = extern struct {
448448 \\ x: c_int align(4),
449 \\ pub fn y(self: anytype) @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int) {
450 \\ const Intermediate = @import("std").meta.FlexibleArrayType(@TypeOf(self), u8);
451 \\ const ReturnType = @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int);
449 \\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) {
450 \\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8);
451 \\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int);
452452 \\ return @ptrCast(ReturnType, @alignCast(@alignOf(c_int), @ptrCast(Intermediate, self) + 4));
453453 \\ }
454454 \\};
......@@ -583,7 +583,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
583583 cases.add("#define hex literal with capital X",
584584 \\#define VAL 0XF00D
585585 , &[_][]const u8{
586 \\pub const VAL = @import("std").meta.promoteIntLiteral(c_int, 0xF00D, .hexadecimal);
586 \\pub const VAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0xF00D, .hexadecimal);
587587 });
588588
589589 cases.add("anonymous struct & unions",
......@@ -1738,7 +1738,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17381738 \\pub const e = @enumToInt(enum_unnamed_1.e);
17391739 \\pub const f = @enumToInt(enum_unnamed_1.f);
17401740 \\pub const g = @enumToInt(enum_unnamed_1.g);
1741 \\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e);
1741 \\pub export var h: enum_unnamed_1 = @import("std").zig.c_translation.cast(enum_unnamed_1, e);
17421742 \\const enum_unnamed_2 = extern enum(
17431743 ++ default_enum_type ++
17441744 \\) {
......@@ -1882,7 +1882,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
18821882 \\typedef struct { int dummy; } NRF_GPIO_Type;
18831883 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
18841884 , &[_][]const u8{
1885 \\pub const NRF_GPIO = @import("std").meta.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
1885 \\pub const NRF_GPIO = @import("std").zig.c_translation.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
18861886 });
18871887
18881888 cases.add("basic macro function",
......@@ -2379,7 +2379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23792379 \\ var a = arg_a;
23802380 \\ var b = arg_b;
23812381 \\ var c = arg_c;
2382 \\ var d: enum_Foo = @import("std").meta.cast(enum_Foo, FooA);
2382 \\ var d: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, FooA);
23832383 \\ var e: c_int = @boolToInt((a != 0) and (b != 0));
23842384 \\ var f: c_int = @boolToInt((b != 0) and (c != null));
23852385 \\ var g: c_int = @boolToInt((a != 0) and (c != null));
......@@ -3182,13 +3182,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31823182 \\#define BAR (void*) a
31833183 \\#define BAZ (uint32_t)(2)
31843184 , &[_][]const u8{
3185 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").meta.cast(?*c_void, baz))) {
3186 \\ return baz(@import("std").meta.cast(?*c_void, baz));
3185 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) {
3186 \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz));
31873187 \\}
31883188 ,
3189 \\pub const BAR = @import("std").meta.cast(?*c_void, a);
3189 \\pub const BAR = @import("std").zig.c_translation.cast(?*c_void, a);
31903190 ,
3191 \\pub const BAZ = @import("std").meta.cast(u32, @as(c_int, 2));
3191 \\pub const BAZ = @import("std").zig.c_translation.cast(u32, @as(c_int, 2));
31923192 });
31933193
31943194 cases.add("macro with cast to unsigned short, long, and long long",
......@@ -3196,9 +3196,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
31963196 \\#define CURLAUTH_BASIC ((unsigned long) 1)
31973197 \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)
31983198 , &[_][]const u8{
3199 \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, @as(c_int, 1));
3200 \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, @as(c_int, 1));
3201 \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, @as(c_int, 1));
3199 \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").zig.c_translation.cast(c_ushort, @as(c_int, 1));
3200 \\pub const CURLAUTH_BASIC = @import("std").zig.c_translation.cast(c_ulong, @as(c_int, 1));
3201 \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").zig.c_translation.cast(c_ulonglong, @as(c_int, 1));
32023202 });
32033203
32043204 cases.add("macro conditional operator",
......@@ -3413,8 +3413,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34133413 \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen)
34143414 \\
34153415 , &[_][]const u8{
3416 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen) {
3417 \\ return @import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen;
3416 \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) {
3417 \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen;
34183418 \\}
34193419 });
34203420
......@@ -3422,9 +3422,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34223422 \\#define NULL ((void*)0)
34233423 \\#define FOO ((int)0x8000)
34243424 , &[_][]const u8{
3425 \\pub const NULL = @import("std").meta.cast(?*c_void, @as(c_int, 0));
3425 \\pub const NULL = @import("std").zig.c_translation.cast(?*c_void, @as(c_int, 0));
34263426 ,
3427 \\pub const FOO = @import("std").meta.cast(c_int, @import("std").meta.promoteIntLiteral(c_int, 0x8000, .hexadecimal));
3427 \\pub const FOO = @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x8000, .hexadecimal));
34283428 });
34293429
34303430 if (std.Target.current.abi == .msvc) {
......@@ -3503,11 +3503,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
35033503 \\pub const GUARANTEED_TO_FIT_1 = @as(c_int, 1024);
35043504 \\pub const GUARANTEED_TO_FIT_2 = @as(c_long, 10241024);
35053505 \\pub const GUARANTEED_TO_FIT_3 = @as(c_ulong, 20482048);
3506 \\pub const MAY_NEED_PROMOTION_1 = @import("std").meta.promoteIntLiteral(c_int, 10241024, .decimal);
3507 \\pub const MAY_NEED_PROMOTION_2 = @import("std").meta.promoteIntLiteral(c_long, 307230723072, .decimal);
3508 \\pub const MAY_NEED_PROMOTION_3 = @import("std").meta.promoteIntLiteral(c_ulong, 819281928192, .decimal);
3509 \\pub const MAY_NEED_PROMOTION_HEX = @import("std").meta.promoteIntLiteral(c_int, 0x80000000, .hexadecimal);
3510 \\pub const MAY_NEED_PROMOTION_OCT = @import("std").meta.promoteIntLiteral(c_int, 0o20000000000, .octal);
3506 \\pub const MAY_NEED_PROMOTION_1 = @import("std").zig.c_translation.promoteIntLiteral(c_int, 10241024, .decimal);
3507 \\pub const MAY_NEED_PROMOTION_2 = @import("std").zig.c_translation.promoteIntLiteral(c_long, 307230723072, .decimal);
3508 \\pub const MAY_NEED_PROMOTION_3 = @import("std").zig.c_translation.promoteIntLiteral(c_ulong, 819281928192, .decimal);
3509 \\pub const MAY_NEED_PROMOTION_HEX = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x80000000, .hexadecimal);
3510 \\pub const MAY_NEED_PROMOTION_OCT = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0o20000000000, .octal);
35113511 });
35123512
35133513 // See __builtin_alloca_with_align comment in std.c.builtins
......@@ -3642,7 +3642,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
36423642 \\typedef long long LONG_PTR;
36433643 \\#define INVALID_HANDLE_VALUE ((void *)(LONG_PTR)-1)
36443644 , &[_][]const u8{
3645 \\pub const MAP_FAILED = @import("std").meta.cast(?*c_void, -@as(c_int, 1));
3646 \\pub const INVALID_HANDLE_VALUE = @import("std").meta.cast(?*c_void, @import("std").meta.cast(LONG_PTR, -@as(c_int, 1)));
3645 \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1));
3646 \\pub const INVALID_HANDLE_VALUE = @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(LONG_PTR, -@as(c_int, 1)));
36473647 });
36483648}