Skip to content

Move bswap back to stdlib #2701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 0 additions & 177 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ export namespace BuiltinNames {
export const isManaged = "~lib/builtins/isManaged";
export const isVoid = "~lib/builtins/isVoid";

export const bswap = "~lib/builtins/bswap";

export const add = "~lib/builtins/add";
export const sub = "~lib/builtins/sub";
export const mul = "~lib/builtins/mul";
Expand Down Expand Up @@ -1193,181 +1191,6 @@ function builtin_idof(ctx: BuiltinFunctionContext): ExpressionRef {
}
builtinFunctions.set(BuiltinNames.idof, builtin_idof);

// bswap<T?>(value: T) -> T
function builtin_bswap(ctx: BuiltinFunctionContext): ExpressionRef {
let compiler = ctx.compiler;
let module = compiler.module;
if (
checkTypeOptional(ctx, true) |
checkArgsRequired(ctx, 1)
) return module.unreachable();

let typeArguments = ctx.typeArguments;
let arg0 = typeArguments
? compiler.compileExpression(
ctx.operands[0],
typeArguments[0].toUnsigned(),
Constraints.ConvImplicit | Constraints.MustWrap
)
: compiler.compileExpression(
ctx.operands[0],
Type.u32,
Constraints.MustWrap
);

let type = compiler.currentType;
if (type.isValue) {
switch (type.kind) {
case TypeKind.Bool:
case TypeKind.I8:
case TypeKind.U8: return arg0;
case TypeKind.I16:
case TypeKind.U16: {
// <T>(x << 8 | x >> 8)
let flow = compiler.currentFlow;
let temp = flow.getTempLocal(type);
flow.setLocalFlag(temp.index, LocalFlags.Wrapped);

let res = module.binary(
BinaryOp.OrI32,
module.binary(
BinaryOp.ShlI32,
module.local_tee(temp.index, arg0, false),
module.i32(8)
),
module.binary(
BinaryOp.ShrU32,
module.local_get(temp.index, TypeRef.I32),
module.i32(8)
)
);
// avoid wrapping for u16 due to it's already done for input arg
if (type.kind == TypeKind.I16) {
res = compiler.ensureSmallIntegerWrap(res, Type.i16);
}
return res;
}
case TypeKind.I32:
case TypeKind.U32:
case TypeKind.Isize:
case TypeKind.Usize: {
if (type.size == 32) {
// rotl(x & 0xFF00FF00, 8) | rotr(x & 0x00FF00FF, 8)
let flow = compiler.currentFlow;
let temp = flow.getTempLocal(type);
flow.setLocalFlag(temp.index, LocalFlags.Wrapped);

let res = module.binary(
BinaryOp.OrI32,
module.binary(
BinaryOp.RotlI32,
module.binary(
BinaryOp.AndI32,
module.local_tee(temp.index, arg0, false),
module.i32(0xFF00FF00)
),
module.i32(8)
),
module.binary(
BinaryOp.RotrI32,
module.binary(
BinaryOp.AndI32,
module.local_get(temp.index, TypeRef.I32),
module.i32(0x00FF00FF)
),
module.i32(8)
),
);
return res;
}
// fall-through
}
case TypeKind.I64:
case TypeKind.U64: {
// let t =
// ((x >>> 8) & 0x00FF00FF00FF00FF) |
// ((x & 0x00FF00FF00FF00FF) << 8)
//
// let res =
// ((t >>> 16) & 0x0000FFFF0000FFFF) |
// ((t & 0x0000FFFF0000FFFF) << 16)
//
// rotr(res, 32)

let flow = compiler.currentFlow;
let temp1 = flow.getTempLocal(type);
flow.setLocalFlag(temp1.index, LocalFlags.Wrapped);
let temp2 = flow.getTempLocal(type);
flow.setLocalFlag(temp2.index, LocalFlags.Wrapped);

// t = ((x >>> 8) & 0x00FF00FF00FF00FF) | ((x & 0x00FF00FF00FF00FF) << 8)
let expr = module.local_tee(
temp2.index,
module.binary(
BinaryOp.OrI64,
module.binary(
BinaryOp.AndI64,
module.binary(
BinaryOp.ShrU64,
module.local_tee(temp1.index, arg0, false),
module.i64(8)
),
module.i64(0x00FF00FF, 0x00FF00FF)
),
module.binary(
BinaryOp.ShlI64,
module.binary(
BinaryOp.AndI64,
module.local_get(temp1.index, TypeRef.I64),
module.i64(0x00FF00FF, 0x00FF00FF)
),
module.i64(8)
),
),
false
);

// ((t >>> 16) & 0x0000FFFF0000FFFF) | ((t & 0x0000FFFF0000FFFF) << 16)
let res = module.binary(
BinaryOp.OrI64,
module.binary(
BinaryOp.AndI64,
module.binary(
BinaryOp.ShrU64,
expr,
module.i64(16)
),
module.i64(0x0000FFFF, 0x0000FFFF)
),
module.binary(
BinaryOp.ShlI64,
module.binary(
BinaryOp.AndI64,
module.local_get(temp2.index, TypeRef.I64),
module.i64(0x0000FFFF, 0x0000FFFF)
),
module.i64(16)
),
);

// rotr(res, 32)
res = module.binary(
BinaryOp.RotrI64,
res,
module.i64(32)
);
return res;
}
}
}
compiler.error(
DiagnosticCode.Operation_0_cannot_be_applied_to_type_1,
ctx.reportNode.typeArgumentsRange, "bswap", type.toString()
);
return module.unreachable();
}
builtinFunctions.set(BuiltinNames.bswap, builtin_bswap);

// === Math ===================================================================================

// NaN
Expand Down
4 changes: 0 additions & 4 deletions std/assembly/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export declare function isVoid<T>(): bool;
@builtin
export declare function lengthof<T>(func?: T): i32;

// @ts-ignore
@builtin
export declare function bswap<T>(value: T): T;

// @ts-ignore: decorator
@builtin
export declare function clz<T>(value: T): T;
Expand Down
27 changes: 27 additions & 0 deletions std/assembly/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function bswap<T extends number>(value: T): T {
if (isInteger<T>()) {
if (sizeof<T>() == 1) {
return value;
}
if (sizeof<T>() == 2) {
return <T>(<u16>value << 8 | (<u16>value >> 8));
}
if (sizeof<T>() == 4) {
return <T>(
rotl(<u32>value & 0xFF00FF00, 8) |
rotr(<u32>value & 0x00FF00FF, 8)
);
}
if (sizeof<T>() == 8) {
let a = (<u64>value >> 8) & 0x00FF00FF00FF00FF;
let b = (<u64>value & 0x00FF00FF00FF00FF) << 8;
let v = a | b;

a = (v >>> 16) & 0x0000FFFF0000FFFF;
b = (v & 0x0000FFFF0000FFFF) << 16;

return <T>rotr(a | b, 32);
}
}
ERROR("Unsupported generic type");
}
Loading