Skip to content

Commit b428fd6

Browse files
author
Ash Pook
committed
Simplifiction. Comments.
1 parent fe00f9d commit b428fd6

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

source/abacus/gen/main/Fixed.X.t4

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
<#= ftd.RawTypeName #> s = f.numerator >> (<#= ftd.BitCount #> - 1); // sign of argument
160160
result.numerator = -f.numerator;
161161
<#= ftd.RawTypeName #> sr = result.numerator >> (<#= ftd.BitCount #> - 1); // sign of result
162+
// Branchless saturation - the only input that can overflow is MinValue
163+
// as there is no +ve equivalent, in this case saturate to MaxValue.
162164
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & <#= ftd.RawTypeName #>.MaxValue);
163165
}
164166

@@ -214,21 +216,21 @@
214216
}
215217

216218
public static void Abs (ref <#= ftd.TypeName #> f, out <#= ftd.TypeName #> result) {
217-
// Based on this: https://www.chessprogramming.org/Avoiding_Branches
219+
// Based on: https://www.chessprogramming.org/Avoiding_Branches
218220
//int abs(int a) {
219221
// int s = a >> 31; // cdq, signed shift, -1 if negative, else 0
220222
// a ^= s; // ones' complement if negative
221223
// a -= s; // plus one if negative -> two's complement if negative
222224
// return a;
223225
//}
224-
<#= ftd.RawTypeName #> temp = f.numerator;
225-
<#= ftd.RawTypeName #> s = temp >> (<#= ftd.BitCount #> - 1); // sign of argument
226-
temp ^= s;
227-
temp -= s;
228-
<#= ftd.RawTypeName #> sr = temp >> (<#= ftd.BitCount #> - 1); // sign of result
226+
result.numerator = f.numerator;
227+
<#= ftd.RawTypeName #> s = result.numerator >> (<#= ftd.BitCount #> - 1); // sign of argument
228+
result.numerator ^= s;
229+
result.numerator -= s;
230+
<#= ftd.RawTypeName #> sr = result.numerator >> (<#= ftd.BitCount #> - 1); // sign of result
229231
// Branchless saturation - the only input that can overflow is MinValue
230232
// as there is no +ve equivalent, in this case saturate to MaxValue.
231-
result.numerator = (temp & ~(sr & s)) | ((sr & s) & <#= ftd.RawTypeName #>.MaxValue);
233+
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & <#= ftd.RawTypeName #>.MaxValue);
232234
}
233235

234236
public static void Sin (ref <#= ftd.TypeName #> f, out <#= ftd.TypeName #> result) {

source/abacus/src/main/Abacus.Fixed32.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public static void Negate (ref Fixed32 f, out Fixed32 result) {
213213
Int32 s = f.numerator >> (32 - 1); // sign of argument
214214
result.numerator = -f.numerator;
215215
Int32 sr = result.numerator >> (32 - 1); // sign of result
216+
// Branchless saturation - the only input that can overflow is MinValue
217+
// as there is no +ve equivalent, in this case saturate to MaxValue.
216218
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & Int32.MaxValue);
217219
}
218220

@@ -268,21 +270,21 @@ public static void Sqrt (ref Fixed32 f, out Fixed32 result) {
268270
}
269271

270272
public static void Abs (ref Fixed32 f, out Fixed32 result) {
271-
// Based on this: https://www.chessprogramming.org/Avoiding_Branches
273+
// Based on: https://www.chessprogramming.org/Avoiding_Branches
272274
//int abs(int a) {
273275
// int s = a >> 31; // cdq, signed shift, -1 if negative, else 0
274276
// a ^= s; // ones' complement if negative
275277
// a -= s; // plus one if negative -> two's complement if negative
276278
// return a;
277279
//}
278-
Int32 temp = f.numerator;
279-
Int32 s = temp >> (32 - 1); // sign of argument
280-
temp ^= s;
281-
temp -= s;
282-
Int32 sr = temp >> (32 - 1); // sign of result
280+
result.numerator = f.numerator;
281+
Int32 s = result.numerator >> (32 - 1); // sign of argument
282+
result.numerator ^= s;
283+
result.numerator -= s;
284+
Int32 sr = result.numerator >> (32 - 1); // sign of result
283285
// Branchless saturation - the only input that can overflow is MinValue
284286
// as there is no +ve equivalent, in this case saturate to MaxValue.
285-
result.numerator = (temp & ~(sr & s)) | ((sr & s) & Int32.MaxValue);
287+
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & Int32.MaxValue);
286288
}
287289

288290
public static void Sin (ref Fixed32 f, out Fixed32 result) {

source/abacus/src/main/Abacus.Fixed64.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ public static void Negate (ref Fixed64 f, out Fixed64 result) {
214214
Int64 s = f.numerator >> (64 - 1); // sign of argument
215215
result.numerator = -f.numerator;
216216
Int64 sr = result.numerator >> (64 - 1); // sign of result
217+
// Branchless saturation - the only input that can overflow is MinValue
218+
// as there is no +ve equivalent, in this case saturate to MaxValue.
217219
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & Int64.MaxValue);
218220
}
219221

@@ -269,21 +271,21 @@ public static void Sqrt (ref Fixed64 f, out Fixed64 result) {
269271
}
270272

271273
public static void Abs (ref Fixed64 f, out Fixed64 result) {
272-
// Based on this: https://www.chessprogramming.org/Avoiding_Branches
274+
// Based on: https://www.chessprogramming.org/Avoiding_Branches
273275
//int abs(int a) {
274276
// int s = a >> 31; // cdq, signed shift, -1 if negative, else 0
275277
// a ^= s; // ones' complement if negative
276278
// a -= s; // plus one if negative -> two's complement if negative
277279
// return a;
278280
//}
279-
Int64 temp = f.numerator;
280-
Int64 s = temp >> (64 - 1); // sign of argument
281-
temp ^= s;
282-
temp -= s;
283-
Int64 sr = temp >> (64 - 1); // sign of result
281+
result.numerator = f.numerator;
282+
Int64 s = result.numerator >> (64 - 1); // sign of argument
283+
result.numerator ^= s;
284+
result.numerator -= s;
285+
Int64 sr = result.numerator >> (64 - 1); // sign of result
284286
// Branchless saturation - the only input that can overflow is MinValue
285287
// as there is no +ve equivalent, in this case saturate to MaxValue.
286-
result.numerator = (temp & ~(sr & s)) | ((sr & s) & Int64.MaxValue);
288+
result.numerator = (result.numerator & ~(sr & s)) | ((sr & s) & Int64.MaxValue);
287289
}
288290

289291
public static void Sin (ref Fixed64 f, out Fixed64 result) {

0 commit comments

Comments
 (0)