1function test151 2%TEST151 test bitwise operators 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('test151: test bitwise operators\n') ; 8 9[binops, ~, ~, types, ~, ~,] = GB_spec_opsall ; 10types = types.int ; 11ops2 = binops.int ; 12 13int_nbits = [ 8, 16, 32, 64, 8, 16, 32, 64 ] ; 14 15rng ('default') ; 16Cin = sparse (4,4) ; 17C10 = sparse (10,10) ; 18 19for k = 1:8 20 21 type = types {k} ; 22 nbits = int_nbits (k) ; 23 fprintf ('\n%s', type) ; 24 25 for trial = 1:40 26 fprintf ('.') ; 27 28 % dense case 29 30 imax = double (intmax (type) / 4) ; 31 A = GB_mex_cast (imax * rand (4), type) ; 32 B = GB_mex_cast ((nbits-1) * rand (4), type) + 1 ; 33 clear A2 ; A2.matrix = sparse (double (A)) ; A2.class = type ; 34 clear B2 ; B2.matrix = sparse (double (B)) ; B2.class = type ; 35 A2.pattern = logical (spones (A)) ; 36 B2.pattern = logical (spones (B)) ; 37 38 for j = 1:length (ops2) 39 opname = ops2 {j} ; 40 % C1 = bitop (A, B) ; 41 op.opname = opname ; op.optype = type ; 42 43 if (isequal (opname, 'bitshift') || isequal (opname, 'bshift')) 44 B2.class = 'int8' ; 45 else 46 B2.class = type ; 47 end 48 49 C1 = GB_spec_Matrix_eWiseMult(Cin, [ ], [ ], op, A2, B2, [ ]) ; 50 C2 = GB_mex_Matrix_eWiseMult (Cin, [ ], [ ], op, A2, B2, [ ]) ; 51 GB_spec_compare (C1, C2) ; 52 C1 = GB_spec_Matrix_eWiseAdd (Cin, [ ], [ ], op, A2, B2, [ ]) ; 53 C2 = GB_mex_Matrix_eWiseAdd (Cin, [ ], [ ], op, A2, B2, [ ]) ; 54 GB_spec_compare (C1, C2) ; 55 end 56 57 % C1 = bitcmp (A) ; 58 op.opname = 'bitnot' ; op.optype = type ; 59 C1 = GB_spec_apply(Cin, [ ], [ ], op, A2, [ ]) ; 60 C2 = GB_mex_apply (Cin, [ ], [ ], op, A2, [ ]) ; 61 GB_spec_compare (C1, C2) ; 62 63 % sparse case 64 65 A = sprand (10, 10, 0.5) * imax ; 66 Afull = GB_mex_cast (full (A), type) ; 67 % B ranges in value from 0 to 8 68 B = round (sprand (10, 10, 0.5) * nbits) ; 69 Bfull = GB_mex_cast (full (B), type) ; 70 clear A2 ; A2.matrix = sparse (double (Afull)) ; A2.class = type ; 71 clear B2 ; B2.matrix = sparse (double (Bfull)) ; B2.class = type ; 72 A2.pattern = logical (spones (Afull)) ; 73 B2.pattern = logical (spones (Bfull)) ; 74 75 for j = 1:length (ops2) 76 opname = ops2 {j} ; 77 % C1 = bitop (A, B) ; 78 op.opname = opname ; op.optype = type ; 79 80 if (isequal (opname, 'bitshift') || isequal (opname, 'bshift')) 81 B2.class = 'int8' ; 82 else 83 B2.class = type ; 84 end 85 86 C1 = GB_spec_Matrix_eWiseMult(C10, [ ], [ ], op, A2, B2, [ ]) ; 87 C2 = GB_mex_Matrix_eWiseMult (C10, [ ], [ ], op, A2, B2, [ ]) ; 88 GB_spec_compare (C1, C2) ; 89 C1 = GB_spec_Matrix_eWiseAdd (C10, [ ], [ ], op, A2, B2, [ ]) ; 90 C2 = GB_mex_Matrix_eWiseAdd (C10, [ ], [ ], op, A2, B2, [ ]) ; 91 GB_spec_compare (C1, C2) ; 92 end 93 94 % C1 = bitcmp (Afull) ; 95 op.opname = 'bitnot' ; op.optype = type ; 96 C1 = GB_spec_apply(C10, [ ], [ ], op, A2, [ ]) ; 97 C2 = GB_mex_apply (C10, [ ], [ ], op, A2, [ ]) ; 98 GB_spec_compare (C1, C2) ; 99 100 end 101end 102 103fprintf ('\ntest151: all tests passed\n') ; 104 105