2020-03-30 10:55:21 +01:00
|
|
|
const std = @import("std");
|
2022-06-10 09:25:59 +01:00
|
|
|
const builtin = @import("builtin");
|
2020-03-30 10:55:21 +01:00
|
|
|
const Log2Int = std.math.Log2Int;
|
2022-06-10 09:25:59 +01:00
|
|
|
const native_endian = builtin.cpu.arch.endian();
|
2022-06-18 02:10:00 +01:00
|
|
|
const common = @import("common.zig");
|
|
|
|
|
|
|
|
pub const panic = common.panic;
|
2022-06-10 09:25:59 +01:00
|
|
|
|
|
|
|
comptime {
|
2022-06-18 02:10:00 +01:00
|
|
|
@export(__ashlti3, .{ .name = "__ashlti3", .linkage = common.linkage });
|
|
|
|
@export(__ashrti3, .{ .name = "__ashrti3", .linkage = common.linkage });
|
|
|
|
@export(__lshrti3, .{ .name = "__lshrti3", .linkage = common.linkage });
|
|
|
|
|
|
|
|
if (common.want_aeabi) {
|
|
|
|
@export(__aeabi_llsl, .{ .name = "__aeabi_llsl", .linkage = common.linkage });
|
|
|
|
@export(__aeabi_lasr, .{ .name = "__aeabi_lasr", .linkage = common.linkage });
|
|
|
|
@export(__aeabi_llsr, .{ .name = "__aeabi_llsr", .linkage = common.linkage });
|
|
|
|
} else {
|
|
|
|
@export(__ashldi3, .{ .name = "__ashldi3", .linkage = common.linkage });
|
|
|
|
@export(__ashrdi3, .{ .name = "__ashrdi3", .linkage = common.linkage });
|
|
|
|
@export(__lshrdi3, .{ .name = "__lshrdi3", .linkage = common.linkage });
|
2022-06-10 09:25:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 10:55:21 +01:00
|
|
|
|
|
|
|
fn Dwords(comptime T: type, comptime signed_half: bool) type {
|
|
|
|
return extern union {
|
2022-06-10 09:25:59 +01:00
|
|
|
const bits = @divExact(@typeInfo(T).Int.bits, 2);
|
|
|
|
const HalfTU = std.meta.Int(.unsigned, bits);
|
|
|
|
const HalfTS = std.meta.Int(.signed, bits);
|
|
|
|
const HalfT = if (signed_half) HalfTS else HalfTU;
|
2020-03-30 10:55:21 +01:00
|
|
|
|
|
|
|
all: T,
|
2021-04-13 00:43:50 +01:00
|
|
|
s: if (native_endian == .Little)
|
2022-07-11 17:57:41 +01:00
|
|
|
extern struct { low: HalfT, high: HalfT }
|
2020-03-30 10:55:21 +01:00
|
|
|
else
|
2022-07-11 17:57:41 +01:00
|
|
|
extern struct { high: HalfT, low: HalfT },
|
2020-03-30 10:55:21 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetic shift left
|
|
|
|
// Precondition: 0 <= b < bits_in_dword
|
2022-06-10 09:25:59 +01:00
|
|
|
inline fn ashlXi3(comptime T: type, a: T, b: i32) T {
|
2020-03-30 10:55:21 +01:00
|
|
|
const dwords = Dwords(T, false);
|
|
|
|
const S = Log2Int(dwords.HalfT);
|
|
|
|
|
|
|
|
const input = dwords{ .all = a };
|
|
|
|
var output: dwords = undefined;
|
|
|
|
|
2020-09-03 16:09:55 +01:00
|
|
|
if (b >= dwords.bits) {
|
2020-03-30 10:55:21 +01:00
|
|
|
output.s.low = 0;
|
2020-09-03 16:09:55 +01:00
|
|
|
output.s.high = input.s.low << @intCast(S, b - dwords.bits);
|
2020-03-30 10:55:21 +01:00
|
|
|
} else if (b == 0) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
output.s.low = input.s.low << @intCast(S, b);
|
|
|
|
output.s.high = input.s.high << @intCast(S, b);
|
2020-09-03 16:09:55 +01:00
|
|
|
output.s.high |= input.s.low >> @intCast(S, dwords.bits - b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetic shift right
|
|
|
|
// Precondition: 0 <= b < T.bit_count
|
2022-06-10 09:25:59 +01:00
|
|
|
inline fn ashrXi3(comptime T: type, a: T, b: i32) T {
|
2020-03-30 10:55:21 +01:00
|
|
|
const dwords = Dwords(T, true);
|
|
|
|
const S = Log2Int(dwords.HalfT);
|
|
|
|
|
|
|
|
const input = dwords{ .all = a };
|
|
|
|
var output: dwords = undefined;
|
|
|
|
|
2020-09-03 16:09:55 +01:00
|
|
|
if (b >= dwords.bits) {
|
|
|
|
output.s.high = input.s.high >> (dwords.bits - 1);
|
|
|
|
output.s.low = input.s.high >> @intCast(S, b - dwords.bits);
|
2020-03-30 10:55:21 +01:00
|
|
|
} else if (b == 0) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
output.s.high = input.s.high >> @intCast(S, b);
|
2020-09-03 16:09:55 +01:00
|
|
|
output.s.low = input.s.high << @intCast(S, dwords.bits - b);
|
2020-03-30 10:55:21 +01:00
|
|
|
// Avoid sign-extension here
|
|
|
|
output.s.low |= @bitCast(
|
|
|
|
dwords.HalfT,
|
|
|
|
@bitCast(dwords.HalfTU, input.s.low) >> @intCast(S, b),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logical shift right
|
|
|
|
// Precondition: 0 <= b < T.bit_count
|
2022-06-10 09:25:59 +01:00
|
|
|
inline fn lshrXi3(comptime T: type, a: T, b: i32) T {
|
2020-03-30 10:55:21 +01:00
|
|
|
const dwords = Dwords(T, false);
|
|
|
|
const S = Log2Int(dwords.HalfT);
|
|
|
|
|
|
|
|
const input = dwords{ .all = a };
|
|
|
|
var output: dwords = undefined;
|
|
|
|
|
2020-09-03 16:09:55 +01:00
|
|
|
if (b >= dwords.bits) {
|
2020-03-30 10:55:21 +01:00
|
|
|
output.s.high = 0;
|
2020-09-03 16:09:55 +01:00
|
|
|
output.s.low = input.s.high >> @intCast(S, b - dwords.bits);
|
2020-03-30 10:55:21 +01:00
|
|
|
} else if (b == 0) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
output.s.high = input.s.high >> @intCast(S, b);
|
2020-09-03 16:09:55 +01:00
|
|
|
output.s.low = input.s.high << @intCast(S, dwords.bits - b);
|
2020-03-30 10:55:21 +01:00
|
|
|
output.s.low |= input.s.low >> @intCast(S, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn __ashldi3(a: i64, b: i32) callconv(.C) i64 {
|
2022-01-02 02:49:34 +00:00
|
|
|
return ashlXi3(i64, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
2022-06-18 02:10:00 +01:00
|
|
|
fn __aeabi_llsl(a: i64, b: i32) callconv(.AAPCS) i64 {
|
|
|
|
return ashlXi3(i64, a, b);
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:55:21 +01:00
|
|
|
pub fn __ashlti3(a: i128, b: i32) callconv(.C) i128 {
|
2022-01-02 02:49:34 +00:00
|
|
|
return ashlXi3(i128, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
2022-06-18 02:10:00 +01:00
|
|
|
|
2020-03-30 10:55:21 +01:00
|
|
|
pub fn __ashrdi3(a: i64, b: i32) callconv(.C) i64 {
|
2022-01-02 02:49:34 +00:00
|
|
|
return ashrXi3(i64, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
2022-06-18 02:10:00 +01:00
|
|
|
fn __aeabi_lasr(a: i64, b: i32) callconv(.AAPCS) i64 {
|
|
|
|
return ashrXi3(i64, a, b);
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:55:21 +01:00
|
|
|
pub fn __ashrti3(a: i128, b: i32) callconv(.C) i128 {
|
2022-01-02 02:49:34 +00:00
|
|
|
return ashrXi3(i128, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
2022-06-18 02:10:00 +01:00
|
|
|
|
2020-03-30 10:55:21 +01:00
|
|
|
pub fn __lshrdi3(a: i64, b: i32) callconv(.C) i64 {
|
2022-01-02 02:49:34 +00:00
|
|
|
return lshrXi3(i64, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
2022-06-18 02:10:00 +01:00
|
|
|
fn __aeabi_llsr(a: i64, b: i32) callconv(.AAPCS) i64 {
|
|
|
|
return lshrXi3(i64, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-18 02:10:00 +01:00
|
|
|
pub fn __lshrti3(a: i128, b: i32) callconv(.C) i128 {
|
|
|
|
return lshrXi3(i128, a, b);
|
2020-03-30 10:55:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:45:28 +00:00
|
|
|
test {
|
2020-03-30 10:55:21 +01:00
|
|
|
_ = @import("ashrdi3_test.zig");
|
|
|
|
_ = @import("ashrti3_test.zig");
|
|
|
|
|
|
|
|
_ = @import("ashldi3_test.zig");
|
|
|
|
_ = @import("ashlti3_test.zig");
|
|
|
|
|
|
|
|
_ = @import("lshrdi3_test.zig");
|
|
|
|
_ = @import("lshrti3_test.zig");
|
|
|
|
}
|