index.html 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>SIMD | 彪哥博客</title>
  7. <meta name="generator" content="VuePress 1.9.5">
  8. <link rel="icon" href="/blog/img/favicon.ico">
  9. <meta name="description" content="web前端技术博客,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,React,python,css3,html5,Node,git,github等技术文章。">
  10. <meta name="keywords" content="前端博客,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
  11. <meta name="theme-color" content="#11a8cd">
  12. <link rel="preload" href="/blog/assets/css/0.styles.cf8cd190.css" as="style"><link rel="preload" href="/blog/assets/js/app.14d95a24.js" as="script"><link rel="preload" href="/blog/assets/js/2.395c0d18.js" as="script"><link rel="preload" href="/blog/assets/js/3.6748bd5c.js" as="script"><link rel="preload" href="/blog/assets/js/120.9be3d6fd.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.cad3aa70.js"><link rel="prefetch" href="/blog/assets/js/100.08a8b2d8.js"><link rel="prefetch" href="/blog/assets/js/101.2aabb12c.js"><link rel="prefetch" href="/blog/assets/js/102.13f3cc4d.js"><link rel="prefetch" href="/blog/assets/js/103.c19aee03.js"><link rel="prefetch" href="/blog/assets/js/104.724d4908.js"><link rel="prefetch" href="/blog/assets/js/105.72966789.js"><link rel="prefetch" href="/blog/assets/js/106.207422de.js"><link rel="prefetch" href="/blog/assets/js/107.bf754f60.js"><link rel="prefetch" href="/blog/assets/js/108.46d6298b.js"><link rel="prefetch" href="/blog/assets/js/109.de6075c6.js"><link rel="prefetch" href="/blog/assets/js/11.f2e9eca8.js"><link rel="prefetch" href="/blog/assets/js/110.f41596b0.js"><link rel="prefetch" href="/blog/assets/js/111.da99a105.js"><link rel="prefetch" href="/blog/assets/js/112.9dd75c6f.js"><link rel="prefetch" href="/blog/assets/js/113.9322f157.js"><link rel="prefetch" href="/blog/assets/js/114.c5150cc6.js"><link rel="prefetch" href="/blog/assets/js/115.f58b49f8.js"><link rel="prefetch" href="/blog/assets/js/116.8f7d9c6f.js"><link rel="prefetch" href="/blog/assets/js/117.3c970f48.js"><link rel="prefetch" href="/blog/assets/js/118.921e1d54.js"><link rel="prefetch" href="/blog/assets/js/119.6ef74e70.js"><link rel="prefetch" href="/blog/assets/js/12.98512c60.js"><link rel="prefetch" href="/blog/assets/js/121.a0b3693a.js"><link rel="prefetch" href="/blog/assets/js/122.6c7dd225.js"><link rel="prefetch" href="/blog/assets/js/123.dbff103c.js"><link rel="prefetch" href="/blog/assets/js/124.493776ef.js"><link rel="prefetch" href="/blog/assets/js/125.554c9fbf.js"><link rel="prefetch" href="/blog/assets/js/126.9d3b75dc.js"><link rel="prefetch" href="/blog/assets/js/127.aad20a7e.js"><link rel="prefetch" href="/blog/assets/js/128.6543adba.js"><link rel="prefetch" href="/blog/assets/js/129.d7c56b92.js"><link rel="prefetch" href="/blog/assets/js/13.a79fa0c7.js"><link rel="prefetch" href="/blog/assets/js/130.593d21f0.js"><link rel="prefetch" href="/blog/assets/js/131.4c90d8b8.js"><link rel="prefetch" href="/blog/assets/js/132.4ad12bdc.js"><link rel="prefetch" href="/blog/assets/js/133.485de1b9.js"><link rel="prefetch" href="/blog/assets/js/134.dc7521fd.js"><link rel="prefetch" href="/blog/assets/js/135.196c8be6.js"><link rel="prefetch" href="/blog/assets/js/136.d99350df.js"><link rel="prefetch" href="/blog/assets/js/137.71e8757e.js"><link rel="prefetch" href="/blog/assets/js/138.75e29c6f.js"><link rel="prefetch" href="/blog/assets/js/139.766f20b7.js"><link rel="prefetch" href="/blog/assets/js/14.0fdf0c78.js"><link rel="prefetch" href="/blog/assets/js/140.0d9b8fbc.js"><link rel="prefetch" href="/blog/assets/js/141.128b6e26.js"><link rel="prefetch" href="/blog/assets/js/142.d7c4dea7.js"><link rel="prefetch" href="/blog/assets/js/143.9aa933f3.js"><link rel="prefetch" href="/blog/assets/js/144.277e558e.js"><link rel="prefetch" href="/blog/assets/js/145.87ec869d.js"><link rel="prefetch" href="/blog/assets/js/146.4185092c.js"><link rel="prefetch" href="/blog/assets/js/147.ac0e55d3.js"><link rel="prefetch" href="/blog/assets/js/148.30f02604.js"><link rel="prefetch" href="/blog/assets/js/149.7a6ca4f7.js"><link rel="prefetch" href="/blog/assets/js/15.fce722b2.js"><link rel="prefetch" href="/blog/assets/js/150.c3450fb8.js"><link rel="prefetch" href="/blog/assets/js/151.07f1a9e9.js"><link rel="prefetch" href="/blog/assets/js/152.0f318f01.js"><link rel="prefetch" href="/blog/assets/js/153.a8608e73.js"><link rel="prefetch" href="/blog/assets/js/154.8ab912b6.js"><link rel="prefetch" href="/blog/assets/js/155.5ee5b308.js"><link rel="prefetch" href="/blog/assets/js/156.4a049fb7.js"><link rel="prefetch" href="/blog/assets/js/157.99ee745c.js"><link rel="prefetch" href="/blog/assets/js/158.09b15f2b.js"><link rel="prefetch" href="/blog/assets/js/159.a83db4c9.js"><link rel="prefetch" href="/blog/assets/js/16.7cf1b239.js"><link rel="prefetch" href="/blog/assets/js/160.4cded216.js"><link rel="prefetch" href="/blog/assets/js/161.9471d56d.js"><link rel="prefetch" href="/blog/assets/js/162.0c32d380.js"><link rel="prefetch" href="/blog/assets/js/163.41d60a5c.js"><link rel="prefetch" href="/blog/assets/js/164.6dbc8ae8.js"><link rel="prefetch" href="/blog/assets/js/165.7e1b67fe.js"><link rel="prefetch" href="/blog/assets/js/166.ca626fb4.js"><link rel="prefetch" href="/blog/assets/js/167.02bee9f4.js"><link rel="prefetch" href="/blog/assets/js/168.330ac31c.js"><link rel="prefetch" href="/blog/assets/js/169.b7e43910.js"><link rel="prefetch" href="/blog/assets/js/17.93f492a1.js"><link rel="prefetch" href="/blog/assets/js/170.70ecb003.js"><link rel="prefetch" href="/blog/assets/js/171.8828622b.js"><link rel="prefetch" href="/blog/assets/js/172.b659d767.js"><link rel="prefetch" href="/blog/assets/js/173.1b915b91.js"><link rel="prefetch" href="/blog/assets/js/174.307c1d13.js"><link rel="prefetch" href="/blog/assets/js/175.44316a23.js"><link rel="prefetch" href="/blog/assets/js/176.044e8f54.js"><link rel="prefetch" href="/blog/assets/js/177.fae8b8e2.js"><link rel="prefetch" href="/blog/assets/js/178.b6e40e29.js"><link rel="prefetch" href="/blog/assets/js/179.c69ea8d8.js"><link rel="prefetch" href="/blog/assets/js/18.f17de23a.js"><link rel="prefetch" href="/blog/assets/js/180.955aa8ec.js"><link rel="prefetch" href="/blog/assets/js/181.a53e32e0.js"><link rel="prefetch" href="/blog/assets/js/182.38687994.js"><link rel="prefetch" href="/blog/assets/js/183.544fef00.js"><link rel="prefetch" href="/blog/assets/js/184.ae09b86e.js"><link rel="prefetch" href="/blog/assets/js/185.2f5f6e20.js"><link rel="prefetch" href="/blog/assets/js/186.0efe164b.js"><link rel="prefetch" href="/blog/assets/js/187.df0138d8.js"><link rel="prefetch" href="/blog/assets/js/188.5d767d7a.js"><link rel="prefetch" href="/blog/assets/js/189.137e022f.js"><link rel="prefetch" href="/blog/assets/js/19.6b963460.js"><link rel="prefetch" href="/blog/assets/js/190.5270cf9b.js"><link rel="prefetch" href="/blog/assets/js/191.862607b0.js"><link rel="prefetch" href="/blog/assets/js/192.127fef4c.js"><link rel="prefetch" href="/blog/assets/js/193.781690eb.js"><link rel="prefetch" href="/blog/assets/js/194.1e80b1b8.js"><link rel="prefetch" href="/blog/assets/js/195.003e3d67.js"><link rel="prefetch" href="/blog/assets/js/196.7a3f55e5.js"><link rel="prefetch" href="/blog/assets/js/197.30d4c5b4.js"><link rel="prefetch" href="/blog/assets/js/198.628c2c1a.js"><link rel="prefetch" href="/blog/assets/js/199.89c4e586.js"><link rel="prefetch" href="/blog/assets/js/20.839dae41.js"><link rel="prefetch" href="/blog/assets/js/200.1d9f2ef9.js"><link rel="prefetch" href="/blog/assets/js/201.5ba078d9.js"><link rel="prefetch" href="/blog/assets/js/202.b49b23f4.js"><link rel="prefetch" href="/blog/assets/js/203.d88a03d9.js"><link rel="prefetch" href="/blog/assets/js/204.fb928277.js"><link rel="prefetch" href="/blog/assets/js/205.432c3d8d.js"><link rel="prefetch" href="/blog/assets/js/206.ed726599.js"><link rel="prefetch" href="/blog/assets/js/207.9ac30d7b.js"><link rel="prefetch" href="/blog/assets/js/208.c5e77adc.js"><link rel="prefetch" href="/blog/assets/js/209.d396aad5.js"><link rel="prefetch" href="/blog/assets/js/21.bd21bd29.js"><link rel="prefetch" href="/blog/assets/js/210.02e4ee2f.js"><link rel="prefetch" href="/blog/assets/js/211.0f8a9304.js"><link rel="prefetch" href="/blog/assets/js/212.8a476248.js"><link rel="prefetch" href="/blog/assets/js/213.aab2ac5c.js"><link rel="prefetch" href="/blog/assets/js/214.cd5ec468.js"><link rel="prefetch" href="/blog/assets/js/215.0084d772.js"><link rel="prefetch" href="/blog/assets/js/216.ebd468b9.js"><link rel="prefetch" href="/blog/assets/js/217.fac21407.js"><link rel="prefetch" href="/blog/assets/js/218.9793e19a.js"><link rel="prefetch" href="/blog/assets/js/219.c89175e9.js"><link rel="prefetch" href="/blog/assets/js/22.e6bcf65d.js"><link rel="prefetch" href="/blog/assets/js/220.d496e411.js"><link rel="prefetch" href="/blog/assets/js/221.07a9338a.js"><link rel="prefetch" href="/blog/assets/js/222.22bf261f.js"><link rel="prefetch" href="/blog/assets/js/223.b405119d.js"><link rel="prefetch" href="/blog/assets/js/224.364b4b11.js"><link rel="prefetch" href="/blog/assets/js/225.adec6660.js"><link rel="prefetch" href="/blog/assets/js/226.d7ab4163.js"><link rel="prefetch" href="/blog/assets/js/227.da4481b8.js"><link rel="prefetch" href="/blog/assets/js/228.7243da66.js"><link rel="prefetch" href="/blog/assets/js/229.bf1f474d.js"><link rel="prefetch" href="/blog/assets/js/23.7bb82fc7.js"><link rel="prefetch" href="/blog/assets/js/230.109a1752.js"><link rel="prefetch" href="/blog/assets/js/231.b1c6db75.js"><link rel="prefetch" href="/blog/assets/js/232.176c4df4.js"><link rel="prefetch" href="/blog/assets/js/233.2c81f1b3.js"><link rel="prefetch" href="/blog/assets/js/234.bb73adca.js"><link rel="prefetch" href="/blog/assets/js/235.97ed69db.js"><link rel="prefetch" href="/blog/assets/js/236.a8c51930.js"><link rel="prefetch" href="/blog/assets/js/237.6ae31c88.js"><link rel="prefetch" href="/blog/assets/js/238.2f5c56ae.js"><link rel="prefetch" href="/blog/assets/js/239.506a4e9f.js"><link rel="prefetch" href="/blog/assets/js/24.2c9c4ee6.js"><link rel="prefetch" href="/blog/assets/js/25.b4de33d1.js"><link rel="prefetch" href="/blog/assets/js/26.0bb98ba9.js"><link rel="prefetch" href="/blog/assets/js/27.df98327e.js"><link rel="prefetch" href="/blog/assets/js/28.31289bac.js"><link rel="prefetch" href="/blog/assets/js/29.45af5621.js"><link rel="prefetch" href="/blog/assets/js/30.d5c08e66.js"><link rel="prefetch" href="/blog/assets/js/31.78e43a68.js"><link rel="prefetch" href="/blog/assets/js/32.53ca76ee.js"><link rel="prefetch" href="/blog/assets/js/33.081e8ef6.js"><link rel="prefetch" href="/blog/assets/js/34.cb1866c1.js"><link rel="prefetch" href="/blog/assets/js/35.dca9b927.js"><link rel="prefetch" href="/blog/assets/js/36.fb4476c3.js"><link rel="prefetch" href="/blog/assets/js/37.09dfc1c5.js"><link rel="prefetch" href="/blog/assets/js/38.6624bf02.js"><link rel="prefetch" href="/blog/assets/js/39.df6c26ac.js"><link rel="prefetch" href="/blog/assets/js/4.44654b1a.js"><link rel="prefetch" href="/blog/assets/js/40.80101c19.js"><link rel="prefetch" href="/blog/assets/js/41.2b5e8c27.js"><link rel="prefetch" href="/blog/assets/js/42.c6ded3fe.js"><link rel="prefetch" href="/blog/assets/js/43.6d9424d6.js"><link rel="prefetch" href="/blog/assets/js/44.835e4b5c.js"><link rel="prefetch" href="/blog/assets/js/45.d74d29a2.js"><link rel="prefetch" href="/blog/assets/js/46.d15a7dc0.js"><link rel="prefetch" href="/blog/assets/js/47.8d66ca97.js"><link rel="prefetch" href="/blog/assets/js/48.3c1102e1.js"><link rel="prefetch" href="/blog/assets/js/49.e17a3436.js"><link rel="prefetch" href="/blog/assets/js/5.88de390f.js"><link rel="prefetch" href="/blog/assets/js/50.6750f186.js"><link rel="prefetch" href="/blog/assets/js/51.9f93af9f.js"><link rel="prefetch" href="/blog/assets/js/52.f3ef3b5e.js"><link rel="prefetch" href="/blog/assets/js/53.a6bacd25.js"><link rel="prefetch" href="/blog/assets/js/54.dbb7c9ab.js"><link rel="prefetch" href="/blog/assets/js/55.2562d0c8.js"><link rel="prefetch" href="/blog/assets/js/56.14ea4931.js"><link rel="prefetch" href="/blog/assets/js/57.a2fad780.js"><link rel="prefetch" href="/blog/assets/js/58.8165b971.js"><link rel="prefetch" href="/blog/assets/js/59.556cab0d.js"><link rel="prefetch" href="/blog/assets/js/6.277038ca.js"><link rel="prefetch" href="/blog/assets/js/60.e2a6ab4c.js"><link rel="prefetch" href="/blog/assets/js/61.16bf38a2.js"><link rel="prefetch" href="/blog/assets/js/62.37a94f10.js"><link rel="prefetch" href="/blog/assets/js/63.74811780.js"><link rel="prefetch" href="/blog/assets/js/64.81f21b8a.js"><link rel="prefetch" href="/blog/assets/js/65.d970ff03.js"><link rel="prefetch" href="/blog/assets/js/66.cb805d9b.js"><link rel="prefetch" href="/blog/assets/js/67.39f85baa.js"><link rel="prefetch" href="/blog/assets/js/68.7f79766a.js"><link rel="prefetch" href="/blog/assets/js/69.fa8624bd.js"><link rel="prefetch" href="/blog/assets/js/7.e0a6d1b0.js"><link rel="prefetch" href="/blog/assets/js/70.1f3e978d.js"><link rel="prefetch" href="/blog/assets/js/71.13cd9358.js"><link rel="prefetch" href="/blog/assets/js/72.739b22a8.js"><link rel="prefetch" href="/blog/assets/js/73.03af83b4.js"><link rel="prefetch" href="/blog/assets/js/74.f23e183c.js"><link rel="prefetch" href="/blog/assets/js/75.b8d21d41.js"><link rel="prefetch" href="/blog/assets/js/76.fd0f1cda.js"><link rel="prefetch" href="/blog/assets/js/77.46f6e413.js"><link rel="prefetch" href="/blog/assets/js/78.aebd00ee.js"><link rel="prefetch" href="/blog/assets/js/79.ad91baa5.js"><link rel="prefetch" href="/blog/assets/js/8.9428e7ee.js"><link rel="prefetch" href="/blog/assets/js/80.593a99e8.js"><link rel="prefetch" href="/blog/assets/js/81.101cc131.js"><link rel="prefetch" href="/blog/assets/js/82.077c8298.js"><link rel="prefetch" href="/blog/assets/js/83.2e375d11.js"><link rel="prefetch" href="/blog/assets/js/84.38102a34.js"><link rel="prefetch" href="/blog/assets/js/85.24532d6a.js"><link rel="prefetch" href="/blog/assets/js/86.1dabbf00.js"><link rel="prefetch" href="/blog/assets/js/87.763da0f2.js"><link rel="prefetch" href="/blog/assets/js/88.ff6e5f7c.js"><link rel="prefetch" href="/blog/assets/js/89.187e5e16.js"><link rel="prefetch" href="/blog/assets/js/9.da143545.js"><link rel="prefetch" href="/blog/assets/js/90.3c8cff94.js"><link rel="prefetch" href="/blog/assets/js/91.faacc3e9.js"><link rel="prefetch" href="/blog/assets/js/92.873706ba.js"><link rel="prefetch" href="/blog/assets/js/93.c8ee75e3.js"><link rel="prefetch" href="/blog/assets/js/94.b18a3e9b.js"><link rel="prefetch" href="/blog/assets/js/95.cddef6ae.js"><link rel="prefetch" href="/blog/assets/js/96.80e5a938.js"><link rel="prefetch" href="/blog/assets/js/97.1f5e5197.js"><link rel="prefetch" href="/blog/assets/js/98.e3a275c8.js"><link rel="prefetch" href="/blog/assets/js/99.d33bf89e.js">
  13. <link rel="stylesheet" href="/blog/assets/css/0.styles.cf8cd190.css">
  14. </head>
  15. <body class="theme-mode-light">
  16. <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/img/logo.png" alt="彪哥博客" class="logo"> <span class="site-name can-hide">彪哥博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  17. 个人游戏网站
  18. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  19. GitHub
  20. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/head.jpg"> <div class="blogger-info"><h3>彪哥</h3> <span>爱好前端</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">首页</a></div><div class="nav-item"><a href="http://fseller.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  21. 个人游戏网站
  22. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/blog/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/blog/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/blog/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/blog/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/blog/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/blog/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/blog/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/blog/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/blog/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/blog/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/blog/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/blog/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/blog/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/blog/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/blog/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/blog/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/blog/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/blog/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/blog/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/heBody/blog" target="_blank" rel="noopener noreferrer" class="repo-link">
  23. GitHub
  24. <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><a href="/blog/pages/f344d070a1031ef7/" class="sidebar-link">ECMAScript 6 简介</a></li><li><a href="/blog/pages/c1edd70a6b7c7872/" class="sidebar-link">let 和 const 命令</a></li><li><a href="/blog/pages/b1ab10a62f7564da/" class="sidebar-link">变量的解构赋值</a></li><li><a href="/blog/pages/ca89eca8adeba5f4/" class="sidebar-link">字符串的扩展</a></li><li><a href="/blog/pages/a650b4a0ebfc9350/" class="sidebar-link">字符串的新增方法</a></li><li><a href="/blog/pages/0473261a6ab0ee8c/" class="sidebar-link">正则的扩展</a></li><li><a href="/blog/pages/5dfea9a0f2d1a392/" class="sidebar-link">数值的扩展</a></li><li><a href="/blog/pages/8ed309d668b20264/" class="sidebar-link">函数的扩展</a></li><li><a href="/blog/pages/e34009d60d8bc4b2/" class="sidebar-link">数组的扩展</a></li><li><a href="/blog/pages/b5e3e0a0ff6e9c25/" class="sidebar-link">对象的扩展</a></li><li><a href="/blog/pages/e85e68947502cf90/" class="sidebar-link">对象的新增方法</a></li><li><a href="/blog/pages/02c86eb2792f3262/" class="sidebar-link">Symbol</a></li><li><a href="/blog/pages/0c21dae358fca16b/" class="sidebar-link">Set 和 Map 数据结构</a></li><li><a href="/blog/pages/f56ec2ab97d60483/" class="sidebar-link">Proxy</a></li><li><a href="/blog/pages/74de3e45e4491e95/" class="sidebar-link">Reflect</a></li><li><a href="/blog/pages/2810ae8985e9bd52/" class="sidebar-link">Promise 对象</a></li><li><a href="/blog/pages/48df907ad3570f3d/" class="sidebar-link">Iterator 和 for-of 循环</a></li><li><a href="/blog/pages/718b48ed9ce0adce/" class="sidebar-link">Generator 函数的语法</a></li><li><a href="/blog/pages/75af7031eb66847b/" class="sidebar-link">Generator 函数的异步应用</a></li><li><a href="/blog/pages/3777253e65bac487/" class="sidebar-link">async 函数</a></li><li><a href="/blog/pages/e831e1593c82bbe0/" class="sidebar-link">Class 的基本语法</a></li><li><a href="/blog/pages/83f8c3a0cd87dd83/" class="sidebar-link">Class 的继承</a></li><li><a href="/blog/pages/efe2fb04eb8ac5fb/" class="sidebar-link">Module 的语法</a></li><li><a href="/blog/pages/a79ca2e64ceae213/" class="sidebar-link">Module 的加载实现</a></li><li><a href="/blog/pages/984bf549204bb266/" class="sidebar-link">编程风格</a></li><li><a href="/blog/pages/32c35f7651d6e58e/" class="sidebar-link">读懂 ECMAScript 规格</a></li><li><a href="/blog/pages/16121351be68691b/" class="sidebar-link">异步遍历器</a></li><li><a href="/blog/pages/a2ba314746bfdbdd/" class="sidebar-link">ArrayBuffer</a></li><li><a href="/blog/pages/7188882b8d65af1b/" class="sidebar-link">最新提案</a></li><li><a href="/blog/pages/e97bc1e5626b082c/" class="sidebar-link">装饰器</a></li><li><a href="/blog/pages/1cf50330655efc69/" class="sidebar-link">函数式编程</a></li><li><a href="/blog/pages/6a8e2dc558da1b39/" class="sidebar-link">Mixin</a></li><li><a href="/blog/pages/8e8f80f69b775a56/" aria-current="page" class="active sidebar-link">SIMD</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#概述" class="sidebar-link">概述</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#数据类型" class="sidebar-link">数据类型</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#静态方法-数学运算" class="sidebar-link">静态方法:数学运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-abs-simd-type-neg" class="sidebar-link">SIMD.%type%.abs(),SIMD.%type%.neg()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-add-simd-type-addsaturate" class="sidebar-link">SIMD.%type%.add(),SIMD.%type%.addSaturate()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-sub-simd-type-subsaturate" class="sidebar-link">SIMD.%type%.sub(),SIMD.%type%.subSaturate()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-mul-simd-type-div-simd-type-sqrt" class="sidebar-link">SIMD.%type%.mul(),SIMD.%type%.div(),SIMD.%type%.sqrt()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation" class="sidebar-link">SIMD.%FloatType%.reciprocalApproximation(),SIMD.%type%.reciprocalSqrtApproximation()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-integertype-shiftleftbyscalar" class="sidebar-link">SIMD.%IntegerType%.shiftLeftByScalar()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-integertype-shiftrightbyscalar" class="sidebar-link">SIMD.%IntegerType%.shiftRightByScalar()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#静态方法-通道处理" class="sidebar-link">静态方法:通道处理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-check" class="sidebar-link">SIMD.%type%.check()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-extractlane-simd-type-replacelane" class="sidebar-link">SIMD.%type%.extractLane(),SIMD.%type%.replaceLane()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-load" class="sidebar-link">SIMD.%type%.load()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-store" class="sidebar-link">SIMD.%type%.store()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-splat" class="sidebar-link">SIMD.%type%.splat()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-swizzle" class="sidebar-link">SIMD.%type%.swizzle()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-shuffle" class="sidebar-link">SIMD.%type%.shuffle()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#静态方法-比较运算" class="sidebar-link">静态方法:比较运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-equal-simd-type-notequal" class="sidebar-link">SIMD.%type%.equal(),SIMD.%type%.notEqual()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-greaterthan-simd-type-greaterthanorequal" class="sidebar-link">SIMD.%type%.greaterThan(),SIMD.%type%.greaterThanOrEqual()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-lessthan-simd-type-lessthanorequal" class="sidebar-link">SIMD.%type%.lessThan(),SIMD.%type%.lessThanOrEqual()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-select" class="sidebar-link">SIMD.%type%.select()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-booleantype-alltrue-simd-booleantype-anytrue" class="sidebar-link">SIMD.%BooleanType%.allTrue(),SIMD.%BooleanType%.anyTrue()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-min-simd-type-minnum" class="sidebar-link">SIMD.%type%.min(),SIMD.%type%.minNum()</a></li><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-max-simd-type-maxnum" class="sidebar-link">SIMD.%type%.max(),SIMD.%type%.maxNum()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#静态方法-位运算" class="sidebar-link">静态方法:位运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-and-simd-type-or-simd-type-xor-simd-type-not" class="sidebar-link">SIMD.%type%.and(),SIMD.%type%.or(),SIMD.%type%.xor(),SIMD.%type%.not()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#静态方法-数据类型转换" class="sidebar-link">静态方法:数据类型转换</a></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#实例方法" class="sidebar-link">实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/blog/pages/8e8f80f69b775a56/#simd-type-prototype-tostring" class="sidebar-link">SIMD.%type%.prototype.toString()</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/blog/pages/8e8f80f69b775a56/#实例-求平均值" class="sidebar-link">实例:求平均值</a></li></ul></li><li><a href="/blog/pages/ea6f3b870f6dab69/" class="sidebar-link">参考链接</a></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06970110><div class="articleInfo" data-v-06970110><ul class="breadcrumbs" data-v-06970110><li data-v-06970110><a href="/blog/" title="首页" class="iconfont icon-home router-link-active" data-v-06970110></a></li> <li data-v-06970110><a href="/blog/note/es6/#《ES6 教程》笔记" data-v-06970110>《ES6 教程》笔记</a></li></ul> <div class="info" data-v-06970110><div title="作者" class="author iconfont icon-touxiang" data-v-06970110><a href="javascript:;" data-v-06970110>阮一峰</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06970110><a href="javascript:;" data-v-06970110>2020-02-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABH1JREFUSA3tVl1oHFUUPmdmd2ltklqbpJDiNnXFmgbFktho7YMPNiJSSZM0+CAYSkUELVhM6YuwIPpgoOKDqOBDC0XE2CQoNtQXBUFTTcCi+Wlh1V2TQExsUzcltd3M9Tt3ZjZzZ2fT+OJTL8yeM+eee757fmeJbq//KQL8X3DUSFOcfr7cRsRtxNQMWueeVzOkaITIGqQHNg5y8+jNW9ldM7A6nTpAjuolUikAwq7CE3WcM2RRDz+XGVgN3FptU/aUSlvq9Pa3iZ1+sgAqJyyAFqkipd9dqiwHF3P65YycLWc/6sqGrvoEoIp6DOFaX5h6+dnfjkWprwqsPk0dUGq5vySwDImC10KxFHgGL1SWoc92O3eVht09qdXNH11I2SsTsJYqMWzihqGMi+A+Garf3BAuuLI5oGlULyNfyB/HYNujwktOfRrMr5t77NmevqaUopx0grnKAyvVpmwUDB4x6FPXuGvYLTDwWsejwgtgkYKPqRJg8SV6xaiZ3ZTppGneS4yfH5/66fZSDHv+QZci/+h5c5UHtpy67JUqGppM0sh0Nc1dW6/N1W5Yoqat8/TU/VnadmdeW2PLLSyh0cvxBs3KbqTmwYPpxN4do/mzE8nEpvX/UMu2Wbp74zUAK5q6WkHns7V0eWkdPbPzd3rxkTGybadYySumVzhcaJFbs5UrEkQ/+CK8gF5dnh/6ciIZ73gwQ927L1IitoxKLXYP3SjYdOrHHfTZhRRlFyrorafPk20B3HPD1y2G3qKZME5Jcf3t/HUC13/8tSd++vqFveMUTwAUxSUFI1QekR1+bIze3D9MF2aq6cPvG72CgnldWCFqyRw3lwH8ZMerjTD9ElRO7Gv44wNpC90aASqGfVlz/Rx17srQ57/UU26hkhQqUB7dBR71WmzQhHUnblGmVOEw0jhbV1n9OlXUDCIRGaNV5Jp43N516fN7JmnTHdfp7Hgy0luO4aMhtkLL8Bi3bUWYvzh5Mn1dTxrL6QmGuRhGL/TiTTxRoEdTszSaq9GR0NGA3KdkOz3hqSV3MIDhQ5IVX/Ivx3umBti2es2h4eZby7x8br1rkf7Mo90AqC8aQ3sJeNzqFRu+vSANAQe3PL7l0HGOAdwDCeZYvNKeoZp1Qfs6Aipndh86HmFRi0LAnEO47wsqM6cdfjh3jBPUzhZy7nvlUfFsamED1VQt6aISHVymXZ/B2aCtIG8AI8xfobj2d3en1wWVhOeHELKmLQ1s211s88comkv4UCwWyF787mJdYXtNfhKAXVqnKTq8QZvGAGGOfaTo5pGZ/PwbUCr5+DPr/1J92JNHr9aOl/F3iI5+O1nfybsGxoimvZ3ViWSluDITw3P37mypheDIPY0tw7+O/5ApbkYw+zpfaUVu32Pi98+defdUhEpZkRFq0aqyNh9FuL9hpYbEm6iwi0z2REd09ZmyENEbuhjDWzKvZXTqKYaBIr3tt5kuPtQBZFvEUwHt60vfCNu41XsksH9Ij1BMMz1Y0OOunHNShFIP5868g5zeXmuLwL9T4b6Q2+KejgAAAABJRU5ErkJggg==">SIMD<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="simd"><a href="#simd" class="header-anchor">#</a> SIMD</h1> <h2 id="概述"><a href="#概述" class="header-anchor">#</a> 概述</h2> <p>SIMD(发音<code>/sim-dee/</code>)是“Single Instruction/Multiple Data”的缩写,意为“单指令,多数据”。它是 JavaScript 操作 CPU 对应指令的接口,你可以看做这是一种不同的运算执行模式。与它相对的是 SISD(“Single Instruction/Single Data”),即“单指令,单数据”。
  25. SIMD 的含义是使用一个指令,完成多个数据的运算;SISD 的含义是使用一个指令,完成单个数据的运算,这是 JavaScript 的默认运算模式。显而易见,SIMD 的执行效率要高于 SISD,所以被广泛用于 3D 图形运算、物理模拟等运算量超大的项目之中。</p> <p>为了理解 SIMD,请看下面的例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  26. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  27. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  28. c<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  29. c<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  30. c<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  31. c<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  32. c <span class="token comment">// Array[6, 8, 10, 12]</span>
  33. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,数组<code>a</code>和<code>b</code>的对应成员相加,结果放入数组<code>c</code>。它的运算模式是依次处理每个数组成员,一共有四个数组成员,所以需要运算 4 次。</p> <p>如果采用 SIMD 模式,只要运算一次就够了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  34. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  35. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Float32x4[6, 8, 10, 12]</span>
  36. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码之中,数组<code>a</code>和<code>b</code>的四个成员的各自相加,只用一条指令就完成了。因此,速度比上一种写法提高了 4 倍。</p> <p>一次 SIMD 运算,可以处理多个数据,这些数据被称为“通道”(lane)。上面代码中,一次运算了四个数据,因此就是四个通道。</p> <p>SIMD 通常用于矢量运算。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>v <span class="token operator">+</span> w <span class="token operator">=</span> 〈v1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> vn〉<span class="token operator">+</span> 〈w1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> wn〉
  37. <span class="token operator">=</span> 〈v1<span class="token operator">+</span>w1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> vn<span class="token operator">+</span>wn〉
  38. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中,<code>v</code>和<code>w</code>是两个多元矢量。它们的加运算,在 SIMD 下是一个指令、而不是 n 个指令完成的,这就大大提高了效率。这对于 3D 动画、图像处理、信号处理、数值处理、加密等运算是非常重要的。比如,Canvas 的<code>getImageData()</code>会将图像文件读成一个二进制数组,SIMD 就很适合对于这种数组的处理。</p> <p>总的来说,SIMD 是数据并行处理(parallelism)的一种手段,可以加速一些运算密集型操作的速度。将来与 WebAssembly 结合以后,可以让 JavaScript 达到二进制代码的运行速度。</p> <h2 id="数据类型"><a href="#数据类型" class="header-anchor">#</a> 数据类型</h2> <p>SIMD 提供 12 种数据类型,总长度都是 128 个二进制位。</p> <ul><li>Float32x4:四个 32 位浮点数</li> <li>Float64x2:两个 64 位浮点数</li> <li>Int32x4:四个 32 位整数</li> <li>Int16x8:八个 16 位整数</li> <li>Int8x16:十六个 8 位整数</li> <li>Uint32x4:四个无符号的 32 位整数</li> <li>Uint16x8:八个无符号的 16 位整数</li> <li>Uint8x16:十六个无符号的 8 位整数</li> <li>Bool32x4:四个 32 位布尔值</li> <li>Bool16x8:八个 16 位布尔值</li> <li>Bool8x16:十六个 8 位布尔值</li> <li>Bool64x2:两个 64 位布尔值</li></ul> <p>每种数据类型被<code>x</code>符号分隔成两部分,后面的部分表示通道数,前面的部分表示每个通道的宽度和类型。比如,<code>Float32x4</code>就表示这个值有 4 个通道,每个通道是一个 32 位浮点数。</p> <p>每个通道之中,可以放置四种数据。</p> <ul><li>浮点数(float,比如 1.0)</li> <li>带符号的整数(Int,比如-1)</li> <li>无符号的整数(Uint,比如 1)</li> <li>布尔值(Bool,包含<code>true</code>和<code>false</code>两种值)</li></ul> <p>每种 SIMD 的数据类型都是一个函数方法,可以传入参数,生成对应的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  39. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中,变量<code>a</code>就是一个 128 位、包含四个 32 位浮点数(即四个通道)的值。</p> <p>注意,这些数据类型方法都不是构造函数,前面不能加<code>new</code>,否则会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> v <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SIMD<span class="token punctuation">.</span>Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  40. <span class="token comment">// TypeError: SIMD.Float32x4 is not a constructor</span>
  41. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="静态方法-数学运算"><a href="#静态方法-数学运算" class="header-anchor">#</a> 静态方法:数学运算</h2> <p>每种数据类型都有一系列运算符,支持基本的数学运算。</p> <h3 id="simd-type-abs-simd-type-neg"><a href="#simd-type-abs-simd-type-neg" class="header-anchor">#</a> SIMD.%type%.abs(),SIMD.%type%.neg()</h3> <p><code>abs</code>方法接受一个 SIMD 值作为参数,将它的每个通道都转成绝对值,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  42. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
  43. <span class="token comment">// Float32x4[1, 2, 0, NaN]</span>
  44. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>neg</code>方法接受一个 SIMD 值作为参数,将它的每个通道都转成负值,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  45. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">neg</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
  46. <span class="token comment">// Float32x4[1, 2, -3, -0]</span>
  47. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  48. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">neg</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
  49. <span class="token comment">// Float64x2[NaN, -Infinity]</span>
  50. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="simd-type-add-simd-type-addsaturate"><a href="#simd-type-add-simd-type-addsaturate" class="header-anchor">#</a> SIMD.%type%.add(),SIMD.%type%.addSaturate()</h3> <p><code>add</code>方法接受两个 SIMD 值作为参数,将它们的每个通道相加,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  51. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5.0</span><span class="token punctuation">,</span> <span class="token number">10.0</span><span class="token punctuation">,</span> <span class="token number">15.0</span><span class="token punctuation">,</span> <span class="token number">20.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  52. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  53. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,经过加法运算,新的 SIMD 值为<code>(6.0, 12.0, 18.0. 24.0)</code>。</p> <p><code>addSaturate</code>方法跟<code>add</code>方法的作用相同,都是两个通道相加,但是溢出的处理不一致。对于<code>add</code>方法,如果两个值相加发生溢出,溢出的二进制位会被丢弃; <code>addSaturate</code>方法则是返回该数据类型的最大值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">65533</span><span class="token punctuation">,</span> <span class="token number">65534</span><span class="token punctuation">,</span> <span class="token number">65535</span><span class="token punctuation">,</span> <span class="token number">65535</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  54. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  55. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint16x8<span class="token punctuation">.</span><span class="token function">addSaturate</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  56. <span class="token comment">// Uint16x8[65534, 65535, 65535, 65535, 2, 2, 2, 2]</span>
  57. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">32765</span><span class="token punctuation">,</span> <span class="token number">32766</span><span class="token punctuation">,</span> <span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  58. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  59. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">addSaturate</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
  60. <span class="token comment">// Int16x8[32766, 32767, 32767, 32767, 2, 2, 2, 2]</span>
  61. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>Uint16</code>的最大值是 65535,<code>Int16</code>的最大值是 32767。一旦发生溢出,就返回这两个值。</p> <p>注意,<code>Uint32x4</code>和<code>Int32x4</code>这两种数据类型没有<code>addSaturate</code>方法。</p> <h3 id="simd-type-sub-simd-type-subsaturate"><a href="#simd-type-sub-simd-type-subsaturate" class="header-anchor">#</a> SIMD.%type%.sub(),SIMD.%type%.subSaturate()</h3> <p><code>sub</code>方法接受两个 SIMD 值作为参数,将它们的每个通道相减,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  62. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  63. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">sub</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  64. <span class="token comment">// Float32x4[-4, -5, 0, 1]</span>
  65. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>subSaturate</code>方法跟<code>sub</code>方法的作用相同,都是两个通道相减,但是溢出的处理不一致。对于<code>sub</code>方法,如果两个值相减发生溢出,溢出的二进制位会被丢弃; <code>subSaturate</code>方法则是返回该数据类型的最小值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  66. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  67. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint16x8<span class="token punctuation">.</span><span class="token function">subSaturate</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  68. <span class="token comment">// Uint16x8[0, 0, 0, 0, 0, 0, 0, 0]</span>
  69. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  70. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  71. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">subSaturate</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
  72. <span class="token comment">// Int16x8[-32768, 0, 0, 0, 0, 0, 0, 0, 0]</span>
  73. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>Uint16</code>的最小值是<code>0</code>,<code>Int16</code>的最小值是<code>-32678</code>。一旦运算发生溢出,就返回最小值。</p> <h3 id="simd-type-mul-simd-type-div-simd-type-sqrt"><a href="#simd-type-mul-simd-type-div-simd-type-sqrt" class="header-anchor">#</a> SIMD.%type%.mul(),SIMD.%type%.div(),SIMD.%type%.sqrt()</h3> <p><code>mul</code>方法接受两个 SIMD 值作为参数,将它们的每个通道相乘,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  74. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  75. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">mul</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  76. <span class="token comment">// Float32x4[-3, -6, 9, 12]</span>
  77. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>div</code>方法接受两个 SIMD 值作为参数,将它们的每个通道相除,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  78. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  79. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">div</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  80. <span class="token comment">// Float32x4[0.5, 0.5, 0.5, 0.5]</span>
  81. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>sqrt</code>方法接受一个 SIMD 值作为参数,求出每个通道的平方根,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  82. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
  83. <span class="token comment">// Float64x2[2, 2.8284271247461903]</span>
  84. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation"><a href="#simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation" class="header-anchor">#</a> SIMD.%FloatType%.reciprocalApproximation(),SIMD.%type%.reciprocalSqrtApproximation()</h3> <p><code>reciprocalApproximation</code>方法接受一个 SIMD 值作为参数,求出每个通道的倒数(<code>1 / x</code>),作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  85. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">reciprocalApproximation</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
  86. <span class="token comment">// Float32x4[1, 0.5, 0.3333333432674408, 0.25]</span>
  87. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>reciprocalSqrtApproximation</code>方法接受一个 SIMD 值作为参数,求出每个通道的平方根的倒数(<code>1 / (x^0.5)</code>),作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  88. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">reciprocalSqrtApproximation</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
  89. <span class="token comment">// Float32x4[1, 0.7071067690849304, 0.5773502588272095, 0.5]</span>
  90. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意,只有浮点数的数据类型才有这两个方法。</p> <h3 id="simd-integertype-shiftleftbyscalar"><a href="#simd-integertype-shiftleftbyscalar" class="header-anchor">#</a> SIMD.%IntegerType%.shiftLeftByScalar()</h3> <p><code>shiftLeftByScalar</code>方法接受一个 SIMD 值作为参数,然后将每个通道的值左移指定的位数,作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  91. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftLeftByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  92. <span class="token comment">// Int32x4[2, 4, 8, 16]</span>
  93. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果左移后,新的值超出了当前数据类型的位数,溢出的部分会被丢弃。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ix4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  94. <span class="token keyword">var</span> jx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftLeftByScalar</span><span class="token punctuation">(</span>ix4<span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  95. <span class="token comment">// Int32x4[0, 0, 0, 0]</span>
  96. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意,只有整数的数据类型才有这个方法。</p> <h3 id="simd-integertype-shiftrightbyscalar"><a href="#simd-integertype-shiftrightbyscalar" class="header-anchor">#</a> SIMD.%IntegerType%.shiftRightByScalar()</h3> <p><code>shiftRightByScalar</code>方法接受一个 SIMD 值作为参数,然后将每个通道的值右移指定的位数,返回一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  97. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftRightByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  98. <span class="token comment">// Int32x4[0, 1, 2, -4]</span>
  99. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果原来通道的值是带符号的值,则符号位保持不变,不受右移影响。如果是不带符号位的值,则右移后头部会补<code>0</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  100. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint32x4<span class="token punctuation">.</span><span class="token function">shiftRightByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  101. <span class="token comment">// Uint32x4[0, 1, 2, 2147483644]</span>
  102. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>-8</code>右移一位变成了<code>2147483644</code>,是因为对于 32 位无符号整数来说,<code>-8</code>的二进制形式是<code>11111111111111111111111111111000</code>,右移一位就变成了<code>01111111111111111111111111111100</code>,相当于<code>2147483644</code>。</p> <p>注意,只有整数的数据类型才有这个方法。</p> <h2 id="静态方法-通道处理"><a href="#静态方法-通道处理" class="header-anchor">#</a> 静态方法:通道处理</h2> <h3 id="simd-type-check"><a href="#simd-type-check" class="header-anchor">#</a> SIMD.%type%.check()</h3> <p><code>check</code>方法用于检查一个值是否为当前类型的 SIMD 值。如果是的,就返回这个值,否则就报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  103. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
  104. <span class="token comment">// Float32x4[1, 2, 3, 9]</span>
  105. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 报错</span>
  106. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">// 报错</span>
  107. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span> <span class="token comment">// 报错</span>
  108. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-extractlane-simd-type-replacelane"><a href="#simd-type-extractlane-simd-type-replacelane" class="header-anchor">#</a> SIMD.%type%.extractLane(),SIMD.%type%.replaceLane()</h3> <p><code>extractLane</code>方法用于返回给定通道的值。它接受两个参数,分别是 SIMD 值和通道编号。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  109. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
  110. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>replaceLane</code>方法用于替换指定通道的值,并返回一个新的 SIMD 值。它接受三个参数,分别是原来的 SIMD 值、通道编号和新的通道值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  111. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">replaceLane</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span>
  112. <span class="token comment">// Float32x4[1, 2, 42, 4]</span>
  113. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="simd-type-load"><a href="#simd-type-load" class="header-anchor">#</a> SIMD.%type%.load()</h3> <p><code>load</code>方法用于从二进制数组读入数据,生成一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  114. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  115. <span class="token comment">// Int32x4[1, 2, 3, 4]</span>
  116. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  117. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  118. <span class="token comment">// Int32x4[3, 4, 5, 6]</span>
  119. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>load</code>方法接受两个参数:一个二进制数组和开始读取的位置(从 0 开始)。如果位置不合法(比如<code>-1</code>或者超出二进制数组的大小),就会抛出一个错误。</p> <p>这个方法还有三个变种<code>load1()</code>、<code>load2()</code>、<code>load3()</code>,表示从指定位置开始,只加载一个通道、二个通道、三个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 格式</span>
  120. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
  121. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load1</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
  122. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load2</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
  123. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load3</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
  124. <span class="token comment">// 实例</span>
  125. <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  126. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load1</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  127. <span class="token comment">// Int32x4[1, 0, 0, 0]</span>
  128. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load2</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  129. <span class="token comment">// Int32x4[1, 2, 0, 0]</span>
  130. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load3</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  131. <span class="token comment">// Int32x4[1, 2, 3,0]</span>
  132. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="simd-type-store"><a href="#simd-type-store" class="header-anchor">#</a> SIMD.%type%.store()</h3> <p><code>store</code>方法用于将一个 SIMD 值,写入一个二进制数组。它接受三个参数,分别是二进制数组、开始写入的数组位置、SIMD 值。它返回写入值以后的二进制数组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  133. <span class="token keyword">var</span> v1 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  134. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store</span><span class="token punctuation">(</span>t1<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> v1<span class="token punctuation">)</span>
  135. <span class="token comment">// Int32Array[1, 2, 3, 4, 0, 0, 0, 0]</span>
  136. <span class="token keyword">var</span> t2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  137. <span class="token keyword">var</span> v2 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  138. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store</span><span class="token punctuation">(</span>t2<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> v2<span class="token punctuation">)</span>
  139. <span class="token comment">// Int32Array[0, 0, 1, 2, 3, 4, 0, 0]</span>
  140. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中,<code>t1</code>是一个二进制数组,<code>v1</code>是一个 SIMD 值,只有四个通道。所以写入<code>t1</code>以后,只有前四个位置有值,后四个位置都是 0。而<code>t2</code>是从 2 号位置开始写入,所以前两个位置和后两个位置都是 0。</p> <p>这个方法还有三个变种<code>store1()</code>、<code>store2()</code>和<code>store3()</code>,表示只写入一个通道、二个通道和三个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> tarray <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  141. <span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  142. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store1</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  143. <span class="token comment">// Int32Array[1, 0, 0, 0, 0, 0, 0, 0]</span>
  144. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="simd-type-splat"><a href="#simd-type-splat" class="header-anchor">#</a> SIMD.%type%.splat()</h3> <p><code>splat</code>方法返回一个新的 SIMD 值,该值的所有通道都会设成同一个预先给定的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  145. <span class="token comment">// Float32x4[3, 3, 3, 3]</span>
  146. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  147. <span class="token comment">// Float64x2[3, 3]</span>
  148. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果省略参数,所有整数型的 SIMD 值都会设定<code>0</code>,浮点型的 SIMD 值都会设成<code>NaN</code>。</p> <h3 id="simd-type-swizzle"><a href="#simd-type-swizzle" class="header-anchor">#</a> SIMD.%type%.swizzle()</h3> <p><code>swizzle</code>方法返回一个新的 SIMD 值,重新排列原有的 SIMD 值的通道顺序。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  149. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  150. <span class="token comment">// Float32x4[2,3,1,4]</span>
  151. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>swizzle</code>方法的第一个参数是原有的 SIMD 值,后面的参数对应将要返回的 SIMD 值的四个通道。它的意思是新的 SIMD 的四个通道,依次是原来 SIMD 值的 1 号通道、2 号通道、0 号通道、3 号通道。由于 SIMD 值最多可以有 16 个通道,所以<code>swizzle</code>方法除了第一个参数以外,最多还可以接受 16 个参数。</p> <p>下面是另一个例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  152. <span class="token comment">// Float32x4[1.0, 2.0, 3.0, 4.0]</span>
  153. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  154. <span class="token comment">// Float32x4[1.0, 1.0, 2.0, 2.0]</span>
  155. <span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  156. <span class="token comment">// Float32x4[4.0, 4.0, 4.0, 4.0]</span>
  157. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  158. <span class="token comment">// Float32x4[4.0, 3.0, 2.0, 1.0]</span>
  159. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="simd-type-shuffle"><a href="#simd-type-shuffle" class="header-anchor">#</a> SIMD.%type%.shuffle()</h3> <p><code>shuffle</code>方法从两个 SIMD 值之中取出指定通道,返回一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  160. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  161. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">shuffle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  162. <span class="token comment">// Float32x4[2, 6, 8, 3]</span>
  163. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中,<code>a</code>和<code>b</code>一共有 8 个通道,依次编号为 0 到 7。<code>shuffle</code>根据编号,取出相应的通道,返回一个新的 SIMD 值。</p> <h2 id="静态方法-比较运算"><a href="#静态方法-比较运算" class="header-anchor">#</a> 静态方法:比较运算</h2> <h3 id="simd-type-equal-simd-type-notequal"><a href="#simd-type-equal-simd-type-notequal" class="header-anchor">#</a> SIMD.%type%.equal(),SIMD.%type%.notEqual()</h3> <p><code>equal</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道,根据两者是否精确相等(<code>a === b</code>),得到一个布尔值。最后,所有通道的比较结果,组成一个新的 SIMD 值,作为掩码返回。<code>notEqual</code>方法则是比较两个通道是否不相等(<code>a !== b</code>)。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  164. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  165. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">equal</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
  166. <span class="token comment">// Bool32x4[true, false, false, true]</span>
  167. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">notEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  168. <span class="token comment">// Bool32x4[false, true, true, false]</span>
  169. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-greaterthan-simd-type-greaterthanorequal"><a href="#simd-type-greaterthan-simd-type-greaterthanorequal" class="header-anchor">#</a> SIMD.%type%.greaterThan(),SIMD.%type%.greaterThanOrEqual()</h3> <p><code>greatThan</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道,如果在该通道中,<code>a</code>较大就得到<code>true</code>,否则得到<code>false</code>。最后,所有通道的比较结果,组成一个新的 SIMD 值,作为掩码返回。<code>greaterThanOrEqual</code>则是比较<code>a</code>是否大于等于<code>b</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  170. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  171. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">greaterThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  172. <span class="token comment">// Bool32x4[false, true, false, true]</span>
  173. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">greaterThanOrEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  174. <span class="token comment">// Bool32x4[true, true, false, true]</span>
  175. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-lessthan-simd-type-lessthanorequal"><a href="#simd-type-lessthan-simd-type-lessthanorequal" class="header-anchor">#</a> SIMD.%type%.lessThan(),SIMD.%type%.lessThanOrEqual()</h3> <p><code>lessThan</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道,如果在该通道中,<code>a</code>较小就得到<code>true</code>,否则得到<code>false</code>。最后,所有通道的比较结果,会组成一个新的 SIMD 值,作为掩码返回。<code>lessThanOrEqual</code>方法则是比较<code>a</code>是否等于<code>b</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  176. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  177. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  178. <span class="token comment">// Bool32x4[false, true, true, false]</span>
  179. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThanOrEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  180. <span class="token comment">// Bool32x4[true, true, true, false]</span>
  181. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-select"><a href="#simd-type-select" class="header-anchor">#</a> SIMD.%type%.select()</h3> <p><code>select</code>方法通过掩码生成一个新的 SIMD 值。它接受三个参数,分别是掩码和两个 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  182. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  183. <span class="token keyword">var</span> mask <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  184. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>mask<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  185. <span class="token comment">// Float32x4[1, 6, 7, 4]</span>
  186. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中,<code>select</code>方法接受掩码和两个 SIMD 值作为参数。当某个通道对应的掩码为<code>true</code>时,会选择第一个 SIMD 值的对应通道,否则选择第二个 SIMD 值的对应通道。</p> <p>这个方法通常与比较运算符结合使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  187. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  188. <span class="token keyword">var</span> mask <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  189. <span class="token comment">// Bool32x4[false, false, true, true]</span>
  190. <span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>mask<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  191. <span class="token comment">// Float32x4[0, 6, 3, 4]</span>
  192. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中,先通过<code>lessThan</code>方法生成一个掩码,然后通过<code>select</code>方法生成一个由每个通道的较小值组成的新的 SIMD 值。</p> <h3 id="simd-booleantype-alltrue-simd-booleantype-anytrue"><a href="#simd-booleantype-alltrue-simd-booleantype-anytrue" class="header-anchor">#</a> SIMD.%BooleanType%.allTrue(),SIMD.%BooleanType%.anyTrue()</h3> <p><code>allTrue</code>方法接受一个 SIMD 值作为参数,然后返回一个布尔值,表示该 SIMD 值的所有通道是否都为<code>true</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  193. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  194. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  195. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
  196. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>anyTrue</code>方法则是只要有一个通道为<code>true</code>,就返回<code>true</code>,否则返回<code>false</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  197. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  198. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
  199. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  200. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>注意,只有四种布尔值数据类型(<code>Bool32x4</code>、<code>Bool16x8</code>、<code>Bool8x16</code>、<code>Bool64x2</code>)才有这两个方法。</p> <p>这两个方法通常与比较运算符结合使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ax4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  201. <span class="token keyword">var</span> bx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">6.0</span><span class="token punctuation">,</span> <span class="token number">7.0</span><span class="token punctuation">,</span> <span class="token number">8.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  202. <span class="token keyword">var</span> ix4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
  203. <span class="token keyword">var</span> b1 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>ix4<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
  204. <span class="token keyword">var</span> b2 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>ix4<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
  205. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="simd-type-min-simd-type-minnum"><a href="#simd-type-min-simd-type-minnum" class="header-anchor">#</a> SIMD.%type%.min(),SIMD.%type%.minNum()</h3> <p><code>min</code>方法接受两个 SIMD 值作为参数,将两者的对应通道的较小值,组成一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  206. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  207. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  208. <span class="token comment">// Float32x4[-1, -4, 3, 5.2]</span>
  209. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果有一个通道的值是<code>NaN</code>,则会优先返回<code>NaN</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
  210. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  211. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
  212. <span class="token comment">// Float64x2[NaN, 42]</span>
  213. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>minNum</code>方法与<code>min</code>的作用一模一样,唯一的区别是如果有一个通道的值是<code>NaN</code>,则会优先返回另一个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ax4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  214. <span class="token keyword">var</span> bx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  215. <span class="token keyword">var</span> cx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
  216. <span class="token comment">// Float32x4[1.0, 1.0, NaN, NaN]</span>
  217. <span class="token keyword">var</span> dx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">minNum</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
  218. <span class="token comment">// Float32x4[1.0, 1.0, 3.0, NaN]</span>
  219. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="simd-type-max-simd-type-maxnum"><a href="#simd-type-max-simd-type-maxnum" class="header-anchor">#</a> SIMD.%type%.max(),SIMD.%type%.maxNum()</h3> <p><code>max</code>方法接受两个 SIMD 值作为参数,将两者的对应通道的较大值,组成一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  220. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  221. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
  222. <span class="token comment">// Float32x4[0, -2, 6, 5.5]</span>
  223. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果有一个通道的值是<code>NaN</code>,则会优先返回<code>NaN</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
  224. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  225. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
  226. <span class="token comment">// Float64x2[NaN, Infinity]</span>
  227. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>maxNum</code>方法与<code>max</code>的作用一模一样,唯一的区别是如果有一个通道的值是<code>NaN</code>,则会优先返回另一个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
  228. <span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  229. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">maxNum</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
  230. <span class="token comment">// Float64x2[1337, Infinity]</span>
  231. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="静态方法-位运算"><a href="#静态方法-位运算" class="header-anchor">#</a> 静态方法:位运算</h2> <h3 id="simd-type-and-simd-type-or-simd-type-xor-simd-type-not"><a href="#simd-type-and-simd-type-or-simd-type-xor-simd-type-not" class="header-anchor">#</a> SIMD.%type%.and(),SIMD.%type%.or(),SIMD.%type%.xor(),SIMD.%type%.not()</h3> <p><code>and</code>方法接受两个 SIMD 值作为参数,返回两者对应的通道进行二进制<code>AND</code>运算(<code>&amp;</code>)后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  232. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  233. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">and</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  234. <span class="token comment">// Int32x4[1, 0, 4, 0]</span>
  235. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中,以通道<code>0</code>为例,<code>1</code>的二进制形式是<code>0001</code>,<code>5</code>的二进制形式是<code>01001</code>,所以进行<code>AND</code>运算以后,得到<code>0001</code>。</p> <p><code>or</code>方法接受两个 SIMD 值作为参数,返回两者对应的通道进行二进制<code>OR</code>运算(<code>|</code>)后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  236. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  237. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  238. <span class="token comment">// Int32x4[5, 7, 5, 13]</span>
  239. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>xor</code>方法接受两个 SIMD 值作为参数,返回两者对应的通道进行二进制“异或”运算(<code>^</code>)后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  240. <span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  241. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">xor</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
  242. <span class="token comment">// Int32x4[4, 7, 1, 13]</span>
  243. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>not</code>方法接受一个 SIMD 值作为参数,返回每个通道进行二进制“否”运算(<code>~</code>)后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  244. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">not</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
  245. <span class="token comment">// Int32x4[-2, -3, -5, -9]</span>
  246. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,<code>1</code>的否运算之所以得到<code>-2</code>,是因为在计算机内部,负数采用”2 的补码“这种形式进行表示。也就是说,整数<code>n</code>的负数形式<code>-n</code>,是对每一个二进制位取反以后,再加上 1。因此,直接取反就相当于负数形式再减去 1,比如<code>1</code>的负数形式是<code>-1</code>,再减去 1,就得到了<code>-2</code>。</p> <h2 id="静态方法-数据类型转换"><a href="#静态方法-数据类型转换" class="header-anchor">#</a> 静态方法:数据类型转换</h2> <p>SIMD 提供以下方法,用来将一种数据类型转为另一种数据类型。</p> <ul><li><code>SIMD.%type%.fromFloat32x4()</code></li> <li><code>SIMD.%type%.fromFloat32x4Bits()</code></li> <li><code>SIMD.%type%.fromFloat64x2Bits()</code></li> <li><code>SIMD.%type%.fromInt32x4()</code></li> <li><code>SIMD.%type%.fromInt32x4Bits()</code></li> <li><code>SIMD.%type%.fromInt16x8Bits()</code></li> <li><code>SIMD.%type%.fromInt8x16Bits()</code></li> <li><code>SIMD.%type%.fromUint32x4()</code></li> <li><code>SIMD.%type%.fromUint32x4Bits()</code></li> <li><code>SIMD.%type%.fromUint16x8Bits()</code></li> <li><code>SIMD.%type%.fromUint8x16Bits()</code></li></ul> <p>带有<code>Bits</code>后缀的方法,会原封不动地将二进制位拷贝到新的数据类型;不带后缀的方法,则会进行数据类型转换。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  247. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">fromFloat32x4</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
  248. <span class="token comment">// Int32x4[1, 2, 3, 4]</span>
  249. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">fromFloat32x4Bits</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
  250. <span class="token comment">// Int32x4[1065353216, 1073741824, 1077936128, 1082130432]</span>
  251. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中,<code>fromFloat32x4</code>是将浮点数转为整数,然后存入新的数据类型;<code>fromFloat32x4Bits</code>则是将二进制位原封不动地拷贝进入新的数据类型,然后进行解读。</p> <p><code>Bits</code>后缀的方法,还可以用于通道数目不对等的拷贝。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  252. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">fromFloat32x4Bits</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
  253. <span class="token comment">// Int16x8[0, 16256, 0, 16384, 0, 16448, 0, 16512]</span>
  254. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中,原始 SIMD 值<code>t</code>是 4 通道的,而目标值是 8 通道的。</p> <p>如果数据转换时,原通道的数据大小,超过了目标通道的最大宽度,就会报错。</p> <h2 id="实例方法"><a href="#实例方法" class="header-anchor">#</a> 实例方法</h2> <h3 id="simd-type-prototype-tostring"><a href="#simd-type-prototype-tostring" class="header-anchor">#</a> SIMD.%type%.prototype.toString()</h3> <p><code>toString</code>方法返回一个 SIMD 值的字符串形式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  255. a<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;SIMD.Float32x4(11, 22, 33, 44)&quot;</span>
  256. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="实例-求平均值"><a href="#实例-求平均值" class="header-anchor">#</a> 实例:求平均值</h2> <p>正常模式下,计算<code>n</code>个值的平均值,需要运算<code>n</code>次。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token parameter">list</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  257. <span class="token keyword">var</span> n <span class="token operator">=</span> list<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  258. <span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
  259. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  260. sum <span class="token operator">+=</span> list<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
  261. <span class="token punctuation">}</span>
  262. <span class="token keyword">return</span> sum <span class="token operator">/</span> n<span class="token punctuation">;</span>
  263. <span class="token punctuation">}</span>
  264. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>使用 SIMD,可以将计算次数减少到<code>n</code>次的四分之一。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token parameter">list</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  265. <span class="token keyword">var</span> n <span class="token operator">=</span> list<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  266. <span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  267. <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  268. sum <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>
  269. sum<span class="token punctuation">,</span>
  270. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
  271. <span class="token punctuation">)</span><span class="token punctuation">;</span>
  272. <span class="token punctuation">}</span>
  273. <span class="token keyword">var</span> total <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">+</span>
  274. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span>
  275. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span>
  276. <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  277. <span class="token keyword">return</span> total <span class="token operator">/</span> n<span class="token punctuation">;</span>
  278. <span class="token punctuation">}</span>
  279. </code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码先是每隔四位,将所有的值读入一个 SIMD,然后立刻累加。然后,得到累加值四个通道的总和,再除以<code>n</code>就可以了。</p></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/heBody/blog/edit/master/docs/《ES6 教程》笔记/33.SIMD.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/blog/tags/?tag=ES6" title="标签">#ES6</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/12/14, 19:36:42</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/blog/pages/6a8e2dc558da1b39/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Mixin</div></a> <a href="/blog/pages/ea6f3b870f6dab69/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">参考链接</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
  280. <a href="/blog/pages/6a8e2dc558da1b39/" class="prev">Mixin</a></span> <span class="next"><a href="/blog/pages/ea6f3b870f6dab69/">参考链接</a>→
  281. </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/blog/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/blog/pages/922650/"><div>
  282. Git修改分支名
  283. <!----></div></a> <span class="date">08-11</span></dt></dl><dl><dd>02</dd> <dt><a href="/blog/pages/55f894/"><div>
  284. CSS给table的tbody添加滚动条
  285. <!----></div></a> <span class="date">06-29</span></dt></dl><dl><dd>03</dd> <dt><a href="/blog/pages/829589/"><div>
  286. 我做了一个手写春联小网页,祝大家虎年暴富
  287. <span class="title-tag">
  288. 原创
  289. </span></div></a> <span class="date">01-28</span></dt></dl> <dl><dd></dd> <dt><a href="/blog/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:30363811@qq.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/heBody" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
  290. Copyright © 2016-2022
  291. <span>Hesb | <a href="https://github.com/heBody/blob" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
  292. 跟随系统
  293. </li><li class="iconfont icon-rijianmoshi">
  294. 浅色模式
  295. </li><li class="iconfont icon-yejianmoshi">
  296. 深色模式
  297. </li><li class="iconfont icon-yuedu">
  298. 阅读模式
  299. </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
  300. <script src="/blog/assets/js/app.14d95a24.js" defer></script><script src="/blog/assets/js/2.395c0d18.js" defer></script><script src="/blog/assets/js/3.6748bd5c.js" defer></script><script src="/blog/assets/js/120.9be3d6fd.js" defer></script>
  301. </body>
  302. </html>