http://my.oschina.net/yunnet/blog/365241
发现上次写的CRC16速度太慢,所以升级一下,
1、CRCccitt XModem版本
1%%%------------------------------------------------------------------- 2%%% @author <yunnet> 3%%% @copyright (C) 2015, 4%%% @doc CRCccitt XModem版本 5%%% CRC-CCITT (0xFFFF) 6%%% @end 7%%% Created : 15 Jan 2015 by <yunnet@gmail.com> 8%%%------------------------------------------------------------------- 9 10-module(crc16Ex). 11-export([crc16/1, test/0]). 12 13crc_index(0) -> 16#0000; 14crc_index(1) -> 16#1021; 15crc_index(2) -> 16#2042; 16crc_index(3) -> 16#3063; 17crc_index(4) -> 16#4084; 18crc_index(5) -> 16#50a5; 19crc_index(6) -> 16#60c6; 20crc_index(7) -> 16#70e7; 21 22crc_index(8) -> 16#8108; 23crc_index(9) -> 16#9129; 24crc_index(10) -> 16#a14a; 25crc_index(11) -> 16#b16b; 26crc_index(12) -> 16#c18c; 27crc_index(13) -> 16#d1ad; 28crc_index(14) -> 16#e1ce; 29crc_index(15) -> 16#f1ef; 30 31crc_index(16) -> 16#1231; 32crc_index(17) -> 16#0210; 33crc_index(18) -> 16#3273; 34crc_index(19) -> 16#2252; 35crc_index(20) -> 16#52b5; 36crc_index(21) -> 16#4294; 37crc_index(22) -> 16#72f7; 38crc_index(23) -> 16#62d6; 39 40crc_index(24) -> 16#9339; 41crc_index(25) -> 16#8318; 42crc_index(26) -> 16#b37b; 43crc_index(27) -> 16#a35a; 44crc_index(28) -> 16#d3bd; 45crc_index(29) -> 16#c39c; 46crc_index(30) -> 16#f3ff; 47crc_index(31) -> 16#e3de; 48 49crc_index(32) -> 16#2462; 50crc_index(33) -> 16#3443; 51crc_index(34) -> 16#0420; 52crc_index(35) -> 16#1401; 53crc_index(36) -> 16#64e6; 54crc_index(37) -> 16#74c7; 55crc_index(38) -> 16#44a4; 56crc_index(39) -> 16#5485; 57 58crc_index(40) -> 16#a56a; 59crc_index(41) -> 16#b54b; 60crc_index(42) -> 16#8528; 61crc_index(43) -> 16#9509; 62crc_index(44) -> 16#e5ee; 63crc_index(45) -> 16#f5cf; 64crc_index(46) -> 16#c5ac; 65crc_index(47) -> 16#d58d; 66 67crc_index(48) -> 16#3653; 68crc_index(49) -> 16#2672; 69crc_index(50) -> 16#1611; 70crc_index(51) -> 16#0630; 71crc_index(52) -> 16#76d7; 72crc_index(53) -> 16#66f6; 73crc_index(54) -> 16#5695; 74crc_index(55) -> 16#46b4; 75 76crc_index(56) -> 16#b75b; 77crc_index(57) -> 16#a77a; 78crc_index(58) -> 16#9719; 79crc_index(59) -> 16#8738; 80crc_index(60) -> 16#f7df; 81crc_index(61) -> 16#e7fe; 82crc_index(62) -> 16#d79d; 83crc_index(63) -> 16#c7bc; 84 85crc_index(64) -> 16#48c4; 86crc_index(65) -> 16#58e5; 87crc_index(66) -> 16#6886; 88crc_index(67) -> 16#78a7; 89crc_index(68) -> 16#0840; 90crc_index(69) -> 16#1861; 91crc_index(70) -> 16#2802; 92crc_index(71) -> 16#3823; 93 94crc_index(72) -> 16#c9cc; 95crc_index(73) -> 16#d9ed; 96crc_index(74) -> 16#e98e; 97crc_index(75) -> 16#f9af; 98crc_index(76) -> 16#8948; 99crc_index(77) -> 16#9969; 100crc_index(78) -> 16#a90a; 101crc_index(79) -> 16#b92b; 102 103crc_index(80) -> 16#5af5; 104crc_index(81) -> 16#4ad4; 105crc_index(82) -> 16#7ab7; 106crc_index(83) -> 16#6a96; 107crc_index(84) -> 16#1a71; 108crc_index(85) -> 16#0a50; 109crc_index(86) -> 16#3a33; 110crc_index(87) -> 16#2a12; 111 112crc_index(88) -> 16#dbfd; 113crc_index(89) -> 16#cbdc; 114crc_index(90) -> 16#fbbf; 115crc_index(91) -> 16#eb9e; 116crc_index(92) -> 16#9b79; 117crc_index(93) -> 16#8b58; 118crc_index(94) -> 16#bb3b; 119crc_index(95) -> 16#ab1a; 120 121crc_index(96) -> 16#6ca6; 122crc_index(97) -> 16#7c87; 123crc_index(98) -> 16#4ce4; 124crc_index(99) -> 16#5cc5; 125crc_index(100) -> 16#2c22; 126crc_index(101) -> 16#3c03; 127crc_index(102) -> 16#0c60; 128crc_index(103) -> 16#1c41; 129 130crc_index(104) -> 16#edae; 131crc_index(105) -> 16#fd8f; 132crc_index(106) -> 16#cdec; 133crc_index(107) -> 16#ddcd; 134crc_index(108) -> 16#ad2a; 135crc_index(109) -> 16#bd0b; 136crc_index(110) -> 16#8d68; 137crc_index(111) -> 16#9d49; 138 139crc_index(112) -> 16#7e97; 140crc_index(113) -> 16#6eb6; 141crc_index(114) -> 16#5ed5; 142crc_index(115) -> 16#4ef4; 143crc_index(116) -> 16#3e13; 144crc_index(117) -> 16#2e32; 145crc_index(118) -> 16#1e51; 146crc_index(119) -> 16#0e70; 147 148crc_index(120) -> 16#ff9f; 149crc_index(121) -> 16#efbe; 150crc_index(122) -> 16#dfdd; 151crc_index(123) -> 16#cffc; 152crc_index(124) -> 16#bf1b; 153crc_index(125) -> 16#af3a; 154crc_index(126) -> 16#9f59; 155crc_index(127) -> 16#8f78; 156 157crc_index(128) -> 16#9188; 158crc_index(129) -> 16#81a9; 159crc_index(130) -> 16#b1ca; 160crc_index(131) -> 16#a1eb; 161crc_index(132) -> 16#d10c; 162crc_index(133) -> 16#c12d; 163crc_index(134) -> 16#f14e; 164crc_index(135) -> 16#e16f; 165 166crc_index(136) -> 16#1080; 167crc_index(137) -> 16#00a1; 168crc_index(138) -> 16#30c2; 169crc_index(139) -> 16#20e3; 170crc_index(140) -> 16#5004; 171crc_index(141) -> 16#4025; 172crc_index(142) -> 16#7046; 173crc_index(143) -> 16#6067; 174 175crc_index(144) -> 16#83b9; 176crc_index(145) -> 16#9398; 177crc_index(146) -> 16#a3fb; 178crc_index(147) -> 16#b3da; 179crc_index(148) -> 16#c33d; 180crc_index(149) -> 16#d31c; 181crc_index(150) -> 16#e37f; 182crc_index(151) -> 16#f35e; 183 184crc_index(152) -> 16#02b1; 185crc_index(153) -> 16#1290; 186crc_index(154) -> 16#22f3; 187crc_index(155) -> 16#32d2; 188crc_index(156) -> 16#4235; 189crc_index(157) -> 16#5214; 190crc_index(158) -> 16#6277; 191crc_index(159) -> 16#7256; 192 193crc_index(160) -> 16#b5ea; 194crc_index(161) -> 16#a5cb; 195crc_index(162) -> 16#95a8; 196crc_index(163) -> 16#8589; 197crc_index(164) -> 16#f56e; 198crc_index(165) -> 16#e54f; 199crc_index(166) -> 16#d52c; 200crc_index(167) -> 16#c50d; 201 202crc_index(168) -> 16#34e2; 203crc_index(169) -> 16#24c3; 204crc_index(170) -> 16#14a0; 205crc_index(171) -> 16#0481; 206crc_index(172) -> 16#7466; 207crc_index(173) -> 16#6447; 208crc_index(174) -> 16#5424; 209crc_index(175) -> 16#4405; 210 211crc_index(176) -> 16#a7db; 212crc_index(177) -> 16#b7fa; 213crc_index(178) -> 16#8799; 214crc_index(179) -> 16#97b8; 215crc_index(180) -> 16#e75f; 216crc_index(181) -> 16#f77e; 217crc_index(182) -> 16#c71d; 218crc_index(183) -> 16#d73c; 219 220crc_index(184) -> 16#26d3; 221crc_index(185) -> 16#36f2; 222crc_index(186) -> 16#0691; 223crc_index(187) -> 16#16b0; 224crc_index(188) -> 16#6657; 225crc_index(189) -> 16#7676; 226crc_index(190) -> 16#4615; 227crc_index(191) -> 16#5634; 228 229crc_index(192) -> 16#d94c; 230crc_index(193) -> 16#c96d; 231crc_index(194) -> 16#f90e; 232crc_index(195) -> 16#e92f; 233crc_index(196) -> 16#99c8; 234crc_index(197) -> 16#89e9; 235crc_index(198) -> 16#b98a; 236crc_index(199) -> 16#a9ab; 237 238crc_index(200) -> 16#5844; 239crc_index(201) -> 16#4865; 240crc_index(202) -> 16#7806; 241crc_index(203) -> 16#6827; 242crc_index(204) -> 16#18c0; 243crc_index(205) -> 16#08e1; 244crc_index(206) -> 16#3882; 245crc_index(207) -> 16#28a3; 246 247crc_index(208) -> 16#cb7d; 248crc_index(209) -> 16#db5c; 249crc_index(210) -> 16#eb3f; 250crc_index(211) -> 16#fb1e; 251crc_index(212) -> 16#8bf9; 252crc_index(213) -> 16#9bd8; 253crc_index(214) -> 16#abbb; 254crc_index(215) -> 16#bb9a; 255 256crc_index(216) -> 16#4a75; 257crc_index(217) -> 16#5a54; 258crc_index(218) -> 16#6a37; 259crc_index(219) -> 16#7a16; 260crc_index(220) -> 16#0af1; 261crc_index(221) -> 16#1ad0; 262crc_index(222) -> 16#2ab3; 263crc_index(223) -> 16#3a92; 264 265crc_index(224) -> 16#fd2e; 266crc_index(225) -> 16#ed0f; 267crc_index(226) -> 16#dd6c; 268crc_index(227) -> 16#cd4d; 269crc_index(228) -> 16#bdaa; 270crc_index(229) -> 16#ad8b; 271crc_index(230) -> 16#9de8; 272crc_index(231) -> 16#8dc9; 273 274crc_index(232) -> 16#7c26; 275crc_index(233) -> 16#6c07; 276crc_index(234) -> 16#5c64; 277crc_index(235) -> 16#4c45; 278crc_index(236) -> 16#3ca2; 279crc_index(237) -> 16#2c83; 280crc_index(238) -> 16#1ce0; 281crc_index(239) -> 16#0cc1; 282 283crc_index(240) -> 16#ef1f; 284crc_index(241) -> 16#ff3e; 285crc_index(242) -> 16#cf5d; 286crc_index(243) -> 16#df7c; 287crc_index(244) -> 16#af9b; 288crc_index(245) -> 16#bfba; 289crc_index(246) -> 16#8fd9; 290crc_index(247) -> 16#9ff8; 291 292crc_index(248) -> 16#6e17; 293crc_index(249) -> 16#7e36; 294crc_index(250) -> 16#4e55; 295crc_index(251) -> 16#5e74; 296crc_index(252) -> 16#2e93; 297crc_index(253) -> 16#3eb2; 298crc_index(254) -> 16#0ed1; 299crc_index(255) -> 16#1ef0. 300 301calcByte(Fcs, B) -> 302 Idx = ((Fcs bsr 8) band 16#ff) bxor B, 303 ((Fcs bsl 8) band 16#ffff) bxor crc_index(Idx). 304 305crc16(X) when is_list(X) -> 306 lists:foldl(fun (B, Fsc)-> calcByte(Fsc, B) end, 16#0, X). 307 308test()-> 309 X = "123456789", 310 Crc16 = crc16(X), 311 io:format("crc16(~p) is ~p. ~n",[X, Crc16]).
2、CRC-CCITT (0xFFFF) 版本
1%%%------------------------------------------------------------------- 2%%% @author <yunnet> 3%%% @copyright (C) 2015, 4%%% @doc CRC-CCITT (0xFFFF) 5%%% CRC-CCITT (0xFFFF) 6%%% @end 7%%% Created : 15 Jan 2015 by <yunnet@gmail.com> 8%%%------------------------------------------------------------------- 9 10-module(crc16Ex). 11-export([crc16/1, test/0]). 12 13crc_index(0) -> 16#0000; 14crc_index(1) -> 16#1021; 15crc_index(2) -> 16#2042; 16crc_index(3) -> 16#3063; 17crc_index(4) -> 16#4084; 18crc_index(5) -> 16#50a5; 19crc_index(6) -> 16#60c6; 20crc_index(7) -> 16#70e7; 21 22crc_index(8) -> 16#8108; 23crc_index(9) -> 16#9129; 24crc_index(10) -> 16#a14a; 25crc_index(11) -> 16#b16b; 26crc_index(12) -> 16#c18c; 27crc_index(13) -> 16#d1ad; 28crc_index(14) -> 16#e1ce; 29crc_index(15) -> 16#f1ef; 30 31crc_index(16) -> 16#1231; 32crc_index(17) -> 16#0210; 33crc_index(18) -> 16#3273; 34crc_index(19) -> 16#2252; 35crc_index(20) -> 16#52b5; 36crc_index(21) -> 16#4294; 37crc_index(22) -> 16#72f7; 38crc_index(23) -> 16#62d6; 39 40crc_index(24) -> 16#9339; 41crc_index(25) -> 16#8318; 42crc_index(26) -> 16#b37b; 43crc_index(27) -> 16#a35a; 44crc_index(28) -> 16#d3bd; 45crc_index(29) -> 16#c39c; 46crc_index(30) -> 16#f3ff; 47crc_index(31) -> 16#e3de; 48 49crc_index(32) -> 16#2462; 50crc_index(33) -> 16#3443; 51crc_index(34) -> 16#0420; 52crc_index(35) -> 16#1401; 53crc_index(36) -> 16#64e6; 54crc_index(37) -> 16#74c7; 55crc_index(38) -> 16#44a4; 56crc_index(39) -> 16#5485; 57 58crc_index(40) -> 16#a56a; 59crc_index(41) -> 16#b54b; 60crc_index(42) -> 16#8528; 61crc_index(43) -> 16#9509; 62crc_index(44) -> 16#e5ee; 63crc_index(45) -> 16#f5cf; 64crc_index(46) -> 16#c5ac; 65crc_index(47) -> 16#d58d; 66 67crc_index(48) -> 16#3653; 68crc_index(49) -> 16#2672; 69crc_index(50) -> 16#1611; 70crc_index(51) -> 16#0630; 71crc_index(52) -> 16#76d7; 72crc_index(53) -> 16#66f6; 73crc_index(54) -> 16#5695; 74crc_index(55) -> 16#46b4; 75 76crc_index(56) -> 16#b75b; 77crc_index(57) -> 16#a77a; 78crc_index(58) -> 16#9719; 79crc_index(59) -> 16#8738; 80crc_index(60) -> 16#f7df; 81crc_index(61) -> 16#e7fe; 82crc_index(62) -> 16#d79d; 83crc_index(63) -> 16#c7bc; 84 85crc_index(64) -> 16#48c4; 86crc_index(65) -> 16#58e5; 87crc_index(66) -> 16#6886; 88crc_index(67) -> 16#78a7; 89crc_index(68) -> 16#0840; 90crc_index(69) -> 16#1861; 91crc_index(70) -> 16#2802; 92crc_index(71) -> 16#3823; 93 94crc_index(72) -> 16#c9cc; 95crc_index(73) -> 16#d9ed; 96crc_index(74) -> 16#e98e; 97crc_index(75) -> 16#f9af; 98crc_index(76) -> 16#8948; 99crc_index(77) -> 16#9969; 100crc_index(78) -> 16#a90a; 101crc_index(79) -> 16#b92b; 102 103crc_index(80) -> 16#5af5; 104crc_index(81) -> 16#4ad4; 105crc_index(82) -> 16#7ab7; 106crc_index(83) -> 16#6a96; 107crc_index(84) -> 16#1a71; 108crc_index(85) -> 16#0a50; 109crc_index(86) -> 16#3a33; 110crc_index(87) -> 16#2a12; 111 112crc_index(88) -> 16#dbfd; 113crc_index(89) -> 16#cbdc; 114crc_index(90) -> 16#fbbf; 115crc_index(91) -> 16#eb9e; 116crc_index(92) -> 16#9b79; 117crc_index(93) -> 16#8b58; 118crc_index(94) -> 16#bb3b; 119crc_index(95) -> 16#ab1a; 120 121crc_index(96) -> 16#6ca6; 122crc_index(97) -> 16#7c87; 123crc_index(98) -> 16#4ce4; 124crc_index(99) -> 16#5cc5; 125crc_index(100) -> 16#2c22; 126crc_index(101) -> 16#3c03; 127crc_index(102) -> 16#0c60; 128crc_index(103) -> 16#1c41; 129 130crc_index(104) -> 16#edae; 131crc_index(105) -> 16#fd8f; 132crc_index(106) -> 16#cdec; 133crc_index(107) -> 16#ddcd; 134crc_index(108) -> 16#ad2a; 135crc_index(109) -> 16#bd0b; 136crc_index(110) -> 16#8d68; 137crc_index(111) -> 16#9d49; 138 139crc_index(112) -> 16#7e97; 140crc_index(113) -> 16#6eb6; 141crc_index(114) -> 16#5ed5; 142crc_index(115) -> 16#4ef4; 143crc_index(116) -> 16#3e13; 144crc_index(117) -> 16#2e32; 145crc_index(118) -> 16#1e51; 146crc_index(119) -> 16#0e70; 147 148crc_index(120) -> 16#ff9f; 149crc_index(121) -> 16#efbe; 150crc_index(122) -> 16#dfdd; 151crc_index(123) -> 16#cffc; 152crc_index(124) -> 16#bf1b; 153crc_index(125) -> 16#af3a; 154crc_index(126) -> 16#9f59; 155crc_index(127) -> 16#8f78; 156 157crc_index(128) -> 16#9188; 158crc_index(129) -> 16#81a9; 159crc_index(130) -> 16#b1ca; 160crc_index(131) -> 16#a1eb; 161crc_index(132) -> 16#d10c; 162crc_index(133) -> 16#c12d; 163crc_index(134) -> 16#f14e; 164crc_index(135) -> 16#e16f; 165 166crc_index(136) -> 16#1080; 167crc_index(137) -> 16#00a1; 168crc_index(138) -> 16#30c2; 169crc_index(139) -> 16#20e3; 170crc_index(140) -> 16#5004; 171crc_index(141) -> 16#4025; 172crc_index(142) -> 16#7046; 173crc_index(143) -> 16#6067; 174 175crc_index(144) -> 16#83b9; 176crc_index(145) -> 16#9398; 177crc_index(146) -> 16#a3fb; 178crc_index(147) -> 16#b3da; 179crc_index(148) -> 16#c33d; 180crc_index(149) -> 16#d31c; 181crc_index(150) -> 16#e37f; 182crc_index(151) -> 16#f35e; 183 184crc_index(152) -> 16#02b1; 185crc_index(153) -> 16#1290; 186crc_index(154) -> 16#22f3; 187crc_index(155) -> 16#32d2; 188crc_index(156) -> 16#4235; 189crc_index(157) -> 16#5214; 190crc_index(158) -> 16#6277; 191crc_index(159) -> 16#7256; 192 193crc_index(160) -> 16#b5ea; 194crc_index(161) -> 16#a5cb; 195crc_index(162) -> 16#95a8; 196crc_index(163) -> 16#8589; 197crc_index(164) -> 16#f56e; 198crc_index(165) -> 16#e54f; 199crc_index(166) -> 16#d52c; 200crc_index(167) -> 16#c50d; 201 202crc_index(168) -> 16#34e2; 203crc_index(169) -> 16#24c3; 204crc_index(170) -> 16#14a0; 205crc_index(171) -> 16#0481; 206crc_index(172) -> 16#7466; 207crc_index(173) -> 16#6447; 208crc_index(174) -> 16#5424; 209crc_index(175) -> 16#4405; 210 211crc_index(176) -> 16#a7db; 212crc_index(177) -> 16#b7fa; 213crc_index(178) -> 16#8799; 214crc_index(179) -> 16#97b8; 215crc_index(180) -> 16#e75f; 216crc_index(181) -> 16#f77e; 217crc_index(182) -> 16#c71d; 218crc_index(183) -> 16#d73c; 219 220crc_index(184) -> 16#26d3; 221crc_index(185) -> 16#36f2; 222crc_index(186) -> 16#0691; 223crc_index(187) -> 16#16b0; 224crc_index(188) -> 16#6657; 225crc_index(189) -> 16#7676; 226crc_index(190) -> 16#4615; 227crc_index(191) -> 16#5634; 228 229crc_index(192) -> 16#d94c; 230crc_index(193) -> 16#c96d; 231crc_index(194) -> 16#f90e; 232crc_index(195) -> 16#e92f; 233crc_index(196) -> 16#99c8; 234crc_index(197) -> 16#89e9; 235crc_index(198) -> 16#b98a; 236crc_index(199) -> 16#a9ab; 237 238crc_index(200) -> 16#5844; 239crc_index(201) -> 16#4865; 240crc_index(202) -> 16#7806; 241crc_index(203) -> 16#6827; 242crc_index(204) -> 16#18c0; 243crc_index(205) -> 16#08e1; 244crc_index(206) -> 16#3882; 245crc_index(207) -> 16#28a3; 246 247crc_index(208) -> 16#cb7d; 248crc_index(209) -> 16#db5c; 249crc_index(210) -> 16#eb3f; 250crc_index(211) -> 16#fb1e; 251crc_index(212) -> 16#8bf9; 252crc_index(213) -> 16#9bd8; 253crc_index(214) -> 16#abbb; 254crc_index(215) -> 16#bb9a; 255 256crc_index(216) -> 16#4a75; 257crc_index(217) -> 16#5a54; 258crc_index(218) -> 16#6a37; 259crc_index(219) -> 16#7a16; 260crc_index(220) -> 16#0af1; 261crc_index(221) -> 16#1ad0; 262crc_index(222) -> 16#2ab3; 263crc_index(223) -> 16#3a92; 264 265crc_index(224) -> 16#fd2e; 266crc_index(225) -> 16#ed0f; 267crc_index(226) -> 16#dd6c; 268crc_index(227) -> 16#cd4d; 269crc_index(228) -> 16#bdaa; 270crc_index(229) -> 16#ad8b; 271crc_index(230) -> 16#9de8; 272crc_index(231) -> 16#8dc9; 273 274crc_index(232) -> 16#7c26; 275crc_index(233) -> 16#6c07; 276crc_index(234) -> 16#5c64; 277crc_index(235) -> 16#4c45; 278crc_index(236) -> 16#3ca2; 279crc_index(237) -> 16#2c83; 280crc_index(238) -> 16#1ce0; 281crc_index(239) -> 16#0cc1; 282 283crc_index(240) -> 16#ef1f; 284crc_index(241) -> 16#ff3e; 285crc_index(242) -> 16#cf5d; 286crc_index(243) -> 16#df7c; 287crc_index(244) -> 16#af9b; 288crc_index(245) -> 16#bfba; 289crc_index(246) -> 16#8fd9; 290crc_index(247) -> 16#9ff8; 291 292crc_index(248) -> 16#6e17; 293crc_index(249) -> 16#7e36; 294crc_index(250) -> 16#4e55; 295crc_index(251) -> 16#5e74; 296crc_index(252) -> 16#2e93; 297crc_index(253) -> 16#3eb2; 298crc_index(254) -> 16#0ed1; 299crc_index(255) -> 16#1ef0. 300 301calcByte(Fcs, B) -> 302 Idx = ((Fcs bsr 8) band 16#ff) bxor B, 303 ((Fcs bsl 8) band 16#ffff) bxor crc_index(Idx). 304 305crc16(X) when is_list(X) -> 306 lists:foldl(fun (B, Fsc)-> calcByte(Fsc, B) end, 16#FFFF, X). 307 308test()-> 309 X = "123456789", 310 Crc16 = crc16(X), 311 io:format("crc16(~p) is ~p. ~n",[X, Crc16]).
测试速度,比原来提高了20倍。
继续优化,还有空间可以提升。
:)