aboutsummaryrefslogtreecommitdiff
path: root/Saturation/module.jai
blob: f36dc56120c4d894b75ea39324e9f6f936bbf791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Integer saturating arighmetic (with assembly branch-free procedures for x64 - expecting signed values in two's complement).

#module_parameters(PREFER_BRANCH_FREE_CODE := false);

#import "Basic";
#import "Math";
#import "String";


INTEGER_ARITHMETIC_TYPES_CHECK :: #string DONE
    type_info_x := cast(*Type_Info)Tx;
    type_info_y := cast(*Type_Info)Ty;
    if type_info_x.type != .INTEGER || type_info_y.type != .INTEGER return false, "Non integers values passed.";
    tx := cast(*Type_Info_Integer)type_info_x;
    ty := cast(*Type_Info_Integer)type_info_y;

    largest_type :=
        ifx tx.runtime_size > ty.runtime_size then Tx else
        ifx ty.runtime_size > tx.runtime_size then Ty else
        ifx tx.signed == ty.signed then Tx else
        void;

    // Only allow to add different signedness values if largest type is the signed one (as in JAI).
    if tx.signed == ty.signed {
        Tx = largest_type;
        Ty = largest_type;
        Tr = largest_type;
    }
    else if tx.signed && Tx == largest_type {
        Ty = largest_type;
        Tr = largest_type;
    }
    else if ty.signed && Ty == largest_type {
        Tx = largest_type;
        Tr = largest_type;
    }
    else return false, "Number signedness mismatch.";

    return true;
DONE

add :: (x: $Tx, y: $Ty, $prefer_branch_free_code := PREFER_BRANCH_FREE_CODE) -> result: $Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; }
{

    #if !(prefer_branch_free_code && CPU == .X64) {
    
        #if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
            
            #if Tr ==  s8 { MAX ::  S8_MAX; MIN ::  S8_MIN; }
            #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
            #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
            #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
            
            if (y > 0 && x > MAX - y) then return MAX, true;
            if (y < 0 && x < MIN - y) then return MIN, true;
            
        } else {
            
            #if Tr ==  u8 { MAX ::  U8_MAX; }
            #if Tr == u16 { MAX :: U16_MAX; }
            #if Tr == u32 { MAX :: U32_MAX; }
            #if Tr == u64 { MAX :: U64_MAX; }
            
            if (x > MAX - y) then return MAX, true;
            
        }
        
        return x + y, false;
        
    } else {
    
        result: Tr = ---;
        saturated: bool = ---;
        
        SIGNED      :: #run (cast(*Type_Info_Integer)Tr).signed;
        SIZE        :: size_of(Tr) * 8;
        SIGN_BIT    :: SIZE-1;
        
        #if SIGNED #asm {
            mov         result, -1;             // Pre-set result with signed maximum (set all bits...
            shr?SIZE    result, 1;              // ...then, clear MSB).
            bt          x, SIGN_BIT;            // Test sign bit (affect CF).
            adc         result, 0;              // Overflow signed maximum to signed minimum if CF is set.
            
            add?SIZE    x, y;                   // Add values (affect OF).
            seto        saturated;              // Set saturated flag if OF.
            cmovno      result, x;              // Move add-result to result if NOT OF.
        } else #asm {
            mov         result, -1;             // Pre-set result with unsigned maximum.
            add?SIZE    x, y;                   // Add values (affect CF).
            setc        saturated;              // Set saturated flag if CF.
            cmovnc      result, x;              // Move add-result to result if NOT CF.
        }
        
        return result, saturated;
        
    }
}

sub :: (x: $Tx, y: $Ty, $prefer_branch_free_code := PREFER_BRANCH_FREE_CODE) -> result: $Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; }
{
    
    #if !(prefer_branch_free_code && CPU == .X64) {
        
        #if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
            
            #if Tr ==  s8 { MAX ::  S8_MAX; MIN ::  S8_MIN; }
            #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
            #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
            #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
            
            if (y < 0 && x > MAX + y) then return MAX, true;
            if (y > 0 && x < MIN + y) then return MIN, true;
            
        } else {
            
            if (y > x) then return 0, true;
            
        }
        
        return x - y, false;
        
    } else {
        
        result: Tr = ---;
        saturated: bool = ---;
        
        SIGNED      :: #run (cast(*Type_Info_Integer)Tr).signed;
        SIZE        :: size_of(Tr) * 8;
        SIGN_BIT    :: SIZE-1;
        
        #if SIGNED #asm {
            mov         result, -1;             // Pre-set result with signed maximum (set all bits...
            shr?SIZE    result, 1;              // ...then, clear MSB).
            bt          x, SIGN_BIT;            // Test signal bit (affect CF).
            adc         result, 0;              // Overflow signed maximum to signed minimum if CF is set.
            
            sub?SIZE    x, y;                   // Subtract values (affect OF).
            seto        saturated;              // Set saturated flag if OF.
            cmovno      result, x;              // Move subtract-result to result if NOT OF.
        } else #asm {
            xor         result, result;         // Pre-set result with usigned minimum (zero).
            sub?SIZE    x, y;                   // Subtract values (affect CF).
            setc        saturated;              // Set saturated flag if CF.
            cmovnc      result, x;              // Move subtract-result to result if NOT CF.
        }
        
        return result, saturated;
        
    }
}

mul :: (x: $Tx, y: $Ty, $prefer_branch_free_code := PREFER_BRANCH_FREE_CODE) -> result: $Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; }
{
    
    #if !(prefer_branch_free_code && CPU == .X64) {
        
        #if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
            
            #if Tr ==  s8 { MAX ::  S8_MAX; MIN ::  S8_MIN; }
            #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
            #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
            #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
            
            if x == 0 || y == 0 then return 0, false;
            if x > 0 && y > 0 && x > MAX / y then return MAX, true;
            if x < 0 && y < 0 && x < MAX / y then return MAX, true;
            if (y < 0 && x > 0 && y < MIN / x) || (x < 0 && y > 0 && x < MIN / y) then return MIN, true;
            
        } else {
            
            #if Tr ==  u8 { MAX ::  U8_MAX; }
            #if Tr == u16 { MAX :: U16_MAX; }
            #if Tr == u32 { MAX :: U32_MAX; }
            #if Tr == u64 { MAX :: U64_MAX; }
            
            if x == 0 || y == 0 then return 0, false;
            if x > MAX / y then return MAX, true;
            
        }
        
        return x * y, false;
        
    } else {
        
        result: Tr = ---;
        saturated: bool = ---;
        
        SIGNED      :: #run (cast(*Type_Info_Integer)Tr).signed;
        SIZE        :: size_of(Tr) * 8;
        SIGN_BIT    :: SIZE-1;
        
        #if SIGNED #asm {
            // Using two copies of the x value (x_, sign) seems to be a bit faster (not sure why).
            mov         x_: gpr === a, x;       // Pin copy of x value to register A.
            
            mov         result, -1;             // Pre-set result with signed maximum (set all bits...
            shr?SIZE    result, 1;              // ...then, clear MSB).
            mov         sign:, x;               // Use copy of x value.
            xor         sign, y;                // Calculate result signal bit using xor.
            bt          sign, SIGN_BIT;         // Test signal bit (affect CF).
            adc         result, 0;              // Overflow signed maximum to signed minimum if CF is set.
            
            imul?SIZE   x_, y;                  // Multiply values (affect OF).
            seto        saturated;              // Set saturated flag if OF.
            cmovno      result, x_;             // Move multiply-result to result if NOT OF.
        
        } else #if SIZE == 8 #asm {
            result === a;                       // Pin result to register A.

            mov         result, x;              // Move value x to result.
            // For 8bits mul, we do not need D register (as in the else below).
            mul?SIZE    result, y;      // Multiply values (affect CF).
            setc        saturated;              // Set saturated flag if CF.
            sbb         mask:, mask;            // If CF: mask = -1 (all bits set); else: mask = 0.
            or          result, mask;           // If CF was set, then result will be set to unsigned maximum (all bits set).
        
        } else #asm {
            result === a;                       // Pin result to register A.
            
            mov         result, x;              // Move value x to result.
            mul?SIZE    reg_d:, result, y;      // Multiply values (affect CF).
            setc        saturated;              // Set saturated flag if CF.
            sbb         mask:, mask;            // If CF: mask = -1 (all bits set); else: mask = 0.
            or          result, mask;           // If CF was set, then result will be set to unsigned maximum (all bits set).
        }
        
        return result, saturated;
        
    }
}

div :: (x: $Tx, y: $Ty, $prefer_branch_free_code := PREFER_BRANCH_FREE_CODE) -> result: $Tr, remainder: Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; }
{
    
    #if !(prefer_branch_free_code && CPU == .X64) {
        
        #if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
            
            #if Tr ==  s8 { MAX ::  S8_MAX; MIN ::  S8_MIN; }
            #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
            #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
            #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
            
            if x == MIN && y == -1 then return MAX, -1, true;
            
        }
        
        result := x / y;
        remainder := x - (y * result);
        return result, remainder, false;
        
    } else {
        
        result: Tr = ---;
        remainder: Tr = ---;
        saturated: bool = ---;

        SIZE        :: size_of(Tr) * 8;
        
        DIV_SIGNED_ASM :: #string DONE
        #asm {
            result === a;                       // Pin result to register A (to be used as dividend on idiv).
            remainder === d;                    // Pin remainder to register D.
            
            xor         saturated, saturated;   // Clear saturated.
            
            // Detect div(MIN/-1) and flag it on ZF.
            mov         t_dividend:, -1;        // Pre-set t_dividend with signed minimum (set all bits...
            shr?SIZE    t_dividend, 1;          // ...then, clear MSB...
            not         t_dividend;             // ...then, negate to obtain MSB set and all other bits cleared).
            //
            mov         limit:, t_dividend;     // Keep copy of signed minimum on limit.
            add         limit, 1;               // Set limit as signed minimum + 1.
            //
            xor?SIZE    t_dividend, x;          // Clear dividend if x value is equal to signed minimum.
            //
            mov         t_divisor:, -1;         // Pre-set test_divisor with -1.
            xor?SIZE    t_divisor, y;           // Clear test_divisor if y value is equal to -1.
            //
            or?SIZE     t_dividend, t_divisor;  // Or t_dividend with t_divisor (affect ZF).
            
            setz        saturated;              // Set saturated flag if ZF.
            mov         result, x;              // Copy x value to result (dividend).
            cmovz       result, limit;          // If ZF: copy limit (signed minimum + 1) to result (dividend).
            
            DIVIDE_PLACEHOLDER
            
            sub?SIZE    remainder, saturated;   // If saturated: remainder = 0 - 1; otherwise: remainder = x - 0.
        }
        DONE

        DIV_SIGNED_CALC_8BITS :: #string DONE
            cbw         result;                 // Prepare dividend high bits (sign-extend).
            idiv?SIZE   result, y;              // Divide values.
            mov         remainder, result;      // Extract remainder from result's high bits.
            sar         remainder, 8;           // Shift remainder from high to low bits.
        DONE

        DIV_SIGNED_CALC_16BITS :: #string DONE
            cwd         remainder, result;      // Prepare dividend high bits (sign-extend).
            idiv?SIZE   remainder, result, y;   // Divide values.
        DONE

        DIV_SIGNED_CALC_32BITS :: #string DONE
            cdq         remainder, result;      // Prepare dividend high bits (sign-extend).
            idiv?SIZE   remainder, result, y;   // Divide values.
        DONE

        DIV_SIGNED_CALC_64BITS :: #string DONE
            cqo         remainder, result;      // Prepare dividend high bits (sign-extend).
            idiv?SIZE   remainder, result, y;   // Divide values.
        DONE
        
        #if Tr == s8
            #insert #run replace(DIV_SIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_SIGNED_CALC_8BITS);
        #if Tr == s16
            #insert #run replace(DIV_SIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_SIGNED_CALC_16BITS);
        #if Tr == s32
            #insert #run replace(DIV_SIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_SIGNED_CALC_32BITS);
        #if Tr == s64
            #insert #run replace(DIV_SIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_SIGNED_CALC_64BITS);
        
        
        DIV_UNSIGNED_ASM :: #string DONE
        #asm {
            result === a;                       // Pin result to register A.
            remainder === d;                    // Pin remainder to register D.

            xor         result, result;         // Clear result.
            xor         remainder, remainder;   // Clear remainder (required when used as dividend's high bits).
            xor         saturated, saturated;   // Clear saturated (unsigned division never saturates).
            mov?SIZE    result, x;              // Copy x value to result.

            DIVIDE_PLACEHOLDER
        }
        DONE

        DIV_UNSIGNED_CALC_8BITS :: #string DONE
            div?SIZE    result, y;              // Divide values.
            mov         remainder, result;      // Extract remainder from result's high bits.
            sar         remainder, 8;           // Shift remainder from high to low bits.
        DONE
        
        DIV_UNSIGNED_CALC :: #string DONE
            div?SIZE    remainder, result, y;   // Divide values.
        DONE
        
        #if Tr == u8
            #insert #run replace(DIV_UNSIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_UNSIGNED_CALC_8BITS);
        #if Tr == u16
            #insert #run replace(DIV_UNSIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_UNSIGNED_CALC);
        #if Tr == u32
            #insert #run replace(DIV_UNSIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_UNSIGNED_CALC);
        #if Tr == u64
            #insert #run replace(DIV_UNSIGNED_ASM, "DIVIDE_PLACEHOLDER", DIV_UNSIGNED_CALC);
        
        
        return result, remainder, saturated;
        
    }
}