Annotate

fallbacks/bit.lua @ 9865:37278b420c74

util.serialization: Use util.hex
author Kim Alvefur <zash@zash.se>
date Sun, 17 Mar 2019 21:25:33 +0100
parent 7499:b4f781ed7045
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1137
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 2923
diff changeset
4 --
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1137
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1137
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1137
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1137
diff changeset
8
1137
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local type = type;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local tonumber = tonumber;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local setmetatable = setmetatable;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local error = error;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local tostring = tostring;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local print = print;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local xor_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=0;[18]=3;[19]=2;[20]=5;[21]=4;[22]=7;[23]=6;[24]=9;[25]=8;[26]=11;[27]=10;[28]=13;[29]=12;[30]=15;[31]=14;[32]=2;[33]=3;[34]=0;[35]=1;[36]=6;[37]=7;[38]=4;[39]=5;[40]=10;[41]=11;[42]=8;[43]=9;[44]=14;[45]=15;[46]=12;[47]=13;[48]=3;[49]=2;[50]=1;[51]=0;[52]=7;[53]=6;[54]=5;[55]=4;[56]=11;[57]=10;[58]=9;[59]=8;[60]=15;[61]=14;[62]=13;[63]=12;[64]=4;[65]=5;[66]=6;[67]=7;[68]=0;[69]=1;[70]=2;[71]=3;[72]=12;[73]=13;[74]=14;[75]=15;[76]=8;[77]=9;[78]=10;[79]=11;[80]=5;[81]=4;[82]=7;[83]=6;[84]=1;[85]=0;[86]=3;[87]=2;[88]=13;[89]=12;[90]=15;[91]=14;[92]=9;[93]=8;[94]=11;[95]=10;[96]=6;[97]=7;[98]=4;[99]=5;[100]=2;[101]=3;[102]=0;[103]=1;[104]=14;[105]=15;[106]=12;[107]=13;[108]=10;[109]=11;[110]=8;[111]=9;[112]=7;[113]=6;[114]=5;[115]=4;[116]=3;[117]=2;[118]=1;[119]=0;[120]=15;[121]=14;[122]=13;[123]=12;[124]=11;[125]=10;[126]=9;[127]=8;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=0;[137]=1;[138]=2;[139]=3;[140]=4;[141]=5;[142]=6;[143]=7;[144]=9;[145]=8;[146]=11;[147]=10;[148]=13;[149]=12;[150]=15;[151]=14;[152]=1;[153]=0;[154]=3;[155]=2;[156]=5;[157]=4;[158]=7;[159]=6;[160]=10;[161]=11;[162]=8;[163]=9;[164]=14;[165]=15;[166]=12;[167]=13;[168]=2;[169]=3;[170]=0;[171]=1;[172]=6;[173]=7;[174]=4;[175]=5;[176]=11;[177]=10;[178]=9;[179]=8;[180]=15;[181]=14;[182]=13;[183]=12;[184]=3;[185]=2;[186]=1;[187]=0;[188]=7;[189]=6;[190]=5;[191]=4;[192]=12;[193]=13;[194]=14;[195]=15;[196]=8;[197]=9;[198]=10;[199]=11;[200]=4;[201]=5;[202]=6;[203]=7;[204]=0;[205]=1;[206]=2;[207]=3;[208]=13;[209]=12;[210]=15;[211]=14;[212]=9;[213]=8;[214]=11;[215]=10;[216]=5;[217]=4;[218]=7;[219]=6;[220]=1;[221]=0;[222]=3;[223]=2;[224]=14;[225]=15;[226]=12;[227]=13;[228]=10;[229]=11;[230]=8;[231]=9;[232]=6;[233]=7;[234]=4;[235]=5;[236]=2;[237]=3;[238]=0;[239]=1;[240]=15;[241]=14;[242]=13;[243]=12;[244]=11;[245]=10;[246]=9;[247]=8;[248]=7;[249]=6;[250]=5;[251]=4;[252]=3;[253]=2;[254]=1;[255]=0;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local or_map = {[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=1;[18]=3;[19]=3;[20]=5;[21]=5;[22]=7;[23]=7;[24]=9;[25]=9;[26]=11;[27]=11;[28]=13;[29]=13;[30]=15;[31]=15;[32]=2;[33]=3;[34]=2;[35]=3;[36]=6;[37]=7;[38]=6;[39]=7;[40]=10;[41]=11;[42]=10;[43]=11;[44]=14;[45]=15;[46]=14;[47]=15;[48]=3;[49]=3;[50]=3;[51]=3;[52]=7;[53]=7;[54]=7;[55]=7;[56]=11;[57]=11;[58]=11;[59]=11;[60]=15;[61]=15;[62]=15;[63]=15;[64]=4;[65]=5;[66]=6;[67]=7;[68]=4;[69]=5;[70]=6;[71]=7;[72]=12;[73]=13;[74]=14;[75]=15;[76]=12;[77]=13;[78]=14;[79]=15;[80]=5;[81]=5;[82]=7;[83]=7;[84]=5;[85]=5;[86]=7;[87]=7;[88]=13;[89]=13;[90]=15;[91]=15;[92]=13;[93]=13;[94]=15;[95]=15;[96]=6;[97]=7;[98]=6;[99]=7;[100]=6;[101]=7;[102]=6;[103]=7;[104]=14;[105]=15;[106]=14;[107]=15;[108]=14;[109]=15;[110]=14;[111]=15;[112]=7;[113]=7;[114]=7;[115]=7;[116]=7;[117]=7;[118]=7;[119]=7;[120]=15;[121]=15;[122]=15;[123]=15;[124]=15;[125]=15;[126]=15;[127]=15;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=8;[137]=9;[138]=10;[139]=11;[140]=12;[141]=13;[142]=14;[143]=15;[144]=9;[145]=9;[146]=11;[147]=11;[148]=13;[149]=13;[150]=15;[151]=15;[152]=9;[153]=9;[154]=11;[155]=11;[156]=13;[157]=13;[158]=15;[159]=15;[160]=10;[161]=11;[162]=10;[163]=11;[164]=14;[165]=15;[166]=14;[167]=15;[168]=10;[169]=11;[170]=10;[171]=11;[172]=14;[173]=15;[174]=14;[175]=15;[176]=11;[177]=11;[178]=11;[179]=11;[180]=15;[181]=15;[182]=15;[183]=15;[184]=11;[185]=11;[186]=11;[187]=11;[188]=15;[189]=15;[190]=15;[191]=15;[192]=12;[193]=13;[194]=14;[195]=15;[196]=12;[197]=13;[198]=14;[199]=15;[200]=12;[201]=13;[202]=14;[203]=15;[204]=12;[205]=13;[206]=14;[207]=15;[208]=13;[209]=13;[210]=15;[211]=15;[212]=13;[213]=13;[214]=15;[215]=15;[216]=13;[217]=13;[218]=15;[219]=15;[220]=13;[221]=13;[222]=15;[223]=15;[224]=14;[225]=15;[226]=14;[227]=15;[228]=14;[229]=15;[230]=14;[231]=15;[232]=14;[233]=15;[234]=14;[235]=15;[236]=14;[237]=15;[238]=14;[239]=15;[240]=15;[241]=15;[242]=15;[243]=15;[244]=15;[245]=15;[246]=15;[247]=15;[248]=15;[249]=15;[250]=15;[251]=15;[252]=15;[253]=15;[254]=15;[255]=15;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local and_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=0;[9]=0;[10]=0;[11]=0;[12]=0;[13]=0;[14]=0;[15]=0;[16]=0;[17]=1;[18]=0;[19]=1;[20]=0;[21]=1;[22]=0;[23]=1;[24]=0;[25]=1;[26]=0;[27]=1;[28]=0;[29]=1;[30]=0;[31]=1;[32]=0;[33]=0;[34]=2;[35]=2;[36]=0;[37]=0;[38]=2;[39]=2;[40]=0;[41]=0;[42]=2;[43]=2;[44]=0;[45]=0;[46]=2;[47]=2;[48]=0;[49]=1;[50]=2;[51]=3;[52]=0;[53]=1;[54]=2;[55]=3;[56]=0;[57]=1;[58]=2;[59]=3;[60]=0;[61]=1;[62]=2;[63]=3;[64]=0;[65]=0;[66]=0;[67]=0;[68]=4;[69]=4;[70]=4;[71]=4;[72]=0;[73]=0;[74]=0;[75]=0;[76]=4;[77]=4;[78]=4;[79]=4;[80]=0;[81]=1;[82]=0;[83]=1;[84]=4;[85]=5;[86]=4;[87]=5;[88]=0;[89]=1;[90]=0;[91]=1;[92]=4;[93]=5;[94]=4;[95]=5;[96]=0;[97]=0;[98]=2;[99]=2;[100]=4;[101]=4;[102]=6;[103]=6;[104]=0;[105]=0;[106]=2;[107]=2;[108]=4;[109]=4;[110]=6;[111]=6;[112]=0;[113]=1;[114]=2;[115]=3;[116]=4;[117]=5;[118]=6;[119]=7;[120]=0;[121]=1;[122]=2;[123]=3;[124]=4;[125]=5;[126]=6;[127]=7;[128]=0;[129]=0;[130]=0;[131]=0;[132]=0;[133]=0;[134]=0;[135]=0;[136]=8;[137]=8;[138]=8;[139]=8;[140]=8;[141]=8;[142]=8;[143]=8;[144]=0;[145]=1;[146]=0;[147]=1;[148]=0;[149]=1;[150]=0;[151]=1;[152]=8;[153]=9;[154]=8;[155]=9;[156]=8;[157]=9;[158]=8;[159]=9;[160]=0;[161]=0;[162]=2;[163]=2;[164]=0;[165]=0;[166]=2;[167]=2;[168]=8;[169]=8;[170]=10;[171]=10;[172]=8;[173]=8;[174]=10;[175]=10;[176]=0;[177]=1;[178]=2;[179]=3;[180]=0;[181]=1;[182]=2;[183]=3;[184]=8;[185]=9;[186]=10;[187]=11;[188]=8;[189]=9;[190]=10;[191]=11;[192]=0;[193]=0;[194]=0;[195]=0;[196]=4;[197]=4;[198]=4;[199]=4;[200]=8;[201]=8;[202]=8;[203]=8;[204]=12;[205]=12;[206]=12;[207]=12;[208]=0;[209]=1;[210]=0;[211]=1;[212]=4;[213]=5;[214]=4;[215]=5;[216]=8;[217]=9;[218]=8;[219]=9;[220]=12;[221]=13;[222]=12;[223]=13;[224]=0;[225]=0;[226]=2;[227]=2;[228]=4;[229]=4;[230]=6;[231]=6;[232]=8;[233]=8;[234]=10;[235]=10;[236]=12;[237]=12;[238]=14;[239]=14;[240]=0;[241]=1;[242]=2;[243]=3;[244]=4;[245]=5;[246]=6;[247]=7;[248]=8;[249]=9;[250]=10;[251]=11;[252]=12;[253]=13;[254]=14;[255]=15;}
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local not_map = {[0]=15;[1]=14;[2]=13;[3]=12;[4]=11;[5]=10;[6]=9;[7]=8;[8]=7;[9]=6;[10]=5;[11]=4;[12]=3;[13]=2;[14]=1;[15]=0;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local rshift1_map = {[0]=0;[1]=0;[2]=1;[3]=1;[4]=2;[5]=2;[6]=3;[7]=3;[8]=4;[9]=4;[10]=5;[11]=5;[12]=6;[13]=6;[14]=7;[15]=7;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local rshift1carry_map = {[0]=0;[1]=8;[2]=0;[3]=8;[4]=0;[5]=8;[6]=0;[7]=8;[8]=0;[9]=8;[10]=0;[11]=8;[12]=0;[13]=8;[14]=0;[15]=8;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local lshift1_map = {[0]=0;[1]=2;[2]=4;[3]=6;[4]=8;[5]=10;[6]=12;[7]=14;[8]=0;[9]=2;[10]=4;[11]=6;[12]=8;[13]=10;[14]=12;[15]=14;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local lshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=1;[9]=1;[10]=1;[11]=1;[12]=1;[13]=1;[14]=1;[15]=1;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local arshift1carry_map = {[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=8;[9]=8;[10]=8;[11]=8;[12]=8;[13]=8;[14]=8;[15]=8;};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 module "bit"
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local bit_mt = {__tostring = function(t) return ("%x%x%x%x%x%x%x%x"):format(t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]); end};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 local function do_bop(a, b, op)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return setmetatable({
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 op[a[1]*16+b[1]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 op[a[2]*16+b[2]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 op[a[3]*16+b[3]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 op[a[4]*16+b[4]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 op[a[5]*16+b[5]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 op[a[6]*16+b[6]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 op[a[7]*16+b[7]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 op[a[8]*16+b[8]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 }, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local function do_uop(a, op)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 return setmetatable({
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 op[a[1]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 op[a[2]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 op[a[3]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 op[a[4]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 op[a[5]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 op[a[6]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 op[a[7]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 op[a[8]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 }, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 function bxor(a, b) return do_bop(a, b, xor_map); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 function bor(a, b) return do_bop(a, b, or_map); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 function band(a, b) return do_bop(a, b, and_map); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 function bnot(a) return do_uop(a, not_map); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 local function _rshift1(t)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 local carry = 0;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 for i=1,8 do
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local t_i = rshift1_map[t[i]] + carry;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 carry = rshift1carry_map[t[i]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 t[i] = t_i;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 function rshift(a, i)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
7499
b4f781ed7045 fallbacks.bit: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
71 for _ = 1, i do _rshift1(t); end
1137
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 return setmetatable(t, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 local function _arshift1(t)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 local carry = arshift1carry_map[t[1]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 for i=1,8 do
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 local t_i = rshift1_map[t[i]] + carry;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 carry = rshift1carry_map[t[i]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 t[i] = t_i;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 function arshift(a, i)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
7499
b4f781ed7045 fallbacks.bit: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
84 for _ = 1, i do _arshift1(t); end
1137
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 return setmetatable(t, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local function _lshift1(t)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local carry = 0;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 for i=8,1,-1 do
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 local t_i = lshift1_map[t[i]] + carry;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 carry = lshift1carry_map[t[i]];
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 t[i] = t_i;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 function lshift(a, i)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 local t = {a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]};
7499
b4f781ed7045 fallbacks.bit: remove unused one-letter loop variables [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
97 for _ = 1, i do _lshift1(t); end
1137
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 return setmetatable(t, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 local function _cast(a)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 if type(a) == "number" then a = ("%x"):format(a);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 elseif type(a) == "table" then return a;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 elseif type(a) ~= "string" then error("string expected, got "..type(a), 2); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 local t = {0,0,0,0,0,0,0,0};
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 a = "00000000"..a;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 a = a:sub(-8);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 for i = 1,8 do
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 t[i] = tonumber(a:sub(i,i), 16) or error("Number format error", 2);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 return setmetatable(t, bit_mt);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 local function wrap1(f)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 return function(a, ...)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if type(a) ~= "table" then a = _cast(a); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 a = f(a, ...);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 a = tonumber(tostring(a), 16);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 if a > 0x7fffffff then a = a - 1 - 0xffffffff; end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 return a;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 end;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 local function wrap2(f)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 return function(a, b, ...)
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 if type(a) ~= "table" then a = _cast(a); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 if type(b) ~= "table" then b = _cast(b); end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 a = f(a, b, ...);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 a = tonumber(tostring(a), 16);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 if a > 0x7fffffff then a = a - 1 - 0xffffffff; end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 return a;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 bxor = wrap2(bxor);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 bor = wrap2(bor);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 band = wrap2(band);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 bnot = wrap1(bnot);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 lshift = wrap1(lshift);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 rshift = wrap1(rshift);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 arshift = wrap1(arshift);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 cast = wrap1(_cast);
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 bits = 32;
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144
4a39a6d503d0 fallbacks/bit: bit manipulation API (compatible with bitlib for now)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 return _M;