endian.html 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 1.5.8">
  8. <meta name="author" content="Beman Dawes">
  9. <title>Boost.Endian: The Boost Endian Library</title>
  10. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
  11. <style>
  12. /* Asciidoctor default stylesheet | MIT License | http://asciidoctor.org */
  13. /* Uncomment @import statement below to use as custom stylesheet */
  14. /*@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700";*/
  15. article,aside,details,figcaption,figure,footer,header,hgroup,main,nav,section,summary{display:block}
  16. audio,canvas,video{display:inline-block}
  17. audio:not([controls]){display:none;height:0}
  18. script{display:none!important}
  19. html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}
  20. a{background:transparent}
  21. a:focus{outline:thin dotted}
  22. a:active,a:hover{outline:0}
  23. h1{font-size:2em;margin:.67em 0}
  24. abbr[title]{border-bottom:1px dotted}
  25. b,strong{font-weight:bold}
  26. dfn{font-style:italic}
  27. hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}
  28. mark{background:#ff0;color:#000}
  29. code,kbd,pre,samp{font-family:monospace;font-size:1em}
  30. pre{white-space:pre-wrap}
  31. q{quotes:"\201C" "\201D" "\2018" "\2019"}
  32. small{font-size:80%}
  33. sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
  34. sup{top:-.5em}
  35. sub{bottom:-.25em}
  36. img{border:0}
  37. svg:not(:root){overflow:hidden}
  38. figure{margin:0}
  39. fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}
  40. legend{border:0;padding:0}
  41. button,input,select,textarea{font-family:inherit;font-size:100%;margin:0}
  42. button,input{line-height:normal}
  43. button,select{text-transform:none}
  44. button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}
  45. button[disabled],html input[disabled]{cursor:default}
  46. input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}
  47. button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
  48. textarea{overflow:auto;vertical-align:top}
  49. table{border-collapse:collapse;border-spacing:0}
  50. *,*::before,*::after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}
  51. html,body{font-size:100%}
  52. body{background:#fff;color:rgba(0,0,0,.8);padding:0;margin:0;font-family:"Noto Serif","DejaVu Serif",serif;font-weight:400;font-style:normal;line-height:1;position:relative;cursor:auto;tab-size:4;-moz-osx-font-smoothing:grayscale;-webkit-font-smoothing:antialiased}
  53. a:hover{cursor:pointer}
  54. img,object,embed{max-width:100%;height:auto}
  55. object,embed{height:100%}
  56. img{-ms-interpolation-mode:bicubic}
  57. .left{float:left!important}
  58. .right{float:right!important}
  59. .text-left{text-align:left!important}
  60. .text-right{text-align:right!important}
  61. .text-center{text-align:center!important}
  62. .text-justify{text-align:justify!important}
  63. .hide{display:none}
  64. img,object,svg{display:inline-block;vertical-align:middle}
  65. textarea{height:auto;min-height:50px}
  66. select{width:100%}
  67. .center{margin-left:auto;margin-right:auto}
  68. .stretch{width:100%}
  69. .subheader,.admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{line-height:1.45;color:#7a2518;font-weight:400;margin-top:0;margin-bottom:.25em}
  70. div,dl,dt,dd,ul,ol,li,h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6,pre,form,p,blockquote,th,td{margin:0;padding:0;direction:ltr}
  71. a{color:#2156a5;text-decoration:underline;line-height:inherit}
  72. a:hover,a:focus{color:#1d4b8f}
  73. a img{border:none}
  74. p{font-family:inherit;font-weight:400;font-size:1em;line-height:1.6;margin-bottom:1.25em;text-rendering:optimizeLegibility}
  75. p aside{font-size:.875em;line-height:1.35;font-style:italic}
  76. h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Open Sans","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#ba3925;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.0125em}
  77. h1 small,h2 small,h3 small,#toctitle small,.sidebarblock>.content>.title small,h4 small,h5 small,h6 small{font-size:60%;color:#e99b8f;line-height:0}
  78. h1{font-size:2.125em}
  79. h2{font-size:1.6875em}
  80. h3,#toctitle,.sidebarblock>.content>.title{font-size:1.375em}
  81. h4,h5{font-size:1.125em}
  82. h6{font-size:1em}
  83. hr{border:solid #dddddf;border-width:1px 0 0;clear:both;margin:1.25em 0 1.1875em;height:0}
  84. em,i{font-style:italic;line-height:inherit}
  85. strong,b{font-weight:bold;line-height:inherit}
  86. small{font-size:60%;line-height:inherit}
  87. code{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;color:rgba(0,0,0,.9)}
  88. ul,ol,dl{font-size:1em;line-height:1.6;margin-bottom:1.25em;list-style-position:outside;font-family:inherit}
  89. ul,ol{margin-left:1.5em}
  90. ul li ul,ul li ol{margin-left:1.25em;margin-bottom:0;font-size:1em}
  91. ul.square li ul,ul.circle li ul,ul.disc li ul{list-style:inherit}
  92. ul.square{list-style-type:square}
  93. ul.circle{list-style-type:circle}
  94. ul.disc{list-style-type:disc}
  95. ol li ul,ol li ol{margin-left:1.25em;margin-bottom:0}
  96. dl dt{margin-bottom:.3125em;font-weight:bold}
  97. dl dd{margin-bottom:1.25em}
  98. abbr,acronym{text-transform:uppercase;font-size:90%;color:rgba(0,0,0,.8);border-bottom:1px dotted #ddd;cursor:help}
  99. abbr{text-transform:none}
  100. blockquote{margin:0 0 1.25em;padding:.5625em 1.25em 0 1.1875em;border-left:1px solid #ddd}
  101. blockquote cite{display:block;font-size:.9375em;color:rgba(0,0,0,.6)}
  102. blockquote cite::before{content:"\2014 \0020"}
  103. blockquote cite a,blockquote cite a:visited{color:rgba(0,0,0,.6)}
  104. blockquote,blockquote p{line-height:1.6;color:rgba(0,0,0,.85)}
  105. @media screen and (min-width:768px){h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2}
  106. h1{font-size:2.75em}
  107. h2{font-size:2.3125em}
  108. h3,#toctitle,.sidebarblock>.content>.title{font-size:1.6875em}
  109. h4{font-size:1.4375em}}
  110. table{background:#fff;margin-bottom:1.25em;border:solid 1px #dedede}
  111. table thead,table tfoot{background:#f7f8f7}
  112. table thead tr th,table thead tr td,table tfoot tr th,table tfoot tr td{padding:.5em .625em .625em;font-size:inherit;color:rgba(0,0,0,.8);text-align:left}
  113. table tr th,table tr td{padding:.5625em .625em;font-size:inherit;color:rgba(0,0,0,.8)}
  114. table tr.even,table tr.alt,table tr:nth-of-type(even){background:#f8f8f7}
  115. table thead tr th,table tfoot tr th,table tbody tr td,table tr td,table tfoot tr td{display:table-cell;line-height:1.6}
  116. h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{line-height:1.2;word-spacing:-.05em}
  117. h1 strong,h2 strong,h3 strong,#toctitle strong,.sidebarblock>.content>.title strong,h4 strong,h5 strong,h6 strong{font-weight:400}
  118. .clearfix::before,.clearfix::after,.float-group::before,.float-group::after{content:" ";display:table}
  119. .clearfix::after,.float-group::after{clear:both}
  120. *:not(pre)>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background-color:#f7f7f8;-webkit-border-radius:4px;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed;word-wrap:break-word}
  121. *:not(pre)>code.nobreak{word-wrap:normal}
  122. *:not(pre)>code.nowrap{white-space:nowrap}
  123. pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeSpeed}
  124. em em{font-style:normal}
  125. strong strong{font-weight:400}
  126. .keyseq{color:rgba(51,51,51,.8)}
  127. kbd{font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
  128. .keyseq kbd:first-child{margin-left:0}
  129. .keyseq kbd:last-child{margin-right:0}
  130. .menuseq,.menuref{color:#000}
  131. .menuseq b:not(.caret),.menuref{font-weight:inherit}
  132. .menuseq{word-spacing:-.02em}
  133. .menuseq b.caret{font-size:1.25em;line-height:.8}
  134. .menuseq i.caret{font-weight:bold;text-align:center;width:.45em}
  135. b.button::before,b.button::after{position:relative;top:-1px;font-weight:400}
  136. b.button::before{content:"[";padding:0 3px 0 2px}
  137. b.button::after{content:"]";padding:0 2px 0 3px}
  138. p a>code:hover{color:rgba(0,0,0,.9)}
  139. #header,#content,#footnotes,#footer{width:100%;margin-left:auto;margin-right:auto;margin-top:0;margin-bottom:0;max-width:62.5em;*zoom:1;position:relative;padding-left:.9375em;padding-right:.9375em}
  140. #header::before,#header::after,#content::before,#content::after,#footnotes::before,#footnotes::after,#footer::before,#footer::after{content:" ";display:table}
  141. #header::after,#content::after,#footnotes::after,#footer::after{clear:both}
  142. #content{margin-top:1.25em}
  143. #content::before{content:none}
  144. #header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0}
  145. #header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #dddddf}
  146. #header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px}
  147. #header .details{border-bottom:1px solid #dddddf;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:-ms-flexbox;display:-webkit-flex;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap}
  148. #header .details span:first-child{margin-left:-.125em}
  149. #header .details span.email a{color:rgba(0,0,0,.85)}
  150. #header .details br{display:none}
  151. #header .details br+span::before{content:"\00a0\2013\00a0"}
  152. #header .details br+span.author::before{content:"\00a0\22c5\00a0";color:rgba(0,0,0,.85)}
  153. #header .details br+span#revremark::before{content:"\00a0|\00a0"}
  154. #header #revnumber{text-transform:capitalize}
  155. #header #revnumber::after{content:"\00a0"}
  156. #content>h1:first-child:not([class]){color:rgba(0,0,0,.85);border-bottom:1px solid #dddddf;padding-bottom:8px;margin-top:0;padding-top:1rem;margin-bottom:1.25rem}
  157. #toc{border-bottom:1px solid #e7e7e9;padding-bottom:.5em}
  158. #toc>ul{margin-left:.125em}
  159. #toc ul.sectlevel0>li>a{font-style:italic}
  160. #toc ul.sectlevel0 ul.sectlevel1{margin:.5em 0}
  161. #toc ul{font-family:"Open Sans","DejaVu Sans",sans-serif;list-style-type:none}
  162. #toc li{line-height:1.3334;margin-top:.3334em}
  163. #toc a{text-decoration:none}
  164. #toc a:active{text-decoration:underline}
  165. #toctitle{color:#7a2518;font-size:1.2em}
  166. @media screen and (min-width:768px){#toctitle{font-size:1.375em}
  167. body.toc2{padding-left:15em;padding-right:0}
  168. #toc.toc2{margin-top:0!important;background-color:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #e7e7e9;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto}
  169. #toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em}
  170. #toc.toc2>ul{font-size:.9em;margin-bottom:0}
  171. #toc.toc2 ul ul{margin-left:0;padding-left:1em}
  172. #toc.toc2 ul.sectlevel0 ul.sectlevel1{padding-left:0;margin-top:.5em;margin-bottom:.5em}
  173. body.toc2.toc-right{padding-left:0;padding-right:15em}
  174. body.toc2.toc-right #toc.toc2{border-right-width:0;border-left:1px solid #e7e7e9;left:auto;right:0}}
  175. @media screen and (min-width:1280px){body.toc2{padding-left:20em;padding-right:0}
  176. #toc.toc2{width:20em}
  177. #toc.toc2 #toctitle{font-size:1.375em}
  178. #toc.toc2>ul{font-size:.95em}
  179. #toc.toc2 ul ul{padding-left:1.25em}
  180. body.toc2.toc-right{padding-left:0;padding-right:20em}}
  181. #content #toc{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
  182. #content #toc>:first-child{margin-top:0}
  183. #content #toc>:last-child{margin-bottom:0}
  184. #footer{max-width:100%;background-color:rgba(0,0,0,.8);padding:1.25em}
  185. #footer-text{color:rgba(255,255,255,.8);line-height:1.44}
  186. #content{margin-bottom:.625em}
  187. .sect1{padding-bottom:.625em}
  188. @media screen and (min-width:768px){#content{margin-bottom:1.25em}
  189. .sect1{padding-bottom:1.25em}}
  190. .sect1:last-child{padding-bottom:0}
  191. .sect1+.sect1{border-top:1px solid #e7e7e9}
  192. #content h1>a.anchor,h2>a.anchor,h3>a.anchor,#toctitle>a.anchor,.sidebarblock>.content>.title>a.anchor,h4>a.anchor,h5>a.anchor,h6>a.anchor{position:absolute;z-index:1001;width:1.5ex;margin-left:-1.5ex;display:block;text-decoration:none!important;visibility:hidden;text-align:center;font-weight:400}
  193. #content h1>a.anchor::before,h2>a.anchor::before,h3>a.anchor::before,#toctitle>a.anchor::before,.sidebarblock>.content>.title>a.anchor::before,h4>a.anchor::before,h5>a.anchor::before,h6>a.anchor::before{content:"\00A7";font-size:.85em;display:block;padding-top:.1em}
  194. #content h1:hover>a.anchor,#content h1>a.anchor:hover,h2:hover>a.anchor,h2>a.anchor:hover,h3:hover>a.anchor,#toctitle:hover>a.anchor,.sidebarblock>.content>.title:hover>a.anchor,h3>a.anchor:hover,#toctitle>a.anchor:hover,.sidebarblock>.content>.title>a.anchor:hover,h4:hover>a.anchor,h4>a.anchor:hover,h5:hover>a.anchor,h5>a.anchor:hover,h6:hover>a.anchor,h6>a.anchor:hover{visibility:visible}
  195. #content h1>a.link,h2>a.link,h3>a.link,#toctitle>a.link,.sidebarblock>.content>.title>a.link,h4>a.link,h5>a.link,h6>a.link{color:#ba3925;text-decoration:none}
  196. #content h1>a.link:hover,h2>a.link:hover,h3>a.link:hover,#toctitle>a.link:hover,.sidebarblock>.content>.title>a.link:hover,h4>a.link:hover,h5>a.link:hover,h6>a.link:hover{color:#a53221}
  197. .audioblock,.imageblock,.literalblock,.listingblock,.stemblock,.videoblock{margin-bottom:1.25em}
  198. .admonitionblock td.content>.title,.audioblock>.title,.exampleblock>.title,.imageblock>.title,.listingblock>.title,.literalblock>.title,.stemblock>.title,.openblock>.title,.paragraph>.title,.quoteblock>.title,table.tableblock>.title,.verseblock>.title,.videoblock>.title,.dlist>.title,.olist>.title,.ulist>.title,.qlist>.title,.hdlist>.title{text-rendering:optimizeLegibility;text-align:left;font-family:"Noto Serif","DejaVu Serif",serif;font-size:1rem;font-style:italic}
  199. table.tableblock.fit-content>caption.title{white-space:nowrap;width:0}
  200. .paragraph.lead>p,#preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:1.21875em;line-height:1.6;color:rgba(0,0,0,.85)}
  201. table.tableblock #preamble>.sectionbody>[class="paragraph"]:first-of-type p{font-size:inherit}
  202. .admonitionblock>table{border-collapse:separate;border:0;background:none;width:100%}
  203. .admonitionblock>table td.icon{text-align:center;width:80px}
  204. .admonitionblock>table td.icon img{max-width:none}
  205. .admonitionblock>table td.icon .title{font-weight:bold;font-family:"Open Sans","DejaVu Sans",sans-serif;text-transform:uppercase}
  206. .admonitionblock>table td.content{padding-left:1.125em;padding-right:1.25em;border-left:1px solid #dddddf;color:rgba(0,0,0,.6)}
  207. .admonitionblock>table td.content>:last-child>:last-child{margin-bottom:0}
  208. .exampleblock>.content{border-style:solid;border-width:1px;border-color:#e6e6e6;margin-bottom:1.25em;padding:1.25em;background:#fff;-webkit-border-radius:4px;border-radius:4px}
  209. .exampleblock>.content>:first-child{margin-top:0}
  210. .exampleblock>.content>:last-child{margin-bottom:0}
  211. .sidebarblock{border-style:solid;border-width:1px;border-color:#e0e0dc;margin-bottom:1.25em;padding:1.25em;background:#f8f8f7;-webkit-border-radius:4px;border-radius:4px}
  212. .sidebarblock>:first-child{margin-top:0}
  213. .sidebarblock>:last-child{margin-bottom:0}
  214. .sidebarblock>.content>.title{color:#7a2518;margin-top:0;text-align:center}
  215. .exampleblock>.content>:last-child>:last-child,.exampleblock>.content .olist>ol>li:last-child>:last-child,.exampleblock>.content .ulist>ul>li:last-child>:last-child,.exampleblock>.content .qlist>ol>li:last-child>:last-child,.sidebarblock>.content>:last-child>:last-child,.sidebarblock>.content .olist>ol>li:last-child>:last-child,.sidebarblock>.content .ulist>ul>li:last-child>:last-child,.sidebarblock>.content .qlist>ol>li:last-child>:last-child{margin-bottom:0}
  216. .literalblock pre,.listingblock pre:not(.highlight),.listingblock pre[class="highlight"],.listingblock pre[class^="highlight "],.listingblock pre.CodeRay,.listingblock pre.prettyprint{background:#f7f7f8}
  217. .sidebarblock .literalblock pre,.sidebarblock .listingblock pre:not(.highlight),.sidebarblock .listingblock pre[class="highlight"],.sidebarblock .listingblock pre[class^="highlight "],.sidebarblock .listingblock pre.CodeRay,.sidebarblock .listingblock pre.prettyprint{background:#f2f1f1}
  218. .literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{-webkit-border-radius:4px;border-radius:4px;word-wrap:break-word;overflow-x:auto;padding:1em;font-size:.8125em}
  219. @media screen and (min-width:768px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:.90625em}}
  220. @media screen and (min-width:1280px){.literalblock pre,.literalblock pre[class],.listingblock pre,.listingblock pre[class]{font-size:1em}}
  221. .literalblock pre.nowrap,.literalblock pre.nowrap pre,.listingblock pre.nowrap,.listingblock pre.nowrap pre{white-space:pre;word-wrap:normal}
  222. .literalblock.output pre{color:#f7f7f8;background-color:rgba(0,0,0,.9)}
  223. .listingblock pre.highlightjs{padding:0}
  224. .listingblock pre.highlightjs>code{padding:1em;-webkit-border-radius:4px;border-radius:4px}
  225. .listingblock pre.prettyprint{border-width:0}
  226. .listingblock>.content{position:relative}
  227. .listingblock code[data-lang]::before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:#999}
  228. .listingblock:hover code[data-lang]::before{display:block}
  229. .listingblock.terminal pre .command::before{content:attr(data-prompt);padding-right:.5em;color:#999}
  230. .listingblock.terminal pre .command:not([data-prompt])::before{content:"$"}
  231. table.pyhltable{border-collapse:separate;border:0;margin-bottom:0;background:none}
  232. table.pyhltable td{vertical-align:top;padding-top:0;padding-bottom:0;line-height:1.45}
  233. table.pyhltable td.code{padding-left:.75em;padding-right:0}
  234. pre.pygments .lineno,table.pyhltable td:not(.code){color:#999;padding-left:0;padding-right:.5em;border-right:1px solid #dddddf}
  235. pre.pygments .lineno{display:inline-block;margin-right:.25em}
  236. table.pyhltable .linenodiv{background:none!important;padding-right:0!important}
  237. .quoteblock{margin:0 1em 1.25em 1.5em;display:table}
  238. .quoteblock>.title{margin-left:-1.5em;margin-bottom:.75em}
  239. .quoteblock blockquote,.quoteblock p{color:rgba(0,0,0,.85);font-size:1.15rem;line-height:1.75;word-spacing:.1em;letter-spacing:0;font-style:italic;text-align:justify}
  240. .quoteblock blockquote{margin:0;padding:0;border:0}
  241. .quoteblock blockquote::before{content:"\201c";float:left;font-size:2.75em;font-weight:bold;line-height:.6em;margin-left:-.6em;color:#7a2518;text-shadow:0 1px 2px rgba(0,0,0,.1)}
  242. .quoteblock blockquote>.paragraph:last-child p{margin-bottom:0}
  243. .quoteblock .attribution{margin-top:.75em;margin-right:.5ex;text-align:right}
  244. .verseblock{margin:0 1em 1.25em}
  245. .verseblock pre{font-family:"Open Sans","DejaVu Sans",sans;font-size:1.15rem;color:rgba(0,0,0,.85);font-weight:300;text-rendering:optimizeLegibility}
  246. .verseblock pre strong{font-weight:400}
  247. .verseblock .attribution{margin-top:1.25rem;margin-left:.5ex}
  248. .quoteblock .attribution,.verseblock .attribution{font-size:.9375em;line-height:1.45;font-style:italic}
  249. .quoteblock .attribution br,.verseblock .attribution br{display:none}
  250. .quoteblock .attribution cite,.verseblock .attribution cite{display:block;letter-spacing:-.025em;color:rgba(0,0,0,.6)}
  251. .quoteblock.abstract blockquote::before,.quoteblock.excerpt blockquote::before,.quoteblock .quoteblock blockquote::before{display:none}
  252. .quoteblock.abstract blockquote,.quoteblock.abstract p,.quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{line-height:1.6;word-spacing:0}
  253. .quoteblock.abstract{margin:0 1em 1.25em;display:block}
  254. .quoteblock.abstract>.title{margin:0 0 .375em;font-size:1.15em;text-align:center}
  255. .quoteblock.excerpt,.quoteblock .quoteblock{margin:0 0 1.25em;padding:0 0 .25em 1em;border-left:.25em solid #dddddf}
  256. .quoteblock.excerpt blockquote,.quoteblock.excerpt p,.quoteblock .quoteblock blockquote,.quoteblock .quoteblock p{color:inherit;font-size:1.0625rem}
  257. .quoteblock.excerpt .attribution,.quoteblock .quoteblock .attribution{color:inherit;text-align:left;margin-right:0}
  258. table.tableblock{max-width:100%;border-collapse:separate}
  259. p.tableblock:last-child{margin-bottom:0}
  260. td.tableblock>.content{margin-bottom:-1.25em}
  261. table.tableblock,th.tableblock,td.tableblock{border:0 solid #dedede}
  262. table.grid-all>thead>tr>.tableblock,table.grid-all>tbody>tr>.tableblock{border-width:0 1px 1px 0}
  263. table.grid-all>tfoot>tr>.tableblock{border-width:1px 1px 0 0}
  264. table.grid-cols>*>tr>.tableblock{border-width:0 1px 0 0}
  265. table.grid-rows>thead>tr>.tableblock,table.grid-rows>tbody>tr>.tableblock{border-width:0 0 1px}
  266. table.grid-rows>tfoot>tr>.tableblock{border-width:1px 0 0}
  267. table.grid-all>*>tr>.tableblock:last-child,table.grid-cols>*>tr>.tableblock:last-child{border-right-width:0}
  268. table.grid-all>tbody>tr:last-child>.tableblock,table.grid-all>thead:last-child>tr>.tableblock,table.grid-rows>tbody>tr:last-child>.tableblock,table.grid-rows>thead:last-child>tr>.tableblock{border-bottom-width:0}
  269. table.frame-all{border-width:1px}
  270. table.frame-sides{border-width:0 1px}
  271. table.frame-topbot,table.frame-ends{border-width:1px 0}
  272. table.stripes-all tr,table.stripes-odd tr:nth-of-type(odd){background:#f8f8f7}
  273. table.stripes-none tr,table.stripes-odd tr:nth-of-type(even){background:none}
  274. th.halign-left,td.halign-left{text-align:left}
  275. th.halign-right,td.halign-right{text-align:right}
  276. th.halign-center,td.halign-center{text-align:center}
  277. th.valign-top,td.valign-top{vertical-align:top}
  278. th.valign-bottom,td.valign-bottom{vertical-align:bottom}
  279. th.valign-middle,td.valign-middle{vertical-align:middle}
  280. table thead th,table tfoot th{font-weight:bold}
  281. tbody tr th{display:table-cell;line-height:1.6;background:#f7f8f7}
  282. tbody tr th,tbody tr th p,tfoot tr th,tfoot tr th p{color:rgba(0,0,0,.8);font-weight:bold}
  283. p.tableblock>code:only-child{background:none;padding:0}
  284. p.tableblock{font-size:1em}
  285. td>div.verse{white-space:pre}
  286. ol{margin-left:1.75em}
  287. ul li ol{margin-left:1.5em}
  288. dl dd{margin-left:1.125em}
  289. dl dd:last-child,dl dd:last-child>:last-child{margin-bottom:0}
  290. ol>li p,ul>li p,ul dd,ol dd,.olist .olist,.ulist .ulist,.ulist .olist,.olist .ulist{margin-bottom:.625em}
  291. ul.checklist,ul.none,ol.none,ul.no-bullet,ol.no-bullet,ol.unnumbered,ul.unstyled,ol.unstyled{list-style-type:none}
  292. ul.no-bullet,ol.no-bullet,ol.unnumbered{margin-left:.625em}
  293. ul.unstyled,ol.unstyled{margin-left:0}
  294. ul.checklist{margin-left:.625em}
  295. ul.checklist li>p:first-child>.fa-square-o:first-child,ul.checklist li>p:first-child>.fa-check-square-o:first-child{width:1.25em;font-size:.8em;position:relative;bottom:.125em}
  296. ul.checklist li>p:first-child>input[type="checkbox"]:first-child{margin-right:.25em}
  297. ul.inline{display:-ms-flexbox;display:-webkit-box;display:flex;-ms-flex-flow:row wrap;-webkit-flex-flow:row wrap;flex-flow:row wrap;list-style:none;margin:0 0 .625em -1.25em}
  298. ul.inline>li{margin-left:1.25em}
  299. .unstyled dl dt{font-weight:400;font-style:normal}
  300. ol.arabic{list-style-type:decimal}
  301. ol.decimal{list-style-type:decimal-leading-zero}
  302. ol.loweralpha{list-style-type:lower-alpha}
  303. ol.upperalpha{list-style-type:upper-alpha}
  304. ol.lowerroman{list-style-type:lower-roman}
  305. ol.upperroman{list-style-type:upper-roman}
  306. ol.lowergreek{list-style-type:lower-greek}
  307. .hdlist>table,.colist>table{border:0;background:none}
  308. .hdlist>table>tbody>tr,.colist>table>tbody>tr{background:none}
  309. td.hdlist1,td.hdlist2{vertical-align:top;padding:0 .625em}
  310. td.hdlist1{font-weight:bold;padding-bottom:1.25em}
  311. .literalblock+.colist,.listingblock+.colist{margin-top:-.5em}
  312. .colist td:not([class]):first-child{padding:.4em .75em 0;line-height:1;vertical-align:top}
  313. .colist td:not([class]):first-child img{max-width:none}
  314. .colist td:not([class]):last-child{padding:.25em 0}
  315. .thumb,.th{line-height:0;display:inline-block;border:solid 4px #fff;-webkit-box-shadow:0 0 0 1px #ddd;box-shadow:0 0 0 1px #ddd}
  316. .imageblock.left{margin:.25em .625em 1.25em 0}
  317. .imageblock.right{margin:.25em 0 1.25em .625em}
  318. .imageblock>.title{margin-bottom:0}
  319. .imageblock.thumb,.imageblock.th{border-width:6px}
  320. .imageblock.thumb>.title,.imageblock.th>.title{padding:0 .125em}
  321. .image.left,.image.right{margin-top:.25em;margin-bottom:.25em;display:inline-block;line-height:0}
  322. .image.left{margin-right:.625em}
  323. .image.right{margin-left:.625em}
  324. a.image{text-decoration:none;display:inline-block}
  325. a.image object{pointer-events:none}
  326. sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super}
  327. sup.footnote a,sup.footnoteref a{text-decoration:none}
  328. sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline}
  329. #footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em}
  330. #footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em;border-width:1px 0 0}
  331. #footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;margin-bottom:.2em}
  332. #footnotes .footnote a:first-of-type{font-weight:bold;text-decoration:none;margin-left:-1.05em}
  333. #footnotes .footnote:last-of-type{margin-bottom:0}
  334. #content #footnotes{margin-top:-.625em;margin-bottom:0;padding:.75em 0}
  335. .gist .file-data>table{border:0;background:#fff;width:100%;margin-bottom:0}
  336. .gist .file-data>table td.line-data{width:99%}
  337. div.unbreakable{page-break-inside:avoid}
  338. .big{font-size:larger}
  339. .small{font-size:smaller}
  340. .underline{text-decoration:underline}
  341. .overline{text-decoration:overline}
  342. .line-through{text-decoration:line-through}
  343. .aqua{color:#00bfbf}
  344. .aqua-background{background-color:#00fafa}
  345. .black{color:#000}
  346. .black-background{background-color:#000}
  347. .blue{color:#0000bf}
  348. .blue-background{background-color:#0000fa}
  349. .fuchsia{color:#bf00bf}
  350. .fuchsia-background{background-color:#fa00fa}
  351. .gray{color:#606060}
  352. .gray-background{background-color:#7d7d7d}
  353. .green{color:#006000}
  354. .green-background{background-color:#007d00}
  355. .lime{color:#00bf00}
  356. .lime-background{background-color:#00fa00}
  357. .maroon{color:#600000}
  358. .maroon-background{background-color:#7d0000}
  359. .navy{color:#000060}
  360. .navy-background{background-color:#00007d}
  361. .olive{color:#606000}
  362. .olive-background{background-color:#7d7d00}
  363. .purple{color:#600060}
  364. .purple-background{background-color:#7d007d}
  365. .red{color:#bf0000}
  366. .red-background{background-color:#fa0000}
  367. .silver{color:#909090}
  368. .silver-background{background-color:#bcbcbc}
  369. .teal{color:#006060}
  370. .teal-background{background-color:#007d7d}
  371. .white{color:#bfbfbf}
  372. .white-background{background-color:#fafafa}
  373. .yellow{color:#bfbf00}
  374. .yellow-background{background-color:#fafa00}
  375. span.icon>.fa{cursor:default}
  376. a span.icon>.fa{cursor:inherit}
  377. .admonitionblock td.icon [class^="fa icon-"]{font-size:2.5em;text-shadow:1px 1px 2px rgba(0,0,0,.5);cursor:default}
  378. .admonitionblock td.icon .icon-note::before{content:"\f05a";color:#19407c}
  379. .admonitionblock td.icon .icon-tip::before{content:"\f0eb";text-shadow:1px 1px 2px rgba(155,155,0,.8);color:#111}
  380. .admonitionblock td.icon .icon-warning::before{content:"\f071";color:#bf6900}
  381. .admonitionblock td.icon .icon-caution::before{content:"\f06d";color:#bf3400}
  382. .admonitionblock td.icon .icon-important::before{content:"\f06a";color:#bf0000}
  383. .conum[data-value]{display:inline-block;color:#fff!important;background-color:rgba(0,0,0,.8);-webkit-border-radius:100px;border-radius:100px;text-align:center;font-size:.75em;width:1.67em;height:1.67em;line-height:1.67em;font-family:"Open Sans","DejaVu Sans",sans-serif;font-style:normal;font-weight:bold}
  384. .conum[data-value] *{color:#fff!important}
  385. .conum[data-value]+b{display:none}
  386. .conum[data-value]::after{content:attr(data-value)}
  387. pre .conum[data-value]{position:relative;top:-.125em}
  388. b.conum *{color:inherit!important}
  389. .conum:not([data-value]):empty{display:none}
  390. dt,th.tableblock,td.content,div.footnote{text-rendering:optimizeLegibility}
  391. h1,h2,p,td.content,span.alt{letter-spacing:-.01em}
  392. p strong,td.content strong,div.footnote strong{letter-spacing:-.005em}
  393. p,blockquote,dt,td.content,span.alt{font-size:1.0625rem}
  394. p{margin-bottom:1.25rem}
  395. .sidebarblock p,.sidebarblock dt,.sidebarblock td.content,p.tableblock{font-size:1em}
  396. .exampleblock>.content{background-color:#fffef7;border-color:#e0e0dc;-webkit-box-shadow:0 1px 4px #e0e0dc;box-shadow:0 1px 4px #e0e0dc}
  397. .print-only{display:none!important}
  398. @page{margin:1.25cm .75cm}
  399. @media print{*{-webkit-box-shadow:none!important;box-shadow:none!important;text-shadow:none!important}
  400. html{font-size:80%}
  401. a{color:inherit!important;text-decoration:underline!important}
  402. a.bare,a[href^="#"],a[href^="mailto:"]{text-decoration:none!important}
  403. a[href^="http:"]:not(.bare)::after,a[href^="https:"]:not(.bare)::after{content:"(" attr(href) ")";display:inline-block;font-size:.875em;padding-left:.25em}
  404. abbr[title]::after{content:" (" attr(title) ")"}
  405. pre,blockquote,tr,img,object,svg{page-break-inside:avoid}
  406. thead{display:table-header-group}
  407. svg{max-width:100%}
  408. p,blockquote,dt,td.content{font-size:1em;orphans:3;widows:3}
  409. h2,h3,#toctitle,.sidebarblock>.content>.title{page-break-after:avoid}
  410. #toc,.sidebarblock,.exampleblock>.content{background:none!important}
  411. #toc{border-bottom:1px solid #dddddf!important;padding-bottom:0!important}
  412. body.book #header{text-align:center}
  413. body.book #header>h1:first-child{border:0!important;margin:2.5em 0 1em}
  414. body.book #header .details{border:0!important;display:block;padding:0!important}
  415. body.book #header .details span:first-child{margin-left:0!important}
  416. body.book #header .details br{display:block}
  417. body.book #header .details br+span::before{content:none!important}
  418. body.book #toc{border:0!important;text-align:left!important;padding:0!important;margin:0!important}
  419. body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-break-before:always}
  420. .listingblock code[data-lang]::before{display:block}
  421. #footer{padding:0 .9375em}
  422. .hide-on-print{display:none!important}
  423. .print-only{display:block!important}
  424. .hide-for-print{display:none!important}
  425. .show-for-print{display:inherit!important}}
  426. @media print,amzn-kf8{#header>h1:first-child{margin-top:1.25rem}
  427. .sect1{padding:0!important}
  428. .sect1+.sect1{border:0}
  429. #footer{background:none}
  430. #footer-text{color:rgba(0,0,0,.6);font-size:.9em}}
  431. @media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}}
  432. </style>
  433. </head>
  434. <body class="article toc2 toc-left">
  435. <div id="header">
  436. <h1>Boost.Endian: The Boost Endian Library</h1>
  437. <div class="details">
  438. <span id="author" class="author">Beman Dawes</span><br>
  439. </div>
  440. <div id="toc" class="toc2">
  441. <div id="toctitle">Table of Contents</div>
  442. <ul class="sectlevel1">
  443. <li><a href="#overview">Overview</a>
  444. <ul class="sectlevel2">
  445. <li><a href="#overview_abstract">Abstract</a></li>
  446. <li><a href="#overview_endianness">Introduction to endianness</a></li>
  447. <li><a href="#overview_introduction">Introduction to the Boost.Endian library</a></li>
  448. <li><a href="#overview_choosing_between_conversion_functions_buffer_types_and_arithmetic_types">Choosing between conversion functions, buffer types, and arithmetic types</a></li>
  449. <li><a href="#overview_intrinsics">Built-in support for Intrinsics</a></li>
  450. <li><a href="#overview_performance">Performance</a></li>
  451. <li><a href="#overview_faq">Overall FAQ</a></li>
  452. <li><a href="#overview_history">History</a></li>
  453. <li><a href="#overview_compatibility_with_interim_releases">Compatibility with interim releases</a></li>
  454. <li><a href="#overview_cpp03_support">C&#43;&#43;03 support for C&#43;&#43;11 features</a></li>
  455. <li><a href="#overview_future_directions">Future directions</a></li>
  456. <li><a href="#overview_acknowledgements">Acknowledgements</a></li>
  457. </ul>
  458. </li>
  459. <li><a href="#changelog">Revision History</a>
  460. <ul class="sectlevel2">
  461. <li><a href="#overview_changes_in_1_72_0">Changes in 1.72.0</a></li>
  462. <li><a href="#overview_changes_in_1_71_0">Changes in 1.71.0</a></li>
  463. </ul>
  464. </li>
  465. <li><a href="#conversion">Endian Conversion Functions</a>
  466. <ul class="sectlevel2">
  467. <li><a href="#conversion_introduction">Introduction</a></li>
  468. <li><a href="#conversion_reference">Reference</a></li>
  469. <li><a href="#conversion_faq">FAQ</a></li>
  470. <li><a href="#conversion_acknowledgements">Acknowledgements</a></li>
  471. </ul>
  472. </li>
  473. <li><a href="#buffers">Endian Buffer Types</a>
  474. <ul class="sectlevel2">
  475. <li><a href="#buffers_introduction">Introduction</a></li>
  476. <li><a href="#buffers_example">Example</a></li>
  477. <li><a href="#buffers_limitations">Limitations</a></li>
  478. <li><a href="#buffers_feature_set">Feature set</a></li>
  479. <li><a href="#buffers_enums_and_typedefs">Enums and typedefs</a></li>
  480. <li><a href="#buffers_class_template_endian_buffer">Class template <code>endian_buffer</code></a></li>
  481. <li><a href="#buffers_faq">FAQ</a></li>
  482. <li><a href="#buffers_design_considerations_for_boost_endian_buffers">Design considerations for Boost.Endian buffers</a></li>
  483. <li><a href="#buffers_c11">C&#43;&#43;11</a></li>
  484. <li><a href="#buffers_compilation">Compilation</a></li>
  485. </ul>
  486. </li>
  487. <li><a href="#arithmetic">Endian Arithmetic Types</a>
  488. <ul class="sectlevel2">
  489. <li><a href="#arithmetic_introduction">Introduction</a></li>
  490. <li><a href="#arithmetic_example">Example</a></li>
  491. <li><a href="#arithmetic_limitations">Limitations</a></li>
  492. <li><a href="#arithmetic_feature_set">Feature set</a></li>
  493. <li><a href="#arithmetic_enums_and_typedefs">Enums and typedefs</a></li>
  494. <li><a href="#arithmetic_class_template_endian_arithmetic">Class template <code>endian_arithmetic</code></a></li>
  495. <li><a href="#arithmetic_faq">FAQ</a></li>
  496. <li><a href="#arithmetic_design_considerations_for_boost_endian_types">Design considerations for Boost.Endian types</a></li>
  497. <li><a href="#arithmetic_experience">Experience</a></li>
  498. <li><a href="#arithmetic_motivating_use_cases">Motivating use cases</a></li>
  499. <li><a href="#arithmetic_c11">C&#43;&#43;11</a></li>
  500. <li><a href="#arithmetic_compilation">Compilation</a></li>
  501. <li><a href="#arithmetic_acknowledgements">Acknowledgements</a></li>
  502. </ul>
  503. </li>
  504. <li><a href="#choosing">Choosing Approach</a>
  505. <ul class="sectlevel2">
  506. <li><a href="#choosing_introduction">Introduction</a></li>
  507. <li><a href="#choosing_choosing_between_conversion_functions_buffer_types_and_arithmetic_types">Choosing between conversion functions, buffer types, and arithmetic types</a></li>
  508. </ul>
  509. </li>
  510. <li><a href="#appendix_mini_review_topics">Appendix A: Endian Mini-Review</a></li>
  511. <li><a href="#choosing_copyright_and_license">Appendix B: Copyright and License</a></li>
  512. </ul>
  513. </div>
  514. </div>
  515. <div id="content">
  516. <div class="sect1">
  517. <h2 id="overview">Overview</h2>
  518. <div class="sectionbody">
  519. <div class="sect2">
  520. <h3 id="overview_abstract">Abstract</h3>
  521. <div class="paragraph">
  522. <p>Boost.Endian provides facilities to manipulate the
  523. <a href="#overview_endianness">endianness</a> of integers and user-defined types.</p>
  524. </div>
  525. <div class="ulist">
  526. <ul>
  527. <li>
  528. <p>Three approaches to endianness are supported. Each has a long history of
  529. successful use, and each approach has use cases where it is preferred over the
  530. other approaches.</p>
  531. </li>
  532. <li>
  533. <p>Primary uses:</p>
  534. <div class="ulist">
  535. <ul>
  536. <li>
  537. <p>Data portability. The Endian library supports binary data exchange, via
  538. either external media or network transmission, regardless of platform
  539. endianness.</p>
  540. </li>
  541. <li>
  542. <p>Program portability. POSIX-based and Windows-based operating systems
  543. traditionally supply libraries with non-portable functions to perform endian
  544. conversion. There are at least four incompatible sets of functions in common
  545. use. The Endian library is portable across all C&#43;&#43; platforms.</p>
  546. </li>
  547. </ul>
  548. </div>
  549. </li>
  550. <li>
  551. <p>Secondary use: Minimizing data size via sizes and/or alignments not supported
  552. by the standard C&#43;&#43; integer types.</p>
  553. </li>
  554. </ul>
  555. </div>
  556. </div>
  557. <div class="sect2">
  558. <h3 id="overview_endianness">Introduction to endianness</h3>
  559. <div class="paragraph">
  560. <p>Consider the following code:</p>
  561. </div>
  562. <div class="listingblock">
  563. <div class="content">
  564. <pre class="highlight"><code>int16_t i = 0x0102;
  565. FILE * file = fopen("test.bin", "wb"); // binary file!
  566. fwrite(&amp;i, sizeof(int16_t), 1, file);
  567. fclose(file);</code></pre>
  568. </div>
  569. </div>
  570. <div class="paragraph">
  571. <p>On OS X, Linux, or Windows systems with an Intel CPU, a hex dump of the
  572. "test.bin" output file produces:</p>
  573. </div>
  574. <div class="listingblock">
  575. <div class="content">
  576. <pre class="highlight"><code>0201</code></pre>
  577. </div>
  578. </div>
  579. <div class="paragraph">
  580. <p>On OS X systems with a PowerPC CPU, or Solaris systems with a SPARC CPU, a hex
  581. dump of the "test.bin" output file produces:</p>
  582. </div>
  583. <div class="listingblock">
  584. <div class="content">
  585. <pre class="highlight"><code>0102</code></pre>
  586. </div>
  587. </div>
  588. <div class="paragraph">
  589. <p>What&#8217;s happening here is that Intel CPUs order the bytes of an integer with the
  590. least-significant byte first, while SPARC CPUs place the most-significant byte
  591. first. Some CPUs, such as the PowerPC, allow the operating system to choose
  592. which ordering applies.</p>
  593. </div>
  594. <div class="paragraph">
  595. <p>Most-significant-byte-first ordering is traditionally called "big endian"
  596. ordering and least-significant-byte-first is traditionally called
  597. "little-endian" ordering. The names are derived from
  598. <a href="http://en.wikipedia.org/wiki/Jonathan_Swift">Jonathan Swift</a>'s satirical novel
  599. <em><a href="http://en.wikipedia.org/wiki/Gulliver&#8217;s_Travels">Gulliver&#8217;s Travels</a></em>, where
  600. rival kingdoms opened their soft-boiled eggs at different ends.</p>
  601. </div>
  602. <div class="paragraph">
  603. <p>See Wikipedia&#8217;s <a href="http://en.wikipedia.org/wiki/Endianness">Endianness</a> article for
  604. an extensive discussion of endianness.</p>
  605. </div>
  606. <div class="paragraph">
  607. <p>Programmers can usually ignore endianness, except when reading a core dump on
  608. little-endian systems. But programmers have to deal with endianness when
  609. exchanging binary integers and binary floating point values between computer
  610. systems with differing endianness, whether by physical file transfer or over a
  611. network. And programmers may also want to use the library when minimizing either
  612. internal or external data sizes is advantageous.</p>
  613. </div>
  614. </div>
  615. <div class="sect2">
  616. <h3 id="overview_introduction">Introduction to the Boost.Endian library</h3>
  617. <div class="paragraph">
  618. <p>Boost.Endian provides three different approaches to dealing with endianness. All
  619. three approaches support integers and user-define types (UDTs).</p>
  620. </div>
  621. <div class="paragraph">
  622. <p>Each approach has a long history of successful use, and each approach has use
  623. cases where it is preferred to the other approaches.</p>
  624. </div>
  625. <div class="dlist">
  626. <dl>
  627. <dt class="hdlist1"><a href="#conversion">Endian conversion functions</a></dt>
  628. <dd>
  629. <p>The application uses the built-in integer types to hold values, and calls the
  630. provided conversion functions to convert byte ordering as needed. Both mutating
  631. and non-mutating conversions are supplied, and each comes in unconditional and
  632. conditional variants.</p>
  633. </dd>
  634. <dt class="hdlist1"><a href="#buffers">Endian buffer types</a></dt>
  635. <dd>
  636. <p>The application uses the provided endian buffer types to hold values, and
  637. explicitly converts to and from the built-in integer types. Buffer sizes of 8,
  638. 16, 24, 32, 40, 48, 56, and 64 bits (i.e. 1, 2, 3, 4, 5, 6, 7, and 8 bytes) are
  639. provided. Unaligned integer buffer types are provided for all sizes, and aligned
  640. buffer types are provided for 16, 32, and 64-bit sizes. The provided specific
  641. types are typedefs for a generic class template that may be used directly for
  642. less common use cases.</p>
  643. </dd>
  644. <dt class="hdlist1"><a href="#arithmetic">Endian arithmetic types</a></dt>
  645. <dd>
  646. <p>The application uses the provided endian arithmetic types, which supply the same
  647. operations as the built-in C&#43;&#43; arithmetic types. All conversions are implicit.
  648. Arithmetic sizes of 8, 16, 24, 32, 40, 48, 56, and 64 bits (i.e. 1, 2, 3, 4, 5,
  649. 6, 7, and 8 bytes) are provided. Unaligned integer types are provided for all
  650. sizes and aligned arithmetic types are provided for 16, 32, and 64-bit sizes.
  651. The provided specific types are typedefs for a generic class template that may
  652. be used directly in generic code of for less common use cases.</p>
  653. </dd>
  654. </dl>
  655. </div>
  656. <div class="paragraph">
  657. <p>Boost Endian is a header-only library. C&#43;&#43;11 features affecting interfaces,
  658. such as <code>noexcept</code>, are used only if available. See
  659. <a href="#overview_cpp03_support">C&#43;&#43;03 support for C&#43;&#43;11 features</a> for details.</p>
  660. </div>
  661. </div>
  662. <div class="sect2">
  663. <h3 id="overview_choosing_between_conversion_functions_buffer_types_and_arithmetic_types">Choosing between conversion functions, buffer types, and arithmetic types</h3>
  664. <div class="paragraph">
  665. <p>This section has been moved to its own <a href="#choosing">Choosing the Approach</a> page.</p>
  666. </div>
  667. </div>
  668. <div class="sect2">
  669. <h3 id="overview_intrinsics">Built-in support for Intrinsics</h3>
  670. <div class="paragraph">
  671. <p>Most compilers, including GCC, Clang, and Visual C&#43;&#43;, supply built-in support
  672. for byte swapping intrinsics. The Endian library uses these intrinsics when
  673. available since they may result in smaller and faster generated code,
  674. particularly for optimized builds.</p>
  675. </div>
  676. <div class="paragraph">
  677. <p>Defining the macro <code>BOOST_ENDIAN_NO_INTRINSICS</code> will suppress use of the
  678. intrinsics. This is useful when a compiler has no intrinsic support or fails to
  679. locate the appropriate header, perhaps because it is an older release or has
  680. very limited supporting libraries.</p>
  681. </div>
  682. <div class="paragraph">
  683. <p>The macro <code>BOOST_ENDIAN_INTRINSIC_MSG</code> is defined as either
  684. <code>"no byte swap intrinsics"</code> or a string describing the particular set of
  685. intrinsics being used. This is useful for eliminating missing intrinsics as a
  686. source of performance issues.</p>
  687. </div>
  688. </div>
  689. <div class="sect2">
  690. <h3 id="overview_performance">Performance</h3>
  691. <div class="paragraph">
  692. <p>Consider this problem:</p>
  693. </div>
  694. <div class="sect3">
  695. <h4 id="overview_example_1">Example 1</h4>
  696. <div class="paragraph">
  697. <p>Add 100 to a big endian value in a file, then write the result to a file</p>
  698. </div>
  699. <table class="tableblock frame-all grid-all stretch">
  700. <colgroup>
  701. <col style="width: 50%;">
  702. <col style="width: 50%;">
  703. </colgroup>
  704. <thead>
  705. <tr>
  706. <th class="tableblock halign-left valign-top">Endian arithmetic type approach</th>
  707. <th class="tableblock halign-left valign-top">Endian conversion function approach</th>
  708. </tr>
  709. </thead>
  710. <tbody>
  711. <tr>
  712. <td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
  713. <div class="content">
  714. <pre>big_int32_at x;
  715. ... read into x from a file ...
  716. x += 100;
  717. ... write x to a file ...</pre>
  718. </div>
  719. </div></div></td>
  720. <td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
  721. <div class="content">
  722. <pre>int32_t x;
  723. ... read into x from a file ...
  724. big_to_native_inplace(x);
  725. x += 100;
  726. native_to_big_inplace(x);
  727. ... write x to a file ...</pre>
  728. </div>
  729. </div></div></td>
  730. </tr>
  731. </tbody>
  732. </table>
  733. <div class="paragraph">
  734. <p><strong>There will be no performance difference between the two approaches in optimized
  735. builds, regardless of the native endianness of the machine.</strong> That&#8217;s because
  736. optimizing compilers will generate exactly the same code for each. That
  737. conclusion was confirmed by studying the generated assembly code for GCC and
  738. Visual C&#43;&#43;. Furthermore, time spent doing I/O will determine the speed of this
  739. application.</p>
  740. </div>
  741. <div class="paragraph">
  742. <p>Now consider a slightly different problem:</p>
  743. </div>
  744. </div>
  745. <div class="sect3">
  746. <h4 id="overview_example_2">Example 2</h4>
  747. <div class="paragraph">
  748. <p>Add a million values to a big endian value in a file, then write the result to a
  749. file</p>
  750. </div>
  751. <table class="tableblock frame-all grid-all stretch">
  752. <colgroup>
  753. <col style="width: 50%;">
  754. <col style="width: 50%;">
  755. </colgroup>
  756. <thead>
  757. <tr>
  758. <th class="tableblock halign-left valign-top">Endian arithmetic type approach</th>
  759. <th class="tableblock halign-left valign-top">Endian conversion function approach</th>
  760. </tr>
  761. </thead>
  762. <tbody>
  763. <tr>
  764. <td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
  765. <div class="content">
  766. <pre>big_int32_at x;
  767. ... read into x from a file ...
  768. for (int32_t i = 0; i &lt; 1000000; ++i)
  769. x += i;
  770. ... write x to a file ...</pre>
  771. </div>
  772. </div></div></td>
  773. <td class="tableblock halign-left valign-top"><div class="content"><div class="listingblock">
  774. <div class="content">
  775. <pre>int32_t x;
  776. ... read into x from a file ...
  777. big_to_native_inplace(x);
  778. for (int32_t i = 0; i &lt; 1000000; ++i)
  779. x += i;
  780. native_to_big_inplace(x);
  781. ... write x to a file ...</pre>
  782. </div>
  783. </div></div></td>
  784. </tr>
  785. </tbody>
  786. </table>
  787. <div class="paragraph">
  788. <p>With the Endian arithmetic approach, on little endian platforms an implicit
  789. conversion from and then back to big endian is done inside the loop. With the
  790. Endian conversion function approach, the user has ensured the conversions are
  791. done outside the loop, so the code may run more quickly on little endian
  792. platforms.</p>
  793. </div>
  794. </div>
  795. <div class="sect3">
  796. <h4 id="overview_timings">Timings</h4>
  797. <div class="paragraph">
  798. <p>These tests were run against release builds on a circa 2012 4-core little endian
  799. X64 Intel Core i5-3570K CPU @ 3.40GHz under Windows 7.</p>
  800. </div>
  801. <div class="admonitionblock caution">
  802. <table>
  803. <tr>
  804. <td class="icon">
  805. <div class="title">Caution</div>
  806. </td>
  807. <td class="content">
  808. The Windows CPU timer has very high granularity. Repeated runs of the
  809. same tests often yield considerably different results.
  810. </td>
  811. </tr>
  812. </table>
  813. </div>
  814. <div class="paragraph">
  815. <p>See <code>test/loop_time_test.cpp</code> for the actual code and <code>benchmark/Jamfile.v2</code> for
  816. the build setup.</p>
  817. </div>
  818. <div class="sect4">
  819. <h5 id="overview_gnu_c_version_4_8_2_on_linux_virtual_machine">GNU C++ version 4.8.2 on Linux virtual machine</h5>
  820. <div class="paragraph">
  821. <p>Iterations: 10'000'000'000, Intrinsics: <code>__builtin_bswap16</code>, etc.</p>
  822. </div>
  823. <table class="tableblock frame-all grid-all stretch">
  824. <colgroup>
  825. <col style="width: 33.3333%;">
  826. <col style="width: 33.3333%;">
  827. <col style="width: 33.3334%;">
  828. </colgroup>
  829. <thead>
  830. <tr>
  831. <th class="tableblock halign-left valign-top">Test Case</th>
  832. <th class="tableblock halign-left valign-top">Endian arithmetic type</th>
  833. <th class="tableblock halign-left valign-top">Endian conversion function</th>
  834. </tr>
  835. </thead>
  836. <tbody>
  837. <tr>
  838. <td class="tableblock halign-left valign-top"><p class="tableblock">16-bit aligned big endian</p></td>
  839. <td class="tableblock halign-left valign-top"><p class="tableblock">8.46 s</p></td>
  840. <td class="tableblock halign-left valign-top"><p class="tableblock">5.28 s</p></td>
  841. </tr>
  842. <tr>
  843. <td class="tableblock halign-left valign-top"><p class="tableblock">16-bit aligned little endian</p></td>
  844. <td class="tableblock halign-left valign-top"><p class="tableblock">5.28 s</p></td>
  845. <td class="tableblock halign-left valign-top"><p class="tableblock">5.22 s</p></td>
  846. </tr>
  847. <tr>
  848. <td class="tableblock halign-left valign-top"><p class="tableblock">32-bit aligned big endian</p></td>
  849. <td class="tableblock halign-left valign-top"><p class="tableblock">8.40 s</p></td>
  850. <td class="tableblock halign-left valign-top"><p class="tableblock">2.11 s</p></td>
  851. </tr>
  852. <tr>
  853. <td class="tableblock halign-left valign-top"><p class="tableblock">32-bit aligned little endian</p></td>
  854. <td class="tableblock halign-left valign-top"><p class="tableblock">2.11 s</p></td>
  855. <td class="tableblock halign-left valign-top"><p class="tableblock">2.10 s</p></td>
  856. </tr>
  857. <tr>
  858. <td class="tableblock halign-left valign-top"><p class="tableblock">64-bit aligned big endian</p></td>
  859. <td class="tableblock halign-left valign-top"><p class="tableblock">14.02 s</p></td>
  860. <td class="tableblock halign-left valign-top"><p class="tableblock">3.10 s</p></td>
  861. </tr>
  862. <tr>
  863. <td class="tableblock halign-left valign-top"><p class="tableblock">64-bit aligned little endian</p></td>
  864. <td class="tableblock halign-left valign-top"><p class="tableblock">3.00 s</p></td>
  865. <td class="tableblock halign-left valign-top"><p class="tableblock">3.03 s</p></td>
  866. </tr>
  867. </tbody>
  868. </table>
  869. </div>
  870. <div class="sect4">
  871. <h5 id="overview_microsoft_visual_c_version_14_0">Microsoft Visual C++ version 14.0</h5>
  872. <div class="paragraph">
  873. <p>Iterations: 10'000'000'000, Intrinsics: <code>&lt;cstdlib&gt;</code> <code>_byteswap_ushort</code>, etc.</p>
  874. </div>
  875. <table class="tableblock frame-all grid-all stretch">
  876. <colgroup>
  877. <col style="width: 33.3333%;">
  878. <col style="width: 33.3333%;">
  879. <col style="width: 33.3334%;">
  880. </colgroup>
  881. <thead>
  882. <tr>
  883. <th class="tableblock halign-left valign-top">Test Case</th>
  884. <th class="tableblock halign-left valign-top">Endian arithmetic type</th>
  885. <th class="tableblock halign-left valign-top">Endian conversion function</th>
  886. </tr>
  887. </thead>
  888. <tbody>
  889. <tr>
  890. <td class="tableblock halign-left valign-top"><p class="tableblock">16-bit aligned big endian</p></td>
  891. <td class="tableblock halign-left valign-top"><p class="tableblock">8.27 s</p></td>
  892. <td class="tableblock halign-left valign-top"><p class="tableblock">5.26 s</p></td>
  893. </tr>
  894. <tr>
  895. <td class="tableblock halign-left valign-top"><p class="tableblock">16-bit aligned little endian</p></td>
  896. <td class="tableblock halign-left valign-top"><p class="tableblock">5.29 s</p></td>
  897. <td class="tableblock halign-left valign-top"><p class="tableblock">5.32 s</p></td>
  898. </tr>
  899. <tr>
  900. <td class="tableblock halign-left valign-top"><p class="tableblock">32-bit aligned big endian</p></td>
  901. <td class="tableblock halign-left valign-top"><p class="tableblock">8.36 s</p></td>
  902. <td class="tableblock halign-left valign-top"><p class="tableblock">5.24 s</p></td>
  903. </tr>
  904. <tr>
  905. <td class="tableblock halign-left valign-top"><p class="tableblock">32-bit aligned little endian</p></td>
  906. <td class="tableblock halign-left valign-top"><p class="tableblock">5.24 s</p></td>
  907. <td class="tableblock halign-left valign-top"><p class="tableblock">5.24 s</p></td>
  908. </tr>
  909. <tr>
  910. <td class="tableblock halign-left valign-top"><p class="tableblock">64-bit aligned big endian</p></td>
  911. <td class="tableblock halign-left valign-top"><p class="tableblock">13.65 s</p></td>
  912. <td class="tableblock halign-left valign-top"><p class="tableblock">3.34 s</p></td>
  913. </tr>
  914. <tr>
  915. <td class="tableblock halign-left valign-top"><p class="tableblock">64-bit aligned little endian</p></td>
  916. <td class="tableblock halign-left valign-top"><p class="tableblock">3.35 s</p></td>
  917. <td class="tableblock halign-left valign-top"><p class="tableblock">2.73 s</p></td>
  918. </tr>
  919. </tbody>
  920. </table>
  921. </div>
  922. </div>
  923. </div>
  924. <div class="sect2">
  925. <h3 id="overview_faq">Overall FAQ</h3>
  926. <div class="dlist">
  927. <dl>
  928. <dt class="hdlist1">Is the implementation header only?</dt>
  929. <dd>
  930. <p>Yes.</p>
  931. </dd>
  932. <dt class="hdlist1">Are C&#43;&#43;03 compilers supported?</dt>
  933. <dd>
  934. <p>Yes.</p>
  935. </dd>
  936. <dt class="hdlist1">Does the implementation use compiler intrinsic built-in byte swapping?</dt>
  937. <dd>
  938. <p>Yes, if available. See <a href="#overview_intrinsics">Intrinsic built-in support</a>.</p>
  939. </dd>
  940. <dt class="hdlist1">Why bother with endianness?</dt>
  941. <dd>
  942. <p>Binary data portability is the primary use case.</p>
  943. </dd>
  944. <dt class="hdlist1">Does endianness have any uses outside of portable binary file or network I/O formats?</dt>
  945. <dd>
  946. <p>Using the unaligned integer types with a size tailored to the application&#8217;s
  947. needs is a minor secondary use that saves internal or external memory space. For
  948. example, using <code>big_int40_buf_t</code> or <code>big_int40_t</code> in a large array saves a lot
  949. of space compared to one of the 64-bit types.</p>
  950. </dd>
  951. <dt class="hdlist1">Why bother with binary I/O? Why not just use C&#43;&#43; Standard Library stream inserters and extractors?</dt>
  952. <dd>
  953. <div class="ulist">
  954. <ul>
  955. <li>
  956. <p>Data interchange formats often specify binary integer data. Binary integer
  957. data is smaller and therefore I/O is faster and file sizes are smaller. Transfer
  958. between systems is less expensive.</p>
  959. </li>
  960. <li>
  961. <p>Furthermore, binary integer data is of fixed size, and so fixed-size disk
  962. records are possible without padding, easing sorting and allowing random access.</p>
  963. </li>
  964. <li>
  965. <p>Disadvantages, such as the inability to use text utilities on the resulting
  966. files, limit usefulness to applications where the binary I/O advantages are
  967. paramount.</p>
  968. </li>
  969. </ul>
  970. </div>
  971. </dd>
  972. <dt class="hdlist1">Which is better, big-endian or little-endian?</dt>
  973. <dd>
  974. <p>Big-endian tends to be preferred in a networking environment and is a bit more
  975. of an industry standard, but little-endian may be preferred for applications
  976. that run primarily on x86, x86-64, and other little-endian CPU&#8217;s. The
  977. <a href="http://en.wikipedia.org/wiki/Endian">Wikipedia</a> article gives more pros and cons.</p>
  978. </dd>
  979. <dt class="hdlist1">Why are only big and little native endianness supported?</dt>
  980. <dd>
  981. <p>These are the only endian schemes that have any practical value today. PDP-11
  982. and the other middle endian approaches are interesting curiosities but have no
  983. relevance for today&#8217;s C&#43;&#43; developers. The same is true for architectures that
  984. allow runtime endianness switching. The
  985. <a href="#conversion_native_order_specification">specification for native ordering</a> has
  986. been carefully crafted to allow support for such orderings in the future, should
  987. the need arise. Thanks to Howard Hinnant for suggesting this.</p>
  988. </dd>
  989. <dt class="hdlist1">Why do both the buffer and arithmetic types exist?</dt>
  990. <dd>
  991. <p>Conversions in the buffer types are explicit. Conversions in the arithmetic
  992. types are implicit. This fundamental difference is a deliberate design feature
  993. that would be lost if the inheritance hierarchy were collapsed.
  994. The original design provided only arithmetic types. Buffer types were requested
  995. during formal review by those wishing total control over when conversion occurs.
  996. They also felt that buffer types would be less likely to be misused by
  997. maintenance programmers not familiar with the implications of performing a lot
  998. of integer operations on the endian arithmetic integer types.</p>
  999. </dd>
  1000. <dt class="hdlist1">What is gained by using the buffer types rather than always just using the arithmetic types?</dt>
  1001. <dd>
  1002. <p>Assurance that hidden conversions are not performed. This is of overriding
  1003. importance to users concerned about achieving the ultimate in terms of speed.
  1004. "Always just using the arithmetic types" is fine for other users. When the
  1005. ultimate in speed needs to be ensured, the arithmetic types can be used in the
  1006. same design patterns or idioms that would be used for buffer types, resulting in
  1007. the same code being generated for either types.</p>
  1008. </dd>
  1009. <dt class="hdlist1">What are the limitations of integer support?</dt>
  1010. <dd>
  1011. <p>Tests have only been performed on machines that use two&#8217;s complement
  1012. arithmetic. The Endian conversion functions only support 16, 32, and 64-bit
  1013. aligned integers. The endian types only support 8, 16, 24, 32, 40, 48, 56, and
  1014. 64-bit unaligned integers, and 8, 16, 32, and 64-bit aligned integers.</p>
  1015. </dd>
  1016. <dt class="hdlist1">Why is there no floating point support?</dt>
  1017. <dd>
  1018. <p>An attempt was made to support four-byte <code>float</code>s and eight-byte
  1019. <code>double</code>s, limited to
  1020. <a href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a> (also known as
  1021. ISO/IEC/IEEE 60559) floating point and further limited to systems where floating
  1022. point endianness does not differ from integer endianness. Even with those
  1023. limitations, support for floating point types was not reliable and was removed.
  1024. For example, simply reversing the endianness of a floating point number can
  1025. result in a signaling-NAN. For all practical purposes, binary serialization and
  1026. endianness for integers are one and the same problem. That is not true for
  1027. floating point numbers, so binary serialization interfaces and formats for
  1028. floating point does not fit well in an endian-based library.</p>
  1029. </dd>
  1030. </dl>
  1031. </div>
  1032. </div>
  1033. <div class="sect2">
  1034. <h3 id="overview_history">History</h3>
  1035. <div class="sect3">
  1036. <h4 id="overview_changes_requested_by_formal_review">Changes requested by formal review</h4>
  1037. <div class="paragraph">
  1038. <p>The library was reworked from top to bottom to accommodate changes requested
  1039. during the formal review. See <a href="#appendix_mini_review_topics">Mini-Review</a>
  1040. page for details.</p>
  1041. </div>
  1042. </div>
  1043. <div class="sect3">
  1044. <h4 id="overview_other_changes_since_formal_review">Other changes since formal review</h4>
  1045. <div class="ulist">
  1046. <ul>
  1047. <li>
  1048. <p>Header <code>boost/endian/endian.hpp</code> has been renamed to
  1049. <code>boost/endian/arithmetic.hpp</code>. Headers
  1050. <code>boost/endian/conversion.hpp</code> and <code>boost/endian/buffers.hpp</code> have been added.
  1051. Infrastructure file names were changed accordingly.</p>
  1052. </li>
  1053. <li>
  1054. <p>The endian arithmetic type aliases have been renamed, using a naming pattern
  1055. that is consistent for both integer and floating point, and a consistent set of
  1056. aliases supplied for the endian buffer types.</p>
  1057. </li>
  1058. <li>
  1059. <p>The unaligned-type alias names still have the <code>_t</code> suffix, but the
  1060. aligned-type alias names now have an <code>_at</code> suffix.</p>
  1061. </li>
  1062. <li>
  1063. <p><code>endian_reverse()</code> overloads for <code>int8_t</code> and <code>uint8_t</code> have been added for
  1064. improved generality. (Pierre Talbot)</p>
  1065. </li>
  1066. <li>
  1067. <p>Overloads of <code>endian_reverse_inplace()</code> have been replaced with a single
  1068. <code>endian_reverse_inplace()</code> template. (Pierre Talbot)</p>
  1069. </li>
  1070. <li>
  1071. <p>For X86 and X64 architectures, which permit unaligned loads and stores,
  1072. unaligned little endian buffer and arithmetic types use regular loads and
  1073. stores when the size is exact. This makes unaligned little endian buffer and
  1074. arithmetic types significantly more efficient on these architectures. (Jeremy
  1075. Maitin-Shepard)</p>
  1076. </li>
  1077. <li>
  1078. <p>C&#43;&#43;11 features affecting interfaces, such as <code>noexcept</code>, are now used.
  1079. C&#43;&#43;03 compilers are still supported.</p>
  1080. </li>
  1081. <li>
  1082. <p>Acknowledgements have been updated.</p>
  1083. </li>
  1084. </ul>
  1085. </div>
  1086. </div>
  1087. </div>
  1088. <div class="sect2">
  1089. <h3 id="overview_compatibility_with_interim_releases">Compatibility with interim releases</h3>
  1090. <div class="paragraph">
  1091. <p>Prior to the official Boost release, class template <code>endian_arithmetic</code> has been
  1092. used for a decade or more with the same functionality but under the name
  1093. <code>endian</code>. Other names also changed in the official release. If the macro
  1094. <code>BOOST_ENDIAN_DEPRECATED_NAMES</code> is defined, those old now deprecated names are
  1095. still supported. However, the class template <code>endian</code> name is only provided for
  1096. compilers supporting C&#43;&#43;11 template aliases. For C&#43;&#43;03 compilers, the name
  1097. will have to be changed to <code>endian_arithmetic</code>.</p>
  1098. </div>
  1099. <div class="paragraph">
  1100. <p>To support backward header compatibility, deprecated header
  1101. <code>boost/endian/endian.hpp</code> forwards to <code>boost/endian/arithmetic.hpp</code>. It requires
  1102. <code>BOOST_ENDIAN_DEPRECATED_NAMES</code> be defined. It should only be used while
  1103. transitioning to the official Boost release of the library as it will be removed
  1104. in some future release.</p>
  1105. </div>
  1106. </div>
  1107. <div class="sect2">
  1108. <h3 id="overview_cpp03_support">C&#43;&#43;03 support for C&#43;&#43;11 features</h3>
  1109. <table class="tableblock frame-all grid-all stretch">
  1110. <colgroup>
  1111. <col style="width: 50%;">
  1112. <col style="width: 50%;">
  1113. </colgroup>
  1114. <thead>
  1115. <tr>
  1116. <th class="tableblock halign-left valign-top">C&#43;&#43;11 Feature</th>
  1117. <th class="tableblock halign-left valign-top">Action with C&#43;&#43;03 Compilers</th>
  1118. </tr>
  1119. </thead>
  1120. <tbody>
  1121. <tr>
  1122. <td class="tableblock halign-left valign-top"><p class="tableblock">Scoped enums</p></td>
  1123. <td class="tableblock halign-left valign-top"><p class="tableblock">Uses header
  1124. <a href="http://www.boost.org/libs/core/doc/html/core/scoped_enum.html">boost/core/scoped_enum.hpp</a>
  1125. to emulate C&#43;&#43;11 scoped enums.</p></td>
  1126. </tr>
  1127. <tr>
  1128. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>noexcept</code></p></td>
  1129. <td class="tableblock halign-left valign-top"><p class="tableblock">Uses <code>BOOST_NOEXCEPT</code> macro, which is defined as null for compilers not
  1130. supporting this C&#43;&#43;11 feature.</p></td>
  1131. </tr>
  1132. <tr>
  1133. <td class="tableblock halign-left valign-top"><p class="tableblock">C&#43;&#43;11 PODs
  1134. (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2342.htm">N2342</a>)</p></td>
  1135. <td class="tableblock halign-left valign-top"><p class="tableblock">Takes advantage of C&#43;&#43;03 compilers that relax C&#43;&#43;03 POD rules, but see
  1136. Limitations <a href="#buffers_limitations">here</a> and <a href="#arithmetic_limitations">here</a>.
  1137. Also see macros for explicit POD control <a href="#buffers_compilation">here</a> and
  1138. <a href="#arithmetic_compilation">here</a></p></td>
  1139. </tr>
  1140. </tbody>
  1141. </table>
  1142. </div>
  1143. <div class="sect2">
  1144. <h3 id="overview_future_directions">Future directions</h3>
  1145. <div class="dlist">
  1146. <dl>
  1147. <dt class="hdlist1">Standardization.</dt>
  1148. <dd>
  1149. <p>The plan is to submit Boost.Endian to the C&#43;&#43; standards committee for possible
  1150. inclusion in a Technical Specification or the C&#43;&#43; standard itself.</p>
  1151. </dd>
  1152. <dt class="hdlist1">Specializations for <code>numeric_limits</code>.</dt>
  1153. <dd>
  1154. <p>Roger Leigh requested that all <code>boost::endian</code> types provide <code>numeric_limits</code>
  1155. specializations.
  1156. See <a href="https://github.com/boostorg/endian/issues/4">GitHub issue 4</a>.</p>
  1157. </dd>
  1158. <dt class="hdlist1">Character buffer support.</dt>
  1159. <dd>
  1160. <p>Peter Dimov pointed out during the mini-review that getting and setting basic
  1161. arithmetic types (or <code>&lt;cstdint&gt;</code> equivalents) from/to an offset into an array of
  1162. unsigned char is a common need. See
  1163. <a href="http://lists.boost.org/Archives/boost/2015/01/219574.php">Boost.Endian
  1164. mini-review posting</a>.</p>
  1165. </dd>
  1166. <dt class="hdlist1">Out-of-range detection.</dt>
  1167. <dd>
  1168. <p>Peter Dimov pointed suggested during the mini-review that throwing an exception
  1169. on buffer values being out-of-range might be desirable. See the end of
  1170. <a href="http://lists.boost.org/Archives/boost/2015/01/219659.php">this posting</a> and
  1171. subsequent replies.</p>
  1172. </dd>
  1173. </dl>
  1174. </div>
  1175. </div>
  1176. <div class="sect2">
  1177. <h3 id="overview_acknowledgements">Acknowledgements</h3>
  1178. <div class="paragraph">
  1179. <p>Comments and suggestions were received from Adder, Benaka Moorthi, Christopher
  1180. Kohlhoff, Cliff Green, Daniel James, Dave Handley, Gennaro Proto, Giovanni Piero
  1181. Deretta, Gordon Woodhull, dizzy, Hartmut Kaiser, Howard Hinnant, Jason Newton,
  1182. Jeff Flinn, Jeremy Maitin-Shepard, John Filo, John Maddock, Kim Barrett, Marsh
  1183. Ray, Martin Bonner, Mathias Gaunard, Matias Capeletto, Neil Mayhew, Nevin Liber,
  1184. Olaf van der Spek, Paul Bristow, Peter Dimov, Pierre Talbot, Phil Endecott,
  1185. Philip Bennefall, Pyry Jahkola, Rene Rivera, Robert Stewart, Roger Leigh, Roland
  1186. Schwarz, Scott McMurray, Sebastian Redl, Tim Blechmann, Tim Moore, tymofey,
  1187. Tomas Puverle, Vincente Botet, Yuval Ronen and Vitaly Budovsk. Apologies if
  1188. anyone has been missed.</p>
  1189. </div>
  1190. <div class="paragraph">
  1191. <p>The documentation was converted into Asciidoc format by Glen Fernandes.</p>
  1192. </div>
  1193. </div>
  1194. </div>
  1195. </div>
  1196. <div class="sect1">
  1197. <h2 id="changelog">Revision History</h2>
  1198. <div class="sectionbody">
  1199. <div class="sect2">
  1200. <h3 id="overview_changes_in_1_72_0">Changes in 1.72.0</h3>
  1201. <div class="ulist">
  1202. <ul>
  1203. <li>
  1204. <p>Made <code>endian_reverse</code>, <code>conditional_reverse</code> and <code>*_to_*</code> <code>constexpr</code>
  1205. on GCC and Clang</p>
  1206. </li>
  1207. <li>
  1208. <p>Added convenience load and store functions</p>
  1209. </li>
  1210. <li>
  1211. <p>Added floating point convenience typedefs</p>
  1212. </li>
  1213. <li>
  1214. <p>Added a non-const overload of <code>data()</code>; changed its return type to <code>unsigned char*</code></p>
  1215. </li>
  1216. <li>
  1217. <p>Added <code>__int128</code> support to <code>endian_reverse</code> when available</p>
  1218. </li>
  1219. <li>
  1220. <p>Added a convenience header <code>boost/endian.hpp</code></p>
  1221. </li>
  1222. </ul>
  1223. </div>
  1224. </div>
  1225. <div class="sect2">
  1226. <h3 id="overview_changes_in_1_71_0">Changes in 1.71.0</h3>
  1227. <div class="ulist">
  1228. <ul>
  1229. <li>
  1230. <p>Clarified requirements on the value type template parameter</p>
  1231. </li>
  1232. <li>
  1233. <p>Added support for <code>float</code> and <code>double</code></p>
  1234. </li>
  1235. <li>
  1236. <p>Added <code>endian_load</code>, <code>endian_store</code></p>
  1237. </li>
  1238. <li>
  1239. <p>Updated <code>endian_reverse</code> to correctly support all non-<code>bool</code> integral types</p>
  1240. </li>
  1241. <li>
  1242. <p>Moved deprecated names to the deprecated header <code>endian.hpp</code></p>
  1243. </li>
  1244. </ul>
  1245. </div>
  1246. </div>
  1247. </div>
  1248. </div>
  1249. <div class="sect1">
  1250. <h2 id="conversion">Endian Conversion Functions</h2>
  1251. <div class="sectionbody">
  1252. <div class="sect2">
  1253. <h3 id="conversion_introduction">Introduction</h3>
  1254. <div class="paragraph">
  1255. <p>Header <code>boost/endian/conversion.hpp</code> provides byte order reversal and conversion
  1256. functions that convert objects of the built-in integer types between native,
  1257. big, or little endian byte ordering. User defined types are also supported.</p>
  1258. </div>
  1259. </div>
  1260. <div class="sect2">
  1261. <h3 id="conversion_reference">Reference</h3>
  1262. <div class="paragraph">
  1263. <p>Functions are implemented <code>inline</code> if appropriate. For C&#43;&#43;03 compilers,
  1264. <code>noexcept</code> is elided. Boost scoped enum emulation is used so that the library
  1265. still works for compilers that do not support scoped enums.</p>
  1266. </div>
  1267. <div class="sect3">
  1268. <h4 id="conversion_definitions">Definitions</h4>
  1269. <div class="paragraph">
  1270. <p><strong>Endianness</strong> refers to the ordering of bytes within internal or external
  1271. integers and other arithmetic data. Most-significant byte first is called
  1272. <strong>big endian</strong> ordering. Least-significant byte first is called
  1273. <strong>little endian</strong> ordering. Other orderings are possible and some CPU
  1274. architectures support both big and little ordering.</p>
  1275. </div>
  1276. <div class="admonitionblock note">
  1277. <table>
  1278. <tr>
  1279. <td class="icon">
  1280. <div class="title">Note</div>
  1281. </td>
  1282. <td class="content">
  1283. The names are derived from
  1284. <a href="http://en.wikipedia.org/wiki/Jonathan_Swift">Jonathan Swift</a>'s satirical novel
  1285. <em><a href="http://en.wikipedia.org/wiki/Gulliver&#8217;s_Travels">Gulliver&#8217;s Travels</a></em>, where
  1286. rival kingdoms opened their soft-boiled eggs at different ends. Wikipedia has an
  1287. extensive description of <a href="https://en.wikipedia.org/wiki/Endianness">Endianness</a>.
  1288. </td>
  1289. </tr>
  1290. </table>
  1291. </div>
  1292. <div class="paragraph">
  1293. <p>The standard integral types (C&#43;&#43;std 3.9.1) except <code>bool</code> are collectively
  1294. called the <strong>endian types</strong>.</p>
  1295. </div>
  1296. </div>
  1297. <div class="sect3">
  1298. <h4 id="conversion_header_boostendianconversion_hpp_synopsis">Header <code>&lt;boost/endian/conversion.hpp&gt;</code> Synopsis</h4>
  1299. <div class="listingblock">
  1300. <div class="content">
  1301. <pre class="highlight"><code>#define BOOST_ENDIAN_INTRINSIC_MSG \
  1302. &#8220;message describing presence or absence of intrinsics&#8221;
  1303. namespace boost
  1304. {
  1305. namespace endian
  1306. {
  1307. enum class order
  1308. {
  1309. native = <code>see below</code>,
  1310. big = <code>see below</code>,
  1311. little = <code>see below</code>,
  1312. };
  1313. // Byte reversal functions
  1314. template &lt;class Endian&gt;
  1315. Endian endian_reverse(Endian x) noexcept;
  1316. template &lt;class EndianReversible&gt;
  1317. EndianReversible big_to_native(EndianReversible x) noexcept;
  1318. template &lt;class EndianReversible&gt;
  1319. EndianReversible native_to_big(EndianReversible x) noexcept;
  1320. template &lt;class EndianReversible&gt;
  1321. EndianReversible little_to_native(EndianReversible x) noexcept;
  1322. template &lt;class EndianReversible&gt;
  1323. EndianReversible native_to_little(EndianReversible x) noexcept;
  1324. template &lt;order O1, order O2, class EndianReversible&gt;
  1325. EndianReversible conditional_reverse(EndianReversible x) noexcept;
  1326. template &lt;class EndianReversible&gt;
  1327. EndianReversible conditional_reverse(EndianReversible x,
  1328. order order1, order order2) noexcept;
  1329. // In-place byte reversal functions
  1330. template &lt;class EndianReversible&gt;
  1331. void endian_reverse_inplace(EndianReversible&amp; x) noexcept;
  1332. template &lt;class EndianReversibleInplace&gt;
  1333. void big_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;
  1334. template &lt;class EndianReversibleInplace&gt;
  1335. void native_to_big_inplace(EndianReversibleInplace&amp; x) noexcept;
  1336. template &lt;class EndianReversibleInplace&gt;
  1337. void little_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;
  1338. template &lt;class EndianReversibleInplace&gt;
  1339. void native_to_little_inplace(EndianReversibleInplace&amp; x) noexcept;
  1340. template &lt;order O1, order O2, class EndianReversibleInplace&gt;
  1341. void conditional_reverse_inplace(EndianReversibleInplace&amp; x) noexcept;
  1342. template &lt;class EndianReversibleInplace&gt;
  1343. void conditional_reverse_inplace(EndianReversibleInplace&amp; x,
  1344. order order1, order order2) noexcept;
  1345. // Generic load and store functions
  1346. template&lt;class T, std::size_t N, order Order&gt;
  1347. T endian_load( unsigned char const * p ) noexcept;
  1348. template&lt;class T, std::size_t N, order Order&gt;
  1349. void endian_store( unsigned char * p, T const &amp; v ) noexcept;
  1350. // Convenience load functions
  1351. boost::int16_t load_little_s16( unsigned char const * p ) noexcept;
  1352. boost::uint16_t load_little_u16( unsigned char const * p ) noexcept;
  1353. boost::int16_t load_big_s16( unsigned char const * p ) noexcept;
  1354. boost::uint16_t load_big_u16( unsigned char const * p ) noexcept;
  1355. boost::int32_t load_little_s24( unsigned char const * p ) noexcept;
  1356. boost::uint32_t load_little_u24( unsigned char const * p ) noexcept;
  1357. boost::int32_t load_big_s24( unsigned char const * p ) noexcept;
  1358. boost::uint32_t load_big_u24( unsigned char const * p ) noexcept;
  1359. boost::int32_t load_little_s32( unsigned char const * p ) noexcept;
  1360. boost::uint32_t load_little_u32( unsigned char const * p ) noexcept;
  1361. boost::int32_t load_big_s32( unsigned char const * p ) noexcept;
  1362. boost::uint32_t load_big_u32( unsigned char const * p ) noexcept;
  1363. boost::int64_t load_little_s40( unsigned char const * p ) noexcept;
  1364. boost::uint64_t load_little_u40( unsigned char const * p ) noexcept;
  1365. boost::int64_t load_big_s40( unsigned char const * p ) noexcept;
  1366. boost::uint64_t load_big_u40( unsigned char const * p ) noexcept;
  1367. boost::int64_t load_little_s48( unsigned char const * p ) noexcept;
  1368. boost::uint64_t load_little_u48( unsigned char const * p ) noexcept;
  1369. boost::int64_t load_big_s48( unsigned char const * p ) noexcept;
  1370. boost::uint64_t load_big_u48( unsigned char const * p ) noexcept;
  1371. boost::int64_t load_little_s56( unsigned char const * p ) noexcept;
  1372. boost::uint64_t load_little_u56( unsigned char const * p ) noexcept;
  1373. boost::int64_t load_big_s56( unsigned char const * p ) noexcept;
  1374. boost::uint64_t load_big_u56( unsigned char const * p ) noexcept;
  1375. boost::int64_t load_little_s64( unsigned char const * p ) noexcept;
  1376. boost::uint64_t load_little_u64( unsigned char const * p ) noexcept;
  1377. boost::int64_t load_big_s64( unsigned char const * p ) noexcept;
  1378. boost::uint64_t load_big_u64( unsigned char const * p ) noexcept;
  1379. // Convenience store functions
  1380. void store_little_s16( unsigned char * p, boost::int16_t v ) noexcept;
  1381. void store_little_u16( unsigned char * p, boost::uint16_t v ) noexcept;
  1382. void store_big_s16( unsigned char * p, boost::int16_t v ) noexcept;
  1383. void store_big_u16( unsigned char * p, boost::uint16_t v ) noexcept;
  1384. void store_little_s24( unsigned char * p, boost::int32_t v ) noexcept;
  1385. void store_little_u24( unsigned char * p, boost::uint32_t v ) noexcept;
  1386. void store_big_s24( unsigned char * p, boost::int32_t v ) noexcept;
  1387. void store_big_u24( unsigned char * p, boost::uint32_t v ) noexcept;
  1388. void store_little_s32( unsigned char * p, boost::int32_t v ) noexcept;
  1389. void store_little_u32( unsigned char * p, boost::uint32_t v ) noexcept;
  1390. void store_big_s32( unsigned char * p, boost::int32_t v ) noexcept;
  1391. void store_big_u32( unsigned char * p, boost::uint32_t v ) noexcept;
  1392. void store_little_s40( unsigned char * p, boost::int64_t v ) noexcept;
  1393. void store_little_u40( unsigned char * p, boost::uint64_t v ) noexcept;
  1394. void store_big_s40( unsigned char * p, boost::int64_t v ) noexcept;
  1395. void store_big_u40( unsigned char * p, boost::uint64_t v ) noexcept;
  1396. void store_little_s48( unsigned char * p, boost::int64_t v ) noexcept;
  1397. void store_little_u48( unsigned char * p, boost::uint64_t v ) noexcept;
  1398. void store_big_s48( unsigned char * p, boost::int64_t v ) noexcept;
  1399. void store_big_u48( unsigned char * p, boost::uint64_t v ) noexcept;
  1400. void store_little_s56( unsigned char * p, boost::int64_t v ) noexcept;
  1401. void store_little_u56( unsigned char * p, boost::uint64_t v ) noexcept;
  1402. void store_big_s56( unsigned char * p, boost::int64_t v ) noexcept;
  1403. void store_big_u56( unsigned char * p, boost::uint64_t v ) noexcept;
  1404. void store_little_s64( unsigned char * p, boost::int64_t v ) noexcept;
  1405. void store_little_u64( unsigned char * p, boost::uint64_t v ) noexcept;
  1406. void store_big_s64( unsigned char * p, boost::int64_t v ) noexcept;
  1407. void store_big_u64( unsigned char * p, boost::uint64_t v ) noexcept;
  1408. } // namespace endian
  1409. } // namespace boost</code></pre>
  1410. </div>
  1411. </div>
  1412. <div class="paragraph">
  1413. <p>The values of <code>order::little</code> and <code>order::big</code> shall not be equal to one
  1414. another.</p>
  1415. </div>
  1416. <div class="paragraph">
  1417. <p>The value of <code>order::native</code> shall be:</p>
  1418. </div>
  1419. <div class="ulist">
  1420. <ul>
  1421. <li>
  1422. <p>equal to <code>order::big</code> if the execution environment is big endian, otherwise</p>
  1423. </li>
  1424. <li>
  1425. <p>equal to <code>order::little</code> if the execution environment is little endian,
  1426. otherwise</p>
  1427. </li>
  1428. <li>
  1429. <p>unequal to both <code>order::little</code> and <code>order::big</code>.</p>
  1430. </li>
  1431. </ul>
  1432. </div>
  1433. </div>
  1434. <div class="sect3">
  1435. <h4 id="conversion_requirements">Requirements</h4>
  1436. <div class="sect4">
  1437. <h5 id="conversion_template_argument_requirements">Template argument requirements</h5>
  1438. <div class="paragraph">
  1439. <p>The template definitions in the <code>boost/endian/conversion.hpp</code> header refer to
  1440. various named requirements whose details are set out in the tables in this
  1441. subsection. In these tables, <code>T</code> is an object or reference type to be supplied
  1442. by a C&#43;&#43; program instantiating a template; <code>x</code> is a value of type (possibly
  1443. <code>const</code>) <code>T</code>; <code>mlx</code> is a modifiable lvalue of type <code>T</code>.</p>
  1444. </div>
  1445. <div class="sect5">
  1446. <h6 id="conversion_endianreversible">EndianReversible requirements (in addition to <code>CopyConstructible</code>)</h6>
  1447. <table class="tableblock frame-all grid-all stretch">
  1448. <colgroup>
  1449. <col style="width: 33.3333%;">
  1450. <col style="width: 33.3333%;">
  1451. <col style="width: 33.3334%;">
  1452. </colgroup>
  1453. <thead>
  1454. <tr>
  1455. <th class="tableblock halign-left valign-top">Expression</th>
  1456. <th class="tableblock halign-left valign-top">Return</th>
  1457. <th class="tableblock halign-left valign-top">Requirements</th>
  1458. </tr>
  1459. </thead>
  1460. <tbody>
  1461. <tr>
  1462. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>endian_reverse(x)</code></p></td>
  1463. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>T</code></p></td>
  1464. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  1465. <p><code>T</code> is an endian type or a class type.</p>
  1466. </div>
  1467. <div class="paragraph">
  1468. <p>If <code>T</code> is an endian type, returns the value of <code>x</code> with the order of bytes
  1469. reversed.</p>
  1470. </div>
  1471. <div class="paragraph">
  1472. <p>If <code>T</code> is a class type, the function:</p>
  1473. </div>
  1474. <div class="ulist">
  1475. <ul>
  1476. <li>
  1477. <p>Returns the value of <code>x</code> with the order of bytes reversed for all data members
  1478. of types or arrays of types that meet the <code>EndianReversible</code> requirements, and;</p>
  1479. </li>
  1480. <li>
  1481. <p>Is a non-member function in the same namespace as <code>T</code> that can be found by
  1482. argument dependent lookup (ADL).</p>
  1483. </li>
  1484. </ul>
  1485. </div></div></td>
  1486. </tr>
  1487. </tbody>
  1488. </table>
  1489. </div>
  1490. <div class="sect5">
  1491. <h6 id="conversion_endianreversibleinplace">EndianReversibleInplace requirements (in addition to <code>CopyConstructible</code>)</h6>
  1492. <table class="tableblock frame-all grid-all stretch">
  1493. <colgroup>
  1494. <col style="width: 50%;">
  1495. <col style="width: 50%;">
  1496. </colgroup>
  1497. <thead>
  1498. <tr>
  1499. <th class="tableblock halign-left valign-top">Expression</th>
  1500. <th class="tableblock halign-left valign-top">Requirements</th>
  1501. </tr>
  1502. </thead>
  1503. <tbody>
  1504. <tr>
  1505. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>endian_reverse_inplace(mlx)</code></p></td>
  1506. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  1507. <p><code>T</code> is an endian type or a class type.</p>
  1508. </div>
  1509. <div class="paragraph">
  1510. <p>If <code>T</code> is an endian type, reverses the order of bytes in <code>mlx</code>.</p>
  1511. </div>
  1512. <div class="paragraph">
  1513. <p>If <code>T</code> is a class type, the function:</p>
  1514. </div>
  1515. <div class="ulist">
  1516. <ul>
  1517. <li>
  1518. <p>Reverses the order of bytes of all data members of <code>mlx</code> that have types or
  1519. arrays of types that meet the <code>EndianReversible</code> or <code>EndianReversibleInplace</code>
  1520. requirements, and;</p>
  1521. </li>
  1522. <li>
  1523. <p>Is a non-member function in the same namespace as <code>T</code> that can be found by
  1524. argument dependent lookup (ADL).</p>
  1525. </li>
  1526. </ul>
  1527. </div></div></td>
  1528. </tr>
  1529. </tbody>
  1530. </table>
  1531. <div class="admonitionblock note">
  1532. <table>
  1533. <tr>
  1534. <td class="icon">
  1535. <div class="title">Note</div>
  1536. </td>
  1537. <td class="content">
  1538. Because there is a function template for <code>endian_reverse_inplace</code> that
  1539. calls <code>endian_reverse</code>, only <code>endian_reverse</code> is required for a user-defined
  1540. type to meet the <code>EndianReversibleInplace</code> requirements. Although User-defined
  1541. types are not required to supply an <code>endian_reverse_inplace</code> function, doing so
  1542. may improve efficiency.
  1543. </td>
  1544. </tr>
  1545. </table>
  1546. </div>
  1547. </div>
  1548. </div>
  1549. <div class="sect4">
  1550. <h5 id="conversion_customization_points_for_user_defined_types_udts">Customization points for user-defined types (UDTs)</h5>
  1551. <div class="paragraph">
  1552. <p>This subsection describes requirements on the Endian library&#8217;s implementation.</p>
  1553. </div>
  1554. <div class="paragraph">
  1555. <p>The library&#8217;s function templates requiring
  1556. <code><a href="#conversion_endianreversible">EndianReversible</a></code> are required to perform
  1557. reversal of endianness if needed by making an unqualified call to
  1558. <code>endian_reverse()</code>.</p>
  1559. </div>
  1560. <div class="paragraph">
  1561. <p>The library&#8217;s function templates requiring
  1562. <code><a href="#conversion_endianreversibleinplace">EndianReversibleInplace</a></code> are required to
  1563. perform reversal of endianness if needed by making an unqualified call to
  1564. <code>endian_reverse_inplace()</code>.</p>
  1565. </div>
  1566. <div class="paragraph">
  1567. <p>See <code>example/udt_conversion_example.cpp</code> for an example user-defined type.</p>
  1568. </div>
  1569. </div>
  1570. </div>
  1571. <div class="sect3">
  1572. <h4 id="conversion_byte_reversal_functions">Byte Reversal Functions</h4>
  1573. <div class="listingblock">
  1574. <div class="content">
  1575. <pre class="highlight"><code>template &lt;class Endian&gt;
  1576. Endian endian_reverse(Endian x) noexcept;</code></pre>
  1577. </div>
  1578. </div>
  1579. <div class="ulist none">
  1580. <ul class="none">
  1581. <li>
  1582. <p></p>
  1583. <div class="dlist">
  1584. <dl>
  1585. <dt class="hdlist1">Requires</dt>
  1586. <dd>
  1587. <p><code>Endian</code> must be a standard integral type that is not <code>bool</code>.</p>
  1588. </dd>
  1589. <dt class="hdlist1">Returns</dt>
  1590. <dd>
  1591. <p><code>x</code>, with the order of its constituent bytes reversed.</p>
  1592. </dd>
  1593. </dl>
  1594. </div>
  1595. </li>
  1596. </ul>
  1597. </div>
  1598. <div class="listingblock">
  1599. <div class="content">
  1600. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1601. EndianReversible big_to_native(EndianReversible x) noexcept;</code></pre>
  1602. </div>
  1603. </div>
  1604. <div class="ulist none">
  1605. <ul class="none">
  1606. <li>
  1607. <p></p>
  1608. <div class="dlist">
  1609. <dl>
  1610. <dt class="hdlist1">Returns</dt>
  1611. <dd>
  1612. <p><code>conditional_reverse&lt;order::big, order::native&gt;(x)</code>.</p>
  1613. </dd>
  1614. </dl>
  1615. </div>
  1616. </li>
  1617. </ul>
  1618. </div>
  1619. <div class="listingblock">
  1620. <div class="content">
  1621. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1622. EndianReversible native_to_big(EndianReversible x) noexcept;</code></pre>
  1623. </div>
  1624. </div>
  1625. <div class="ulist none">
  1626. <ul class="none">
  1627. <li>
  1628. <p></p>
  1629. <div class="dlist">
  1630. <dl>
  1631. <dt class="hdlist1">Returns</dt>
  1632. <dd>
  1633. <p><code>conditional_reverse&lt;order::native, order::big&gt;(x)</code>.</p>
  1634. </dd>
  1635. </dl>
  1636. </div>
  1637. </li>
  1638. </ul>
  1639. </div>
  1640. <div class="listingblock">
  1641. <div class="content">
  1642. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1643. EndianReversible little_to_native(EndianReversible x) noexcept;</code></pre>
  1644. </div>
  1645. </div>
  1646. <div class="ulist none">
  1647. <ul class="none">
  1648. <li>
  1649. <p></p>
  1650. <div class="dlist">
  1651. <dl>
  1652. <dt class="hdlist1">Returns</dt>
  1653. <dd>
  1654. <p><code>conditional_reverse&lt;order::little, order::native&gt;(x)</code>.</p>
  1655. </dd>
  1656. </dl>
  1657. </div>
  1658. </li>
  1659. </ul>
  1660. </div>
  1661. <div class="listingblock">
  1662. <div class="content">
  1663. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1664. EndianReversible native_to_little(EndianReversible x) noexcept;</code></pre>
  1665. </div>
  1666. </div>
  1667. <div class="ulist none">
  1668. <ul class="none">
  1669. <li>
  1670. <p></p>
  1671. <div class="dlist">
  1672. <dl>
  1673. <dt class="hdlist1">Returns</dt>
  1674. <dd>
  1675. <p><code>conditional_reverse&lt;order::native, order::little&gt;(x)</code>.</p>
  1676. </dd>
  1677. </dl>
  1678. </div>
  1679. </li>
  1680. </ul>
  1681. </div>
  1682. <div class="listingblock">
  1683. <div class="content">
  1684. <pre class="highlight"><code>template &lt;order O1, order O2, class EndianReversible&gt;
  1685. EndianReversible conditional_reverse(EndianReversible x) noexcept;</code></pre>
  1686. </div>
  1687. </div>
  1688. <div class="ulist none">
  1689. <ul class="none">
  1690. <li>
  1691. <p></p>
  1692. <div class="dlist">
  1693. <dl>
  1694. <dt class="hdlist1">Returns</dt>
  1695. <dd>
  1696. <p><code>x</code> if <code>O1 == O2,</code> otherwise <code>endian_reverse(x)</code>.</p>
  1697. </dd>
  1698. <dt class="hdlist1">Remarks</dt>
  1699. <dd>
  1700. <p>Whether <code>x</code> or <code>endian_reverse(x)</code> is to be returned shall be
  1701. determined at compile time.</p>
  1702. </dd>
  1703. </dl>
  1704. </div>
  1705. </li>
  1706. </ul>
  1707. </div>
  1708. <div class="listingblock">
  1709. <div class="content">
  1710. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1711. EndianReversible conditional_reverse(EndianReversible x,
  1712. order order1, order order2) noexcept;</code></pre>
  1713. </div>
  1714. </div>
  1715. <div class="ulist none">
  1716. <ul class="none">
  1717. <li>
  1718. <p></p>
  1719. <div class="dlist">
  1720. <dl>
  1721. <dt class="hdlist1">Returns</dt>
  1722. <dd>
  1723. <p><code>order1 == order2? x: endian_reverse(x)</code>.</p>
  1724. </dd>
  1725. </dl>
  1726. </div>
  1727. </li>
  1728. </ul>
  1729. </div>
  1730. </div>
  1731. <div class="sect3">
  1732. <h4 id="conversion_in_place_byte_reversal_functions">In-place Byte Reversal Functions</h4>
  1733. <div class="listingblock">
  1734. <div class="content">
  1735. <pre class="highlight"><code>template &lt;class EndianReversible&gt;
  1736. void endian_reverse_inplace(EndianReversible&amp; x) noexcept;</code></pre>
  1737. </div>
  1738. </div>
  1739. <div class="ulist none">
  1740. <ul class="none">
  1741. <li>
  1742. <p></p>
  1743. <div class="dlist">
  1744. <dl>
  1745. <dt class="hdlist1">Effects</dt>
  1746. <dd>
  1747. <p><code>x = endian_reverse(x)</code>.</p>
  1748. </dd>
  1749. </dl>
  1750. </div>
  1751. </li>
  1752. </ul>
  1753. </div>
  1754. <div class="listingblock">
  1755. <div class="content">
  1756. <pre class="highlight"><code>template &lt;class EndianReversibleInplace&gt;
  1757. void big_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;</code></pre>
  1758. </div>
  1759. </div>
  1760. <div class="ulist none">
  1761. <ul class="none">
  1762. <li>
  1763. <p></p>
  1764. <div class="dlist">
  1765. <dl>
  1766. <dt class="hdlist1">Effects</dt>
  1767. <dd>
  1768. <p><code>conditional_reverse_inplace&lt;order::big, order::native&gt;(x)</code>.</p>
  1769. </dd>
  1770. </dl>
  1771. </div>
  1772. </li>
  1773. </ul>
  1774. </div>
  1775. <div class="listingblock">
  1776. <div class="content">
  1777. <pre class="highlight"><code>template &lt;class EndianReversibleInplace&gt;
  1778. void native_to_big_inplace(EndianReversibleInplace&amp; x) noexcept;</code></pre>
  1779. </div>
  1780. </div>
  1781. <div class="ulist none">
  1782. <ul class="none">
  1783. <li>
  1784. <p></p>
  1785. <div class="dlist">
  1786. <dl>
  1787. <dt class="hdlist1">Effects</dt>
  1788. <dd>
  1789. <p><code>conditional_reverse_inplace&lt;order::native, order::big&gt;(x)</code>.</p>
  1790. </dd>
  1791. </dl>
  1792. </div>
  1793. </li>
  1794. </ul>
  1795. </div>
  1796. <div class="listingblock">
  1797. <div class="content">
  1798. <pre class="highlight"><code>template &lt;class EndianReversibleInplace&gt;
  1799. void little_to_native_inplace(EndianReversibleInplace&amp; x) noexcept;</code></pre>
  1800. </div>
  1801. </div>
  1802. <div class="ulist none">
  1803. <ul class="none">
  1804. <li>
  1805. <p></p>
  1806. <div class="dlist">
  1807. <dl>
  1808. <dt class="hdlist1">Effects</dt>
  1809. <dd>
  1810. <p><code>conditional_reverse_inplace&lt;order::little, order::native&gt;(x)</code>.</p>
  1811. </dd>
  1812. </dl>
  1813. </div>
  1814. </li>
  1815. </ul>
  1816. </div>
  1817. <div class="listingblock">
  1818. <div class="content">
  1819. <pre class="highlight"><code>template &lt;class EndianReversibleInplace&gt;
  1820. void native_to_little_inplace(EndianReversibleInplace&amp; x) noexcept;</code></pre>
  1821. </div>
  1822. </div>
  1823. <div class="ulist none">
  1824. <ul class="none">
  1825. <li>
  1826. <p></p>
  1827. <div class="dlist">
  1828. <dl>
  1829. <dt class="hdlist1">Effects</dt>
  1830. <dd>
  1831. <p><code>conditional_reverse_inplace&lt;order::native, order::little&gt;(x)</code>.</p>
  1832. </dd>
  1833. </dl>
  1834. </div>
  1835. </li>
  1836. </ul>
  1837. </div>
  1838. <div class="listingblock">
  1839. <div class="content">
  1840. <pre class="highlight"><code>template &lt;order O1, order O2, class EndianReversibleInplace&gt;
  1841. void conditional_reverse_inplace(EndianReversibleInplace&amp; x) noexcept;</code></pre>
  1842. </div>
  1843. </div>
  1844. <div class="ulist none">
  1845. <ul class="none">
  1846. <li>
  1847. <p></p>
  1848. <div class="dlist">
  1849. <dl>
  1850. <dt class="hdlist1">Effects</dt>
  1851. <dd>
  1852. <p>None if <code>O1 == O2,</code> otherwise <code>endian_reverse_inplace(x)</code>.</p>
  1853. </dd>
  1854. <dt class="hdlist1">Remarks</dt>
  1855. <dd>
  1856. <p>Which effect applies shall be determined at compile time.</p>
  1857. </dd>
  1858. </dl>
  1859. </div>
  1860. </li>
  1861. </ul>
  1862. </div>
  1863. <div class="listingblock">
  1864. <div class="content">
  1865. <pre class="highlight"><code>template &lt;class EndianReversibleInplace&gt;
  1866. void conditional_reverse_inplace(EndianReversibleInplace&amp; x,
  1867. order order1, order order2) noexcept;</code></pre>
  1868. </div>
  1869. </div>
  1870. <div class="ulist none">
  1871. <ul class="none">
  1872. <li>
  1873. <p></p>
  1874. <div class="dlist">
  1875. <dl>
  1876. <dt class="hdlist1">Effects</dt>
  1877. <dd>
  1878. <p>If <code>order1 == order2</code> then <code>endian_reverse_inplace(x)</code>.</p>
  1879. </dd>
  1880. </dl>
  1881. </div>
  1882. </li>
  1883. </ul>
  1884. </div>
  1885. </div>
  1886. <div class="sect3">
  1887. <h4 id="conversion_generic_load_and_store_functions">Generic Load and Store Functions</h4>
  1888. <div class="listingblock">
  1889. <div class="content">
  1890. <pre class="highlight"><code>template&lt;class T, std::size_t N, order Order&gt;
  1891. T endian_load( unsigned char const * p ) noexcept;</code></pre>
  1892. </div>
  1893. </div>
  1894. <div class="ulist none">
  1895. <ul class="none">
  1896. <li>
  1897. <p></p>
  1898. <div class="dlist">
  1899. <dl>
  1900. <dt class="hdlist1">Requires</dt>
  1901. <dd>
  1902. <p><code>sizeof(T)</code> must be 1, 2, 4, or 8. <code>N</code> must be between 1 and
  1903. <code>sizeof(T)</code>, inclusive. <code>T</code> must be trivially copyable. If <code>N</code> is not
  1904. equal to <code>sizeof(T)</code>, <code>T</code> must be integral or <code>enum</code>.</p>
  1905. </dd>
  1906. <dt class="hdlist1">Effects</dt>
  1907. <dd>
  1908. <p>Reads <code>N</code> bytes starting from <code>p</code>, in forward or reverse order
  1909. depending on whether <code>Order</code> matches the native endianness or not,
  1910. interprets the resulting bit pattern as a value of type <code>T</code>, and returns it.
  1911. If <code>sizeof(T)</code> is bigger than <code>N</code>, zero-extends when <code>T</code> is unsigned,
  1912. sign-extends otherwise.</p>
  1913. </dd>
  1914. </dl>
  1915. </div>
  1916. </li>
  1917. </ul>
  1918. </div>
  1919. <div class="listingblock">
  1920. <div class="content">
  1921. <pre class="highlight"><code>template&lt;class T, std::size_t N, order Order&gt;
  1922. void endian_store( unsigned char * p, T const &amp; v ) noexcept;</code></pre>
  1923. </div>
  1924. </div>
  1925. <div class="ulist none">
  1926. <ul class="none">
  1927. <li>
  1928. <p></p>
  1929. <div class="dlist">
  1930. <dl>
  1931. <dt class="hdlist1">Requires</dt>
  1932. <dd>
  1933. <p><code>sizeof(T)</code> must be 1, 2, 4, or 8. <code>N</code> must be between 1 and
  1934. <code>sizeof(T)</code>, inclusive. <code>T</code> must be trivially copyable. If <code>N</code> is not
  1935. equal to <code>sizeof(T)</code>, <code>T</code> must be integral or <code>enum</code>.</p>
  1936. </dd>
  1937. <dt class="hdlist1">Effects</dt>
  1938. <dd>
  1939. <p>Writes to <code>p</code> the <code>N</code> least significant bytes from the object
  1940. representation of <code>v</code>, in forward or reverse order depending on whether
  1941. <code>Order</code> matches the native endianness or not.</p>
  1942. </dd>
  1943. </dl>
  1944. </div>
  1945. </li>
  1946. </ul>
  1947. </div>
  1948. </div>
  1949. <div class="sect3">
  1950. <h4 id="conversion_convenience_load_functions">Convenience Load Functions</h4>
  1951. <div class="listingblock">
  1952. <div class="content">
  1953. <pre class="highlight"><code>inline boost::intM_t load_little_sN( unsigned char const * p ) noexcept;</code></pre>
  1954. </div>
  1955. </div>
  1956. <div class="ulist none">
  1957. <ul class="none">
  1958. <li>
  1959. <p></p>
  1960. <div class="paragraph">
  1961. <p>Reads an N-bit signed little-endian integer from <code>p</code>.</p>
  1962. </div>
  1963. <div class="dlist">
  1964. <dl>
  1965. <dt class="hdlist1">Returns</dt>
  1966. <dd>
  1967. <p><code>endian_load&lt;boost::intM_t, N/8, order::little&gt;( p )</code>.</p>
  1968. </dd>
  1969. </dl>
  1970. </div>
  1971. </li>
  1972. </ul>
  1973. </div>
  1974. <div class="listingblock">
  1975. <div class="content">
  1976. <pre class="highlight"><code>inline boost::uintM_t load_little_uN( unsigned char const * p ) noexcept;</code></pre>
  1977. </div>
  1978. </div>
  1979. <div class="ulist none">
  1980. <ul class="none">
  1981. <li>
  1982. <p></p>
  1983. <div class="paragraph">
  1984. <p>Reads an N-bit unsigned little-endian integer from <code>p</code>.</p>
  1985. </div>
  1986. <div class="dlist">
  1987. <dl>
  1988. <dt class="hdlist1">Returns</dt>
  1989. <dd>
  1990. <p><code>endian_load&lt;boost::uintM_t, N/8, order::little&gt;( p )</code>.</p>
  1991. </dd>
  1992. </dl>
  1993. </div>
  1994. </li>
  1995. </ul>
  1996. </div>
  1997. <div class="listingblock">
  1998. <div class="content">
  1999. <pre class="highlight"><code>inline boost::intM_t load_big_sN( unsigned char const * p ) noexcept;</code></pre>
  2000. </div>
  2001. </div>
  2002. <div class="ulist none">
  2003. <ul class="none">
  2004. <li>
  2005. <p></p>
  2006. <div class="paragraph">
  2007. <p>Reads an N-bit signed big-endian integer from <code>p</code>.</p>
  2008. </div>
  2009. <div class="dlist">
  2010. <dl>
  2011. <dt class="hdlist1">Returns</dt>
  2012. <dd>
  2013. <p><code>endian_load&lt;boost::intM_t, N/8, order::big&gt;( p )</code>.</p>
  2014. </dd>
  2015. </dl>
  2016. </div>
  2017. </li>
  2018. </ul>
  2019. </div>
  2020. <div class="listingblock">
  2021. <div class="content">
  2022. <pre class="highlight"><code>inline boost::uintM_t load_big_uN( unsigned char const * p ) noexcept;</code></pre>
  2023. </div>
  2024. </div>
  2025. <div class="ulist none">
  2026. <ul class="none">
  2027. <li>
  2028. <p></p>
  2029. <div class="paragraph">
  2030. <p>Reads an N-bit unsigned big-endian integer from <code>p</code>.</p>
  2031. </div>
  2032. <div class="dlist">
  2033. <dl>
  2034. <dt class="hdlist1">Returns</dt>
  2035. <dd>
  2036. <p><code>endian_load&lt;boost::uintM_t, N/8, order::big&gt;( p )</code>.</p>
  2037. </dd>
  2038. </dl>
  2039. </div>
  2040. </li>
  2041. </ul>
  2042. </div>
  2043. </div>
  2044. <div class="sect3">
  2045. <h4 id="conversion_convenience_store_functions">Convenience Store Functions</h4>
  2046. <div class="listingblock">
  2047. <div class="content">
  2048. <pre class="highlight"><code>inline void store_little_sN( unsigned char * p, boost::intM_t v ) noexcept;</code></pre>
  2049. </div>
  2050. </div>
  2051. <div class="ulist none">
  2052. <ul class="none">
  2053. <li>
  2054. <p></p>
  2055. <div class="paragraph">
  2056. <p>Writes an N-bit signed little-endian integer to <code>p</code>.</p>
  2057. </div>
  2058. <div class="dlist">
  2059. <dl>
  2060. <dt class="hdlist1">Effects</dt>
  2061. <dd>
  2062. <p><code>endian_store&lt;boost::intM_t, N/8, order::little&gt;( p, v )</code>.</p>
  2063. </dd>
  2064. </dl>
  2065. </div>
  2066. </li>
  2067. </ul>
  2068. </div>
  2069. <div class="listingblock">
  2070. <div class="content">
  2071. <pre class="highlight"><code>inline void store_little_uN( unsigned char * p, boost::uintM_t v ) noexcept;</code></pre>
  2072. </div>
  2073. </div>
  2074. <div class="ulist none">
  2075. <ul class="none">
  2076. <li>
  2077. <p></p>
  2078. <div class="paragraph">
  2079. <p>Writes an N-bit unsigned little-endian integer to <code>p</code>.</p>
  2080. </div>
  2081. <div class="dlist">
  2082. <dl>
  2083. <dt class="hdlist1">Effects</dt>
  2084. <dd>
  2085. <p><code>endian_store&lt;boost::uintM_t, N/8, order::little&gt;( p, v )</code>.</p>
  2086. </dd>
  2087. </dl>
  2088. </div>
  2089. </li>
  2090. </ul>
  2091. </div>
  2092. <div class="listingblock">
  2093. <div class="content">
  2094. <pre class="highlight"><code>inline void store_big_sN( unsigned char * p, boost::intM_t v ) noexcept;</code></pre>
  2095. </div>
  2096. </div>
  2097. <div class="ulist none">
  2098. <ul class="none">
  2099. <li>
  2100. <p></p>
  2101. <div class="paragraph">
  2102. <p>Writes an N-bit signed big-endian integer to <code>p</code>.</p>
  2103. </div>
  2104. <div class="dlist">
  2105. <dl>
  2106. <dt class="hdlist1">Effects</dt>
  2107. <dd>
  2108. <p><code>endian_store&lt;boost::intM_t, N/8, order::big&gt;( p, v )</code>.</p>
  2109. </dd>
  2110. </dl>
  2111. </div>
  2112. </li>
  2113. </ul>
  2114. </div>
  2115. <div class="listingblock">
  2116. <div class="content">
  2117. <pre class="highlight"><code>inline void store_big_uN( unsigned char * p, boost::uintM_t v ) noexcept;</code></pre>
  2118. </div>
  2119. </div>
  2120. <div class="ulist none">
  2121. <ul class="none">
  2122. <li>
  2123. <p></p>
  2124. <div class="paragraph">
  2125. <p>Writes an N-bit unsigned big-endian integer to <code>p</code>.</p>
  2126. </div>
  2127. <div class="dlist">
  2128. <dl>
  2129. <dt class="hdlist1">Effects</dt>
  2130. <dd>
  2131. <p><code>endian_store&lt;boost::uintM_t, N/8, order::big&gt;( p, v )</code>.</p>
  2132. </dd>
  2133. </dl>
  2134. </div>
  2135. </li>
  2136. </ul>
  2137. </div>
  2138. </div>
  2139. </div>
  2140. <div class="sect2">
  2141. <h3 id="conversion_faq">FAQ</h3>
  2142. <div class="paragraph">
  2143. <p>See the <a href="#overview_faq">Overview FAQ</a> for a library-wide FAQ.</p>
  2144. </div>
  2145. <div class="paragraph">
  2146. <p><strong>Why are both value returning and modify-in-place functions provided?</strong></p>
  2147. </div>
  2148. <div class="ulist">
  2149. <ul>
  2150. <li>
  2151. <p>Returning the result by value is the standard C and C&#43;&#43; idiom for functions
  2152. that compute a value from an argument. Modify-in-place functions allow cleaner
  2153. code in many real-world endian use cases and are more efficient for user-defined
  2154. types that have members such as string data that do not need to be reversed.
  2155. Thus both forms are provided.</p>
  2156. </li>
  2157. </ul>
  2158. </div>
  2159. <div class="paragraph">
  2160. <p><strong>Why not use the Linux names (htobe16, htole16, be16toh, le16toh, etc.) ?</strong></p>
  2161. </div>
  2162. <div class="ulist">
  2163. <ul>
  2164. <li>
  2165. <p>Those names are non-standard and vary even between POSIX-like operating
  2166. systems. A C&#43;&#43; library TS was going to use those names, but found they were
  2167. sometimes implemented as macros. Since macros do not respect scoping and
  2168. namespace rules, to use them would be very error prone.</p>
  2169. </li>
  2170. </ul>
  2171. </div>
  2172. </div>
  2173. <div class="sect2">
  2174. <h3 id="conversion_acknowledgements">Acknowledgements</h3>
  2175. <div class="paragraph">
  2176. <p>Tomas Puverle was instrumental in identifying and articulating the need to
  2177. support endian conversion as separate from endian integer types. Phil Endecott
  2178. suggested the form of the value returning signatures. Vicente Botet and other
  2179. reviewers suggested supporting user defined types. General reverse template
  2180. implementation approach using <code>std::reverse</code> suggested by Mathias Gaunard.
  2181. Portable implementation approach for 16, 32, and 64-bit integers suggested by
  2182. tymofey, with avoidance of undefined behavior as suggested by Giovanni Piero
  2183. Deretta, and a further refinement suggested by Pyry Jahkola. Intrinsic builtins
  2184. implementation approach for 16, 32, and 64-bit integers suggested by several
  2185. reviewers, and by David Stone, who provided his Boost licensed macro
  2186. implementation that became the starting point for
  2187. <code>boost/endian/detail/intrinsic.hpp</code>. Pierre Talbot provided the
  2188. <code>int8_t endian_reverse()</code> and templated <code>endian_reverse_inplace()</code>
  2189. implementations.</p>
  2190. </div>
  2191. </div>
  2192. </div>
  2193. </div>
  2194. <div class="sect1">
  2195. <h2 id="buffers">Endian Buffer Types</h2>
  2196. <div class="sectionbody">
  2197. <div class="sect2">
  2198. <h3 id="buffers_introduction">Introduction</h3>
  2199. <div class="paragraph">
  2200. <p>The internal byte order of arithmetic types is traditionally called
  2201. <strong>endianness</strong>. See the <a href="http://en.wikipedia.org/wiki/Endian">Wikipedia</a> for a full
  2202. exploration of <strong>endianness</strong>, including definitions of <strong>big endian</strong> and <strong>little
  2203. endian</strong>.</p>
  2204. </div>
  2205. <div class="paragraph">
  2206. <p>Header <code>boost/endian/buffers.hpp</code> provides <code>endian_buffer</code>, a portable endian
  2207. integer binary buffer class template with control over byte order, value type,
  2208. size, and alignment independent of the platform&#8217;s native endianness. Typedefs
  2209. provide easy-to-use names for common configurations.</p>
  2210. </div>
  2211. <div class="paragraph">
  2212. <p>Use cases primarily involve data portability, either via files or network
  2213. connections, but these byte-holders may also be used to reduce memory use, file
  2214. size, or network activity since they provide binary numeric sizes not otherwise
  2215. available.</p>
  2216. </div>
  2217. <div class="paragraph">
  2218. <p>Class <code>endian_buffer</code> is aimed at users who wish explicit control over when
  2219. endianness conversions occur. It also serves as the base class for the
  2220. <a href="#arithmetic">endian_arithmetic</a> class template, which is aimed at users who
  2221. wish fully automatic endianness conversion and direct support for all normal
  2222. arithmetic operations.</p>
  2223. </div>
  2224. </div>
  2225. <div class="sect2">
  2226. <h3 id="buffers_example">Example</h3>
  2227. <div class="paragraph">
  2228. <p>The <code>example/endian_example.cpp</code> program writes a binary file containing
  2229. four-byte, big-endian and little-endian integers:</p>
  2230. </div>
  2231. <div class="listingblock">
  2232. <div class="content">
  2233. <pre class="highlight"><code>#include &lt;iostream&gt;
  2234. #include &lt;cstdio&gt;
  2235. #include &lt;boost/endian/buffers.hpp&gt; // see Synopsis below
  2236. #include &lt;boost/static_assert.hpp&gt;
  2237. using namespace boost::endian;
  2238. namespace
  2239. {
  2240. // This is an extract from a very widely used GIS file format.
  2241. // Why the designer decided to mix big and little endians in
  2242. // the same file is not known. But this is a real-world format
  2243. // and users wishing to write low level code manipulating these
  2244. // files have to deal with the mixed endianness.
  2245. struct header
  2246. {
  2247. big_int32_buf_t file_code;
  2248. big_int32_buf_t file_length;
  2249. little_int32_buf_t version;
  2250. little_int32_buf_t shape_type;
  2251. };
  2252. const char* filename = "test.dat";
  2253. }
  2254. int main(int, char* [])
  2255. {
  2256. header h;
  2257. BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
  2258. h.file_code = 0x01020304;
  2259. h.file_length = sizeof(header);
  2260. h.version = 1;
  2261. h.shape_type = 0x01020304;
  2262. // Low-level I/O such as POSIX read/write or &lt;cstdio&gt;
  2263. // fread/fwrite is sometimes used for binary file operations
  2264. // when ultimate efficiency is important. Such I/O is often
  2265. // performed in some C++ wrapper class, but to drive home the
  2266. // point that endian integers are often used in fairly
  2267. // low-level code that does bulk I/O operations, &lt;cstdio&gt;
  2268. // fopen/fwrite is used for I/O in this example.
  2269. std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
  2270. if (!fi)
  2271. {
  2272. std::cout &lt;&lt; "could not open " &lt;&lt; filename &lt;&lt; '\n';
  2273. return 1;
  2274. }
  2275. if (std::fwrite(&amp;h, sizeof(header), 1, fi) != 1)
  2276. {
  2277. std::cout &lt;&lt; "write failure for " &lt;&lt; filename &lt;&lt; '\n';
  2278. return 1;
  2279. }
  2280. std::fclose(fi);
  2281. std::cout &lt;&lt; "created file " &lt;&lt; filename &lt;&lt; '\n';
  2282. return 0;
  2283. }</code></pre>
  2284. </div>
  2285. </div>
  2286. <div class="paragraph">
  2287. <p>After compiling and executing <code>example/endian_example.cpp</code>, a hex dump of
  2288. <code>test.dat</code> shows:</p>
  2289. </div>
  2290. <div class="listingblock">
  2291. <div class="content">
  2292. <pre class="highlight"><code>01020304 00000010 01000000 04030201</code></pre>
  2293. </div>
  2294. </div>
  2295. <div class="paragraph">
  2296. <p>Notice that the first two 32-bit integers are big endian while the second two
  2297. are little endian, even though the machine this was compiled and run on was
  2298. little endian.</p>
  2299. </div>
  2300. </div>
  2301. <div class="sect2">
  2302. <h3 id="buffers_limitations">Limitations</h3>
  2303. <div class="paragraph">
  2304. <p>Requires <code>&lt;climits&gt;</code>, <code>CHAR_BIT == 8</code>. If <code>CHAR_BIT</code> is some other value,
  2305. compilation will result in an <code>#error</code>. This restriction is in place because the
  2306. design, implementation, testing, and documentation has only considered issues
  2307. related to 8-bit bytes, and there have been no real-world use cases presented
  2308. for other sizes.</p>
  2309. </div>
  2310. <div class="paragraph">
  2311. <p>In C&#43;&#43;03, <code>endian_buffer</code> does not meet the requirements for POD types because
  2312. it has constructors and a private data member. This means that
  2313. common use cases are relying on unspecified behavior in that the C&#43;&#43; Standard
  2314. does not guarantee memory layout for non-POD types. This has not been a problem
  2315. in practice since all known C&#43;&#43; compilers lay out memory as if <code>endian</code> were
  2316. a POD type. In C&#43;&#43;11, it is possible to specify the default constructor as
  2317. trivial, and private data members and base classes no longer disqualify a type
  2318. from being a POD type. Thus under C&#43;&#43;11, <code>endian_buffer</code> will no longer be
  2319. relying on unspecified behavior.</p>
  2320. </div>
  2321. </div>
  2322. <div class="sect2">
  2323. <h3 id="buffers_feature_set">Feature set</h3>
  2324. <div class="ulist">
  2325. <ul>
  2326. <li>
  2327. <p>Big endian| little endian | native endian byte ordering.</p>
  2328. </li>
  2329. <li>
  2330. <p>Signed | unsigned</p>
  2331. </li>
  2332. <li>
  2333. <p>Unaligned | aligned</p>
  2334. </li>
  2335. <li>
  2336. <p>1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)</p>
  2337. </li>
  2338. <li>
  2339. <p>Choice of value type</p>
  2340. </li>
  2341. </ul>
  2342. </div>
  2343. </div>
  2344. <div class="sect2">
  2345. <h3 id="buffers_enums_and_typedefs">Enums and typedefs</h3>
  2346. <div class="paragraph">
  2347. <p>Two scoped enums are provided:</p>
  2348. </div>
  2349. <div class="listingblock">
  2350. <div class="content">
  2351. <pre class="highlight"><code>enum class order { big, little, native };
  2352. enum class align { no, yes };</code></pre>
  2353. </div>
  2354. </div>
  2355. <div class="paragraph">
  2356. <p>One class template is provided:</p>
  2357. </div>
  2358. <div class="listingblock">
  2359. <div class="content">
  2360. <pre class="highlight"><code>template &lt;order Order, typename T, std::size_t Nbits,
  2361. align Align = align::no&gt;
  2362. class endian_buffer;</code></pre>
  2363. </div>
  2364. </div>
  2365. <div class="paragraph">
  2366. <p>Typedefs, such as <code>big_int32_buf_t</code>, provide convenient naming conventions for
  2367. common use cases:</p>
  2368. </div>
  2369. <table class="tableblock frame-all grid-all stretch">
  2370. <colgroup>
  2371. <col style="width: 20%;">
  2372. <col style="width: 20%;">
  2373. <col style="width: 20%;">
  2374. <col style="width: 20%;">
  2375. <col style="width: 20%;">
  2376. </colgroup>
  2377. <thead>
  2378. <tr>
  2379. <th class="tableblock halign-left valign-top">Name</th>
  2380. <th class="tableblock halign-left valign-top">Alignment</th>
  2381. <th class="tableblock halign-left valign-top">Endianness</th>
  2382. <th class="tableblock halign-left valign-top">Sign</th>
  2383. <th class="tableblock halign-left valign-top">Sizes in bits (n)</th>
  2384. </tr>
  2385. </thead>
  2386. <tbody>
  2387. <tr>
  2388. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_intN_buf_t</code></p></td>
  2389. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2390. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  2391. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  2392. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2393. </tr>
  2394. <tr>
  2395. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_uintN_buf_t</code></p></td>
  2396. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2397. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  2398. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  2399. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2400. </tr>
  2401. <tr>
  2402. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_intN_buf_t</code></p></td>
  2403. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2404. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  2405. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  2406. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2407. </tr>
  2408. <tr>
  2409. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_uintN_buf_t</code></p></td>
  2410. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2411. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  2412. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  2413. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2414. </tr>
  2415. <tr>
  2416. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>native_intN_buf_t</code></p></td>
  2417. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2418. <td class="tableblock halign-left valign-top"><p class="tableblock">native</p></td>
  2419. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  2420. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2421. </tr>
  2422. <tr>
  2423. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>native_uintN_buf_t</code></p></td>
  2424. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  2425. <td class="tableblock halign-left valign-top"><p class="tableblock">native</p></td>
  2426. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  2427. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  2428. </tr>
  2429. <tr>
  2430. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_intN_buf_at</code></p></td>
  2431. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  2432. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  2433. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  2434. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  2435. </tr>
  2436. <tr>
  2437. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_uintN_buf_at</code></p></td>
  2438. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  2439. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  2440. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  2441. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  2442. </tr>
  2443. <tr>
  2444. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_intN_buf_at</code></p></td>
  2445. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  2446. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  2447. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  2448. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  2449. </tr>
  2450. <tr>
  2451. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_uintN_buf_at</code></p></td>
  2452. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  2453. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  2454. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  2455. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  2456. </tr>
  2457. </tbody>
  2458. </table>
  2459. <div class="paragraph">
  2460. <p>The unaligned types do not cause compilers to insert padding bytes in classes
  2461. and structs. This is an important characteristic that can be exploited to
  2462. minimize wasted space in memory, files, and network transmissions.</p>
  2463. </div>
  2464. <div class="admonitionblock caution">
  2465. <table>
  2466. <tr>
  2467. <td class="icon">
  2468. <div class="title">Caution</div>
  2469. </td>
  2470. <td class="content">
  2471. Code that uses aligned types is possibly non-portable because alignment
  2472. requirements vary between hardware architectures and because alignment may be
  2473. affected by compiler switches or pragmas. For example, alignment of an 64-bit
  2474. integer may be to a 32-bit boundary on a 32-bit machine and to a 64-bit boundary
  2475. on a 64-bit machine. Furthermore, aligned types are only available on
  2476. architectures with 8, 16, 32, and 64-bit integer types.
  2477. </td>
  2478. </tr>
  2479. </table>
  2480. </div>
  2481. <div class="admonitionblock tip">
  2482. <table>
  2483. <tr>
  2484. <td class="icon">
  2485. <div class="title">Tip</div>
  2486. </td>
  2487. <td class="content">
  2488. Prefer unaligned buffer types.
  2489. </td>
  2490. </tr>
  2491. </table>
  2492. </div>
  2493. <div class="admonitionblock tip">
  2494. <table>
  2495. <tr>
  2496. <td class="icon">
  2497. <div class="title">Tip</div>
  2498. </td>
  2499. <td class="content">
  2500. Protect yourself against alignment ills. For example:
  2501. </td>
  2502. </tr>
  2503. </table>
  2504. </div>
  2505. <div class="dlist none">
  2506. <dl>
  2507. <dt></dt>
  2508. <dd>
  2509. <div class="listingblock">
  2510. <div class="content">
  2511. <pre class="highlight"><code>static_assert(sizeof(containing_struct) == 12, "sizeof(containing_struct) is wrong");</code></pre>
  2512. </div>
  2513. </div>
  2514. </dd>
  2515. </dl>
  2516. </div>
  2517. <div class="paragraph">
  2518. <p>Note: One-byte big and little buffer types have identical layout on all
  2519. platforms, so they never actually reverse endianness. They are provided to
  2520. enable generic code, and to improve code readability and searchability.</p>
  2521. </div>
  2522. </div>
  2523. <div class="sect2">
  2524. <h3 id="buffers_class_template_endian_buffer">Class template <code>endian_buffer</code></h3>
  2525. <div class="paragraph">
  2526. <p>An <code>endian_buffer</code> is a byte-holder for arithmetic types with
  2527. user-specified endianness, value type, size, and alignment.</p>
  2528. </div>
  2529. <div class="sect3">
  2530. <h4 id="buffers_synopsis">Synopsis</h4>
  2531. <div class="listingblock">
  2532. <div class="content">
  2533. <pre class="highlight"><code>namespace boost
  2534. {
  2535. namespace endian
  2536. {
  2537. // C++11 features emulated if not available
  2538. enum class align { no, yes };
  2539. template &lt;order Order, class T, std::size_t Nbits,
  2540. align Align = align::no&gt;
  2541. class endian_buffer
  2542. {
  2543. public:
  2544. typedef T value_type;
  2545. endian_buffer() noexcept = default;
  2546. explicit endian_buffer(T v) noexcept;
  2547. endian_buffer&amp; operator=(T v) noexcept;
  2548. value_type value() const noexcept;
  2549. unsigned char* data() noexcept;
  2550. unsigned char const* data() const noexcept;
  2551. private:
  2552. unsigned char value_[Nbits / CHAR_BIT]; // exposition only
  2553. };
  2554. // stream inserter
  2555. template &lt;class charT, class traits, order Order, class T,
  2556. std::size_t n_bits, align Align&gt;
  2557. std::basic_ostream&lt;charT, traits&gt;&amp;
  2558. operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os,
  2559. const endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
  2560. // stream extractor
  2561. template &lt;class charT, class traits, order Order, class T,
  2562. std::size_t n_bits, align A&gt;
  2563. std::basic_istream&lt;charT, traits&gt;&amp;
  2564. operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is,
  2565. endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);
  2566. // typedefs
  2567. // unaligned big endian signed integer buffers
  2568. typedef endian_buffer&lt;order::big, int_least8_t, 8&gt; big_int8_buf_t;
  2569. typedef endian_buffer&lt;order::big, int_least16_t, 16&gt; big_int16_buf_t;
  2570. typedef endian_buffer&lt;order::big, int_least32_t, 24&gt; big_int24_buf_t;
  2571. typedef endian_buffer&lt;order::big, int_least32_t, 32&gt; big_int32_buf_t;
  2572. typedef endian_buffer&lt;order::big, int_least64_t, 40&gt; big_int40_buf_t;
  2573. typedef endian_buffer&lt;order::big, int_least64_t, 48&gt; big_int48_buf_t;
  2574. typedef endian_buffer&lt;order::big, int_least64_t, 56&gt; big_int56_buf_t;
  2575. typedef endian_buffer&lt;order::big, int_least64_t, 64&gt; big_int64_buf_t;
  2576. // unaligned big endian unsigned integer buffers
  2577. typedef endian_buffer&lt;order::big, uint_least8_t, 8&gt; big_uint8_buf_t;
  2578. typedef endian_buffer&lt;order::big, uint_least16_t, 16&gt; big_uint16_buf_t;
  2579. typedef endian_buffer&lt;order::big, uint_least32_t, 24&gt; big_uint24_buf_t;
  2580. typedef endian_buffer&lt;order::big, uint_least32_t, 32&gt; big_uint32_buf_t;
  2581. typedef endian_buffer&lt;order::big, uint_least64_t, 40&gt; big_uint40_buf_t;
  2582. typedef endian_buffer&lt;order::big, uint_least64_t, 48&gt; big_uint48_buf_t;
  2583. typedef endian_buffer&lt;order::big, uint_least64_t, 56&gt; big_uint56_buf_t;
  2584. typedef endian_buffer&lt;order::big, uint_least64_t, 64&gt; big_uint64_buf_t;
  2585. // unaligned big endian floating point buffers
  2586. typedef endian_buffer&lt;order::big, float, 32&gt; big_float32_buf_t;
  2587. typedef endian_buffer&lt;order::big, double, 64&gt; big_float64_buf_t;
  2588. // unaligned little endian signed integer buffers
  2589. typedef endian_buffer&lt;order::little, int_least8_t, 8&gt; little_int8_buf_t;
  2590. typedef endian_buffer&lt;order::little, int_least16_t, 16&gt; little_int16_buf_t;
  2591. typedef endian_buffer&lt;order::little, int_least32_t, 24&gt; little_int24_buf_t;
  2592. typedef endian_buffer&lt;order::little, int_least32_t, 32&gt; little_int32_buf_t;
  2593. typedef endian_buffer&lt;order::little, int_least64_t, 40&gt; little_int40_buf_t;
  2594. typedef endian_buffer&lt;order::little, int_least64_t, 48&gt; little_int48_buf_t;
  2595. typedef endian_buffer&lt;order::little, int_least64_t, 56&gt; little_int56_buf_t;
  2596. typedef endian_buffer&lt;order::little, int_least64_t, 64&gt; little_int64_buf_t;
  2597. // unaligned little endian unsigned integer buffers
  2598. typedef endian_buffer&lt;order::little, uint_least8_t, 8&gt; little_uint8_buf_t;
  2599. typedef endian_buffer&lt;order::little, uint_least16_t, 16&gt; little_uint16_buf_t;
  2600. typedef endian_buffer&lt;order::little, uint_least32_t, 24&gt; little_uint24_buf_t;
  2601. typedef endian_buffer&lt;order::little, uint_least32_t, 32&gt; little_uint32_buf_t;
  2602. typedef endian_buffer&lt;order::little, uint_least64_t, 40&gt; little_uint40_buf_t;
  2603. typedef endian_buffer&lt;order::little, uint_least64_t, 48&gt; little_uint48_buf_t;
  2604. typedef endian_buffer&lt;order::little, uint_least64_t, 56&gt; little_uint56_buf_t;
  2605. typedef endian_buffer&lt;order::little, uint_least64_t, 64&gt; little_uint64_buf_t;
  2606. // unaligned little endian floating point buffers
  2607. typedef endian_buffer&lt;order::little, float, 32&gt; little_float32_buf_t;
  2608. typedef endian_buffer&lt;order::little, double, 64&gt; little_float64_buf_t;
  2609. // unaligned native endian signed integer types
  2610. typedef implementation-defined_int8_buf_t native_int8_buf_t;
  2611. typedef implementation-defined_int16_buf_t native_int16_buf_t;
  2612. typedef implementation-defined_int24_buf_t native_int24_buf_t;
  2613. typedef implementation-defined_int32_buf_t native_int32_buf_t;
  2614. typedef implementation-defined_int40_buf_t native_int40_buf_t;
  2615. typedef implementation-defined_int48_buf_t native_int48_buf_t;
  2616. typedef implementation-defined_int56_buf_t native_int56_buf_t;
  2617. typedef implementation-defined_int64_buf_t native_int64_buf_t;
  2618. // unaligned native endian unsigned integer types
  2619. typedef implementation-defined_uint8_buf_t native_uint8_buf_t;
  2620. typedef implementation-defined_uint16_buf_t native_uint16_buf_t;
  2621. typedef implementation-defined_uint24_buf_t native_uint24_buf_t;
  2622. typedef implementation-defined_uint32_buf_t native_uint32_buf_t;
  2623. typedef implementation-defined_uint40_buf_t native_uint40_buf_t;
  2624. typedef implementation-defined_uint48_buf_t native_uint48_buf_t;
  2625. typedef implementation-defined_uint56_buf_t native_uint56_buf_t;
  2626. typedef implementation-defined_uint64_buf_t native_uint64_buf_t;
  2627. // unaligned native endian floating point types
  2628. typedef implementation-defined_float32_buf_t native_float32_buf_t;
  2629. typedef implementation-defined_float64_buf_t native_float64_buf_t;
  2630. // aligned big endian signed integer buffers
  2631. typedef endian_buffer&lt;order::big, int8_t, 8, align::yes&gt; big_int8_buf_at;
  2632. typedef endian_buffer&lt;order::big, int16_t, 16, align::yes&gt; big_int16_buf_at;
  2633. typedef endian_buffer&lt;order::big, int32_t, 32, align::yes&gt; big_int32_buf_at;
  2634. typedef endian_buffer&lt;order::big, int64_t, 64, align::yes&gt; big_int64_buf_at;
  2635. // aligned big endian unsigned integer buffers
  2636. typedef endian_buffer&lt;order::big, uint8_t, 8, align::yes&gt; big_uint8_buf_at;
  2637. typedef endian_buffer&lt;order::big, uint16_t, 16, align::yes&gt; big_uint16_buf_at;
  2638. typedef endian_buffer&lt;order::big, uint32_t, 32, align::yes&gt; big_uint32_buf_at;
  2639. typedef endian_buffer&lt;order::big, uint64_t, 64, align::yes&gt; big_uint64_buf_at;
  2640. // aligned big endian floating point buffers
  2641. typedef endian_buffer&lt;order::big, float, 32, align::yes&gt; big_float32_buf_at;
  2642. typedef endian_buffer&lt;order::big, double, 64, align::yes&gt; big_float64_buf_at;
  2643. // aligned little endian signed integer buffers
  2644. typedef endian_buffer&lt;order::little, int8_t, 8, align::yes&gt; little_int8_buf_at;
  2645. typedef endian_buffer&lt;order::little, int16_t, 16, align::yes&gt; little_int16_buf_at;
  2646. typedef endian_buffer&lt;order::little, int32_t, 32, align::yes&gt; little_int32_buf_at;
  2647. typedef endian_buffer&lt;order::little, int64_t, 64, align::yes&gt; little_int64_buf_at;
  2648. // aligned little endian unsigned integer buffers
  2649. typedef endian_buffer&lt;order::little, uint8_t, 8, align::yes&gt; little_uint8_buf_at;
  2650. typedef endian_buffer&lt;order::little, uint16_t, 16, align::yes&gt; little_uint16_buf_at;
  2651. typedef endian_buffer&lt;order::little, uint32_t, 32, align::yes&gt; little_uint32_buf_at;
  2652. typedef endian_buffer&lt;order::little, uint64_t, 64, align::yes&gt; little_uint64_buf_at;
  2653. // aligned little endian floating point buffers
  2654. typedef endian_buffer&lt;order::little, float, 32, align::yes&gt; little_float32_buf_at;
  2655. typedef endian_buffer&lt;order::little, double, 64, align::yes&gt; little_float64_buf_at;
  2656. // aligned native endian typedefs are not provided because
  2657. // &lt;cstdint&gt; types are superior for this use case
  2658. } // namespace endian
  2659. } // namespace boost</code></pre>
  2660. </div>
  2661. </div>
  2662. <div class="paragraph">
  2663. <p>The <code>implementation-defined</code> text in typedefs above is either <code>big</code> or <code>little</code>
  2664. according to the native endianness of the platform.</p>
  2665. </div>
  2666. <div class="paragraph">
  2667. <p>The expository data member <code>value_</code> stores the current value of the
  2668. <code>endian_buffer</code> object as a sequence of bytes ordered as specified by the
  2669. <code>Order</code> template parameter. The <code>CHAR_BIT</code> macro is defined in <code>&lt;climits&gt;</code>.
  2670. The only supported value of <code>CHAR_BIT</code> is 8.</p>
  2671. </div>
  2672. <div class="paragraph">
  2673. <p>The valid values of <code>Nbits</code> are as follows:</p>
  2674. </div>
  2675. <div class="ulist">
  2676. <ul>
  2677. <li>
  2678. <p>When <code>sizeof(T)</code> is 1, <code>Nbits</code> shall be 8;</p>
  2679. </li>
  2680. <li>
  2681. <p>When <code>sizeof(T)</code> is 2, <code>Nbits</code> shall be 16;</p>
  2682. </li>
  2683. <li>
  2684. <p>When <code>sizeof(T)</code> is 4, <code>Nbits</code> shall be 24 or 32;</p>
  2685. </li>
  2686. <li>
  2687. <p>When <code>sizeof(T)</code> is 8, <code>Nbits</code> shall be 40, 48, 56, or 64.</p>
  2688. </li>
  2689. </ul>
  2690. </div>
  2691. <div class="paragraph">
  2692. <p>Other values of <code>sizeof(T)</code> are not supported.</p>
  2693. </div>
  2694. <div class="paragraph">
  2695. <p>When <code>Nbits</code> is equal to <code>sizeof(T)*8</code>, <code>T</code> must be a trivially copyable type
  2696. (such as <code>float</code>) that is assumed to have the same endianness as <code>uintNbits_t</code>.</p>
  2697. </div>
  2698. <div class="paragraph">
  2699. <p>When <code>Nbits</code> is less than <code>sizeof(T)*8</code>, <code>T</code> must be either a standard integral
  2700. type (C&#43;&#43;std, [basic.fundamental]) or an <code>enum</code>.</p>
  2701. </div>
  2702. </div>
  2703. <div class="sect3">
  2704. <h4 id="buffers_members">Members</h4>
  2705. <div class="listingblock">
  2706. <div class="content">
  2707. <pre class="highlight"><code>endian_buffer() noexcept = default;</code></pre>
  2708. </div>
  2709. </div>
  2710. <div class="ulist none">
  2711. <ul class="none">
  2712. <li>
  2713. <p></p>
  2714. <div class="dlist">
  2715. <dl>
  2716. <dt class="hdlist1">Effects</dt>
  2717. <dd>
  2718. <p>Constructs an uninitialized object.</p>
  2719. </dd>
  2720. </dl>
  2721. </div>
  2722. </li>
  2723. </ul>
  2724. </div>
  2725. <div class="listingblock">
  2726. <div class="content">
  2727. <pre class="highlight"><code>explicit endian_buffer(T v) noexcept;</code></pre>
  2728. </div>
  2729. </div>
  2730. <div class="ulist none">
  2731. <ul class="none">
  2732. <li>
  2733. <p></p>
  2734. <div class="dlist">
  2735. <dl>
  2736. <dt class="hdlist1">Effects</dt>
  2737. <dd>
  2738. <p><code>endian_store&lt;T, Nbits/8, Order&gt;( value_, v )</code>.</p>
  2739. </dd>
  2740. </dl>
  2741. </div>
  2742. </li>
  2743. </ul>
  2744. </div>
  2745. <div class="listingblock">
  2746. <div class="content">
  2747. <pre class="highlight"><code>endian_buffer&amp; operator=(T v) noexcept;</code></pre>
  2748. </div>
  2749. </div>
  2750. <div class="ulist none">
  2751. <ul class="none">
  2752. <li>
  2753. <p></p>
  2754. <div class="dlist">
  2755. <dl>
  2756. <dt class="hdlist1">Effects</dt>
  2757. <dd>
  2758. <p><code>endian_store&lt;T, Nbits/8, Order&gt;( value_, v )</code>.</p>
  2759. </dd>
  2760. <dt class="hdlist1">Returns</dt>
  2761. <dd>
  2762. <p><code>*this</code>.</p>
  2763. </dd>
  2764. </dl>
  2765. </div>
  2766. </li>
  2767. </ul>
  2768. </div>
  2769. <div class="listingblock">
  2770. <div class="content">
  2771. <pre class="highlight"><code>value_type value() const noexcept;</code></pre>
  2772. </div>
  2773. </div>
  2774. <div class="ulist none">
  2775. <ul class="none">
  2776. <li>
  2777. <p></p>
  2778. <div class="dlist">
  2779. <dl>
  2780. <dt class="hdlist1">Returns</dt>
  2781. <dd>
  2782. <p><code>endian_load&lt;T, Nbits/8, Order&gt;( value_ )</code>.</p>
  2783. </dd>
  2784. </dl>
  2785. </div>
  2786. </li>
  2787. </ul>
  2788. </div>
  2789. <div class="listingblock">
  2790. <div class="content">
  2791. <pre class="highlight"><code>unsigned char* data() noexcept;</code></pre>
  2792. </div>
  2793. </div>
  2794. <div class="listingblock">
  2795. <div class="content">
  2796. <pre class="highlight"><code>unsigned char const* data() const noexcept;</code></pre>
  2797. </div>
  2798. </div>
  2799. <div class="ulist none">
  2800. <ul class="none">
  2801. <li>
  2802. <p></p>
  2803. <div class="dlist">
  2804. <dl>
  2805. <dt class="hdlist1">Returns</dt>
  2806. <dd>
  2807. <p>A pointer to the first byte of <code>value_</code>.</p>
  2808. </dd>
  2809. </dl>
  2810. </div>
  2811. </li>
  2812. </ul>
  2813. </div>
  2814. </div>
  2815. <div class="sect3">
  2816. <h4 id="buffers_non_member_functions">Non-member functions</h4>
  2817. <div class="listingblock">
  2818. <div class="content">
  2819. <pre class="highlight"><code>template &lt;class charT, class traits, order Order, class T,
  2820. std::size_t n_bits, align Align&gt;
  2821. std::basic_ostream&lt;charT, traits&gt;&amp; operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os,
  2822. const endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);</code></pre>
  2823. </div>
  2824. </div>
  2825. <div class="ulist none">
  2826. <ul class="none">
  2827. <li>
  2828. <p></p>
  2829. <div class="dlist">
  2830. <dl>
  2831. <dt class="hdlist1">Returns</dt>
  2832. <dd>
  2833. <p><code>os &lt;&lt; x.value()</code>.</p>
  2834. </dd>
  2835. </dl>
  2836. </div>
  2837. </li>
  2838. </ul>
  2839. </div>
  2840. <div class="listingblock">
  2841. <div class="content">
  2842. <pre class="highlight"><code>template &lt;class charT, class traits, order Order, class T,
  2843. std::size_t n_bits, align A&gt;
  2844. std::basic_istream&lt;charT, traits&gt;&amp; operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is,
  2845. endian_buffer&lt;Order, T, n_bits, Align&gt;&amp; x);</code></pre>
  2846. </div>
  2847. </div>
  2848. <div class="ulist none">
  2849. <ul class="none">
  2850. <li>
  2851. <p></p>
  2852. <div class="dlist">
  2853. <dl>
  2854. <dt class="hdlist1">Effects</dt>
  2855. <dd>
  2856. <p>As if:</p>
  2857. <div class="listingblock">
  2858. <div class="content">
  2859. <pre class="highlight"><code>T i;
  2860. if (is &gt;&gt; i)
  2861. x = i;</code></pre>
  2862. </div>
  2863. </div>
  2864. </dd>
  2865. <dt class="hdlist1">Returns</dt>
  2866. <dd>
  2867. <p><code>is</code>.</p>
  2868. </dd>
  2869. </dl>
  2870. </div>
  2871. </li>
  2872. </ul>
  2873. </div>
  2874. </div>
  2875. </div>
  2876. <div class="sect2">
  2877. <h3 id="buffers_faq">FAQ</h3>
  2878. <div class="paragraph">
  2879. <p>See the <a href="#overview_faq">Overview FAQ</a> for a library-wide FAQ.</p>
  2880. </div>
  2881. <div class="dlist">
  2882. <dl>
  2883. <dt class="hdlist1">Why not just use Boost.Serialization?</dt>
  2884. <dd>
  2885. <p>Serialization involves a conversion for every object involved in I/O. Endian
  2886. integers require no conversion or copying. They are already in the desired
  2887. format for binary I/O. Thus they can be read or written in bulk.</p>
  2888. </dd>
  2889. <dt class="hdlist1">Are endian types PODs?</dt>
  2890. <dd>
  2891. <p>Yes for C&#43;&#43;11. No for C&#43;&#43;03, although several
  2892. <a href="#buffers_compilation">macros</a> are available to force PODness in all cases.</p>
  2893. </dd>
  2894. <dt class="hdlist1">What are the implications of endian integer types not being PODs with C&#43;&#43;03 compilers?</dt>
  2895. <dd>
  2896. <p>They can&#8217;t be used in unions. Also, compilers aren&#8217;t required to align or lay
  2897. out storage in portable ways, although this potential problem hasn&#8217;t prevented
  2898. use of Boost.Endian with real compilers.</p>
  2899. </dd>
  2900. <dt class="hdlist1">What good is native endianness?</dt>
  2901. <dd>
  2902. <p>It provides alignment and size guarantees not available from the built-in
  2903. types. It eases generic programming.</p>
  2904. </dd>
  2905. <dt class="hdlist1">Why bother with the aligned endian types?</dt>
  2906. <dd>
  2907. <p>Aligned integer operations may be faster (as much as 10 to 20 times faster) if
  2908. the endianness and alignment of the type matches the endianness and alignment
  2909. requirements of the machine. The code, however, is likely to be somewhat less
  2910. portable than with the unaligned types.</p>
  2911. </dd>
  2912. </dl>
  2913. </div>
  2914. </div>
  2915. <div class="sect2">
  2916. <h3 id="buffers_design_considerations_for_boost_endian_buffers">Design considerations for Boost.Endian buffers</h3>
  2917. <div class="ulist">
  2918. <ul>
  2919. <li>
  2920. <p>Must be suitable for I/O - in other words, must be memcpyable.</p>
  2921. </li>
  2922. <li>
  2923. <p>Must provide exactly the size and internal byte ordering specified.</p>
  2924. </li>
  2925. <li>
  2926. <p>Must work correctly when the internal integer representation has more bits
  2927. that the sum of the bits in the external byte representation. Sign extension
  2928. must work correctly when the internal integer representation type has more
  2929. bits than the sum of the bits in the external bytes. For example, using
  2930. a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
  2931. both positive and negative values.</p>
  2932. </li>
  2933. <li>
  2934. <p>Must work correctly (including using the same defined external
  2935. representation) regardless of whether a compiler treats char as signed or
  2936. unsigned.</p>
  2937. </li>
  2938. <li>
  2939. <p>Unaligned types must not cause compilers to insert padding bytes.</p>
  2940. </li>
  2941. <li>
  2942. <p>The implementation should supply optimizations with great care. Experience
  2943. has shown that optimizations of endian integers often become pessimizations
  2944. when changing machines or compilers. Pessimizations can also happen when
  2945. changing compiler switches, compiler versions, or CPU models of the same
  2946. architecture.</p>
  2947. </li>
  2948. </ul>
  2949. </div>
  2950. </div>
  2951. <div class="sect2">
  2952. <h3 id="buffers_c11">C&#43;&#43;11</h3>
  2953. <div class="paragraph">
  2954. <p>The availability of the C&#43;&#43;11
  2955. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  2956. Functions</a> feature is detected automatically, and will be used if present to
  2957. ensure that objects of <code>class endian_buffer</code> are trivial, and thus
  2958. PODs.</p>
  2959. </div>
  2960. </div>
  2961. <div class="sect2">
  2962. <h3 id="buffers_compilation">Compilation</h3>
  2963. <div class="paragraph">
  2964. <p>Boost.Endian is implemented entirely within headers, with no need to link to
  2965. any Boost object libraries.</p>
  2966. </div>
  2967. <div class="paragraph">
  2968. <p>Several macros allow user control over features:</p>
  2969. </div>
  2970. <div class="ulist">
  2971. <ul>
  2972. <li>
  2973. <p><code>BOOST_ENDIAN_NO_CTORS</code> causes <code>class endian_buffer</code> to have no
  2974. constructors. The intended use is for compiling user code that must be
  2975. portable between compilers regardless of C&#43;&#43;11
  2976. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  2977. Functions</a> support. Use of constructors will always fail,</p>
  2978. </li>
  2979. <li>
  2980. <p><code>BOOST_ENDIAN_FORCE_PODNESS</code> causes <code>BOOST_ENDIAN_NO_CTORS</code> to be defined if
  2981. the compiler does not support C&#43;&#43;11
  2982. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  2983. Functions</a>. This is ensures that objects of <code>class endian_buffer</code> are PODs, and
  2984. so can be used in C&#43;&#43;03 unions. In C&#43;&#43;11, <code>class endian_buffer</code> objects are
  2985. PODs, even though they have constructors, so can always be used in unions.</p>
  2986. </li>
  2987. </ul>
  2988. </div>
  2989. </div>
  2990. </div>
  2991. </div>
  2992. <div class="sect1">
  2993. <h2 id="arithmetic">Endian Arithmetic Types</h2>
  2994. <div class="sectionbody">
  2995. <div class="sect2">
  2996. <h3 id="arithmetic_introduction">Introduction</h3>
  2997. <div class="paragraph">
  2998. <p>Header <code>boost/endian/arithmetic.hpp</code> provides integer binary types with
  2999. control over byte order, value type, size, and alignment. Typedefs provide
  3000. easy-to-use names for common configurations.</p>
  3001. </div>
  3002. <div class="paragraph">
  3003. <p>These types provide portable byte-holders for integer data, independent of
  3004. particular computer architectures. Use cases almost always involve I/O, either
  3005. via files or network connections. Although data portability is the primary
  3006. motivation, these integer byte-holders may also be used to reduce memory use,
  3007. file size, or network activity since they provide binary integer sizes not
  3008. otherwise available.</p>
  3009. </div>
  3010. <div class="paragraph">
  3011. <p>Such integer byte-holder types are traditionally called <strong>endian</strong> types. See the
  3012. <a href="http://en.wikipedia.org/wiki/Endian">Wikipedia</a> for a full exploration of
  3013. <strong>endianness</strong>, including definitions of <strong>big endian</strong> and <strong>little endian</strong>.</p>
  3014. </div>
  3015. <div class="paragraph">
  3016. <p>Boost endian integers provide the same full set of C&#43;&#43; assignment, arithmetic,
  3017. and relational operators as C&#43;&#43; standard integral types, with the standard
  3018. semantics.</p>
  3019. </div>
  3020. <div class="paragraph">
  3021. <p>Unary arithmetic operators are <code>+</code>, <code>-</code>, <code>~</code>, <code>!</code>, plus both prefix and postfix
  3022. <code>--</code> and <code>++</code>. Binary arithmetic operators are <code>+</code>, <code>+=</code>, <code>-</code>, <code>-=</code>, <code>*</code>,
  3023. <code>*=</code>, <code>/</code>, <code>/=</code>, <code>&amp;</code>, <code>&amp;=</code>, <code>|</code>, <code>|=</code>, <code>^</code>, <code>^=</code>, <code>&lt;&lt;</code>, <code>&lt;&lt;=</code>, <code>&gt;&gt;</code>, and
  3024. <code>&gt;&gt;=</code>. Binary relational operators are <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code>.</p>
  3025. </div>
  3026. <div class="paragraph">
  3027. <p>Implicit conversion to the underlying value type is provided. An implicit
  3028. constructor converting from the underlying value type is provided.</p>
  3029. </div>
  3030. </div>
  3031. <div class="sect2">
  3032. <h3 id="arithmetic_example">Example</h3>
  3033. <div class="paragraph">
  3034. <p>The <code>endian_example.cpp</code> program writes a binary file containing four-byte,
  3035. big-endian and little-endian integers:</p>
  3036. </div>
  3037. <div class="listingblock">
  3038. <div class="content">
  3039. <pre class="highlight"><code>#include &lt;iostream&gt;
  3040. #include &lt;cstdio&gt;
  3041. #include &lt;boost/endian/arithmetic.hpp&gt;
  3042. #include &lt;boost/static_assert.hpp&gt;
  3043. using namespace boost::endian;
  3044. namespace
  3045. {
  3046. // This is an extract from a very widely used GIS file format.
  3047. // Why the designer decided to mix big and little endians in
  3048. // the same file is not known. But this is a real-world format
  3049. // and users wishing to write low level code manipulating these
  3050. // files have to deal with the mixed endianness.
  3051. struct header
  3052. {
  3053. big_int32_t file_code;
  3054. big_int32_t file_length;
  3055. little_int32_t version;
  3056. little_int32_t shape_type;
  3057. };
  3058. const char* filename = "test.dat";
  3059. }
  3060. int main(int, char* [])
  3061. {
  3062. header h;
  3063. BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
  3064. h.file_code = 0x01020304;
  3065. h.file_length = sizeof(header);
  3066. h.version = 1;
  3067. h.shape_type = 0x01020304;
  3068. // Low-level I/O such as POSIX read/write or &lt;cstdio&gt;
  3069. // fread/fwrite is sometimes used for binary file operations
  3070. // when ultimate efficiency is important. Such I/O is often
  3071. // performed in some C++ wrapper class, but to drive home the
  3072. // point that endian integers are often used in fairly
  3073. // low-level code that does bulk I/O operations, &lt;cstdio&gt;
  3074. // fopen/fwrite is used for I/O in this example.
  3075. std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
  3076. if (!fi)
  3077. {
  3078. std::cout &lt;&lt; "could not open " &lt;&lt; filename &lt;&lt; '\n';
  3079. return 1;
  3080. }
  3081. if (std::fwrite(&amp;h, sizeof(header), 1, fi) != 1)
  3082. {
  3083. std::cout &lt;&lt; "write failure for " &lt;&lt; filename &lt;&lt; '\n';
  3084. return 1;
  3085. }
  3086. std::fclose(fi);
  3087. std::cout &lt;&lt; "created file " &lt;&lt; filename &lt;&lt; '\n';
  3088. return 0;
  3089. }</code></pre>
  3090. </div>
  3091. </div>
  3092. <div class="paragraph">
  3093. <p>After compiling and executing <code>endian_example.cpp</code>, a hex dump of <code>test.dat</code>
  3094. shows:</p>
  3095. </div>
  3096. <div class="listingblock">
  3097. <div class="content">
  3098. <pre class="highlight"><code>01020304 00000010 01000000 04030201</code></pre>
  3099. </div>
  3100. </div>
  3101. <div class="paragraph">
  3102. <p>Notice that the first two 32-bit integers are big endian while the second two
  3103. are little endian, even though the machine this was compiled and run on was
  3104. little endian.</p>
  3105. </div>
  3106. </div>
  3107. <div class="sect2">
  3108. <h3 id="arithmetic_limitations">Limitations</h3>
  3109. <div class="paragraph">
  3110. <p>Requires <code>&lt;climits&gt;</code>, <code>CHAR_BIT == 8</code>. If <code>CHAR_BIT</code> is some other value,
  3111. compilation will result in an <code>#error</code>. This restriction is in place because the
  3112. design, implementation, testing, and documentation has only considered issues
  3113. related to 8-bit bytes, and there have been no real-world use cases presented
  3114. for other sizes.</p>
  3115. </div>
  3116. <div class="paragraph">
  3117. <p>In C&#43;&#43;03, <code>endian_arithmetic</code> does not meet the requirements for POD types
  3118. because it has constructors, private data members, and a base class. This means
  3119. that common use cases are relying on unspecified behavior in that the C&#43;&#43;
  3120. Standard does not guarantee memory layout for non-POD types. This has not been a
  3121. problem in practice since all known C&#43;&#43; compilers lay out memory as if
  3122. <code>endian</code> were a POD type. In C&#43;&#43;11, it is possible to specify the default
  3123. constructor as trivial, and private data members and base classes no longer
  3124. disqualify a type from being a POD type. Thus under C&#43;&#43;11, <code>endian_arithmetic</code>
  3125. will no longer be relying on unspecified behavior.</p>
  3126. </div>
  3127. </div>
  3128. <div class="sect2">
  3129. <h3 id="arithmetic_feature_set">Feature set</h3>
  3130. <div class="ulist">
  3131. <ul>
  3132. <li>
  3133. <p>Big endian| little endian | native endian byte ordering.</p>
  3134. </li>
  3135. <li>
  3136. <p>Signed | unsigned</p>
  3137. </li>
  3138. <li>
  3139. <p>Unaligned | aligned</p>
  3140. </li>
  3141. <li>
  3142. <p>1-8 byte (unaligned) | 1, 2, 4, 8 byte (aligned)</p>
  3143. </li>
  3144. <li>
  3145. <p>Choice of value type</p>
  3146. </li>
  3147. </ul>
  3148. </div>
  3149. </div>
  3150. <div class="sect2">
  3151. <h3 id="arithmetic_enums_and_typedefs">Enums and typedefs</h3>
  3152. <div class="paragraph">
  3153. <p>Two scoped enums are provided:</p>
  3154. </div>
  3155. <div class="listingblock">
  3156. <div class="content">
  3157. <pre class="highlight"><code>enum class order { big, little, native };
  3158. enum class align { no, yes };</code></pre>
  3159. </div>
  3160. </div>
  3161. <div class="paragraph">
  3162. <p>One class template is provided:</p>
  3163. </div>
  3164. <div class="listingblock">
  3165. <div class="content">
  3166. <pre class="highlight"><code>template &lt;order Order, typename T, std::size_t n_bits,
  3167. align Align = align::no&gt;
  3168. class endian_arithmetic;</code></pre>
  3169. </div>
  3170. </div>
  3171. <div class="paragraph">
  3172. <p>Typedefs, such as <code>big_int32_t</code>, provide convenient naming conventions for
  3173. common use cases:</p>
  3174. </div>
  3175. <table class="tableblock frame-all grid-all stretch">
  3176. <colgroup>
  3177. <col style="width: 20%;">
  3178. <col style="width: 20%;">
  3179. <col style="width: 20%;">
  3180. <col style="width: 20%;">
  3181. <col style="width: 20%;">
  3182. </colgroup>
  3183. <thead>
  3184. <tr>
  3185. <th class="tableblock halign-left valign-top">Name</th>
  3186. <th class="tableblock halign-left valign-top">Alignment</th>
  3187. <th class="tableblock halign-left valign-top">Endianness</th>
  3188. <th class="tableblock halign-left valign-top">Sign</th>
  3189. <th class="tableblock halign-left valign-top">Sizes in bits (n)</th>
  3190. </tr>
  3191. </thead>
  3192. <tbody>
  3193. <tr>
  3194. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_intN_t</code></p></td>
  3195. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3196. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  3197. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  3198. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3199. </tr>
  3200. <tr>
  3201. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_uintN_t</code></p></td>
  3202. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3203. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  3204. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  3205. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3206. </tr>
  3207. <tr>
  3208. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_intN_t</code></p></td>
  3209. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3210. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  3211. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  3212. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3213. </tr>
  3214. <tr>
  3215. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_uintN_t</code></p></td>
  3216. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3217. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  3218. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  3219. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3220. </tr>
  3221. <tr>
  3222. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>native_intN_t</code></p></td>
  3223. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3224. <td class="tableblock halign-left valign-top"><p class="tableblock">native</p></td>
  3225. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  3226. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3227. </tr>
  3228. <tr>
  3229. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>native_uintN_t</code></p></td>
  3230. <td class="tableblock halign-left valign-top"><p class="tableblock">no</p></td>
  3231. <td class="tableblock halign-left valign-top"><p class="tableblock">native</p></td>
  3232. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  3233. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,24,32,40,48,56,64</p></td>
  3234. </tr>
  3235. <tr>
  3236. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_intN_at</code></p></td>
  3237. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  3238. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  3239. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  3240. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  3241. </tr>
  3242. <tr>
  3243. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>big_uintN_at</code></p></td>
  3244. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  3245. <td class="tableblock halign-left valign-top"><p class="tableblock">big</p></td>
  3246. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  3247. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  3248. </tr>
  3249. <tr>
  3250. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_intN_at</code></p></td>
  3251. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  3252. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  3253. <td class="tableblock halign-left valign-top"><p class="tableblock">signed</p></td>
  3254. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  3255. </tr>
  3256. <tr>
  3257. <td class="tableblock halign-left valign-top"><p class="tableblock"><code>little_uintN_at</code></p></td>
  3258. <td class="tableblock halign-left valign-top"><p class="tableblock">yes</p></td>
  3259. <td class="tableblock halign-left valign-top"><p class="tableblock">little</p></td>
  3260. <td class="tableblock halign-left valign-top"><p class="tableblock">unsigned</p></td>
  3261. <td class="tableblock halign-left valign-top"><p class="tableblock">8,16,32,64</p></td>
  3262. </tr>
  3263. </tbody>
  3264. </table>
  3265. <div class="paragraph">
  3266. <p>The unaligned types do not cause compilers to insert padding bytes in classes
  3267. and structs. This is an important characteristic that can be exploited to
  3268. minimize wasted space in memory, files, and network transmissions.</p>
  3269. </div>
  3270. <div class="admonitionblock caution">
  3271. <table>
  3272. <tr>
  3273. <td class="icon">
  3274. <div class="title">Caution</div>
  3275. </td>
  3276. <td class="content">
  3277. Code that uses aligned types is possibly non-portable because
  3278. alignment requirements vary between hardware architectures and because
  3279. alignment may be affected by compiler switches or pragmas. For example,
  3280. alignment of an 64-bit integer may be to a 32-bit boundary on a 32-bit machine.
  3281. Furthermore, aligned types are only available on architectures with 8, 16, 32,
  3282. and 64-bit integer types.
  3283. </td>
  3284. </tr>
  3285. </table>
  3286. </div>
  3287. <div class="admonitionblock tip">
  3288. <table>
  3289. <tr>
  3290. <td class="icon">
  3291. <div class="title">Tip</div>
  3292. </td>
  3293. <td class="content">
  3294. Prefer unaligned arithmetic types.
  3295. </td>
  3296. </tr>
  3297. </table>
  3298. </div>
  3299. <div class="admonitionblock tip">
  3300. <table>
  3301. <tr>
  3302. <td class="icon">
  3303. <div class="title">Tip</div>
  3304. </td>
  3305. <td class="content">
  3306. Protect yourself against alignment ills. For example:
  3307. </td>
  3308. </tr>
  3309. </table>
  3310. </div>
  3311. <div class="dlist none">
  3312. <dl>
  3313. <dt></dt>
  3314. <dd>
  3315. <div class="listingblock">
  3316. <div class="content">
  3317. <pre class="highlight"><code>static_assert(sizeof(containing_struct) == 12, "sizeof(containing_struct) is wrong");</code></pre>
  3318. </div>
  3319. </div>
  3320. </dd>
  3321. </dl>
  3322. </div>
  3323. <div class="admonitionblock note">
  3324. <table>
  3325. <tr>
  3326. <td class="icon">
  3327. <div class="title">Note</div>
  3328. </td>
  3329. <td class="content">
  3330. One-byte arithmetic types have identical layout on all platforms, so they
  3331. never actually reverse endianness. They are provided to enable generic code,
  3332. and to improve code readability and searchability.
  3333. </td>
  3334. </tr>
  3335. </table>
  3336. </div>
  3337. </div>
  3338. <div class="sect2">
  3339. <h3 id="arithmetic_class_template_endian_arithmetic">Class template <code>endian_arithmetic</code></h3>
  3340. <div class="paragraph">
  3341. <p>An <code>endian_integer</code> is an integer byte-holder with user-specified endianness,
  3342. value type, size, and alignment. The usual operations on arithmetic types are
  3343. supplied.</p>
  3344. </div>
  3345. <div class="sect3">
  3346. <h4 id="arithmetic_synopsis">Synopsis</h4>
  3347. <div class="listingblock">
  3348. <div class="content">
  3349. <pre class="highlight"><code>#include &lt;boost/endian/buffers.hpp&gt;
  3350. namespace boost
  3351. {
  3352. namespace endian
  3353. {
  3354. // C++11 features emulated if not available
  3355. enum class align { no, yes };
  3356. template &lt;order Order, class T, std::size_t n_bits,
  3357. align Align = align::no&gt;
  3358. class endian_arithmetic
  3359. : public endian_buffer&lt;Order, T, n_bits, Align&gt;
  3360. {
  3361. public:
  3362. typedef T value_type;
  3363. // if BOOST_ENDIAN_FORCE_PODNESS is defined &amp;&amp; C++11 PODs are not
  3364. // available then these two constructors will not be present
  3365. endian_arithmetic() noexcept = default;
  3366. endian_arithmetic(T v) noexcept;
  3367. endian_arithmetic&amp; operator=(T v) noexcept;
  3368. operator value_type() const noexcept;
  3369. value_type value() const noexcept; // for exposition; see endian_buffer
  3370. unsigned char* data() noexcept; // for exposition; see endian_buffer
  3371. unsigned char const* data() const noexcept; // for exposition; see endian_buffer
  3372. // arithmetic operations
  3373. // note that additional operations are provided by the value_type
  3374. value_type operator+() const noexcept;
  3375. endian_arithmetic&amp; operator+=(value_type y) noexcept;
  3376. endian_arithmetic&amp; operator-=(value_type y) noexcept;
  3377. endian_arithmetic&amp; operator*=(value_type y) noexcept;
  3378. endian_arithmetic&amp; operator/=(value_type y) noexcept;
  3379. endian_arithmetic&amp; operator%=(value_type y) noexcept;
  3380. endian_arithmetic&amp; operator&amp;=(value_type y) noexcept;
  3381. endian_arithmetic&amp; operator|=(value_type y) noexcept;
  3382. endian_arithmetic&amp; operator^=(value_type y) noexcept;
  3383. endian_arithmetic&amp; operator&lt;&lt;=(value_type y) noexcept;
  3384. endian_arithmetic&amp; operator&gt;&gt;=(value_type y) noexcept;
  3385. endian_arithmetic&amp; operator++() noexcept;
  3386. endian_arithmetic&amp; operator--() noexcept;
  3387. endian_arithmetic operator++(int) noexcept;
  3388. endian_arithmetic operator--(int) noexcept;
  3389. // Stream inserter
  3390. template &lt;class charT, class traits&gt;
  3391. friend std::basic_ostream&lt;charT, traits&gt;&amp;
  3392. operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os, const endian_arithmetic&amp; x);
  3393. // Stream extractor
  3394. template &lt;class charT, class traits&gt;
  3395. friend std::basic_istream&lt;charT, traits&gt;&amp;
  3396. operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is, endian_arithmetic&amp; x);
  3397. };
  3398. // typedefs
  3399. // unaligned big endian signed integer types
  3400. typedef endian_arithmetic&lt;order::big, int_least8_t, 8&gt; big_int8_t;
  3401. typedef endian_arithmetic&lt;order::big, int_least16_t, 16&gt; big_int16_t;
  3402. typedef endian_arithmetic&lt;order::big, int_least32_t, 24&gt; big_int24_t;
  3403. typedef endian_arithmetic&lt;order::big, int_least32_t, 32&gt; big_int32_t;
  3404. typedef endian_arithmetic&lt;order::big, int_least64_t, 40&gt; big_int40_t;
  3405. typedef endian_arithmetic&lt;order::big, int_least64_t, 48&gt; big_int48_t;
  3406. typedef endian_arithmetic&lt;order::big, int_least64_t, 56&gt; big_int56_t;
  3407. typedef endian_arithmetic&lt;order::big, int_least64_t, 64&gt; big_int64_t;
  3408. // unaligned big endian unsigned integer types
  3409. typedef endian_arithmetic&lt;order::big, uint_least8_t, 8&gt; big_uint8_t;
  3410. typedef endian_arithmetic&lt;order::big, uint_least16_t, 16&gt; big_uint16_t;
  3411. typedef endian_arithmetic&lt;order::big, uint_least32_t, 24&gt; big_uint24_t;
  3412. typedef endian_arithmetic&lt;order::big, uint_least32_t, 32&gt; big_uint32_t;
  3413. typedef endian_arithmetic&lt;order::big, uint_least64_t, 40&gt; big_uint40_t;
  3414. typedef endian_arithmetic&lt;order::big, uint_least64_t, 48&gt; big_uint48_t;
  3415. typedef endian_arithmetic&lt;order::big, uint_least64_t, 56&gt; big_uint56_t;
  3416. typedef endian_arithmetic&lt;order::big, uint_least64_t, 64&gt; big_uint64_t;
  3417. // unaligned big endian floating point types
  3418. typedef endian_arithmetic&lt;order::big, float, 32&gt; big_float32_t;
  3419. typedef endian_arithmetic&lt;order::big, double, 64&gt; big_float64_t;
  3420. // unaligned little endian signed integer types
  3421. typedef endian_arithmetic&lt;order::little, int_least8_t, 8&gt; little_int8_t;
  3422. typedef endian_arithmetic&lt;order::little, int_least16_t, 16&gt; little_int16_t;
  3423. typedef endian_arithmetic&lt;order::little, int_least32_t, 24&gt; little_int24_t;
  3424. typedef endian_arithmetic&lt;order::little, int_least32_t, 32&gt; little_int32_t;
  3425. typedef endian_arithmetic&lt;order::little, int_least64_t, 40&gt; little_int40_t;
  3426. typedef endian_arithmetic&lt;order::little, int_least64_t, 48&gt; little_int48_t;
  3427. typedef endian_arithmetic&lt;order::little, int_least64_t, 56&gt; little_int56_t;
  3428. typedef endian_arithmetic&lt;order::little, int_least64_t, 64&gt; little_int64_t;
  3429. // unaligned little endian unsigned integer types
  3430. typedef endian_arithmetic&lt;order::little, uint_least8_t, 8&gt; little_uint8_t;
  3431. typedef endian_arithmetic&lt;order::little, uint_least16_t, 16&gt; little_uint16_t;
  3432. typedef endian_arithmetic&lt;order::little, uint_least32_t, 24&gt; little_uint24_t;
  3433. typedef endian_arithmetic&lt;order::little, uint_least32_t, 32&gt; little_uint32_t;
  3434. typedef endian_arithmetic&lt;order::little, uint_least64_t, 40&gt; little_uint40_t;
  3435. typedef endian_arithmetic&lt;order::little, uint_least64_t, 48&gt; little_uint48_t;
  3436. typedef endian_arithmetic&lt;order::little, uint_least64_t, 56&gt; little_uint56_t;
  3437. typedef endian_arithmetic&lt;order::little, uint_least64_t, 64&gt; little_uint64_t;
  3438. // unaligned little endian floating point types
  3439. typedef endian_arithmetic&lt;order::little, float, 32&gt; little_float32_t;
  3440. typedef endian_arithmetic&lt;order::little, double, 64&gt; little_float64_t;
  3441. // unaligned native endian signed integer types
  3442. typedef implementation-defined_int8_t native_int8_t;
  3443. typedef implementation-defined_int16_t native_int16_t;
  3444. typedef implementation-defined_int24_t native_int24_t;
  3445. typedef implementation-defined_int32_t native_int32_t;
  3446. typedef implementation-defined_int40_t native_int40_t;
  3447. typedef implementation-defined_int48_t native_int48_t;
  3448. typedef implementation-defined_int56_t native_int56_t;
  3449. typedef implementation-defined_int64_t native_int64_t;
  3450. // unaligned native endian unsigned integer types
  3451. typedef implementation-defined_uint8_t native_uint8_t;
  3452. typedef implementation-defined_uint16_t native_uint16_t;
  3453. typedef implementation-defined_uint24_t native_uint24_t;
  3454. typedef implementation-defined_uint32_t native_uint32_t;
  3455. typedef implementation-defined_uint40_t native_uint40_t;
  3456. typedef implementation-defined_uint48_t native_uint48_t;
  3457. typedef implementation-defined_uint56_t native_uint56_t;
  3458. typedef implementation-defined_uint64_t native_uint64_t;
  3459. // unaligned native endian floating point types
  3460. typedef implementation-defined_float32_t native_float32_t;
  3461. typedef implementation-defined_float64_t native_float64_t;
  3462. // aligned big endian signed integer types
  3463. typedef endian_arithmetic&lt;order::big, int8_t, 8, align::yes&gt; big_int8_at;
  3464. typedef endian_arithmetic&lt;order::big, int16_t, 16, align::yes&gt; big_int16_at;
  3465. typedef endian_arithmetic&lt;order::big, int32_t, 32, align::yes&gt; big_int32_at;
  3466. typedef endian_arithmetic&lt;order::big, int64_t, 64, align::yes&gt; big_int64_at;
  3467. // aligned big endian unsigned integer types
  3468. typedef endian_arithmetic&lt;order::big, uint8_t, 8, align::yes&gt; big_uint8_at;
  3469. typedef endian_arithmetic&lt;order::big, uint16_t, 16, align::yes&gt; big_uint16_at;
  3470. typedef endian_arithmetic&lt;order::big, uint32_t, 32, align::yes&gt; big_uint32_at;
  3471. typedef endian_arithmetic&lt;order::big, uint64_t, 64, align::yes&gt; big_uint64_at;
  3472. // aligned big endian floating point types
  3473. typedef endian_arithmetic&lt;order::big, float, 32, align::yes&gt; big_float32_at;
  3474. typedef endian_arithmetic&lt;order::big, double, 64, align::yes&gt; big_float64_at;
  3475. // aligned little endian signed integer types
  3476. typedef endian_arithmetic&lt;order::little, int8_t, 8, align::yes&gt; little_int8_at;
  3477. typedef endian_arithmetic&lt;order::little, int16_t, 16, align::yes&gt; little_int16_at;
  3478. typedef endian_arithmetic&lt;order::little, int32_t, 32, align::yes&gt; little_int32_at;
  3479. typedef endian_arithmetic&lt;order::little, int64_t, 64, align::yes&gt; little_int64_at;
  3480. // aligned little endian unsigned integer types
  3481. typedef endian_arithmetic&lt;order::little, uint8_t, 8, align::yes&gt; little_uint8_at;
  3482. typedef endian_arithmetic&lt;order::little, uint16_t, 16, align::yes&gt; little_uint16_at;
  3483. typedef endian_arithmetic&lt;order::little, uint32_t, 32, align::yes&gt; little_uint32_at;
  3484. typedef endian_arithmetic&lt;order::little, uint64_t, 64, align::yes&gt; little_uint64_at;
  3485. // aligned little endian floating point types
  3486. typedef endian_arithmetic&lt;order::little, float, 32, align::yes&gt; little_float32_at;
  3487. typedef endian_arithmetic&lt;order::little, double, 64, align::yes&gt; little_float64_at;
  3488. // aligned native endian typedefs are not provided because
  3489. // &lt;cstdint&gt; types are superior for that use case
  3490. } // namespace endian
  3491. } // namespace boost</code></pre>
  3492. </div>
  3493. </div>
  3494. <div class="paragraph">
  3495. <p>The <code>implementation-defined</code> text above is either <code>big</code> or <code>little</code> according
  3496. to the endianness of the platform.</p>
  3497. </div>
  3498. <div class="paragraph">
  3499. <p>The only supported value of <code>CHAR_BIT</code> is 8.</p>
  3500. </div>
  3501. <div class="paragraph">
  3502. <p>The valid values of <code>Nbits</code> are as follows:</p>
  3503. </div>
  3504. <div class="ulist">
  3505. <ul>
  3506. <li>
  3507. <p>When <code>sizeof(T)</code> is 1, <code>Nbits</code> shall be 8;</p>
  3508. </li>
  3509. <li>
  3510. <p>When <code>sizeof(T)</code> is 2, <code>Nbits</code> shall be 16;</p>
  3511. </li>
  3512. <li>
  3513. <p>When <code>sizeof(T)</code> is 4, <code>Nbits</code> shall be 24 or 32;</p>
  3514. </li>
  3515. <li>
  3516. <p>When <code>sizeof(T)</code> is 8, <code>Nbits</code> shall be 40, 48, 56, or 64.</p>
  3517. </li>
  3518. </ul>
  3519. </div>
  3520. <div class="paragraph">
  3521. <p>Other values of <code>sizeof(T)</code> are not supported.</p>
  3522. </div>
  3523. <div class="paragraph">
  3524. <p>When <code>Nbits</code> is equal to <code>sizeof(T)*8</code>, <code>T</code> must be a standard arithmetic type.</p>
  3525. </div>
  3526. <div class="paragraph">
  3527. <p>When <code>Nbits</code> is less than <code>sizeof(T)*8</code>, <code>T</code> must be a standard integral type
  3528. (C&#43;&#43;std, [basic.fundamental]) that is not <code>bool</code>.</p>
  3529. </div>
  3530. </div>
  3531. <div class="sect3">
  3532. <h4 id="arithmetic_members">Members</h4>
  3533. <div class="listingblock">
  3534. <div class="content">
  3535. <pre class="highlight"><code>endian_arithmetic() noexcept = default; // C++03: endian(){}</code></pre>
  3536. </div>
  3537. </div>
  3538. <div class="ulist none">
  3539. <ul class="none">
  3540. <li>
  3541. <p></p>
  3542. <div class="dlist">
  3543. <dl>
  3544. <dt class="hdlist1">Effects</dt>
  3545. <dd>
  3546. <p>Constructs an uninitialized object.</p>
  3547. </dd>
  3548. </dl>
  3549. </div>
  3550. </li>
  3551. </ul>
  3552. </div>
  3553. <div class="listingblock">
  3554. <div class="content">
  3555. <pre class="highlight"><code>endian_arithmetic(T v) noexcept;</code></pre>
  3556. </div>
  3557. </div>
  3558. <div class="ulist none">
  3559. <ul class="none">
  3560. <li>
  3561. <p></p>
  3562. <div class="dlist">
  3563. <dl>
  3564. <dt class="hdlist1">Effects</dt>
  3565. <dd>
  3566. <p>See <code>endian_buffer::endian_buffer(T)</code>.</p>
  3567. </dd>
  3568. </dl>
  3569. </div>
  3570. </li>
  3571. </ul>
  3572. </div>
  3573. <div class="listingblock">
  3574. <div class="content">
  3575. <pre class="highlight"><code>endian_arithmetic&amp; operator=(T v) noexcept;</code></pre>
  3576. </div>
  3577. </div>
  3578. <div class="ulist none">
  3579. <ul class="none">
  3580. <li>
  3581. <p></p>
  3582. <div class="dlist">
  3583. <dl>
  3584. <dt class="hdlist1">Effects</dt>
  3585. <dd>
  3586. <p>See <code>endian_buffer::operator=(T)</code>.</p>
  3587. </dd>
  3588. <dt class="hdlist1">Returns</dt>
  3589. <dd>
  3590. <p><code>*this</code>.</p>
  3591. </dd>
  3592. </dl>
  3593. </div>
  3594. </li>
  3595. </ul>
  3596. </div>
  3597. <div class="listingblock">
  3598. <div class="content">
  3599. <pre class="highlight"><code>operator T() const noexcept;</code></pre>
  3600. </div>
  3601. </div>
  3602. <div class="ulist none">
  3603. <ul class="none">
  3604. <li>
  3605. <p></p>
  3606. <div class="dlist">
  3607. <dl>
  3608. <dt class="hdlist1">Returns</dt>
  3609. <dd>
  3610. <p><code>value()</code>.</p>
  3611. </dd>
  3612. </dl>
  3613. </div>
  3614. </li>
  3615. </ul>
  3616. </div>
  3617. </div>
  3618. <div class="sect3">
  3619. <h4 id="arithmetic_other_operators">Other operators</h4>
  3620. <div class="paragraph">
  3621. <p>Other operators on endian objects are forwarded to the equivalent operator on
  3622. <code>value_type</code>.</p>
  3623. </div>
  3624. </div>
  3625. <div class="sect3">
  3626. <h4 id="arithmetic_stream_inserter">Stream inserter</h4>
  3627. <div class="listingblock">
  3628. <div class="content">
  3629. <pre class="highlight"><code>template &lt;class charT, class traits&gt;
  3630. friend std::basic_ostream&lt;charT, traits&gt;&amp;
  3631. operator&lt;&lt;(std::basic_ostream&lt;charT, traits&gt;&amp; os, const endian_arithmetic&amp; x);</code></pre>
  3632. </div>
  3633. </div>
  3634. <div class="ulist none">
  3635. <ul class="none">
  3636. <li>
  3637. <p></p>
  3638. <div class="dlist">
  3639. <dl>
  3640. <dt class="hdlist1">Returns</dt>
  3641. <dd>
  3642. <p><code>os &lt;&lt; +x</code>.</p>
  3643. </dd>
  3644. </dl>
  3645. </div>
  3646. </li>
  3647. </ul>
  3648. </div>
  3649. </div>
  3650. <div class="sect3">
  3651. <h4 id="arithmetic_stream_extractor">Stream extractor</h4>
  3652. <div class="listingblock">
  3653. <div class="content">
  3654. <pre class="highlight"><code>template &lt;class charT, class traits&gt;
  3655. friend std::basic_istream&lt;charT, traits&gt;&amp;
  3656. operator&gt;&gt;(std::basic_istream&lt;charT, traits&gt;&amp; is, endian_arithmetic&amp; x);</code></pre>
  3657. </div>
  3658. </div>
  3659. <div class="ulist none">
  3660. <ul class="none">
  3661. <li>
  3662. <p></p>
  3663. <div class="dlist">
  3664. <dl>
  3665. <dt class="hdlist1">Effects</dt>
  3666. <dd>
  3667. <p>As if:</p>
  3668. <div class="listingblock">
  3669. <div class="content">
  3670. <pre class="highlight"><code>T i;
  3671. if (is &gt;&gt; i)
  3672. x = i;</code></pre>
  3673. </div>
  3674. </div>
  3675. </dd>
  3676. <dt class="hdlist1">Returns</dt>
  3677. <dd>
  3678. <p><code>is</code>.</p>
  3679. </dd>
  3680. </dl>
  3681. </div>
  3682. </li>
  3683. </ul>
  3684. </div>
  3685. </div>
  3686. </div>
  3687. <div class="sect2">
  3688. <h3 id="arithmetic_faq">FAQ</h3>
  3689. <div class="paragraph">
  3690. <p>See the <a href="#overview_faq">Overview FAQ</a> for a library-wide FAQ.</p>
  3691. </div>
  3692. <div class="dlist">
  3693. <dl>
  3694. <dt class="hdlist1">Why not just use Boost.Serialization?</dt>
  3695. <dd>
  3696. <p>Serialization involves a conversion for every object involved in I/O. Endian
  3697. integers require no conversion or copying. They are already in the desired
  3698. format for binary I/O. Thus they can be read or written in bulk.</p>
  3699. </dd>
  3700. <dt class="hdlist1">Are endian types PODs?</dt>
  3701. <dd>
  3702. <p>Yes for C&#43;&#43;11. No for C&#43;&#43;03, although several
  3703. <a href="#arithmetic_compilation">macros</a> are available to force PODness in all cases.</p>
  3704. </dd>
  3705. <dt class="hdlist1">What are the implications of endian integer types not being PODs with C&#43;&#43;03 compilers?</dt>
  3706. <dd>
  3707. <p>They can&#8217;t be used in unions. Also, compilers aren&#8217;t required to align or lay
  3708. out storage in portable ways, although this potential problem hasn&#8217;t prevented
  3709. use of Boost.Endian with real compilers.</p>
  3710. </dd>
  3711. <dt class="hdlist1">What good is native endianness?</dt>
  3712. <dd>
  3713. <p>It provides alignment and size guarantees not available from the built-in
  3714. types. It eases generic programming.</p>
  3715. </dd>
  3716. <dt class="hdlist1">Why bother with the aligned endian types?</dt>
  3717. <dd>
  3718. <p>Aligned integer operations may be faster (as much as 10 to 20 times faster)
  3719. if the endianness and alignment of the type matches the endianness and
  3720. alignment requirements of the machine. The code, however, will be somewhat less
  3721. portable than with the unaligned types.</p>
  3722. </dd>
  3723. <dt class="hdlist1">Why provide the arithmetic operations?</dt>
  3724. <dd>
  3725. <p>Providing a full set of operations reduces program clutter and makes code
  3726. both easier to write and to read. Consider incrementing a variable in a record.
  3727. It is very convenient to write:</p>
  3728. <div class="listingblock">
  3729. <div class="content">
  3730. <pre class="highlight"><code>++record.foo;</code></pre>
  3731. </div>
  3732. </div>
  3733. <div class="paragraph">
  3734. <p>Rather than:</p>
  3735. </div>
  3736. <div class="listingblock">
  3737. <div class="content">
  3738. <pre class="highlight"><code>int temp(record.foo);
  3739. ++temp;
  3740. record.foo = temp;</code></pre>
  3741. </div>
  3742. </div>
  3743. </dd>
  3744. </dl>
  3745. </div>
  3746. </div>
  3747. <div class="sect2">
  3748. <h3 id="arithmetic_design_considerations_for_boost_endian_types">Design considerations for Boost.Endian types</h3>
  3749. <div class="ulist">
  3750. <ul>
  3751. <li>
  3752. <p>Must be suitable for I/O - in other words, must be memcpyable.</p>
  3753. </li>
  3754. <li>
  3755. <p>Must provide exactly the size and internal byte ordering specified.</p>
  3756. </li>
  3757. <li>
  3758. <p>Must work correctly when the internal integer representation has more bits
  3759. that the sum of the bits in the external byte representation. Sign extension
  3760. must work correctly when the internal integer representation type has more
  3761. bits than the sum of the bits in the external bytes. For example, using
  3762. a 64-bit integer internally to represent 40-bit (5 byte) numbers must work for
  3763. both positive and negative values.</p>
  3764. </li>
  3765. <li>
  3766. <p>Must work correctly (including using the same defined external
  3767. representation) regardless of whether a compiler treats char as signed or
  3768. unsigned.</p>
  3769. </li>
  3770. <li>
  3771. <p>Unaligned types must not cause compilers to insert padding bytes.</p>
  3772. </li>
  3773. <li>
  3774. <p>The implementation should supply optimizations with great care. Experience
  3775. has shown that optimizations of endian integers often become pessimizations
  3776. when changing machines or compilers. Pessimizations can also happen when
  3777. changing compiler switches, compiler versions, or CPU models of the same
  3778. architecture.</p>
  3779. </li>
  3780. </ul>
  3781. </div>
  3782. </div>
  3783. <div class="sect2">
  3784. <h3 id="arithmetic_experience">Experience</h3>
  3785. <div class="paragraph">
  3786. <p>Classes with similar functionality have been independently developed by
  3787. several Boost programmers and used very successful in high-value, high-use
  3788. applications for many years. These independently developed endian libraries
  3789. often evolved from C libraries that were also widely used. Endian types have
  3790. proven widely useful across a wide range of computer architectures and
  3791. applications.</p>
  3792. </div>
  3793. </div>
  3794. <div class="sect2">
  3795. <h3 id="arithmetic_motivating_use_cases">Motivating use cases</h3>
  3796. <div class="paragraph">
  3797. <p>Neil Mayhew writes: "I can also provide a meaningful use-case for this
  3798. library: reading TrueType font files from disk and processing the contents. The
  3799. data format has fixed endianness (big) and has unaligned values in various
  3800. places. Using Boost.Endian simplifies and cleans the code wonderfully."</p>
  3801. </div>
  3802. </div>
  3803. <div class="sect2">
  3804. <h3 id="arithmetic_c11">C&#43;&#43;11</h3>
  3805. <div class="paragraph">
  3806. <p>The availability of the C&#43;&#43;11
  3807. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  3808. Functions</a> feature is detected automatically, and will be used if present to
  3809. ensure that objects of <code>class endian_arithmetic</code> are trivial, and thus PODs.</p>
  3810. </div>
  3811. </div>
  3812. <div class="sect2">
  3813. <h3 id="arithmetic_compilation">Compilation</h3>
  3814. <div class="paragraph">
  3815. <p>Boost.Endian is implemented entirely within headers, with no need to link to any
  3816. Boost object libraries.</p>
  3817. </div>
  3818. <div class="paragraph">
  3819. <p>Several macros allow user control over features:</p>
  3820. </div>
  3821. <div class="ulist">
  3822. <ul>
  3823. <li>
  3824. <p>BOOST_ENDIAN_NO_CTORS causes <code>class endian_arithmetic</code> to have no
  3825. constructors. The intended use is for compiling user code that must be portable
  3826. between compilers regardless of C&#43;&#43;11
  3827. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  3828. Functions</a> support. Use of constructors will always fail,</p>
  3829. </li>
  3830. <li>
  3831. <p>BOOST_ENDIAN_FORCE_PODNESS causes BOOST_ENDIAN_NO_CTORS to be defined if
  3832. the compiler does not support C&#43;&#43;11
  3833. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted
  3834. Functions</a>. This is ensures that objects of <code>class endian_arithmetic</code> are PODs,
  3835. and so can be used in C&#43;&#43;03 unions. In C&#43;&#43;11, <code>class endian_arithmetic</code>
  3836. objects are PODs, even though they have constructors, so can always be used in
  3837. unions.</p>
  3838. </li>
  3839. </ul>
  3840. </div>
  3841. </div>
  3842. <div class="sect2">
  3843. <h3 id="arithmetic_acknowledgements">Acknowledgements</h3>
  3844. <div class="paragraph">
  3845. <p>Original design developed by Darin Adler based on classes developed by Mark
  3846. Borgerding. Four original class templates combined into a single
  3847. <code>endian_arithmetic</code> class template by Beman Dawes, who put the library together,
  3848. provided documentation, added the typedefs, and also added the
  3849. <code>unrolled_byte_loops</code> sign partial specialization to correctly extend the sign
  3850. when cover integer size differs from endian representation size.</p>
  3851. </div>
  3852. </div>
  3853. </div>
  3854. </div>
  3855. <div class="sect1">
  3856. <h2 id="choosing">Choosing Approach</h2>
  3857. <div class="sectionbody">
  3858. <div class="sect2">
  3859. <h3 id="choosing_introduction">Introduction</h3>
  3860. <div class="paragraph">
  3861. <p>Deciding which is the best endianness approach (conversion functions, buffer
  3862. types, or arithmetic types) for a particular application involves complex
  3863. engineering trade-offs. It is hard to assess those trade-offs without some
  3864. understanding of the different interfaces, so you might want to read the
  3865. <a href="#conversion">conversion functions</a>, <a href="#buffers">buffer types</a>, and
  3866. <a href="#arithmetic">arithmetic types</a> pages before diving into this page.</p>
  3867. </div>
  3868. </div>
  3869. <div class="sect2">
  3870. <h3 id="choosing_choosing_between_conversion_functions_buffer_types_and_arithmetic_types">Choosing between conversion functions, buffer types, and arithmetic types</h3>
  3871. <div class="paragraph">
  3872. <p>The best approach to endianness for a particular application depends on the
  3873. interaction between the application&#8217;s needs and the characteristics of each of
  3874. the three approaches.</p>
  3875. </div>
  3876. <div class="paragraph">
  3877. <p><strong>Recommendation:</strong> If you are new to endianness, uncertain, or don&#8217;t want to
  3878. invest the time to study engineering trade-offs, use
  3879. <a href="#arithmetic">endian arithmetic types</a>. They are safe, easy to use, and easy to
  3880. maintain. Use the <em><a href="#choosing_anticipating_need">anticipating need</a></em> design
  3881. pattern locally around performance hot spots like lengthy loops, if needed.</p>
  3882. </div>
  3883. <div class="sect3">
  3884. <h4 id="choosing_background">Background</h4>
  3885. <div class="paragraph">
  3886. <p>A dealing with endianness usually implies a program portability or a data
  3887. portability requirement, and often both. That means real programs dealing with
  3888. endianness are usually complex, so the examples shown here would really be
  3889. written as multiple functions spread across multiple translation units. They
  3890. would involve interfaces that can not be altered as they are supplied by
  3891. third-parties or the standard library.</p>
  3892. </div>
  3893. </div>
  3894. <div class="sect3">
  3895. <h4 id="choosing_characteristics">Characteristics</h4>
  3896. <div class="paragraph">
  3897. <p>The characteristics that differentiate the three approaches to endianness are
  3898. the endianness invariants, conversion explicitness, arithmetic operations, sizes
  3899. available, and alignment requirements.</p>
  3900. </div>
  3901. <div class="sect4">
  3902. <h5 id="choosing_endianness_invariants">Endianness invariants</h5>
  3903. <div class="paragraph">
  3904. <p><strong>Endian conversion functions</strong> use objects of the ordinary C&#43;&#43; arithmetic types
  3905. like <code>int</code> or <code>unsigned short</code> to hold values. That breaks the implicit
  3906. invariant that the C&#43;&#43; language rules apply. The usual language rules only apply
  3907. if the endianness of the object is currently set to the native endianness for
  3908. the platform. That can make it very hard to reason about logic flow, and result
  3909. in difficult to find bugs.</p>
  3910. </div>
  3911. <div class="paragraph">
  3912. <p>For example:</p>
  3913. </div>
  3914. <div class="listingblock">
  3915. <div class="content">
  3916. <pre class="highlight"><code>struct data_t // big endian
  3917. {
  3918. int32_t v1; // description ...
  3919. int32_t v2; // description ...
  3920. ... additional character data members (i.e. non-endian)
  3921. int32_t v3; // description ...
  3922. };
  3923. data_t data;
  3924. read(data);
  3925. big_to_native_inplace(data.v1);
  3926. big_to_native_inplace(data.v2);
  3927. ...
  3928. ++v1;
  3929. third_party::func(data.v2);
  3930. ...
  3931. native_to_big_inplace(data.v1);
  3932. native_to_big_inplace(data.v2);
  3933. write(data);</code></pre>
  3934. </div>
  3935. </div>
  3936. <div class="paragraph">
  3937. <p>The programmer didn&#8217;t bother to convert <code>data.v3</code> to native endianness because
  3938. that member isn&#8217;t used. A later maintainer needs to pass <code>data.v3</code> to the
  3939. third-party function, so adds <code>third_party::func(data.v3);</code> somewhere deep in
  3940. the code. This causes a silent failure because the usual invariant that an
  3941. object of type <code>int32_t</code> holds a value as described by the C&#43;&#43; core language
  3942. does not apply.</p>
  3943. </div>
  3944. <div class="paragraph">
  3945. <p><strong>Endian buffer and arithmetic types</strong> hold values internally as arrays of
  3946. characters with an invariant that the endianness of the array never changes.
  3947. That makes these types easier to use and programs easier to maintain.</p>
  3948. </div>
  3949. <div class="paragraph">
  3950. <p>Here is the same example, using an endian arithmetic type:</p>
  3951. </div>
  3952. <div class="listingblock">
  3953. <div class="content">
  3954. <pre class="highlight"><code>struct data_t
  3955. {
  3956. big_int32_t v1; // description ...
  3957. big_int32_t v2; // description ...
  3958. ... additional character data members (i.e. non-endian)
  3959. big_int32_t v3; // description ...
  3960. };
  3961. data_t data;
  3962. read(data);
  3963. ...
  3964. ++v1;
  3965. third_party::func(data.v2);
  3966. ...
  3967. write(data);</code></pre>
  3968. </div>
  3969. </div>
  3970. <div class="paragraph">
  3971. <p>A later maintainer can add <code>third_party::func(data.v3)</code> and it will just-work.</p>
  3972. </div>
  3973. </div>
  3974. <div class="sect4">
  3975. <h5 id="choosing_conversion_explicitness">Conversion explicitness</h5>
  3976. <div class="paragraph">
  3977. <p><strong>Endian conversion functions</strong> and <strong>buffer types</strong> never perform implicit
  3978. conversions. This gives users explicit control of when conversion occurs, and
  3979. may help avoid unnecessary conversions.</p>
  3980. </div>
  3981. <div class="paragraph">
  3982. <p><strong>Endian arithmetic types</strong> perform conversion implicitly. That makes these types
  3983. very easy to use, but can result in unnecessary conversions. Failure to hoist
  3984. conversions out of inner loops can bring a performance penalty.</p>
  3985. </div>
  3986. </div>
  3987. <div class="sect4">
  3988. <h5 id="choosing_arithmetic_operations">Arithmetic operations</h5>
  3989. <div class="paragraph">
  3990. <p><strong>Endian conversion functions</strong> do not supply arithmetic operations, but this is
  3991. not a concern since this approach uses ordinary C&#43;&#43; arithmetic types to hold
  3992. values.</p>
  3993. </div>
  3994. <div class="paragraph">
  3995. <p><strong>Endian buffer types</strong> do not supply arithmetic operations. Although this
  3996. approach avoids unnecessary conversions, it can result in the introduction of
  3997. additional variables and confuse maintenance programmers.</p>
  3998. </div>
  3999. <div class="paragraph">
  4000. <p><strong>Endian arithmetic types</strong> do supply arithmetic operations. They are very easy to
  4001. use if lots of arithmetic is involved.</p>
  4002. </div>
  4003. </div>
  4004. </div>
  4005. <div class="sect3">
  4006. <h4 id="choosing_sizes">Sizes</h4>
  4007. <div class="paragraph">
  4008. <p><strong>Endianness conversion functions</strong> only support 1, 2, 4, and 8 byte integers.
  4009. That&#8217;s sufficient for many applications.</p>
  4010. </div>
  4011. <div class="paragraph">
  4012. <p><strong>Endian buffer and arithmetic types</strong> support 1, 2, 3, 4, 5, 6, 7, and 8 byte
  4013. integers. For an application where memory use or I/O speed is the limiting
  4014. factor, using sizes tailored to application needs can be useful.</p>
  4015. </div>
  4016. <div class="sect4">
  4017. <h5 id="choosing_alignments">Alignments</h5>
  4018. <div class="paragraph">
  4019. <p><strong>Endianness conversion functions</strong> only support aligned integer and
  4020. floating-point types. That&#8217;s sufficient for most applications.</p>
  4021. </div>
  4022. <div class="paragraph">
  4023. <p><strong>Endian buffer and arithmetic types</strong> support both aligned and unaligned
  4024. integer and floating-point types. Unaligned types are rarely needed, but when
  4025. needed they are often very useful and workarounds are painful. For example:</p>
  4026. </div>
  4027. <div class="paragraph">
  4028. <p>Non-portable code like this:</p>
  4029. </div>
  4030. <div class="listingblock">
  4031. <div class="content">
  4032. <pre class="highlight"><code>struct S {
  4033. uint16_t a; // big endian
  4034. uint32_t b; // big endian
  4035. } __attribute__ ((packed));</code></pre>
  4036. </div>
  4037. </div>
  4038. <div class="paragraph">
  4039. <p>Can be replaced with portable code like this:</p>
  4040. </div>
  4041. <div class="listingblock">
  4042. <div class="content">
  4043. <pre class="highlight"><code>struct S {
  4044. big_uint16_ut a;
  4045. big_uint32_ut b;
  4046. };</code></pre>
  4047. </div>
  4048. </div>
  4049. </div>
  4050. </div>
  4051. <div class="sect3">
  4052. <h4 id="choosing_design_patterns">Design patterns</h4>
  4053. <div class="paragraph">
  4054. <p>Applications often traffic in endian data as records or packets containing
  4055. multiple endian data elements. For simplicity, we will just call them records.</p>
  4056. </div>
  4057. <div class="paragraph">
  4058. <p>If desired endianness differs from native endianness, a conversion has to be
  4059. performed. When should that conversion occur? Three design patterns have
  4060. evolved.</p>
  4061. </div>
  4062. <div class="sect4">
  4063. <h5 id="choosing_convert_only_as_needed_i_e_lazy">Convert only as needed (i.e. lazy)</h5>
  4064. <div class="paragraph">
  4065. <p>This pattern defers conversion to the point in the code where the data
  4066. element is actually used.</p>
  4067. </div>
  4068. <div class="paragraph">
  4069. <p>This pattern is appropriate when which endian element is actually used varies
  4070. greatly according to record content or other circumstances</p>
  4071. </div>
  4072. </div>
  4073. <div class="sect4">
  4074. <h5 id="choosing_anticipating_need">Convert in anticipation of need</h5>
  4075. <div class="paragraph">
  4076. <p>This pattern performs conversion to native endianness in anticipation of use,
  4077. such as immediately after reading records. If needed, conversion to the output
  4078. endianness is performed after all possible needs have passed, such as just
  4079. before writing records.</p>
  4080. </div>
  4081. <div class="paragraph">
  4082. <p>One implementation of this pattern is to create a proxy record with endianness
  4083. converted to native in a read function, and expose only that proxy to the rest
  4084. of the implementation. If a write function, if needed, handles the conversion
  4085. from native to the desired output endianness.</p>
  4086. </div>
  4087. <div class="paragraph">
  4088. <p>This pattern is appropriate when all endian elements in a record are typically
  4089. used regardless of record content or other circumstances.</p>
  4090. </div>
  4091. </div>
  4092. <div class="sect4">
  4093. <h5 id="choosing_convert_only_as_needed_except_locally_in_anticipation_of_need">Convert only as needed, except locally in anticipation of need</h5>
  4094. <div class="paragraph">
  4095. <p>This pattern in general defers conversion but for specific local needs does
  4096. anticipatory conversion. Although particularly appropriate when coupled with the
  4097. endian buffer or arithmetic types, it also works well with the conversion
  4098. functions.</p>
  4099. </div>
  4100. <div class="paragraph">
  4101. <p>Example:</p>
  4102. </div>
  4103. <div class="listingblock">
  4104. <div class="content">
  4105. <pre class="highlight"><code>struct data_t
  4106. {
  4107. big_int32_t v1;
  4108. big_int32_t v2;
  4109. big_int32_t v3;
  4110. };
  4111. data_t data;
  4112. read(data);
  4113. ...
  4114. ++v1;
  4115. ...
  4116. int32_t v3_temp = data.v3; // hoist conversion out of loop
  4117. for (int32_t i = 0; i &lt; <code>large-number</code>; ++i)
  4118. {
  4119. ... <code>lengthy computation that accesses v3_temp</code> ...
  4120. }
  4121. data.v3 = v3_temp;
  4122. write(data);</code></pre>
  4123. </div>
  4124. </div>
  4125. <div class="paragraph">
  4126. <p>In general the above pseudo-code leaves conversion up to the endian arithmetic
  4127. type <code>big_int32_t</code>. But to avoid conversion inside the loop, a temporary is
  4128. created before the loop is entered, and then used to set the new value of
  4129. <code>data.v3</code> after the loop is complete.</p>
  4130. </div>
  4131. <div class="paragraph">
  4132. <p>Question: Won&#8217;t the compiler&#8217;s optimizer hoist the conversion out of the loop
  4133. anyhow?</p>
  4134. </div>
  4135. <div class="paragraph">
  4136. <p>Answer: VC&#43;&#43; 2015 Preview, and probably others, does not, even for a toy test
  4137. program. Although the savings is small (two register <code>bswap</code> instructions), the
  4138. cost might be significant if the loop is repeated enough times. On the other
  4139. hand, the program may be so dominated by I/O time that even a lengthy loop will
  4140. be immaterial.</p>
  4141. </div>
  4142. </div>
  4143. </div>
  4144. <div class="sect3">
  4145. <h4 id="choosing_use_case_examples">Use case examples</h4>
  4146. <div class="sect4">
  4147. <h5 id="choosing_porting_endian_unaware_codebase">Porting endian unaware codebase</h5>
  4148. <div class="paragraph">
  4149. <p>An existing codebase runs on big endian systems. It does not currently deal
  4150. with endianness. The codebase needs to be modified so it can run on little
  4151. endian systems under various operating systems. To ease transition and protect
  4152. value of existing files, external data will continue to be maintained as big
  4153. endian.</p>
  4154. </div>
  4155. <div class="paragraph">
  4156. <p>The <a href="#arithmetic">endian arithmetic approach</a> is recommended to meet these
  4157. needs. A relatively small number of header files dealing with binary I/O layouts
  4158. need to change types. For example, <code>short</code> or <code>int16_t</code> would change to
  4159. <code>big_int16_t</code>. No changes are required for <code>.cpp</code> files.</p>
  4160. </div>
  4161. </div>
  4162. <div class="sect4">
  4163. <h5 id="choosing_porting_endian_aware_codebase">Porting endian aware codebase</h5>
  4164. <div class="paragraph">
  4165. <p>An existing codebase runs on little-endian Linux systems. It already deals with
  4166. endianness via
  4167. <a href="http://man7.org/linux/man-pages/man3/endian.3.html">Linux provided functions</a>.
  4168. Because of a business merger, the codebase has to be quickly modified for
  4169. Windows and possibly other operating systems, while still supporting Linux. The
  4170. codebase is reliable and the programmers are all well-aware of endian issues.</p>
  4171. </div>
  4172. <div class="paragraph">
  4173. <p>These factors all argue for an <a href="#conversion">endian conversion approach</a> that
  4174. just mechanically changes the calls to <code>htobe32</code>, etc. to
  4175. <code>boost::endian::native_to_big</code>, etc. and replaces <code>&lt;endian.h&gt;</code> with
  4176. <code>&lt;boost/endian/conversion.hpp&gt;</code>.</p>
  4177. </div>
  4178. </div>
  4179. <div class="sect4">
  4180. <h5 id="choosing_reliability_and_arithmetic_speed">Reliability and arithmetic-speed</h5>
  4181. <div class="paragraph">
  4182. <p>A new, complex, multi-threaded application is to be developed that must run
  4183. on little endian machines, but do big endian network I/O. The developers believe
  4184. computational speed for endian variable is critical but have seen numerous bugs
  4185. result from inability to reason about endian conversion state. They are also
  4186. worried that future maintenance changes could inadvertently introduce a lot of
  4187. slow conversions if full-blown endian arithmetic types are used.</p>
  4188. </div>
  4189. <div class="paragraph">
  4190. <p>The <a href="#buffers">endian buffers</a> approach is made-to-order for this use case.</p>
  4191. </div>
  4192. </div>
  4193. <div class="sect4">
  4194. <h5 id="choosing_reliability_and_ease_of_use">Reliability and ease-of-use</h5>
  4195. <div class="paragraph">
  4196. <p>A new, complex, multi-threaded application is to be developed that must run on
  4197. little endian machines, but do big endian network I/O. The developers believe
  4198. computational speed for endian variables is <strong>not critical</strong> but have seen
  4199. numerous bugs result from inability to reason about endian conversion state.
  4200. They are also concerned about ease-of-use both during development and long-term
  4201. maintenance.</p>
  4202. </div>
  4203. <div class="paragraph">
  4204. <p>Removing concern about conversion speed and adding concern about ease-of-use
  4205. tips the balance strongly in favor the
  4206. <a href="#arithmetic">endian arithmetic approach</a>.</p>
  4207. </div>
  4208. </div>
  4209. </div>
  4210. </div>
  4211. </div>
  4212. </div>
  4213. <div class="sect1">
  4214. <h2 id="appendix_mini_review_topics">Appendix A: Endian Mini-Review</h2>
  4215. <div class="sectionbody">
  4216. <div class="paragraph">
  4217. <p>The results of the Boost.Endian formal review included a list of issues to be
  4218. resolved before a mini-review.</p>
  4219. </div>
  4220. <div class="paragraph">
  4221. <p>The issues are shown in <strong>bold</strong> below, with the resolution indicated.</p>
  4222. </div>
  4223. <div class="dlist">
  4224. <dl>
  4225. <dt class="hdlist1">Common use case scenarios should be developed.</dt>
  4226. <dd>
  4227. <p>Done. The documentation have been refactored. A page is now devoted to
  4228. <a href="#choosing">Choosing the Approach</a> to endianness. See
  4229. <a href="#choosing_use_cases">Use cases</a> for use case scenarios.</p>
  4230. </dd>
  4231. <dt class="hdlist1">Example programs should be developed for the common use case scenarios.</dt>
  4232. <dd>
  4233. <p>Done. See <a href="#choosing">Choosing the Approach</a>. Example code has been added
  4234. throughout.</p>
  4235. </dd>
  4236. <dt class="hdlist1">Documentation should illuminate the differences between endian integer/float type and endian conversion approaches to the common use case scenarios, and provide guidelines for choosing the most appropriate approach in user&#8217;s applications.</dt>
  4237. <dd>
  4238. <p>Done. See <a href="#choosing">Choosing the Approach</a>.</p>
  4239. </dd>
  4240. <dt class="hdlist1">Conversion functions supplying results via return should be provided.</dt>
  4241. <dd>
  4242. <p>Done. See <a href="#conversion">Conversion Functions</a>.</p>
  4243. </dd>
  4244. <dt class="hdlist1">Platform specific performance enhancements such as use of compiler intrinsics or relaxed alignment requirements should be supported.</dt>
  4245. <dd>
  4246. <p>Done. Compiler (Clang, GCC, VisualC&#43;&#43;, etc.) intrinsics and built-in
  4247. functions are used in the implementation where appropriate, as requested. See
  4248. <a href="#overview_intrinsic">Built-in support for Intrinsics</a>. See
  4249. <a href="#overview_timings">Timings for Example 2</a> to gauge the impact of intrinsics.</p>
  4250. </dd>
  4251. <dt class="hdlist1">Endian integer (and floating) types should be implemented via the conversion functions. If that can&#8217;t be done efficiently, consideration should be given to expanding the conversion function signatures to resolve the inefficiencies.</dt>
  4252. <dd>
  4253. <p>Done. For the endian types, the implementation uses the endian conversion
  4254. functions, and thus the intrinsics, as requested.</p>
  4255. </dd>
  4256. <dt class="hdlist1">Benchmarks that measure performance should be provided. It should be possible to compare platform specific performance enhancements against portable base implementations, and to compare endian integer approaches against endian conversion approaches for the common use case scenarios.</dt>
  4257. <dd>
  4258. <p>Done. See <a href="#overview_timings">Timings for Example 2</a>. The <code>endian/test</code>
  4259. directory also contains several additional benchmark and speed test programs.</p>
  4260. </dd>
  4261. <dt class="hdlist1">Float (32-bits) and double (64-bits) should be supported. IEEE 754 is the primary use case.</dt>
  4262. <dd>
  4263. <p>Done. The <a href="#buffers">endian buffer types</a>,
  4264. <a href="#arithmetic">endian arithmetic types</a> and
  4265. <a href="#conversion">endian conversion functions</a> now support 32-bit <code>(float)</code>
  4266. and 64-bit <code>(double)</code> floating point, as requested.</p>
  4267. </dd>
  4268. <dt class="hdlist1">Support for user defined types (UDTs) is desirable, and should be provided where there would be no conflict with the other concerns.</dt>
  4269. <dd>
  4270. <p>Done. See <a href="#conversion_customization">Customization points for user-defined
  4271. types (UDTs)</a>.</p>
  4272. </dd>
  4273. <dt class="hdlist1">There is some concern that endian integer/float arithmetic operations might used inadvertently or inappropriately. The impact of adding an endian_buffer class without arithmetic operations should be investigated.</dt>
  4274. <dd>
  4275. <p>Done. The endian types have been decomposed into class template
  4276. <code><a href="#buffers">endian_buffer</a></code> and class template
  4277. <code><a href="#arithmetic">endian_arithmetic</a></code>. Class <code>endian_buffer</code> is a public base
  4278. class for <code>endian_arithmetic</code>, and can also be used by users as a stand-alone
  4279. class.</p>
  4280. </dd>
  4281. <dt class="hdlist1">Stream insertion and extraction of the endian integer/float types should be documented and included in the test coverage.</dt>
  4282. <dd>
  4283. <p>Done. See <a href="#buffers_stream_inserter">Stream inserter</a> and
  4284. <a href="#buffers_stream_extractor">Stream extractor</a>.</p>
  4285. </dd>
  4286. <dt class="hdlist1">Binary I/O support that was investigated during development of the Endian library should be put up for mini-review for inclusion in the Boost I/O library.</dt>
  4287. <dd>
  4288. <p>Not done yet. Will be handled as a separate min-review soon after the Endian
  4289. mini-review.</p>
  4290. </dd>
  4291. <dt class="hdlist1">Other requested changes.</dt>
  4292. <dd>
  4293. <p> In addition to the named-endianness conversion functions, functions that
  4294. perform compile-time (via template) and run-time (via function argument)
  4295. dispatch are now provided.
  4296. <code>order*native</code> is now a synonym for <code>order*big</code> or <code>order*little</code> according
  4297. to the endianness of the platform. This reduces the number of template
  4298. specializations required.
  4299. Headers have been reorganized to make them easier to read, with a synopsis
  4300. at the front and implementation following.</p>
  4301. </dd>
  4302. </dl>
  4303. </div>
  4304. </div>
  4305. </div>
  4306. <div class="sect1">
  4307. <h2 id="choosing_copyright_and_license">Appendix B: Copyright and License</h2>
  4308. <div class="sectionbody">
  4309. <div class="paragraph">
  4310. <p>This documentation is</p>
  4311. </div>
  4312. <div class="ulist">
  4313. <ul>
  4314. <li>
  4315. <p>Copyright 2011-2016 Beman Dawes</p>
  4316. </li>
  4317. <li>
  4318. <p>Copyright 2019 Peter Dimov</p>
  4319. </li>
  4320. </ul>
  4321. </div>
  4322. <div class="paragraph">
  4323. <p>and is distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</p>
  4324. </div>
  4325. </div>
  4326. </div>
  4327. </div>
  4328. <div id="footer">
  4329. <div id="footer-text">
  4330. Last updated 2019-12-10 00:18:50 UTC
  4331. </div>
  4332. </div>
  4333. <style>
  4334. *:not(pre)>code { background: none; color: #600000; }
  4335. :not(pre):not([class^=L])>code { background: none; color: #600000; }
  4336. table tr.even, table tr.alt, table tr:nth-of-type(even) { background: none; }
  4337. </style>
  4338. </body>
  4339. </html>