aboutsummaryrefslogtreecommitdiff
path: root/Math_Ext.jai
blob: 368779f097f10fa798f1c999a69352d2102384df (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Integer saturating arighmetic (with branch-free procedures on x64).
// Expects signed values in two's complement.

#import "Basic";
#import "Compiler";
#import "Math";

is_signed :: ($t: Type) -> bool { return (cast(*Type_Info_Integer)type_info(t)).signed; }

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, $USE_GENERIC: bool = false) -> result: $Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } // #dump
{

    #if USE_GENERIC || CPU != .X64 {

        // #if #run is_signed(Tr) { // TODO Maybe use this?
        #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 {

        #import "String";
        result: Tr = ---;
        saturated: bool = ---;


        S_ADD_ASM :: #string DONE
        #asm {
            
            // Performance
            // s8    | s16   | s32   | s64
            // 1.243 | 1.242 | 1.215 | 1.210
            //
            mov         result, -1;             // Pre-set result with signed maximum: set all ones.
            shr.SIZE    result, 1;              // Pre-set result with signed maximum: insert zero on MSB.
            bt          x, SHIFT;               // Test signal 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 flat if OF.
            cmovno      result, x;              // Move add-result to result if NOT OF.
            
            // Performance - a bit of improvement... with some more code.
            // s8    | s16   | s32   | s64
            // 1.336 | 1.305 | 1.217 | 1.210
            //
            // mov         sign: gpr, x;               // Copy x value to sign variable.
            // mov         limit: gpr, MAX;            // Pre-set limit with signed maximum.
            // shr.SIZE    sign, SHIFT;                // Get sign of x value.
            // add.SIZE    limit, sign;                // If sign is 1, overflow from signed maximum to signed minimum.
            // 
            // mov         result, x;                  // Copy x value to result.
            // add.SIZE    result, y;                  // Add values (affect OF).
            // seto        saturated;                  // Set saturated flag if OF.
            // cmovo       result, limit;              // Move limit to result if OF.
        }
        DONE

        #if Tr == s8
            #insert #run replace(replace(S_ADD_ASM, ".SIZE", ".b"), "SHIFT", "7"); // , "MAX", "127")
        #if Tr == s16
            #insert #run replace(replace(S_ADD_ASM, ".SIZE", ".w"), "SHIFT", "15"); // , "MAX", "32767")
        #if Tr == s32
            #insert #run replace(replace(S_ADD_ASM, ".SIZE", ".d"), "SHIFT", "31"); // , "MAX", "2147483647")
        #if Tr == s64
            #insert #run replace(replace(S_ADD_ASM, ".SIZE", ".q"), "SHIFT", "63"); // , "MAX", "9223372036854775807")

        
        U_ADD_ASM :: #string DONE
        #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.
        }
        DONE

        #if Tr == u8
            #insert #run replace(U_ADD_ASM, ".SIZE", ".b");
        #if Tr == u16
            #insert #run replace(U_ADD_ASM, ".SIZE", ".w");
        #if Tr == u32
            #insert #run replace(U_ADD_ASM, ".SIZE", ".d");
        #if Tr == u64
            #insert #run replace(U_ADD_ASM, ".SIZE", ".q");


        return result, saturated;
        
    }
}

sub :: (x: $Tx, y: $Ty, $USE_GENERIC: bool = false) -> result: $Tr, overflow: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } // #dump
{

    #if USE_GENERIC || 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 {
        
        #import "String";
        result: Tr = ---;
        saturated: bool = ---;


        S_SUB_ASM :: #string DONE
        #asm {
            // Calculate limit based on x's sign.
            mov         limit: gpr, MAX;
            mov         sign: gpr, x;
            shr.SIZE    sign, BITS;
            add.SIZE    limit, sign;            // If sign is 1, then limit will overflow from MAX to MIN.

            mov         result, x;
            sub.SIZE    result, y;
            seto        saturated;
            cmovo       result, limit;
        }
        DONE

        #if Tr == s8
            #insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".b"), "MAX", "127"), "BITS", "7");
        #if Tr == s16
            #insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".w"), "MAX", "32767"), "BITS", "15");
        #if Tr == s32
            #insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".d"), "MAX", "2147483647"), "BITS", "31");
        #if Tr == s64
            #insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".q"), "MAX", "9223372036854775807"), "BITS", "63");
        
        
        U_SUB_ASM :: #string DONE
        #asm {
            mov         limit: gpr, 0;
            mov         result, x;
            sub.SIZE    result, y;
            setc        saturated;
            cmovc       result, limit;
        }
        DONE

        #if Tr == u8
            #insert #run replace(U_SUB_ASM, ".SIZE", ".b");
        #if Tr == u16
            #insert #run replace(U_SUB_ASM, ".SIZE", ".w");
        #if Tr == u32
            #insert #run replace(U_SUB_ASM, ".SIZE", ".d");
        #if Tr == u64
            #insert #run replace(U_SUB_ASM, ".SIZE", ".q");
        
        
        return result, saturated;

    }
    
}

mul :: (x: $Tx, y: $Ty, $USE_GENERIC: bool = false) -> result: $Tr, overflow: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } // #dump
{

    #if USE_GENERIC || CPU != .X64 {

        // #if #run is_signed(Tr) { // TODO Maybe use this?
        #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 {
        
        #import "String";
        result: Tr = ---;
        saturated: bool = ---;

        S_MUL_ASM :: #string DONE
        #asm {
            result === a;    // TODO Try changing to non-aregister to see if we're using the single-argument version of imul.
        
            // Calculate limit based on (x^y)'s sign.
            mov         limit: gpr, MAX;
            mov         sign: gpr, x;
            xor         sign, y;
            shr.SIZE    sign, BITS;
            add.SIZE    limit, sign;            // If sign is 1, then limit will overflow from MAX to MIN.

            mov         result, x;
            imul.SIZE   result, y;
            seto        saturated;
            cmovo       result, limit;
        }
        DONE

        #if Tr == s8
            #insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".b"), "MAX", "127"), "BITS", "7");
        #if Tr == s16
            #insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".w"), "MAX", "32767"), "BITS", "15");
        #if Tr == s32
            #insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".d"), "MAX", "2147483647"), "BITS", "31");
        #if Tr == s64
            #insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".q"), "MAX", "9223372036854775807"), "BITS", "63");


        U_MUL_ASM :: #string DONE
        #asm {
            result === a;
            
            mov         result, x;
            mul.SIZE    r_d:, result, y;      // TODO Try to use same as below (remove r_d)
            setc        saturated;
            sbb         limit:, limit;              // If CF: limit = -1 (all bits set); otherwise: limit = 0.
            or          result, limit;
        }
        DONE

        U_MUL_ASM_8BITS :: #string DONE
        #asm {
            result === a;
            
            mov         result, x;
            mul.SIZE    result, y;
            setc        saturated;
            sbb         limit:, limit;              // If CF: limit = -1 (all bits set); otherwise: limit = 0.
            or          result, limit;
        }
        DONE

        #if Tr == u8
            #insert #run replace(U_MUL_ASM_8BITS, ".SIZE", ".b");
        #if Tr == u16
            #insert #run replace(U_MUL_ASM, ".SIZE", ".w");
        #if Tr == u32
            #insert #run replace(U_MUL_ASM, ".SIZE", ".d");
        #if Tr == u64
            #insert #run replace(U_MUL_ASM, ".SIZE", ".q");
        
        
        return result, saturated;
        
    }
}

div :: (x: $Tx, y: $Ty, $USE_GENERIC: bool = false) -> result: $Tr, remainder: Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } //#dump
{

    #if USE_GENERIC || CPU != .X64 {

        // #if #run is_signed(Tr) { // TODO Maybe use this?
        #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 {
        
        #import "String";
        result: Tr = ---;
        remainder: Tr = ---;
        saturated: bool = ---;
        
        S_DIV_ASM :: #string DONE
        #asm {
            result === a;
            remainder === d;

            // Detect div(MIN/-1) and flag it on ZF.
            mov         xT: gpr, MIN;   // TODO Rename xT to x_test
            mov         xV: gpr, x;     // TODO Rename xV to x_val
            xor.SIZE    xT, xV;
            mov         yT: gpr, y;
            xor.SIZE    yT, -1;
            or.SIZE     xT, yT;

            mov         limit: gpr, LIMIT;
            mov         result, x;
            cmovz       result, limit;          // If ZF: limit dividend to MIN-1.
            mov.SIZE       saturated, 0;        // Clear register up to the size used on last operation "sub.SIZE""
            setz        saturated;
            SIGN_EXT    remainder, result;      // Prepare dividend high bits.
            idiv.SIZE   remainder, result, y;

            // If saturated: remainder = 0 - 1; otherwise: remainder = x - 0.
            sub.SIZE    remainder, saturated;
        }
        DONE

        S_DIV_ASM_8BITS :: #string DONE
        #asm {

            result === a;
            remainder === d;

            // Detect div(MIN/-1) and flag it on ZF.
            mov         t_x: gpr, x;
            mov         t_y: gpr, y;
            xor.SIZE    t_x, MIN;
            xor.SIZE    t_y, -1;
            or.SIZE     t_x, t_y;

            
            mov         limit: gpr, LIMIT;
            mov         result, x;
            cmovz       result, limit;          // If ZF: limit dividend to MIN-1.
            cbw         result;                 // Sign-extension.
            setz        saturated;
            idiv.SIZE   result, y;

            // Extract remainder from result's high bits.
            mov         remainder, result;
            sar         remainder, 8;

            // If saturated: remainder = 0 - 1; otherwise: remainder = x - 0.
            sub.SIZE    remainder, saturated;

        }
        DONE

        #if Tr == s8
            #insert #run replace(replace(replace(S_DIV_ASM_8BITS, ".SIZE", ".b"), "MIN", "-128"), "LIMIT", "-127");
        #if Tr == s16
            #insert #run replace(replace(replace(replace(S_DIV_ASM, ".SIZE", ".w"), "MIN", "-32768"), "LIMIT", "-32767"), "SIGN_EXT", "cwd");
        #if Tr == s32
            #insert #run replace(replace(replace(replace(S_DIV_ASM, ".SIZE", ".d"), "MIN", "-2147483648"), "LIMIT", "-2147483647"), "SIGN_EXT", "cdq");
        #if Tr == s64
            #insert #run replace(replace(replace(replace(S_DIV_ASM, ".SIZE", ".q"), "MIN", "-9223372036854775808"), "LIMIT", "-9223372036854775807"), "SIGN_EXT", "cqo");


        U_DIV_ASM :: #string DONE
        #asm {
            result === a;
            remainder === d;

            mov         saturated, 0;
            mov         result, x;
            mov         remainder, 0;           // Prepare dividend high bits.
            div.SIZE    remainder, result, y;
        }
        DONE

        U_DIV_ASM_8BITS :: #string DONE
        #asm {
            result === a;
            remainder === d;

            mov         saturated, 0;
            movzxbw     result, x;              // Move zero-extended byte to word.
            div.SIZE    result, y;

            // Extract remainder from result's high bits.
            mov         remainder, result;
            sar         remainder, 8;
        }
        DONE

        #if Tr == u8
            #insert #run replace(U_DIV_ASM_8BITS, ".SIZE", ".b");
        #if Tr == u16
            #insert #run replace(U_DIV_ASM, ".SIZE", ".w");
        #if Tr == u32
            #insert #run replace(U_DIV_ASM, ".SIZE", ".d");
        #if Tr == u64
            #insert #run replace(U_DIV_ASM, ".SIZE", ".q");

        
        return result, remainder, saturated;
        
    }
}