authorgravatar for william@sengir.comWilliam Sengir <william@sengir.com> 2022-03-26 15:50:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-16 13:55:26-07:00
log86a928ce61ae7df52d3d54fa5c653195a7a4cbef
treed1eeaf2df9c633b69b9bf0bb5c96b5e887fdc60d
parente8117bab6f786348142a72b5279380a937c3a151

stage2: perform comptime vectorization of `*_with_overflow` in `Value`


1 files changed, 98 insertions(+), 5 deletions(-)

src/value.zig+98-5
......@@ -2961,7 +2961,8 @@ pub const Value = extern union {
29612961 }
29622962
29632963 pub const OverflowArithmeticResult = struct {
2964 overflowed: bool,
2964 /// TODO: Rename to `overflow_bit` and make of type `u1`.
2965 overflowed: Value,
29652966 wrapped_result: Value,
29662967 };
29672968
......@@ -2971,6 +2972,29 @@ pub const Value = extern union {
29712972 ty: Type,
29722973 arena: Allocator,
29732974 target: Target,
2975 ) !OverflowArithmeticResult {
2976 if (ty.zigTypeTag() == .Vector) {
2977 const overflowed_data = try arena.alloc(Value, ty.vectorLen());
2978 const result_data = try arena.alloc(Value, ty.vectorLen());
2979 for (result_data) |*scalar, i| {
2980 const of_math_result = try intAddWithOverflowScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), ty.scalarType(), arena, target);
2981 overflowed_data[i] = of_math_result.overflowed;
2982 scalar.* = of_math_result.wrapped_result;
2983 }
2984 return OverflowArithmeticResult{
2985 .overflowed = try Value.Tag.aggregate.create(arena, overflowed_data),
2986 .wrapped_result = try Value.Tag.aggregate.create(arena, result_data),
2987 };
2988 }
2989 return intAddWithOverflowScalar(lhs, rhs, ty, arena, target);
2990 }
2991
2992 pub fn intAddWithOverflowScalar(
2993 lhs: Value,
2994 rhs: Value,
2995 ty: Type,
2996 arena: Allocator,
2997 target: Target,
29742998 ) !OverflowArithmeticResult {
29752999 const info = ty.intInfo(target);
29763000
......@@ -2986,7 +3010,7 @@ pub const Value = extern union {
29863010 const overflowed = result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
29873011 const result = try fromBigInt(arena, result_bigint.toConst());
29883012 return OverflowArithmeticResult{
2989 .overflowed = overflowed,
3013 .overflowed = makeBool(overflowed),
29903014 .wrapped_result = result,
29913015 };
29923016 }
......@@ -3097,6 +3121,29 @@ pub const Value = extern union {
30973121 ty: Type,
30983122 arena: Allocator,
30993123 target: Target,
3124 ) !OverflowArithmeticResult {
3125 if (ty.zigTypeTag() == .Vector) {
3126 const overflowed_data = try arena.alloc(Value, ty.vectorLen());
3127 const result_data = try arena.alloc(Value, ty.vectorLen());
3128 for (result_data) |*scalar, i| {
3129 const of_math_result = try intSubWithOverflowScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), ty.scalarType(), arena, target);
3130 overflowed_data[i] = of_math_result.overflowed;
3131 scalar.* = of_math_result.wrapped_result;
3132 }
3133 return OverflowArithmeticResult{
3134 .overflowed = try Value.Tag.aggregate.create(arena, overflowed_data),
3135 .wrapped_result = try Value.Tag.aggregate.create(arena, result_data),
3136 };
3137 }
3138 return intSubWithOverflowScalar(lhs, rhs, ty, arena, target);
3139 }
3140
3141 pub fn intSubWithOverflowScalar(
3142 lhs: Value,
3143 rhs: Value,
3144 ty: Type,
3145 arena: Allocator,
3146 target: Target,
31003147 ) !OverflowArithmeticResult {
31013148 const info = ty.intInfo(target);
31023149
......@@ -3112,7 +3159,7 @@ pub const Value = extern union {
31123159 const overflowed = result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
31133160 const wrapped_result = try fromBigInt(arena, result_bigint.toConst());
31143161 return OverflowArithmeticResult{
3115 .overflowed = overflowed,
3162 .overflowed = makeBool(overflowed),
31163163 .wrapped_result = wrapped_result,
31173164 };
31183165 }
......@@ -3207,6 +3254,29 @@ pub const Value = extern union {
32073254 ty: Type,
32083255 arena: Allocator,
32093256 target: Target,
3257 ) !OverflowArithmeticResult {
3258 if (ty.zigTypeTag() == .Vector) {
3259 const overflowed_data = try arena.alloc(Value, ty.vectorLen());
3260 const result_data = try arena.alloc(Value, ty.vectorLen());
3261 for (result_data) |*scalar, i| {
3262 const of_math_result = try intMulWithOverflowScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), ty.scalarType(), arena, target);
3263 overflowed_data[i] = of_math_result.overflowed;
3264 scalar.* = of_math_result.wrapped_result;
3265 }
3266 return OverflowArithmeticResult{
3267 .overflowed = try Value.Tag.aggregate.create(arena, overflowed_data),
3268 .wrapped_result = try Value.Tag.aggregate.create(arena, result_data),
3269 };
3270 }
3271 return intMulWithOverflowScalar(lhs, rhs, ty, arena, target);
3272 }
3273
3274 pub fn intMulWithOverflowScalar(
3275 lhs: Value,
3276 rhs: Value,
3277 ty: Type,
3278 arena: Allocator,
3279 target: Target,
32103280 ) !OverflowArithmeticResult {
32113281 const info = ty.intInfo(target);
32123282
......@@ -3231,7 +3301,7 @@ pub const Value = extern union {
32313301 }
32323302
32333303 return OverflowArithmeticResult{
3234 .overflowed = overflowed,
3304 .overflowed = makeBool(overflowed),
32353305 .wrapped_result = try fromBigInt(arena, result_bigint.toConst()),
32363306 };
32373307 }
......@@ -3921,6 +3991,29 @@ pub const Value = extern union {
39213991 ty: Type,
39223992 allocator: Allocator,
39233993 target: Target,
3994 ) !OverflowArithmeticResult {
3995 if (ty.zigTypeTag() == .Vector) {
3996 const overflowed_data = try allocator.alloc(Value, ty.vectorLen());
3997 const result_data = try allocator.alloc(Value, ty.vectorLen());
3998 for (result_data) |*scalar, i| {
3999 const of_math_result = try shlWithOverflowScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), ty.scalarType(), allocator, target);
4000 overflowed_data[i] = of_math_result.overflowed;
4001 scalar.* = of_math_result.wrapped_result;
4002 }
4003 return OverflowArithmeticResult{
4004 .overflowed = try Value.Tag.aggregate.create(allocator, overflowed_data),
4005 .wrapped_result = try Value.Tag.aggregate.create(allocator, result_data),
4006 };
4007 }
4008 return shlWithOverflowScalar(lhs, rhs, ty, allocator, target);
4009 }
4010
4011 pub fn shlWithOverflowScalar(
4012 lhs: Value,
4013 rhs: Value,
4014 ty: Type,
4015 allocator: Allocator,
4016 target: Target,
39244017 ) !OverflowArithmeticResult {
39254018 const info = ty.intInfo(target);
39264019 var lhs_space: Value.BigIntSpace = undefined;
......@@ -3941,7 +4034,7 @@ pub const Value = extern union {
39414034 result_bigint.truncate(result_bigint.toConst(), info.signedness, info.bits);
39424035 }
39434036 return OverflowArithmeticResult{
3944 .overflowed = overflowed,
4037 .overflowed = makeBool(overflowed),
39454038 .wrapped_result = try fromBigInt(allocator, result_bigint.toConst()),
39464039 };
39474040 }