xhalf_float_impl.hpp 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036
  1. // half - IEEE 754-based half-precision floating-point library.
  2. //
  3. // Copyright (c) 2012-2019 Christian Rau <rauy@users.sourceforge.net>
  4. // Copyright (c) 2020 0xBYTESHIFT
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
  7. // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  8. // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14. // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  16. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. /// \file
  18. /// Main header file for half-precision functionality.
  19. #pragma once
  20. #define HALF_TWOS_COMPLEMENT_INT 1
  21. // any error throwing C++ exceptions?
  22. #if defined(HALF_ERRHANDLING_THROW_INVALID) || defined(HALF_ERRHANDLING_THROW_DIVBYZERO) || defined(HALF_ERRHANDLING_THROW_OVERFLOW) || defined(HALF_ERRHANDLING_THROW_UNDERFLOW) || defined(HALF_ERRHANDLING_THROW_INEXACT)
  23. #define HALF_ERRHANDLING_THROWS 1
  24. #endif
  25. // any error handling enabled?
  26. #define HALF_ERRHANDLING (HALF_ERRHANDLING_FLAGS||HALF_ERRHANDLING_ERRNO||HALF_ERRHANDLING_FENV||HALF_ERRHANDLING_THROWS)
  27. #if HALF_ERRHANDLING
  28. #define HALF_UNUSED_NOERR(name) name
  29. #else
  30. #define HALF_UNUSED_NOERR(name)
  31. #endif
  32. // support constexpr
  33. #if HALF_ERRHANDLING
  34. #define constexpr_NOERR
  35. #else
  36. #define constexpr_NOERR constexpr
  37. #endif
  38. #include <utility>
  39. #include <algorithm>
  40. #include <istream>
  41. #include <ostream>
  42. #include <limits>
  43. #include <stdexcept>
  44. #include <climits>
  45. #include <cmath>
  46. #include <cstring>
  47. #include <cstdlib>
  48. #include <type_traits>
  49. #include <cstdint>
  50. #if HALF_ERRHANDLING_ERRNO
  51. #include <cerrno>
  52. #endif
  53. #include <cfenv>
  54. #include <functional>
  55. #ifndef HALF_ENABLE_F16C_INTRINSICS
  56. /// Enable F16C intruction set intrinsics.
  57. /// Defining this to 1 enables the use of [F16C compiler intrinsics](https://en.wikipedia.org/wiki/F16C) for converting between
  58. /// half-precision and single-precision values which may result in improved performance. This will not perform additional checks
  59. /// for support of the F16C instruction set, so an appropriate target platform is required when enabling this feature.
  60. ///
  61. /// Unless predefined it will be enabled automatically when the `__F16C__` symbol is defined, which some compilers do on supporting platforms.
  62. #define HALF_ENABLE_F16C_INTRINSICS __F16C__
  63. #endif
  64. #if HALF_ENABLE_F16C_INTRINSICS
  65. #include <immintrin.h>
  66. #endif
  67. #ifndef HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
  68. /// Raise INEXACT exception on overflow.
  69. /// Defining this to 1 (default) causes overflow errors to automatically raise inexact exceptions in addition.
  70. /// These will be raised after any possible handling of the underflow exception.
  71. #define HALF_ERRHANDLING_OVERFLOW_TO_INEXACT 1
  72. #endif
  73. #ifndef HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
  74. /// Raise INEXACT exception on underflow.
  75. /// Defining this to 1 (default) causes underflow errors to automatically raise inexact exceptions in addition.
  76. /// These will be raised after any possible handling of the underflow exception.
  77. ///
  78. /// **Note:** This will actually cause underflow (and the accompanying inexact) exceptions to be raised *only* when the result
  79. /// is inexact, while if disabled bare underflow errors will be raised for *any* (possibly exact) subnormal result.
  80. #define HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT 1
  81. #endif
  82. /// Default rounding mode.
  83. /// This specifies the rounding mode used for all conversions between [half](\ref half_float::half)s and more precise types
  84. /// (unless using half_cast() and specifying the rounding mode directly) as well as in arithmetic operations and mathematical
  85. /// functions. It can be redefined (before including half.hpp) to one of the standard rounding modes using their respective
  86. /// constants or the equivalent values of
  87. /// [std::float_round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/float_round_style):
  88. ///
  89. /// `std::float_round_style` | value | rounding
  90. /// ---------------------------------|-------|-------------------------
  91. /// `std::round_indeterminate` | -1 | fastest
  92. /// `std::round_toward_zero` | 0 | toward zero
  93. /// `std::round_to_nearest` | 1 | to nearest (default)
  94. /// `std::round_toward_infinity` | 2 | toward positive infinity
  95. /// `std::round_toward_neg_infinity` | 3 | toward negative infinity
  96. ///
  97. /// By default this is set to `1` (`std::round_to_nearest`), which rounds results to the nearest representable value. It can even
  98. /// be set to [std::numeric_limits<float>::round_style](https://en.cppreference.com/w/cpp/types/numeric_limits/round_style) to synchronize
  99. /// the rounding mode with that of the built-in single-precision implementation (which is likely `std::round_to_nearest`, though).
  100. #ifndef HALF_ROUND_STYLE
  101. #define HALF_ROUND_STYLE 1 // = std::round_to_nearest
  102. #endif
  103. /// Value signaling overflow.
  104. /// In correspondence with `HUGE_VAL[F|L]` from `<cmath>` this symbol expands to a positive value signaling the overflow of an
  105. /// operation, in particular it just evaluates to positive infinity.
  106. ///
  107. /// **See also:** Documentation for [HUGE_VAL](https://en.cppreference.com/w/cpp/numeric/math/HUGE_VAL)
  108. #define HUGE_VALH std::numeric_limits<half_float::half>::infinity()
  109. /// Fast half-precision fma function.
  110. /// This symbol is defined if the fma() function generally executes as fast as, or faster than, a separate
  111. /// half-precision multiplication followed by an addition, which is always the case.
  112. ///
  113. /// **See also:** Documentation for [FP_FAST_FMA](https://en.cppreference.com/w/cpp/numeric/math/fma)
  114. #define FP_FAST_FMAH 1
  115. /// Half rounding mode.
  116. /// In correspondence with `FLT_ROUNDS` from `<cfloat>` this symbol expands to the rounding mode used for
  117. /// half-precision operations. It is an alias for [HALF_ROUND_STYLE](\ref HALF_ROUND_STYLE).
  118. ///
  119. /// **See also:** Documentation for [FLT_ROUNDS](https://en.cppreference.com/w/cpp/types/climits/FLT_ROUNDS)
  120. #define HLF_ROUNDS HALF_ROUND_STYLE
  121. #ifndef FP_ILOGB0
  122. #define FP_ILOGB0 INT_MIN
  123. #endif
  124. #ifndef FP_ILOGBNAN
  125. #define FP_ILOGBNAN INT_MAX
  126. #endif
  127. #ifndef FP_SUBNORMAL
  128. #define FP_SUBNORMAL 0
  129. #endif
  130. #ifndef FP_ZERO
  131. #define FP_ZERO 1
  132. #endif
  133. #ifndef FP_NAN
  134. #define FP_NAN 2
  135. #endif
  136. #ifndef FP_INFINITE
  137. #define FP_INFINITE 3
  138. #endif
  139. #ifndef FP_NORMAL
  140. #define FP_NORMAL 4
  141. #endif
  142. #if !defined(FE_ALL_EXCEPT)
  143. #define FE_INVALID 0x10
  144. #define FE_DIVBYZERO 0x08
  145. #define FE_OVERFLOW 0x04
  146. #define FE_UNDERFLOW 0x02
  147. #define FE_INEXACT 0x01
  148. #define FE_ALL_EXCEPT (FE_INVALID|FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW|FE_INEXACT)
  149. #endif
  150. /// Main namespace for half-precision functionality.
  151. /// This namespace contains all the functionality provided by the library.
  152. namespace half_float {
  153. class half;
  154. /// Library-defined half-precision literals.
  155. /// Import this namespace to enable half-precision floating-point literals:
  156. /// ~~~~{.cpp}
  157. /// using namespace half_float::literal;
  158. /// half_float::half = 4.2_h;
  159. /// ~~~~
  160. namespace literal {
  161. half operator "" _h(long double);
  162. }
  163. /// \internal
  164. /// \brief Implementation details.
  165. namespace detail {
  166. /// Conditional type.
  167. template<bool B, class T, class F> struct conditional : std::conditional<B,T,F> {};
  168. /// Helper for tag dispatching.
  169. template<bool B> struct bool_type : std::integral_constant<bool,B> {};
  170. using std::true_type;
  171. using std::false_type;
  172. /// Type traits for floating-point types.
  173. template<class T> struct is_float : std::is_floating_point<T> {};
  174. /// Type traits for floating-point bits.
  175. template<class T> struct bits { using type = unsigned char; };
  176. template<class T> struct bits<const T> : bits<T> {};
  177. template<class T> struct bits<volatile T> : bits<T> {};
  178. template<class T> struct bits<const volatile T> : bits<T> {};
  179. /// Unsigned integer of (at least) 16 bits width.
  180. using uint16 = std::uint_least16_t;
  181. /// Fastest unsigned integer of (at least) 32 bits width.
  182. using uint32 = std::uint_fast32_t;
  183. /// Fastest signed integer of (at least) 32 bits width.
  184. using int32 = std::int_fast32_t;
  185. /// Unsigned integer of (at least) 32 bits width.
  186. template<> struct bits<float> { using type = std::uint_least32_t; };
  187. /// Unsigned integer of (at least) 64 bits width.
  188. template<> struct bits<double> { using type = std::uint_least64_t; };
  189. template<class T> using bits_t = typename bits<T>::type;
  190. #ifdef HALF_ARITHMETIC_TYPE
  191. /// Type to use for arithmetic computations and mathematic functions internally.
  192. typedef HALF_ARITHMETIC_TYPE internal_t;
  193. #endif
  194. /// Tag type for binary construction.
  195. struct binary_t {};
  196. /// Tag for binary construction.
  197. constexpr binary_t binary = binary_t();
  198. /// \name Implementation defined classification and arithmetic
  199. /// \{
  200. /// Check for infinity.
  201. /// \tparam T argument type (builtin floating-point type)
  202. /// \param arg value to query
  203. /// \retval true if infinity
  204. /// \retval false else
  205. template<class T> bool builtin_isinf(T arg) { return std::isinf(arg); }
  206. /// Check for NaN.
  207. /// \tparam T argument type (builtin floating-point type)
  208. /// \param arg value to query
  209. /// \retval true if not a number
  210. /// \retval false else
  211. template<class T> bool builtin_isnan(T arg) { return std::isnan(arg); }
  212. /// Check sign.
  213. /// \tparam T argument type (builtin floating-point type)
  214. /// \param arg value to query
  215. /// \retval true if signbit set
  216. /// \retval false else
  217. template<class T> bool builtin_signbit(T arg) { return std::signbit(arg); }
  218. /// Platform-independent sign mask.
  219. /// \param arg integer value in two's complement
  220. /// \retval -1 if \a arg negative
  221. /// \retval 0 if \a arg positive
  222. inline uint32 sign_mask(uint32 arg) {
  223. static const int N = std::numeric_limits<uint32>::digits - 1;
  224. #if HALF_TWOS_COMPLEMENT_INT
  225. return static_cast<int32>(arg) >> N;
  226. #else
  227. return -((arg>>N)&1);
  228. #endif
  229. }
  230. /// Platform-independent arithmetic right shift.
  231. /// \param arg integer value in two's complement
  232. /// \param i shift amount (at most 31)
  233. /// \return \a arg right shifted for \a i bits with possible sign extension
  234. inline uint32 arithmetic_shift(uint32 arg, int i) {
  235. #if HALF_TWOS_COMPLEMENT_INT
  236. return static_cast<int32>(arg) >> i;
  237. #else
  238. return static_cast<int32>(arg)/(static_cast<int32>(1)<<i) - ((arg>>(std::numeric_limits<uint32>::digits-1))&1);
  239. #endif
  240. }
  241. /// \}
  242. /// \name Error handling
  243. /// \{
  244. /// Internal exception flags.
  245. /// \return reference to global exception flags
  246. inline int& errflags() { thread_local int flags = 0; return flags; }
  247. /// Raise floating-point exception.
  248. /// \param flags exceptions to raise
  249. /// \param cond condition to raise exceptions for
  250. inline void raise(int HALF_UNUSED_NOERR(flags), bool HALF_UNUSED_NOERR(cond) = true) {
  251. #if HALF_ERRHANDLING
  252. if(!cond)
  253. return;
  254. #if HALF_ERRHANDLING_FLAGS
  255. errflags() |= flags;
  256. #endif
  257. #if HALF_ERRHANDLING_ERRNO
  258. if(flags & FE_INVALID)
  259. errno = EDOM;
  260. else if(flags & (FE_DIVBYZERO|FE_OVERFLOW|FE_UNDERFLOW))
  261. errno = ERANGE;
  262. #endif
  263. #if HALF_ERRHANDLING_FENV
  264. std::feraiseexcept(flags);
  265. #endif
  266. #ifdef HALF_ERRHANDLING_THROW_INVALID
  267. if(flags & FE_INVALID)
  268. throw std::domain_error(HALF_ERRHANDLING_THROW_INVALID);
  269. #endif
  270. #ifdef HALF_ERRHANDLING_THROW_DIVBYZERO
  271. if(flags & FE_DIVBYZERO)
  272. throw std::domain_error(HALF_ERRHANDLING_THROW_DIVBYZERO);
  273. #endif
  274. #ifdef HALF_ERRHANDLING_THROW_OVERFLOW
  275. if(flags & FE_OVERFLOW)
  276. throw std::overflow_error(HALF_ERRHANDLING_THROW_OVERFLOW);
  277. #endif
  278. #ifdef HALF_ERRHANDLING_THROW_UNDERFLOW
  279. if(flags & FE_UNDERFLOW)
  280. throw std::underflow_error(HALF_ERRHANDLING_THROW_UNDERFLOW);
  281. #endif
  282. #ifdef HALF_ERRHANDLING_THROW_INEXACT
  283. if(flags & FE_INEXACT)
  284. throw std::range_error(HALF_ERRHANDLING_THROW_INEXACT);
  285. #endif
  286. #if HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
  287. if((flags & FE_UNDERFLOW) && !(flags & FE_INEXACT))
  288. raise(FE_INEXACT);
  289. #endif
  290. #if HALF_ERRHANDLING_OVERFLOW_TO_INEXACT
  291. if((flags & FE_OVERFLOW) && !(flags & FE_INEXACT))
  292. raise(FE_INEXACT);
  293. #endif
  294. #endif
  295. }
  296. /// Check and signal for any NaN.
  297. /// \param x first half-precision value to check
  298. /// \param y second half-precision value to check
  299. /// \retval true if either \a x or \a y is NaN
  300. /// \retval false else
  301. /// \exception FE_INVALID if \a x or \a y is NaN
  302. inline constexpr_NOERR bool compsignal(unsigned int x, unsigned int y) {
  303. #if HALF_ERRHANDLING
  304. raise(FE_INVALID, (x&0x7FFF)>0x7C00 || (y&0x7FFF)>0x7C00);
  305. #endif
  306. return (x&0x7FFF) > 0x7C00 || (y&0x7FFF) > 0x7C00;
  307. }
  308. /// Signal and silence signaling NaN.
  309. /// \param nan half-precision NaN value
  310. /// \return quiet NaN
  311. /// \exception FE_INVALID if \a nan is signaling NaN
  312. inline constexpr_NOERR unsigned int signal(unsigned int nan) {
  313. #if HALF_ERRHANDLING
  314. raise(FE_INVALID, !(nan&0x200));
  315. #endif
  316. return nan | 0x200;
  317. }
  318. /// Signal and silence signaling NaNs.
  319. /// \param x first half-precision value to check
  320. /// \param y second half-precision value to check
  321. /// \return quiet NaN
  322. /// \exception FE_INVALID if \a x or \a y is signaling NaN
  323. inline constexpr_NOERR unsigned int signal(unsigned int x, unsigned int y) {
  324. #if HALF_ERRHANDLING
  325. raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)));
  326. #endif
  327. return ((x&0x7FFF)>0x7C00) ? (x|0x200) : (y|0x200);
  328. }
  329. /// Signal and silence signaling NaNs.
  330. /// \param x first half-precision value to check
  331. /// \param y second half-precision value to check
  332. /// \param z third half-precision value to check
  333. /// \return quiet NaN
  334. /// \exception FE_INVALID if \a x, \a y or \a z is signaling NaN
  335. inline constexpr_NOERR unsigned int signal(unsigned int x, unsigned int y, unsigned int z) {
  336. #if HALF_ERRHANDLING
  337. raise(FE_INVALID, ((x&0x7FFF)>0x7C00 && !(x&0x200)) || ((y&0x7FFF)>0x7C00 && !(y&0x200)) || ((z&0x7FFF)>0x7C00 && !(z&0x200)));
  338. #endif
  339. return ((x&0x7FFF)>0x7C00) ? (x|0x200) : ((y&0x7FFF)>0x7C00) ? (y|0x200) : (z|0x200);
  340. }
  341. /// Select value or signaling NaN.
  342. /// \param x preferred half-precision value
  343. /// \param y ignored half-precision value except for signaling NaN
  344. /// \return \a y if signaling NaN, \a x otherwise
  345. /// \exception FE_INVALID if \a y is signaling NaN
  346. inline constexpr_NOERR unsigned int select(unsigned int x, unsigned int HALF_UNUSED_NOERR(y)) {
  347. #if HALF_ERRHANDLING
  348. return (((y&0x7FFF)>0x7C00) && !(y&0x200)) ? signal(y) : x;
  349. #else
  350. return x;
  351. #endif
  352. }
  353. /// Raise domain error and return NaN.
  354. /// return quiet NaN
  355. /// \exception FE_INVALID
  356. inline constexpr_NOERR unsigned int invalid() {
  357. #if HALF_ERRHANDLING
  358. raise(FE_INVALID);
  359. #endif
  360. return 0x7FFF;
  361. }
  362. /// Raise pole error and return infinity.
  363. /// \param sign half-precision value with sign bit only
  364. /// \return half-precision infinity with sign of \a sign
  365. /// \exception FE_DIVBYZERO
  366. inline constexpr_NOERR unsigned int pole(unsigned int sign = 0) {
  367. #if HALF_ERRHANDLING
  368. raise(FE_DIVBYZERO);
  369. #endif
  370. return sign | 0x7C00;
  371. }
  372. /// Check value for underflow.
  373. /// \param arg non-zero half-precision value to check
  374. /// \return \a arg
  375. /// \exception FE_UNDERFLOW if arg is subnormal
  376. inline constexpr_NOERR unsigned int check_underflow(unsigned int arg) {
  377. #if HALF_ERRHANDLING && !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT
  378. raise(FE_UNDERFLOW, !(arg&0x7C00));
  379. #endif
  380. return arg;
  381. }
  382. /// \}
  383. /// \name Conversion and rounding
  384. /// \{
  385. /// Half-precision overflow.
  386. /// \tparam R rounding mode to use
  387. /// \param sign half-precision value with sign bit only
  388. /// \return rounded overflowing half-precision value
  389. /// \exception FE_OVERFLOW
  390. template<std::float_round_style R> constexpr_NOERR unsigned int overflow(unsigned int sign = 0) {
  391. #if HALF_ERRHANDLING
  392. raise(FE_OVERFLOW);
  393. #endif
  394. return (R==std::round_toward_infinity) ? (sign+0x7C00-(sign>>15)) :
  395. (R==std::round_toward_neg_infinity) ? (sign+0x7BFF+(sign>>15)) :
  396. (R==std::round_toward_zero) ? (sign|0x7BFF) :
  397. (sign|0x7C00);
  398. }
  399. /// Half-precision underflow.
  400. /// \tparam R rounding mode to use
  401. /// \param sign half-precision value with sign bit only
  402. /// \return rounded underflowing half-precision value
  403. /// \exception FE_UNDERFLOW
  404. template<std::float_round_style R> constexpr_NOERR unsigned int underflow(unsigned int sign = 0) {
  405. #if HALF_ERRHANDLING
  406. raise(FE_UNDERFLOW);
  407. #endif
  408. return (R==std::round_toward_infinity) ? (sign+1-(sign>>15)) :
  409. (R==std::round_toward_neg_infinity) ? (sign+(sign>>15)) :
  410. sign;
  411. }
  412. /// Round half-precision number.
  413. /// \tparam R rounding mode to use
  414. /// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
  415. /// \param value finite half-precision number to round
  416. /// \param g guard bit (most significant discarded bit)
  417. /// \param s sticky bit (or of all but the most significant discarded bits)
  418. /// \return rounded half-precision value
  419. /// \exception FE_OVERFLOW on overflows
  420. /// \exception FE_UNDERFLOW on underflows
  421. /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
  422. template<std::float_round_style R,bool I> constexpr_NOERR unsigned int rounded(unsigned int value, int g, int s) {
  423. #if HALF_ERRHANDLING
  424. value += (R==std::round_to_nearest) ? (g&(s|value)) :
  425. (R==std::round_toward_infinity) ? (~(value>>15)&(g|s)) :
  426. (R==std::round_toward_neg_infinity) ? ((value>>15)&(g|s)) : 0;
  427. if((value&0x7C00) == 0x7C00)
  428. raise(FE_OVERFLOW);
  429. else if(value & 0x7C00)
  430. raise(FE_INEXACT, I || (g|s)!=0);
  431. else
  432. raise(FE_UNDERFLOW, !(HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT) || I || (g|s)!=0);
  433. return value;
  434. #else
  435. return (R==std::round_to_nearest) ? (value+(g&(s|value))) :
  436. (R==std::round_toward_infinity) ? (value+(~(value>>15)&(g|s))) :
  437. (R==std::round_toward_neg_infinity) ? (value+((value>>15)&(g|s))) :
  438. value;
  439. #endif
  440. }
  441. /// Round half-precision number to nearest integer value.
  442. /// \tparam R rounding mode to use
  443. /// \tparam E `true` for round to even, `false` for round away from zero
  444. /// \tparam I `true` to raise INEXACT exception (if inexact), `false` to never raise it
  445. /// \param value half-precision value to round
  446. /// \return half-precision bits for nearest integral value
  447. /// \exception FE_INVALID for signaling NaN
  448. /// \exception FE_INEXACT if value had to be rounded and \a I is `true`
  449. template<std::float_round_style R,bool E,bool I> unsigned int integral(unsigned int value) {
  450. unsigned int abs = value & 0x7FFF;
  451. if(abs < 0x3C00) {
  452. raise(FE_INEXACT, I);
  453. return ((R==std::round_to_nearest) ? (0x3C00&-static_cast<unsigned>(abs>=(0x3800+E))) :
  454. (R==std::round_toward_infinity) ? (0x3C00&-(~(value>>15)&(abs!=0))) :
  455. (R==std::round_toward_neg_infinity) ? (0x3C00&-static_cast<unsigned>(value>0x8000)) :
  456. 0) | (value&0x8000);
  457. }
  458. if(abs >= 0x6400)
  459. return (abs>0x7C00) ? signal(value) : value;
  460. unsigned int exp = 25 - (abs>>10), mask = (1<<exp) - 1;
  461. raise(FE_INEXACT, I && (value&mask));
  462. return (( (R==std::round_to_nearest) ? ((1<<(exp-1))-(~(value>>exp)&E)) :
  463. (R==std::round_toward_infinity) ? (mask&((value>>15)-1)) :
  464. (R==std::round_toward_neg_infinity) ? (mask&-(value>>15)) :
  465. 0) + value) & ~mask;
  466. }
  467. /// Convert fixed point to half-precision floating-point.
  468. /// \tparam R rounding mode to use
  469. /// \tparam F number of fractional bits (at least 11)
  470. /// \tparam S `true` for signed, `false` for unsigned
  471. /// \tparam N `true` for additional normalization step, `false` if already normalized to 1.F
  472. /// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
  473. /// \param m mantissa in Q1.F fixed point format
  474. /// \param exp exponent
  475. /// \param sign half-precision value with sign bit only
  476. /// \param s sticky bit (or of all but the most significant already discarded bits)
  477. /// \return value converted to half-precision
  478. /// \exception FE_OVERFLOW on overflows
  479. /// \exception FE_UNDERFLOW on underflows
  480. /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
  481. template<std::float_round_style R,unsigned int F,bool S,bool N,bool I> unsigned int fixed2half(uint32 m, int exp = 14, unsigned int sign = 0, int s = 0) {
  482. if(S) {
  483. uint32 msign = sign_mask(m);
  484. m = (m^msign) - msign;
  485. sign = msign & 0x8000;
  486. }
  487. if(N)
  488. for(; m<(static_cast<uint32>(1)<<F) && exp; m<<=1,--exp) ;
  489. else if(exp < 0)
  490. return rounded<R,I>(sign+(m>>(F-10-exp)), (m>>(F-11-exp))&1, s|((m&((static_cast<uint32>(1)<<(F-11-exp))-1))!=0));
  491. return rounded<R,I>(sign+(exp<<10)+(m>>(F-10)), (m>>(F-11))&1, s|((m&((static_cast<uint32>(1)<<(F-11))-1))!=0));
  492. }
  493. /// Convert IEEE single-precision to half-precision.
  494. /// Credit for this goes to [Jeroen van der Zijp](ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf).
  495. /// \tparam R rounding mode to use
  496. /// \param value single-precision value to convert
  497. /// \return rounded half-precision value
  498. /// \exception FE_OVERFLOW on overflows
  499. /// \exception FE_UNDERFLOW on underflows
  500. /// \exception FE_INEXACT if value had to be rounded
  501. template<std::float_round_style R> unsigned int float2half_impl(float value, true_type) {
  502. #if HALF_ENABLE_F16C_INTRINSICS
  503. return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_set_ss(value),
  504. (R==std::round_to_nearest) ? _MM_FROUND_TO_NEAREST_INT :
  505. (R==std::round_toward_zero) ? _MM_FROUND_TO_ZERO :
  506. (R==std::round_toward_infinity) ? _MM_FROUND_TO_POS_INF :
  507. (R==std::round_toward_neg_infinity) ? _MM_FROUND_TO_NEG_INF :
  508. _MM_FROUND_CUR_DIRECTION));
  509. #else
  510. bits_t<float> fbits;
  511. std::memcpy(&fbits, &value, sizeof(float));
  512. #if 1
  513. unsigned int sign = (fbits>>16) & 0x8000;
  514. fbits &= 0x7FFFFFFF;
  515. if(fbits >= 0x7F800000)
  516. return sign | 0x7C00 | ((fbits>0x7F800000) ? (0x200|((fbits>>13)&0x3FF)) : 0);
  517. if(fbits >= 0x47800000)
  518. return overflow<R>(sign);
  519. if(fbits >= 0x38800000)
  520. return rounded<R,false>(sign|(((fbits>>23)-112)<<10)|((fbits>>13)&0x3FF), (fbits>>12)&1, (fbits&0xFFF)!=0);
  521. if(fbits >= 0x33000000)
  522. {
  523. int i = 125 - (fbits>>23);
  524. fbits = (fbits&0x7FFFFF) | 0x800000;
  525. return rounded<R,false>(sign|(fbits>>(i+1)), (fbits>>i)&1, (fbits&((static_cast<uint32>(1)<<i)-1))!=0);
  526. }
  527. if(fbits != 0)
  528. return underflow<R>(sign);
  529. return sign;
  530. #else
  531. static const uint16 base_table[512] = {
  532. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  533. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  534. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  535. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  536. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  537. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  538. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
  539. 0x0200, 0x0400, 0x0800, 0x0C00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x2400, 0x2800, 0x2C00, 0x3000, 0x3400, 0x3800, 0x3C00,
  540. 0x4000, 0x4400, 0x4800, 0x4C00, 0x5000, 0x5400, 0x5800, 0x5C00, 0x6000, 0x6400, 0x6800, 0x6C00, 0x7000, 0x7400, 0x7800, 0x7BFF,
  541. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  542. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  543. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  544. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  545. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  546. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF,
  547. 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7BFF, 0x7C00,
  548. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  549. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  550. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  551. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  552. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  553. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000,
  554. 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8001, 0x8002, 0x8004, 0x8008, 0x8010, 0x8020, 0x8040, 0x8080, 0x8100,
  555. 0x8200, 0x8400, 0x8800, 0x8C00, 0x9000, 0x9400, 0x9800, 0x9C00, 0xA000, 0xA400, 0xA800, 0xAC00, 0xB000, 0xB400, 0xB800, 0xBC00,
  556. 0xC000, 0xC400, 0xC800, 0xCC00, 0xD000, 0xD400, 0xD800, 0xDC00, 0xE000, 0xE400, 0xE800, 0xEC00, 0xF000, 0xF400, 0xF800, 0xFBFF,
  557. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  558. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  559. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  560. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  561. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  562. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF,
  563. 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFBFF, 0xFC00 };
  564. static const unsigned char shift_table[256] = {
  565. 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  566. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  567. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  568. 25, 25, 25, 25, 25, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  569. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  570. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  571. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  572. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 13 };
  573. int sexp = fbits >> 23, exp = sexp & 0xFF, i = shift_table[exp];
  574. fbits &= 0x7FFFFF;
  575. uint32 m = (fbits|((exp!=0)<<23)) & -static_cast<uint32>(exp!=0xFF);
  576. return rounded<R,false>(base_table[sexp]+(fbits>>i), (m>>(i-1))&1, (((static_cast<uint32>(1)<<(i-1))-1)&m)!=0);
  577. #endif
  578. #endif
  579. }
  580. /// Convert IEEE double-precision to half-precision.
  581. /// \tparam R rounding mode to use
  582. /// \param value double-precision value to convert
  583. /// \return rounded half-precision value
  584. /// \exception FE_OVERFLOW on overflows
  585. /// \exception FE_UNDERFLOW on underflows
  586. /// \exception FE_INEXACT if value had to be rounded
  587. template<std::float_round_style R> unsigned int float2half_impl(double value, true_type) {
  588. #if HALF_ENABLE_F16C_INTRINSICS
  589. if(R == std::round_indeterminate)
  590. return _mm_cvtsi128_si32(_mm_cvtps_ph(_mm_cvtpd_ps(_mm_set_sd(value)), _MM_FROUND_CUR_DIRECTION));
  591. #endif
  592. bits_t<double> dbits;
  593. std::memcpy(&dbits, &value, sizeof(double));
  594. uint32 hi = dbits >> 32, lo = dbits & 0xFFFFFFFF;
  595. unsigned int sign = (hi>>16) & 0x8000;
  596. hi &= 0x7FFFFFFF;
  597. if(hi >= 0x7FF00000)
  598. return sign | 0x7C00 | ((dbits&0xFFFFFFFFFFFFF) ? (0x200|((hi>>10)&0x3FF)) : 0);
  599. if(hi >= 0x40F00000)
  600. return overflow<R>(sign);
  601. if(hi >= 0x3F100000)
  602. return rounded<R,false>(sign|(((hi>>20)-1008)<<10)|((hi>>10)&0x3FF), (hi>>9)&1, ((hi&0x1FF)|lo)!=0);
  603. if(hi >= 0x3E600000) {
  604. int i = 1018 - (hi>>20);
  605. hi = (hi&0xFFFFF) | 0x100000;
  606. return rounded<R,false>(sign|(hi>>(i+1)), (hi>>i)&1, ((hi&((static_cast<uint32>(1)<<i)-1))|lo)!=0);
  607. }
  608. if((hi|lo) != 0)
  609. return underflow<R>(sign);
  610. return sign;
  611. }
  612. /// Convert non-IEEE floating-point to half-precision.
  613. /// \tparam R rounding mode to use
  614. /// \tparam T source type (builtin floating-point type)
  615. /// \param value floating-point value to convert
  616. /// \return rounded half-precision value
  617. /// \exception FE_OVERFLOW on overflows
  618. /// \exception FE_UNDERFLOW on underflows
  619. /// \exception FE_INEXACT if value had to be rounded
  620. template<std::float_round_style R,class T> unsigned int float2half_impl(T value, ...) {
  621. unsigned int hbits = static_cast<unsigned>(builtin_signbit(value)) << 15;
  622. if(value == T())
  623. return hbits;
  624. if(builtin_isnan(value))
  625. return hbits | 0x7FFF;
  626. if(builtin_isinf(value))
  627. return hbits | 0x7C00;
  628. int exp;
  629. std::frexp(value, &exp);
  630. if(exp > 16)
  631. return overflow<R>(hbits);
  632. if(exp < -13)
  633. value = std::ldexp(value, 25);
  634. else {
  635. value = std::ldexp(value, 12-exp);
  636. hbits |= ((exp+13)<<10);
  637. }
  638. T ival, frac = std::modf(value, &ival);
  639. int m = std::abs(static_cast<int>(ival));
  640. return rounded<R,false>(hbits+(m>>1), m&1, frac!=T());
  641. }
  642. /// Convert floating-point to half-precision.
  643. /// \tparam R rounding mode to use
  644. /// \tparam T source type (builtin floating-point type)
  645. /// \param value floating-point value to convert
  646. /// \return rounded half-precision value
  647. /// \exception FE_OVERFLOW on overflows
  648. /// \exception FE_UNDERFLOW on underflows
  649. /// \exception FE_INEXACT if value had to be rounded
  650. template<std::float_round_style R,class T> unsigned int float2half(T value) {
  651. return float2half_impl<R>(value, bool_type<std::numeric_limits<T>::is_iec559&&sizeof(bits_t<T>)==sizeof(T)>());
  652. }
  653. template<class T> unsigned int float2half(T value) {
  654. return float2half_impl<(std::float_round_style)(HALF_ROUND_STYLE)>(value, bool_type<std::numeric_limits<T>::is_iec559&&sizeof(bits_t<T>)==sizeof(T)>());
  655. }
  656. /// Convert integer to half-precision floating-point.
  657. /// \tparam R rounding mode to use
  658. /// \tparam T type to convert (builtin integer type)
  659. /// \param value integral value to convert
  660. /// \return rounded half-precision value
  661. /// \exception FE_OVERFLOW on overflows
  662. /// \exception FE_INEXACT if value had to be rounded
  663. template<std::float_round_style R,class T> unsigned int int2half(T value) {
  664. unsigned int bits = static_cast<unsigned>(value<0) << 15;
  665. if(!value)
  666. return bits;
  667. if(bits)
  668. value = -value;
  669. if(value > 0xFFFF)
  670. return overflow<R>(bits);
  671. unsigned int m = static_cast<unsigned int>(value), exp = 24;
  672. for(; m<0x400; m<<=1,--exp) ;
  673. for(; m>0x7FF; m>>=1,++exp) ;
  674. bits |= (exp<<10) + m;
  675. return (exp>24) ? rounded<R,false>(bits, (value>>(exp-25))&1, (((1<<(exp-25))-1)&value)!=0) : bits;
  676. }
  677. /// Convert half-precision to IEEE single-precision.
  678. /// Credit for this goes to [Jeroen van der Zijp](ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf).
  679. /// \param value half-precision value to convert
  680. /// \return single-precision value
  681. inline float half2float_impl(unsigned int value, float, true_type) {
  682. #if HALF_ENABLE_F16C_INTRINSICS
  683. return _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(value)));
  684. #else
  685. #if 0
  686. bits_t<float> fbits = static_cast<bits_t<float>>(value&0x8000) << 16;
  687. int abs = value & 0x7FFF;
  688. if(abs)
  689. {
  690. fbits |= 0x38000000 << static_cast<unsigned>(abs>=0x7C00);
  691. for(; abs<0x400; abs<<=1,fbits-=0x800000) ;
  692. fbits += static_cast<bits_t<float>>(abs) << 13;
  693. }
  694. #else
  695. static const bits_t<float> mantissa_table[2048] = {
  696. 0x00000000, 0x33800000, 0x34000000, 0x34400000, 0x34800000, 0x34A00000, 0x34C00000, 0x34E00000, 0x35000000, 0x35100000, 0x35200000, 0x35300000, 0x35400000, 0x35500000, 0x35600000, 0x35700000,
  697. 0x35800000, 0x35880000, 0x35900000, 0x35980000, 0x35A00000, 0x35A80000, 0x35B00000, 0x35B80000, 0x35C00000, 0x35C80000, 0x35D00000, 0x35D80000, 0x35E00000, 0x35E80000, 0x35F00000, 0x35F80000,
  698. 0x36000000, 0x36040000, 0x36080000, 0x360C0000, 0x36100000, 0x36140000, 0x36180000, 0x361C0000, 0x36200000, 0x36240000, 0x36280000, 0x362C0000, 0x36300000, 0x36340000, 0x36380000, 0x363C0000,
  699. 0x36400000, 0x36440000, 0x36480000, 0x364C0000, 0x36500000, 0x36540000, 0x36580000, 0x365C0000, 0x36600000, 0x36640000, 0x36680000, 0x366C0000, 0x36700000, 0x36740000, 0x36780000, 0x367C0000,
  700. 0x36800000, 0x36820000, 0x36840000, 0x36860000, 0x36880000, 0x368A0000, 0x368C0000, 0x368E0000, 0x36900000, 0x36920000, 0x36940000, 0x36960000, 0x36980000, 0x369A0000, 0x369C0000, 0x369E0000,
  701. 0x36A00000, 0x36A20000, 0x36A40000, 0x36A60000, 0x36A80000, 0x36AA0000, 0x36AC0000, 0x36AE0000, 0x36B00000, 0x36B20000, 0x36B40000, 0x36B60000, 0x36B80000, 0x36BA0000, 0x36BC0000, 0x36BE0000,
  702. 0x36C00000, 0x36C20000, 0x36C40000, 0x36C60000, 0x36C80000, 0x36CA0000, 0x36CC0000, 0x36CE0000, 0x36D00000, 0x36D20000, 0x36D40000, 0x36D60000, 0x36D80000, 0x36DA0000, 0x36DC0000, 0x36DE0000,
  703. 0x36E00000, 0x36E20000, 0x36E40000, 0x36E60000, 0x36E80000, 0x36EA0000, 0x36EC0000, 0x36EE0000, 0x36F00000, 0x36F20000, 0x36F40000, 0x36F60000, 0x36F80000, 0x36FA0000, 0x36FC0000, 0x36FE0000,
  704. 0x37000000, 0x37010000, 0x37020000, 0x37030000, 0x37040000, 0x37050000, 0x37060000, 0x37070000, 0x37080000, 0x37090000, 0x370A0000, 0x370B0000, 0x370C0000, 0x370D0000, 0x370E0000, 0x370F0000,
  705. 0x37100000, 0x37110000, 0x37120000, 0x37130000, 0x37140000, 0x37150000, 0x37160000, 0x37170000, 0x37180000, 0x37190000, 0x371A0000, 0x371B0000, 0x371C0000, 0x371D0000, 0x371E0000, 0x371F0000,
  706. 0x37200000, 0x37210000, 0x37220000, 0x37230000, 0x37240000, 0x37250000, 0x37260000, 0x37270000, 0x37280000, 0x37290000, 0x372A0000, 0x372B0000, 0x372C0000, 0x372D0000, 0x372E0000, 0x372F0000,
  707. 0x37300000, 0x37310000, 0x37320000, 0x37330000, 0x37340000, 0x37350000, 0x37360000, 0x37370000, 0x37380000, 0x37390000, 0x373A0000, 0x373B0000, 0x373C0000, 0x373D0000, 0x373E0000, 0x373F0000,
  708. 0x37400000, 0x37410000, 0x37420000, 0x37430000, 0x37440000, 0x37450000, 0x37460000, 0x37470000, 0x37480000, 0x37490000, 0x374A0000, 0x374B0000, 0x374C0000, 0x374D0000, 0x374E0000, 0x374F0000,
  709. 0x37500000, 0x37510000, 0x37520000, 0x37530000, 0x37540000, 0x37550000, 0x37560000, 0x37570000, 0x37580000, 0x37590000, 0x375A0000, 0x375B0000, 0x375C0000, 0x375D0000, 0x375E0000, 0x375F0000,
  710. 0x37600000, 0x37610000, 0x37620000, 0x37630000, 0x37640000, 0x37650000, 0x37660000, 0x37670000, 0x37680000, 0x37690000, 0x376A0000, 0x376B0000, 0x376C0000, 0x376D0000, 0x376E0000, 0x376F0000,
  711. 0x37700000, 0x37710000, 0x37720000, 0x37730000, 0x37740000, 0x37750000, 0x37760000, 0x37770000, 0x37780000, 0x37790000, 0x377A0000, 0x377B0000, 0x377C0000, 0x377D0000, 0x377E0000, 0x377F0000,
  712. 0x37800000, 0x37808000, 0x37810000, 0x37818000, 0x37820000, 0x37828000, 0x37830000, 0x37838000, 0x37840000, 0x37848000, 0x37850000, 0x37858000, 0x37860000, 0x37868000, 0x37870000, 0x37878000,
  713. 0x37880000, 0x37888000, 0x37890000, 0x37898000, 0x378A0000, 0x378A8000, 0x378B0000, 0x378B8000, 0x378C0000, 0x378C8000, 0x378D0000, 0x378D8000, 0x378E0000, 0x378E8000, 0x378F0000, 0x378F8000,
  714. 0x37900000, 0x37908000, 0x37910000, 0x37918000, 0x37920000, 0x37928000, 0x37930000, 0x37938000, 0x37940000, 0x37948000, 0x37950000, 0x37958000, 0x37960000, 0x37968000, 0x37970000, 0x37978000,
  715. 0x37980000, 0x37988000, 0x37990000, 0x37998000, 0x379A0000, 0x379A8000, 0x379B0000, 0x379B8000, 0x379C0000, 0x379C8000, 0x379D0000, 0x379D8000, 0x379E0000, 0x379E8000, 0x379F0000, 0x379F8000,
  716. 0x37A00000, 0x37A08000, 0x37A10000, 0x37A18000, 0x37A20000, 0x37A28000, 0x37A30000, 0x37A38000, 0x37A40000, 0x37A48000, 0x37A50000, 0x37A58000, 0x37A60000, 0x37A68000, 0x37A70000, 0x37A78000,
  717. 0x37A80000, 0x37A88000, 0x37A90000, 0x37A98000, 0x37AA0000, 0x37AA8000, 0x37AB0000, 0x37AB8000, 0x37AC0000, 0x37AC8000, 0x37AD0000, 0x37AD8000, 0x37AE0000, 0x37AE8000, 0x37AF0000, 0x37AF8000,
  718. 0x37B00000, 0x37B08000, 0x37B10000, 0x37B18000, 0x37B20000, 0x37B28000, 0x37B30000, 0x37B38000, 0x37B40000, 0x37B48000, 0x37B50000, 0x37B58000, 0x37B60000, 0x37B68000, 0x37B70000, 0x37B78000,
  719. 0x37B80000, 0x37B88000, 0x37B90000, 0x37B98000, 0x37BA0000, 0x37BA8000, 0x37BB0000, 0x37BB8000, 0x37BC0000, 0x37BC8000, 0x37BD0000, 0x37BD8000, 0x37BE0000, 0x37BE8000, 0x37BF0000, 0x37BF8000,
  720. 0x37C00000, 0x37C08000, 0x37C10000, 0x37C18000, 0x37C20000, 0x37C28000, 0x37C30000, 0x37C38000, 0x37C40000, 0x37C48000, 0x37C50000, 0x37C58000, 0x37C60000, 0x37C68000, 0x37C70000, 0x37C78000,
  721. 0x37C80000, 0x37C88000, 0x37C90000, 0x37C98000, 0x37CA0000, 0x37CA8000, 0x37CB0000, 0x37CB8000, 0x37CC0000, 0x37CC8000, 0x37CD0000, 0x37CD8000, 0x37CE0000, 0x37CE8000, 0x37CF0000, 0x37CF8000,
  722. 0x37D00000, 0x37D08000, 0x37D10000, 0x37D18000, 0x37D20000, 0x37D28000, 0x37D30000, 0x37D38000, 0x37D40000, 0x37D48000, 0x37D50000, 0x37D58000, 0x37D60000, 0x37D68000, 0x37D70000, 0x37D78000,
  723. 0x37D80000, 0x37D88000, 0x37D90000, 0x37D98000, 0x37DA0000, 0x37DA8000, 0x37DB0000, 0x37DB8000, 0x37DC0000, 0x37DC8000, 0x37DD0000, 0x37DD8000, 0x37DE0000, 0x37DE8000, 0x37DF0000, 0x37DF8000,
  724. 0x37E00000, 0x37E08000, 0x37E10000, 0x37E18000, 0x37E20000, 0x37E28000, 0x37E30000, 0x37E38000, 0x37E40000, 0x37E48000, 0x37E50000, 0x37E58000, 0x37E60000, 0x37E68000, 0x37E70000, 0x37E78000,
  725. 0x37E80000, 0x37E88000, 0x37E90000, 0x37E98000, 0x37EA0000, 0x37EA8000, 0x37EB0000, 0x37EB8000, 0x37EC0000, 0x37EC8000, 0x37ED0000, 0x37ED8000, 0x37EE0000, 0x37EE8000, 0x37EF0000, 0x37EF8000,
  726. 0x37F00000, 0x37F08000, 0x37F10000, 0x37F18000, 0x37F20000, 0x37F28000, 0x37F30000, 0x37F38000, 0x37F40000, 0x37F48000, 0x37F50000, 0x37F58000, 0x37F60000, 0x37F68000, 0x37F70000, 0x37F78000,
  727. 0x37F80000, 0x37F88000, 0x37F90000, 0x37F98000, 0x37FA0000, 0x37FA8000, 0x37FB0000, 0x37FB8000, 0x37FC0000, 0x37FC8000, 0x37FD0000, 0x37FD8000, 0x37FE0000, 0x37FE8000, 0x37FF0000, 0x37FF8000,
  728. 0x38000000, 0x38004000, 0x38008000, 0x3800C000, 0x38010000, 0x38014000, 0x38018000, 0x3801C000, 0x38020000, 0x38024000, 0x38028000, 0x3802C000, 0x38030000, 0x38034000, 0x38038000, 0x3803C000,
  729. 0x38040000, 0x38044000, 0x38048000, 0x3804C000, 0x38050000, 0x38054000, 0x38058000, 0x3805C000, 0x38060000, 0x38064000, 0x38068000, 0x3806C000, 0x38070000, 0x38074000, 0x38078000, 0x3807C000,
  730. 0x38080000, 0x38084000, 0x38088000, 0x3808C000, 0x38090000, 0x38094000, 0x38098000, 0x3809C000, 0x380A0000, 0x380A4000, 0x380A8000, 0x380AC000, 0x380B0000, 0x380B4000, 0x380B8000, 0x380BC000,
  731. 0x380C0000, 0x380C4000, 0x380C8000, 0x380CC000, 0x380D0000, 0x380D4000, 0x380D8000, 0x380DC000, 0x380E0000, 0x380E4000, 0x380E8000, 0x380EC000, 0x380F0000, 0x380F4000, 0x380F8000, 0x380FC000,
  732. 0x38100000, 0x38104000, 0x38108000, 0x3810C000, 0x38110000, 0x38114000, 0x38118000, 0x3811C000, 0x38120000, 0x38124000, 0x38128000, 0x3812C000, 0x38130000, 0x38134000, 0x38138000, 0x3813C000,
  733. 0x38140000, 0x38144000, 0x38148000, 0x3814C000, 0x38150000, 0x38154000, 0x38158000, 0x3815C000, 0x38160000, 0x38164000, 0x38168000, 0x3816C000, 0x38170000, 0x38174000, 0x38178000, 0x3817C000,
  734. 0x38180000, 0x38184000, 0x38188000, 0x3818C000, 0x38190000, 0x38194000, 0x38198000, 0x3819C000, 0x381A0000, 0x381A4000, 0x381A8000, 0x381AC000, 0x381B0000, 0x381B4000, 0x381B8000, 0x381BC000,
  735. 0x381C0000, 0x381C4000, 0x381C8000, 0x381CC000, 0x381D0000, 0x381D4000, 0x381D8000, 0x381DC000, 0x381E0000, 0x381E4000, 0x381E8000, 0x381EC000, 0x381F0000, 0x381F4000, 0x381F8000, 0x381FC000,
  736. 0x38200000, 0x38204000, 0x38208000, 0x3820C000, 0x38210000, 0x38214000, 0x38218000, 0x3821C000, 0x38220000, 0x38224000, 0x38228000, 0x3822C000, 0x38230000, 0x38234000, 0x38238000, 0x3823C000,
  737. 0x38240000, 0x38244000, 0x38248000, 0x3824C000, 0x38250000, 0x38254000, 0x38258000, 0x3825C000, 0x38260000, 0x38264000, 0x38268000, 0x3826C000, 0x38270000, 0x38274000, 0x38278000, 0x3827C000,
  738. 0x38280000, 0x38284000, 0x38288000, 0x3828C000, 0x38290000, 0x38294000, 0x38298000, 0x3829C000, 0x382A0000, 0x382A4000, 0x382A8000, 0x382AC000, 0x382B0000, 0x382B4000, 0x382B8000, 0x382BC000,
  739. 0x382C0000, 0x382C4000, 0x382C8000, 0x382CC000, 0x382D0000, 0x382D4000, 0x382D8000, 0x382DC000, 0x382E0000, 0x382E4000, 0x382E8000, 0x382EC000, 0x382F0000, 0x382F4000, 0x382F8000, 0x382FC000,
  740. 0x38300000, 0x38304000, 0x38308000, 0x3830C000, 0x38310000, 0x38314000, 0x38318000, 0x3831C000, 0x38320000, 0x38324000, 0x38328000, 0x3832C000, 0x38330000, 0x38334000, 0x38338000, 0x3833C000,
  741. 0x38340000, 0x38344000, 0x38348000, 0x3834C000, 0x38350000, 0x38354000, 0x38358000, 0x3835C000, 0x38360000, 0x38364000, 0x38368000, 0x3836C000, 0x38370000, 0x38374000, 0x38378000, 0x3837C000,
  742. 0x38380000, 0x38384000, 0x38388000, 0x3838C000, 0x38390000, 0x38394000, 0x38398000, 0x3839C000, 0x383A0000, 0x383A4000, 0x383A8000, 0x383AC000, 0x383B0000, 0x383B4000, 0x383B8000, 0x383BC000,
  743. 0x383C0000, 0x383C4000, 0x383C8000, 0x383CC000, 0x383D0000, 0x383D4000, 0x383D8000, 0x383DC000, 0x383E0000, 0x383E4000, 0x383E8000, 0x383EC000, 0x383F0000, 0x383F4000, 0x383F8000, 0x383FC000,
  744. 0x38400000, 0x38404000, 0x38408000, 0x3840C000, 0x38410000, 0x38414000, 0x38418000, 0x3841C000, 0x38420000, 0x38424000, 0x38428000, 0x3842C000, 0x38430000, 0x38434000, 0x38438000, 0x3843C000,
  745. 0x38440000, 0x38444000, 0x38448000, 0x3844C000, 0x38450000, 0x38454000, 0x38458000, 0x3845C000, 0x38460000, 0x38464000, 0x38468000, 0x3846C000, 0x38470000, 0x38474000, 0x38478000, 0x3847C000,
  746. 0x38480000, 0x38484000, 0x38488000, 0x3848C000, 0x38490000, 0x38494000, 0x38498000, 0x3849C000, 0x384A0000, 0x384A4000, 0x384A8000, 0x384AC000, 0x384B0000, 0x384B4000, 0x384B8000, 0x384BC000,
  747. 0x384C0000, 0x384C4000, 0x384C8000, 0x384CC000, 0x384D0000, 0x384D4000, 0x384D8000, 0x384DC000, 0x384E0000, 0x384E4000, 0x384E8000, 0x384EC000, 0x384F0000, 0x384F4000, 0x384F8000, 0x384FC000,
  748. 0x38500000, 0x38504000, 0x38508000, 0x3850C000, 0x38510000, 0x38514000, 0x38518000, 0x3851C000, 0x38520000, 0x38524000, 0x38528000, 0x3852C000, 0x38530000, 0x38534000, 0x38538000, 0x3853C000,
  749. 0x38540000, 0x38544000, 0x38548000, 0x3854C000, 0x38550000, 0x38554000, 0x38558000, 0x3855C000, 0x38560000, 0x38564000, 0x38568000, 0x3856C000, 0x38570000, 0x38574000, 0x38578000, 0x3857C000,
  750. 0x38580000, 0x38584000, 0x38588000, 0x3858C000, 0x38590000, 0x38594000, 0x38598000, 0x3859C000, 0x385A0000, 0x385A4000, 0x385A8000, 0x385AC000, 0x385B0000, 0x385B4000, 0x385B8000, 0x385BC000,
  751. 0x385C0000, 0x385C4000, 0x385C8000, 0x385CC000, 0x385D0000, 0x385D4000, 0x385D8000, 0x385DC000, 0x385E0000, 0x385E4000, 0x385E8000, 0x385EC000, 0x385F0000, 0x385F4000, 0x385F8000, 0x385FC000,
  752. 0x38600000, 0x38604000, 0x38608000, 0x3860C000, 0x38610000, 0x38614000, 0x38618000, 0x3861C000, 0x38620000, 0x38624000, 0x38628000, 0x3862C000, 0x38630000, 0x38634000, 0x38638000, 0x3863C000,
  753. 0x38640000, 0x38644000, 0x38648000, 0x3864C000, 0x38650000, 0x38654000, 0x38658000, 0x3865C000, 0x38660000, 0x38664000, 0x38668000, 0x3866C000, 0x38670000, 0x38674000, 0x38678000, 0x3867C000,
  754. 0x38680000, 0x38684000, 0x38688000, 0x3868C000, 0x38690000, 0x38694000, 0x38698000, 0x3869C000, 0x386A0000, 0x386A4000, 0x386A8000, 0x386AC000, 0x386B0000, 0x386B4000, 0x386B8000, 0x386BC000,
  755. 0x386C0000, 0x386C4000, 0x386C8000, 0x386CC000, 0x386D0000, 0x386D4000, 0x386D8000, 0x386DC000, 0x386E0000, 0x386E4000, 0x386E8000, 0x386EC000, 0x386F0000, 0x386F4000, 0x386F8000, 0x386FC000,
  756. 0x38700000, 0x38704000, 0x38708000, 0x3870C000, 0x38710000, 0x38714000, 0x38718000, 0x3871C000, 0x38720000, 0x38724000, 0x38728000, 0x3872C000, 0x38730000, 0x38734000, 0x38738000, 0x3873C000,
  757. 0x38740000, 0x38744000, 0x38748000, 0x3874C000, 0x38750000, 0x38754000, 0x38758000, 0x3875C000, 0x38760000, 0x38764000, 0x38768000, 0x3876C000, 0x38770000, 0x38774000, 0x38778000, 0x3877C000,
  758. 0x38780000, 0x38784000, 0x38788000, 0x3878C000, 0x38790000, 0x38794000, 0x38798000, 0x3879C000, 0x387A0000, 0x387A4000, 0x387A8000, 0x387AC000, 0x387B0000, 0x387B4000, 0x387B8000, 0x387BC000,
  759. 0x387C0000, 0x387C4000, 0x387C8000, 0x387CC000, 0x387D0000, 0x387D4000, 0x387D8000, 0x387DC000, 0x387E0000, 0x387E4000, 0x387E8000, 0x387EC000, 0x387F0000, 0x387F4000, 0x387F8000, 0x387FC000,
  760. 0x38000000, 0x38002000, 0x38004000, 0x38006000, 0x38008000, 0x3800A000, 0x3800C000, 0x3800E000, 0x38010000, 0x38012000, 0x38014000, 0x38016000, 0x38018000, 0x3801A000, 0x3801C000, 0x3801E000,
  761. 0x38020000, 0x38022000, 0x38024000, 0x38026000, 0x38028000, 0x3802A000, 0x3802C000, 0x3802E000, 0x38030000, 0x38032000, 0x38034000, 0x38036000, 0x38038000, 0x3803A000, 0x3803C000, 0x3803E000,
  762. 0x38040000, 0x38042000, 0x38044000, 0x38046000, 0x38048000, 0x3804A000, 0x3804C000, 0x3804E000, 0x38050000, 0x38052000, 0x38054000, 0x38056000, 0x38058000, 0x3805A000, 0x3805C000, 0x3805E000,
  763. 0x38060000, 0x38062000, 0x38064000, 0x38066000, 0x38068000, 0x3806A000, 0x3806C000, 0x3806E000, 0x38070000, 0x38072000, 0x38074000, 0x38076000, 0x38078000, 0x3807A000, 0x3807C000, 0x3807E000,
  764. 0x38080000, 0x38082000, 0x38084000, 0x38086000, 0x38088000, 0x3808A000, 0x3808C000, 0x3808E000, 0x38090000, 0x38092000, 0x38094000, 0x38096000, 0x38098000, 0x3809A000, 0x3809C000, 0x3809E000,
  765. 0x380A0000, 0x380A2000, 0x380A4000, 0x380A6000, 0x380A8000, 0x380AA000, 0x380AC000, 0x380AE000, 0x380B0000, 0x380B2000, 0x380B4000, 0x380B6000, 0x380B8000, 0x380BA000, 0x380BC000, 0x380BE000,
  766. 0x380C0000, 0x380C2000, 0x380C4000, 0x380C6000, 0x380C8000, 0x380CA000, 0x380CC000, 0x380CE000, 0x380D0000, 0x380D2000, 0x380D4000, 0x380D6000, 0x380D8000, 0x380DA000, 0x380DC000, 0x380DE000,
  767. 0x380E0000, 0x380E2000, 0x380E4000, 0x380E6000, 0x380E8000, 0x380EA000, 0x380EC000, 0x380EE000, 0x380F0000, 0x380F2000, 0x380F4000, 0x380F6000, 0x380F8000, 0x380FA000, 0x380FC000, 0x380FE000,
  768. 0x38100000, 0x38102000, 0x38104000, 0x38106000, 0x38108000, 0x3810A000, 0x3810C000, 0x3810E000, 0x38110000, 0x38112000, 0x38114000, 0x38116000, 0x38118000, 0x3811A000, 0x3811C000, 0x3811E000,
  769. 0x38120000, 0x38122000, 0x38124000, 0x38126000, 0x38128000, 0x3812A000, 0x3812C000, 0x3812E000, 0x38130000, 0x38132000, 0x38134000, 0x38136000, 0x38138000, 0x3813A000, 0x3813C000, 0x3813E000,
  770. 0x38140000, 0x38142000, 0x38144000, 0x38146000, 0x38148000, 0x3814A000, 0x3814C000, 0x3814E000, 0x38150000, 0x38152000, 0x38154000, 0x38156000, 0x38158000, 0x3815A000, 0x3815C000, 0x3815E000,
  771. 0x38160000, 0x38162000, 0x38164000, 0x38166000, 0x38168000, 0x3816A000, 0x3816C000, 0x3816E000, 0x38170000, 0x38172000, 0x38174000, 0x38176000, 0x38178000, 0x3817A000, 0x3817C000, 0x3817E000,
  772. 0x38180000, 0x38182000, 0x38184000, 0x38186000, 0x38188000, 0x3818A000, 0x3818C000, 0x3818E000, 0x38190000, 0x38192000, 0x38194000, 0x38196000, 0x38198000, 0x3819A000, 0x3819C000, 0x3819E000,
  773. 0x381A0000, 0x381A2000, 0x381A4000, 0x381A6000, 0x381A8000, 0x381AA000, 0x381AC000, 0x381AE000, 0x381B0000, 0x381B2000, 0x381B4000, 0x381B6000, 0x381B8000, 0x381BA000, 0x381BC000, 0x381BE000,
  774. 0x381C0000, 0x381C2000, 0x381C4000, 0x381C6000, 0x381C8000, 0x381CA000, 0x381CC000, 0x381CE000, 0x381D0000, 0x381D2000, 0x381D4000, 0x381D6000, 0x381D8000, 0x381DA000, 0x381DC000, 0x381DE000,
  775. 0x381E0000, 0x381E2000, 0x381E4000, 0x381E6000, 0x381E8000, 0x381EA000, 0x381EC000, 0x381EE000, 0x381F0000, 0x381F2000, 0x381F4000, 0x381F6000, 0x381F8000, 0x381FA000, 0x381FC000, 0x381FE000,
  776. 0x38200000, 0x38202000, 0x38204000, 0x38206000, 0x38208000, 0x3820A000, 0x3820C000, 0x3820E000, 0x38210000, 0x38212000, 0x38214000, 0x38216000, 0x38218000, 0x3821A000, 0x3821C000, 0x3821E000,
  777. 0x38220000, 0x38222000, 0x38224000, 0x38226000, 0x38228000, 0x3822A000, 0x3822C000, 0x3822E000, 0x38230000, 0x38232000, 0x38234000, 0x38236000, 0x38238000, 0x3823A000, 0x3823C000, 0x3823E000,
  778. 0x38240000, 0x38242000, 0x38244000, 0x38246000, 0x38248000, 0x3824A000, 0x3824C000, 0x3824E000, 0x38250000, 0x38252000, 0x38254000, 0x38256000, 0x38258000, 0x3825A000, 0x3825C000, 0x3825E000,
  779. 0x38260000, 0x38262000, 0x38264000, 0x38266000, 0x38268000, 0x3826A000, 0x3826C000, 0x3826E000, 0x38270000, 0x38272000, 0x38274000, 0x38276000, 0x38278000, 0x3827A000, 0x3827C000, 0x3827E000,
  780. 0x38280000, 0x38282000, 0x38284000, 0x38286000, 0x38288000, 0x3828A000, 0x3828C000, 0x3828E000, 0x38290000, 0x38292000, 0x38294000, 0x38296000, 0x38298000, 0x3829A000, 0x3829C000, 0x3829E000,
  781. 0x382A0000, 0x382A2000, 0x382A4000, 0x382A6000, 0x382A8000, 0x382AA000, 0x382AC000, 0x382AE000, 0x382B0000, 0x382B2000, 0x382B4000, 0x382B6000, 0x382B8000, 0x382BA000, 0x382BC000, 0x382BE000,
  782. 0x382C0000, 0x382C2000, 0x382C4000, 0x382C6000, 0x382C8000, 0x382CA000, 0x382CC000, 0x382CE000, 0x382D0000, 0x382D2000, 0x382D4000, 0x382D6000, 0x382D8000, 0x382DA000, 0x382DC000, 0x382DE000,
  783. 0x382E0000, 0x382E2000, 0x382E4000, 0x382E6000, 0x382E8000, 0x382EA000, 0x382EC000, 0x382EE000, 0x382F0000, 0x382F2000, 0x382F4000, 0x382F6000, 0x382F8000, 0x382FA000, 0x382FC000, 0x382FE000,
  784. 0x38300000, 0x38302000, 0x38304000, 0x38306000, 0x38308000, 0x3830A000, 0x3830C000, 0x3830E000, 0x38310000, 0x38312000, 0x38314000, 0x38316000, 0x38318000, 0x3831A000, 0x3831C000, 0x3831E000,
  785. 0x38320000, 0x38322000, 0x38324000, 0x38326000, 0x38328000, 0x3832A000, 0x3832C000, 0x3832E000, 0x38330000, 0x38332000, 0x38334000, 0x38336000, 0x38338000, 0x3833A000, 0x3833C000, 0x3833E000,
  786. 0x38340000, 0x38342000, 0x38344000, 0x38346000, 0x38348000, 0x3834A000, 0x3834C000, 0x3834E000, 0x38350000, 0x38352000, 0x38354000, 0x38356000, 0x38358000, 0x3835A000, 0x3835C000, 0x3835E000,
  787. 0x38360000, 0x38362000, 0x38364000, 0x38366000, 0x38368000, 0x3836A000, 0x3836C000, 0x3836E000, 0x38370000, 0x38372000, 0x38374000, 0x38376000, 0x38378000, 0x3837A000, 0x3837C000, 0x3837E000,
  788. 0x38380000, 0x38382000, 0x38384000, 0x38386000, 0x38388000, 0x3838A000, 0x3838C000, 0x3838E000, 0x38390000, 0x38392000, 0x38394000, 0x38396000, 0x38398000, 0x3839A000, 0x3839C000, 0x3839E000,
  789. 0x383A0000, 0x383A2000, 0x383A4000, 0x383A6000, 0x383A8000, 0x383AA000, 0x383AC000, 0x383AE000, 0x383B0000, 0x383B2000, 0x383B4000, 0x383B6000, 0x383B8000, 0x383BA000, 0x383BC000, 0x383BE000,
  790. 0x383C0000, 0x383C2000, 0x383C4000, 0x383C6000, 0x383C8000, 0x383CA000, 0x383CC000, 0x383CE000, 0x383D0000, 0x383D2000, 0x383D4000, 0x383D6000, 0x383D8000, 0x383DA000, 0x383DC000, 0x383DE000,
  791. 0x383E0000, 0x383E2000, 0x383E4000, 0x383E6000, 0x383E8000, 0x383EA000, 0x383EC000, 0x383EE000, 0x383F0000, 0x383F2000, 0x383F4000, 0x383F6000, 0x383F8000, 0x383FA000, 0x383FC000, 0x383FE000,
  792. 0x38400000, 0x38402000, 0x38404000, 0x38406000, 0x38408000, 0x3840A000, 0x3840C000, 0x3840E000, 0x38410000, 0x38412000, 0x38414000, 0x38416000, 0x38418000, 0x3841A000, 0x3841C000, 0x3841E000,
  793. 0x38420000, 0x38422000, 0x38424000, 0x38426000, 0x38428000, 0x3842A000, 0x3842C000, 0x3842E000, 0x38430000, 0x38432000, 0x38434000, 0x38436000, 0x38438000, 0x3843A000, 0x3843C000, 0x3843E000,
  794. 0x38440000, 0x38442000, 0x38444000, 0x38446000, 0x38448000, 0x3844A000, 0x3844C000, 0x3844E000, 0x38450000, 0x38452000, 0x38454000, 0x38456000, 0x38458000, 0x3845A000, 0x3845C000, 0x3845E000,
  795. 0x38460000, 0x38462000, 0x38464000, 0x38466000, 0x38468000, 0x3846A000, 0x3846C000, 0x3846E000, 0x38470000, 0x38472000, 0x38474000, 0x38476000, 0x38478000, 0x3847A000, 0x3847C000, 0x3847E000,
  796. 0x38480000, 0x38482000, 0x38484000, 0x38486000, 0x38488000, 0x3848A000, 0x3848C000, 0x3848E000, 0x38490000, 0x38492000, 0x38494000, 0x38496000, 0x38498000, 0x3849A000, 0x3849C000, 0x3849E000,
  797. 0x384A0000, 0x384A2000, 0x384A4000, 0x384A6000, 0x384A8000, 0x384AA000, 0x384AC000, 0x384AE000, 0x384B0000, 0x384B2000, 0x384B4000, 0x384B6000, 0x384B8000, 0x384BA000, 0x384BC000, 0x384BE000,
  798. 0x384C0000, 0x384C2000, 0x384C4000, 0x384C6000, 0x384C8000, 0x384CA000, 0x384CC000, 0x384CE000, 0x384D0000, 0x384D2000, 0x384D4000, 0x384D6000, 0x384D8000, 0x384DA000, 0x384DC000, 0x384DE000,
  799. 0x384E0000, 0x384E2000, 0x384E4000, 0x384E6000, 0x384E8000, 0x384EA000, 0x384EC000, 0x384EE000, 0x384F0000, 0x384F2000, 0x384F4000, 0x384F6000, 0x384F8000, 0x384FA000, 0x384FC000, 0x384FE000,
  800. 0x38500000, 0x38502000, 0x38504000, 0x38506000, 0x38508000, 0x3850A000, 0x3850C000, 0x3850E000, 0x38510000, 0x38512000, 0x38514000, 0x38516000, 0x38518000, 0x3851A000, 0x3851C000, 0x3851E000,
  801. 0x38520000, 0x38522000, 0x38524000, 0x38526000, 0x38528000, 0x3852A000, 0x3852C000, 0x3852E000, 0x38530000, 0x38532000, 0x38534000, 0x38536000, 0x38538000, 0x3853A000, 0x3853C000, 0x3853E000,
  802. 0x38540000, 0x38542000, 0x38544000, 0x38546000, 0x38548000, 0x3854A000, 0x3854C000, 0x3854E000, 0x38550000, 0x38552000, 0x38554000, 0x38556000, 0x38558000, 0x3855A000, 0x3855C000, 0x3855E000,
  803. 0x38560000, 0x38562000, 0x38564000, 0x38566000, 0x38568000, 0x3856A000, 0x3856C000, 0x3856E000, 0x38570000, 0x38572000, 0x38574000, 0x38576000, 0x38578000, 0x3857A000, 0x3857C000, 0x3857E000,
  804. 0x38580000, 0x38582000, 0x38584000, 0x38586000, 0x38588000, 0x3858A000, 0x3858C000, 0x3858E000, 0x38590000, 0x38592000, 0x38594000, 0x38596000, 0x38598000, 0x3859A000, 0x3859C000, 0x3859E000,
  805. 0x385A0000, 0x385A2000, 0x385A4000, 0x385A6000, 0x385A8000, 0x385AA000, 0x385AC000, 0x385AE000, 0x385B0000, 0x385B2000, 0x385B4000, 0x385B6000, 0x385B8000, 0x385BA000, 0x385BC000, 0x385BE000,
  806. 0x385C0000, 0x385C2000, 0x385C4000, 0x385C6000, 0x385C8000, 0x385CA000, 0x385CC000, 0x385CE000, 0x385D0000, 0x385D2000, 0x385D4000, 0x385D6000, 0x385D8000, 0x385DA000, 0x385DC000, 0x385DE000,
  807. 0x385E0000, 0x385E2000, 0x385E4000, 0x385E6000, 0x385E8000, 0x385EA000, 0x385EC000, 0x385EE000, 0x385F0000, 0x385F2000, 0x385F4000, 0x385F6000, 0x385F8000, 0x385FA000, 0x385FC000, 0x385FE000,
  808. 0x38600000, 0x38602000, 0x38604000, 0x38606000, 0x38608000, 0x3860A000, 0x3860C000, 0x3860E000, 0x38610000, 0x38612000, 0x38614000, 0x38616000, 0x38618000, 0x3861A000, 0x3861C000, 0x3861E000,
  809. 0x38620000, 0x38622000, 0x38624000, 0x38626000, 0x38628000, 0x3862A000, 0x3862C000, 0x3862E000, 0x38630000, 0x38632000, 0x38634000, 0x38636000, 0x38638000, 0x3863A000, 0x3863C000, 0x3863E000,
  810. 0x38640000, 0x38642000, 0x38644000, 0x38646000, 0x38648000, 0x3864A000, 0x3864C000, 0x3864E000, 0x38650000, 0x38652000, 0x38654000, 0x38656000, 0x38658000, 0x3865A000, 0x3865C000, 0x3865E000,
  811. 0x38660000, 0x38662000, 0x38664000, 0x38666000, 0x38668000, 0x3866A000, 0x3866C000, 0x3866E000, 0x38670000, 0x38672000, 0x38674000, 0x38676000, 0x38678000, 0x3867A000, 0x3867C000, 0x3867E000,
  812. 0x38680000, 0x38682000, 0x38684000, 0x38686000, 0x38688000, 0x3868A000, 0x3868C000, 0x3868E000, 0x38690000, 0x38692000, 0x38694000, 0x38696000, 0x38698000, 0x3869A000, 0x3869C000, 0x3869E000,
  813. 0x386A0000, 0x386A2000, 0x386A4000, 0x386A6000, 0x386A8000, 0x386AA000, 0x386AC000, 0x386AE000, 0x386B0000, 0x386B2000, 0x386B4000, 0x386B6000, 0x386B8000, 0x386BA000, 0x386BC000, 0x386BE000,
  814. 0x386C0000, 0x386C2000, 0x386C4000, 0x386C6000, 0x386C8000, 0x386CA000, 0x386CC000, 0x386CE000, 0x386D0000, 0x386D2000, 0x386D4000, 0x386D6000, 0x386D8000, 0x386DA000, 0x386DC000, 0x386DE000,
  815. 0x386E0000, 0x386E2000, 0x386E4000, 0x386E6000, 0x386E8000, 0x386EA000, 0x386EC000, 0x386EE000, 0x386F0000, 0x386F2000, 0x386F4000, 0x386F6000, 0x386F8000, 0x386FA000, 0x386FC000, 0x386FE000,
  816. 0x38700000, 0x38702000, 0x38704000, 0x38706000, 0x38708000, 0x3870A000, 0x3870C000, 0x3870E000, 0x38710000, 0x38712000, 0x38714000, 0x38716000, 0x38718000, 0x3871A000, 0x3871C000, 0x3871E000,
  817. 0x38720000, 0x38722000, 0x38724000, 0x38726000, 0x38728000, 0x3872A000, 0x3872C000, 0x3872E000, 0x38730000, 0x38732000, 0x38734000, 0x38736000, 0x38738000, 0x3873A000, 0x3873C000, 0x3873E000,
  818. 0x38740000, 0x38742000, 0x38744000, 0x38746000, 0x38748000, 0x3874A000, 0x3874C000, 0x3874E000, 0x38750000, 0x38752000, 0x38754000, 0x38756000, 0x38758000, 0x3875A000, 0x3875C000, 0x3875E000,
  819. 0x38760000, 0x38762000, 0x38764000, 0x38766000, 0x38768000, 0x3876A000, 0x3876C000, 0x3876E000, 0x38770000, 0x38772000, 0x38774000, 0x38776000, 0x38778000, 0x3877A000, 0x3877C000, 0x3877E000,
  820. 0x38780000, 0x38782000, 0x38784000, 0x38786000, 0x38788000, 0x3878A000, 0x3878C000, 0x3878E000, 0x38790000, 0x38792000, 0x38794000, 0x38796000, 0x38798000, 0x3879A000, 0x3879C000, 0x3879E000,
  821. 0x387A0000, 0x387A2000, 0x387A4000, 0x387A6000, 0x387A8000, 0x387AA000, 0x387AC000, 0x387AE000, 0x387B0000, 0x387B2000, 0x387B4000, 0x387B6000, 0x387B8000, 0x387BA000, 0x387BC000, 0x387BE000,
  822. 0x387C0000, 0x387C2000, 0x387C4000, 0x387C6000, 0x387C8000, 0x387CA000, 0x387CC000, 0x387CE000, 0x387D0000, 0x387D2000, 0x387D4000, 0x387D6000, 0x387D8000, 0x387DA000, 0x387DC000, 0x387DE000,
  823. 0x387E0000, 0x387E2000, 0x387E4000, 0x387E6000, 0x387E8000, 0x387EA000, 0x387EC000, 0x387EE000, 0x387F0000, 0x387F2000, 0x387F4000, 0x387F6000, 0x387F8000, 0x387FA000, 0x387FC000, 0x387FE000 };
  824. static const bits_t<float> exponent_table[64] = {
  825. 0x00000000, 0x00800000, 0x01000000, 0x01800000, 0x02000000, 0x02800000, 0x03000000, 0x03800000, 0x04000000, 0x04800000, 0x05000000, 0x05800000, 0x06000000, 0x06800000, 0x07000000, 0x07800000,
  826. 0x08000000, 0x08800000, 0x09000000, 0x09800000, 0x0A000000, 0x0A800000, 0x0B000000, 0x0B800000, 0x0C000000, 0x0C800000, 0x0D000000, 0x0D800000, 0x0E000000, 0x0E800000, 0x0F000000, 0x47800000,
  827. 0x80000000, 0x80800000, 0x81000000, 0x81800000, 0x82000000, 0x82800000, 0x83000000, 0x83800000, 0x84000000, 0x84800000, 0x85000000, 0x85800000, 0x86000000, 0x86800000, 0x87000000, 0x87800000,
  828. 0x88000000, 0x88800000, 0x89000000, 0x89800000, 0x8A000000, 0x8A800000, 0x8B000000, 0x8B800000, 0x8C000000, 0x8C800000, 0x8D000000, 0x8D800000, 0x8E000000, 0x8E800000, 0x8F000000, 0xC7800000 };
  829. static const unsigned short offset_table[64] = {
  830. 0, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024,
  831. 0, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024 };
  832. bits_t<float> fbits = mantissa_table[offset_table[value>>10]+(value&0x3FF)] + exponent_table[value>>10];
  833. #endif
  834. float out;
  835. std::memcpy(&out, &fbits, sizeof(float));
  836. return out;
  837. #endif
  838. }
  839. /// Convert half-precision to IEEE double-precision.
  840. /// \param value half-precision value to convert
  841. /// \return double-precision value
  842. inline double half2float_impl(unsigned int value, double, true_type) {
  843. #if HALF_ENABLE_F16C_INTRINSICS
  844. return _mm_cvtsd_f64(_mm_cvtps_pd(_mm_cvtph_ps(_mm_cvtsi32_si128(value))));
  845. #else
  846. uint32 hi = static_cast<uint32>(value&0x8000) << 16;
  847. unsigned int abs = value & 0x7FFF;
  848. if(abs) {
  849. hi |= 0x3F000000 << static_cast<unsigned>(abs>=0x7C00);
  850. for(; abs<0x400; abs<<=1,hi-=0x100000) ;
  851. hi += static_cast<uint32>(abs) << 10;
  852. }
  853. bits_t<double> dbits = static_cast<bits_t<double>>(hi) << 32;
  854. double out;
  855. std::memcpy(&out, &dbits, sizeof(double));
  856. return out;
  857. #endif
  858. }
  859. /// Convert half-precision to non-IEEE floating-point.
  860. /// \tparam T type to convert to (builtin integer type)
  861. /// \param value half-precision value to convert
  862. /// \return floating-point value
  863. template<class T> T half2float_impl(unsigned int value, T, ...) {
  864. T out;
  865. unsigned int abs = value & 0x7FFF;
  866. if(abs > 0x7C00)
  867. out = (std::numeric_limits<T>::has_signaling_NaN && !(abs&0x200)) ? std::numeric_limits<T>::signaling_NaN() :
  868. std::numeric_limits<T>::has_quiet_NaN ? std::numeric_limits<T>::quiet_NaN() : T();
  869. else if(abs == 0x7C00)
  870. out = std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max();
  871. else if(abs > 0x3FF)
  872. out = std::ldexp(static_cast<T>((abs&0x3FF)|0x400), (abs>>10)-25);
  873. else
  874. out = std::ldexp(static_cast<T>(abs), -24);
  875. return (value&0x8000) ? -out : out;
  876. }
  877. /// Convert half-precision to floating-point.
  878. /// \tparam T type to convert to (builtin integer type)
  879. /// \param value half-precision value to convert
  880. /// \return floating-point value
  881. template<class T> T half2float(unsigned int value) {
  882. return half2float_impl(value, T(), bool_type<std::numeric_limits<T>::is_iec559&&sizeof(bits_t<T>)==sizeof(T)>());
  883. }
  884. /// Convert half-precision floating-point to integer.
  885. /// \tparam R rounding mode to use
  886. /// \tparam E `true` for round to even, `false` for round away from zero
  887. /// \tparam I `true` to raise INEXACT exception (if inexact), `false` to never raise it
  888. /// \tparam T type to convert to (buitlin integer type with at least 16 bits precision, excluding any implicit sign bits)
  889. /// \param value half-precision value to convert
  890. /// \return rounded integer value
  891. /// \exception FE_INVALID if value is not representable in type \a T
  892. /// \exception FE_INEXACT if value had to be rounded and \a I is `true`
  893. template<std::float_round_style R,bool E,bool I,class T> T half2int(unsigned int value) {
  894. unsigned int abs = value & 0x7FFF;
  895. if(abs >= 0x7C00) {
  896. raise(FE_INVALID);
  897. return (value&0x8000) ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
  898. }
  899. if(abs < 0x3800) {
  900. raise(FE_INEXACT, I);
  901. return (R==std::round_toward_infinity) ? T(~(value>>15)&(abs!=0)) :
  902. (R==std::round_toward_neg_infinity) ? -T(value>0x8000) :
  903. T();
  904. }
  905. int exp = 25 - (abs>>10);
  906. unsigned int m = (value&0x3FF) | 0x400;
  907. int32 i = static_cast<int32>((exp<=0) ? (m<<-exp) : ((m+(
  908. (R==std::round_to_nearest) ? ((1<<(exp-1))-(~(m>>exp)&E)) :
  909. (R==std::round_toward_infinity) ? (((1<<exp)-1)&((value>>15)-1)) :
  910. (R==std::round_toward_neg_infinity) ? (((1<<exp)-1)&-(value>>15)) : 0))>>exp));
  911. if((!std::numeric_limits<T>::is_signed && (value&0x8000)) || (std::numeric_limits<T>::digits<16 &&
  912. ((value&0x8000) ? (-i<std::numeric_limits<T>::min()) : (i>std::numeric_limits<T>::max()))))
  913. raise(FE_INVALID);
  914. else if(I && exp > 0 && (m&((1<<exp)-1)))
  915. raise(FE_INEXACT);
  916. return static_cast<T>((value&0x8000) ? -i : i);
  917. }
  918. /// \}
  919. /// \name Mathematics
  920. /// \{
  921. /// upper part of 64-bit multiplication.
  922. /// \tparam R rounding mode to use
  923. /// \param x first factor
  924. /// \param y second factor
  925. /// \return upper 32 bit of \a x * \a y
  926. template<std::float_round_style R> uint32 mulhi(uint32 x, uint32 y) {
  927. uint32 xy = (x>>16) * (y&0xFFFF), yx = (x&0xFFFF) * (y>>16), c = (xy&0xFFFF) + (yx&0xFFFF) + (((x&0xFFFF)*(y&0xFFFF))>>16);
  928. return (x>>16)*(y>>16) + (xy>>16) + (yx>>16) + (c>>16) +
  929. ((R==std::round_to_nearest) ? ((c>>15)&1) : (R==std::round_toward_infinity) ? ((c&0xFFFF)!=0) : 0);
  930. }
  931. /// 64-bit multiplication.
  932. /// \param x first factor
  933. /// \param y second factor
  934. /// \return upper 32 bit of \a x * \a y rounded to nearest
  935. inline uint32 multiply64(uint32 x, uint32 y) {
  936. return static_cast<uint32>((static_cast<unsigned long long>(x)*static_cast<unsigned long long>(y)+0x80000000)>>32);
  937. }
  938. /// 64-bit division.
  939. /// \param x upper 32 bit of dividend
  940. /// \param y divisor
  941. /// \param s variable to store sticky bit for rounding
  942. /// \return (\a x << 32) / \a y
  943. inline uint32 divide64(uint32 x, uint32 y, int &s) {
  944. unsigned long long xx = static_cast<unsigned long long>(x) << 32;
  945. return s = (xx%y!=0), static_cast<uint32>(xx/y);
  946. }
  947. /// Half precision positive modulus.
  948. /// \tparam Q `true` to compute full quotient, `false` else
  949. /// \tparam R `true` to compute signed remainder, `false` for positive remainder
  950. /// \param x first operand as positive finite half-precision value
  951. /// \param y second operand as positive finite half-precision value
  952. /// \param quo adress to store quotient at, `nullptr` if \a Q `false`
  953. /// \return modulus of \a x / \a y
  954. template<bool Q,bool R> unsigned int mod(unsigned int x, unsigned int y, int *quo = NULL) {
  955. unsigned int q = 0;
  956. if(x > y) {
  957. int absx = x, absy = y, expx = 0, expy = 0;
  958. for(; absx<0x400; absx<<=1,--expx) ;
  959. for(; absy<0x400; absy<<=1,--expy) ;
  960. expx += absx >> 10;
  961. expy += absy >> 10;
  962. int mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
  963. for(int d=expx-expy; d; --d) {
  964. if(!Q && mx == my)
  965. return 0;
  966. if(mx >= my) {
  967. mx -= my;
  968. q += Q;
  969. }
  970. mx <<= 1;
  971. q <<= static_cast<int>(Q);
  972. }
  973. if(!Q && mx == my)
  974. return 0;
  975. if(mx >= my) {
  976. mx -= my;
  977. ++q;
  978. }
  979. if(Q) {
  980. q &= (1<<(std::numeric_limits<int>::digits-1)) - 1;
  981. if(!mx)
  982. return *quo = q, 0;
  983. }
  984. for(; mx<0x400; mx<<=1,--expy) ;
  985. x = (expy>0) ? ((expy<<10)|(mx&0x3FF)) : (mx>>(1-expy));
  986. }
  987. if(R) {
  988. unsigned int a, b;
  989. if(y < 0x800) {
  990. a = (x<0x400) ? (x<<1) : (x+0x400);
  991. b = y;
  992. } else {
  993. a = x;
  994. b = y - 0x400;
  995. }
  996. if(a > b || (a == b && (q&1))) {
  997. int exp = (y>>10) + (y<=0x3FF), d = exp - (x>>10) - (x<=0x3FF);
  998. int m = (((y&0x3FF)|((y>0x3FF)<<10))<<1) - (((x&0x3FF)|((x>0x3FF)<<10))<<(1-d));
  999. for(; m<0x800 && exp>1; m<<=1,--exp) ;
  1000. x = 0x8000 + ((exp-1)<<10) + (m>>1);
  1001. q += Q;
  1002. }
  1003. }
  1004. if(Q)
  1005. *quo = q;
  1006. return x;
  1007. }
  1008. /// Fixed point square root.
  1009. /// \tparam F number of fractional bits
  1010. /// \param r radicand in Q1.F fixed point format
  1011. /// \param exp exponent
  1012. /// \return square root as Q1.F/2
  1013. template<unsigned int F> uint32 sqrt(uint32 &r, int &exp) {
  1014. int i = exp & 1;
  1015. r <<= i;
  1016. exp = (exp-i) / 2;
  1017. uint32 m = 0;
  1018. for(uint32 bit=static_cast<uint32>(1)<<F; bit; bit>>=2) {
  1019. if(r < m+bit)
  1020. m >>= 1;
  1021. else {
  1022. r -= m + bit;
  1023. m = (m>>1) + bit;
  1024. }
  1025. }
  1026. return m;
  1027. }
  1028. /// Fixed point binary exponential.
  1029. /// This uses the BKM algorithm in E-mode.
  1030. /// \param m exponent in [0,1) as Q0.31
  1031. /// \param n number of iterations (at most 32)
  1032. /// \return 2 ^ \a m as Q1.31
  1033. inline uint32 exp2(uint32 m, unsigned int n = 32) {
  1034. static const uint32 logs[] = {
  1035. 0x80000000, 0x4AE00D1D, 0x2934F098, 0x15C01A3A, 0x0B31FB7D, 0x05AEB4DD, 0x02DCF2D1, 0x016FE50B,
  1036. 0x00B84E23, 0x005C3E10, 0x002E24CA, 0x001713D6, 0x000B8A47, 0x0005C53B, 0x0002E2A3, 0x00017153,
  1037. 0x0000B8AA, 0x00005C55, 0x00002E2B, 0x00001715, 0x00000B8B, 0x000005C5, 0x000002E3, 0x00000171,
  1038. 0x000000B9, 0x0000005C, 0x0000002E, 0x00000017, 0x0000000C, 0x00000006, 0x00000003, 0x00000001 };
  1039. if(!m)
  1040. return 0x80000000;
  1041. uint32 mx = 0x80000000, my = 0;
  1042. for(unsigned int i=1; i<n; ++i) {
  1043. uint32 mz = my + logs[i];
  1044. if(mz <= m) {
  1045. my = mz;
  1046. mx += mx >> i;
  1047. }
  1048. }
  1049. return mx;
  1050. }
  1051. /// Fixed point binary logarithm.
  1052. /// This uses the BKM algorithm in L-mode.
  1053. /// \param m mantissa in [1,2) as Q1.30
  1054. /// \param n number of iterations (at most 32)
  1055. /// \return log2(\a m) as Q0.31
  1056. inline uint32 log2(uint32 m, unsigned int n = 32) {
  1057. static const uint32 logs[] = {
  1058. 0x80000000, 0x4AE00D1D, 0x2934F098, 0x15C01A3A, 0x0B31FB7D, 0x05AEB4DD, 0x02DCF2D1, 0x016FE50B,
  1059. 0x00B84E23, 0x005C3E10, 0x002E24CA, 0x001713D6, 0x000B8A47, 0x0005C53B, 0x0002E2A3, 0x00017153,
  1060. 0x0000B8AA, 0x00005C55, 0x00002E2B, 0x00001715, 0x00000B8B, 0x000005C5, 0x000002E3, 0x00000171,
  1061. 0x000000B9, 0x0000005C, 0x0000002E, 0x00000017, 0x0000000C, 0x00000006, 0x00000003, 0x00000001 };
  1062. if(m == 0x40000000)
  1063. return 0;
  1064. uint32 mx = 0x40000000, my = 0;
  1065. for(unsigned int i=1; i<n; ++i) {
  1066. uint32 mz = mx + (mx>>i);
  1067. if(mz <= m) {
  1068. mx = mz;
  1069. my += logs[i];
  1070. }
  1071. }
  1072. return my;
  1073. }
  1074. /// Fixed point sine and cosine.
  1075. /// This uses the CORDIC algorithm in rotation mode.
  1076. /// \param mz angle in [-pi/2,pi/2] as Q1.30
  1077. /// \param n number of iterations (at most 31)
  1078. /// \return sine and cosine of \a mz as Q1.30
  1079. inline std::pair<uint32,uint32> sincos(uint32 mz, unsigned int n = 31) {
  1080. static const uint32 angles[] = {
  1081. 0x3243F6A9, 0x1DAC6705, 0x0FADBAFD, 0x07F56EA7, 0x03FEAB77, 0x01FFD55C, 0x00FFFAAB, 0x007FFF55,
  1082. 0x003FFFEB, 0x001FFFFD, 0x00100000, 0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00008000,
  1083. 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, 0x00000100, 0x00000080,
  1084. 0x00000040, 0x00000020, 0x00000010, 0x00000008, 0x00000004, 0x00000002, 0x00000001 };
  1085. uint32 mx = 0x26DD3B6A, my = 0;
  1086. for(unsigned int i=0; i<n; ++i) {
  1087. uint32 sign = sign_mask(mz);
  1088. uint32 tx = mx - (arithmetic_shift(my, i)^sign) + sign;
  1089. uint32 ty = my + (arithmetic_shift(mx, i)^sign) - sign;
  1090. mx = tx; my = ty; mz -= (angles[i]^sign) - sign;
  1091. }
  1092. return std::make_pair(my, mx);
  1093. }
  1094. /// Fixed point arc tangent.
  1095. /// This uses the CORDIC algorithm in vectoring mode.
  1096. /// \param my y coordinate as Q0.30
  1097. /// \param mx x coordinate as Q0.30
  1098. /// \param n number of iterations (at most 31)
  1099. /// \return arc tangent of \a my / \a mx as Q1.30
  1100. inline uint32 atan2(uint32 my, uint32 mx, unsigned int n = 31) {
  1101. static const uint32 angles[] = {
  1102. 0x3243F6A9, 0x1DAC6705, 0x0FADBAFD, 0x07F56EA7, 0x03FEAB77, 0x01FFD55C, 0x00FFFAAB, 0x007FFF55,
  1103. 0x003FFFEB, 0x001FFFFD, 0x00100000, 0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00008000,
  1104. 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, 0x00000100, 0x00000080,
  1105. 0x00000040, 0x00000020, 0x00000010, 0x00000008, 0x00000004, 0x00000002, 0x00000001 };
  1106. uint32 mz = 0;
  1107. for(unsigned int i=0; i<n; ++i) {
  1108. uint32 sign = sign_mask(my);
  1109. uint32 tx = mx + (arithmetic_shift(my, i)^sign) - sign;
  1110. uint32 ty = my - (arithmetic_shift(mx, i)^sign) + sign;
  1111. mx = tx; my = ty; mz += (angles[i]^sign) - sign;
  1112. }
  1113. return mz;
  1114. }
  1115. /// Reduce argument for trigonometric functions.
  1116. /// \param abs half-precision floating-point value
  1117. /// \param k value to take quarter period
  1118. /// \return \a abs reduced to [-pi/4,pi/4] as Q0.30
  1119. inline uint32 angle_arg(unsigned int abs, int &k) {
  1120. uint32 m = (abs&0x3FF) | ((abs>0x3FF)<<10);
  1121. int exp = (abs>>10) + (abs<=0x3FF) - 15;
  1122. if(abs < 0x3A48)
  1123. return k = 0, m << (exp+20);
  1124. unsigned long long y = m * 0xA2F9836E4E442, mask = (1ULL<<(62-exp)) - 1, yi = (y+(mask>>1)) & ~mask, f = y - yi;
  1125. uint32 sign = -static_cast<uint32>(f>>63);
  1126. k = static_cast<int>(yi>>(62-exp));
  1127. return (multiply64(static_cast<uint32>((sign ? -f : f)>>(31-exp)), 0xC90FDAA2)^sign) - sign;
  1128. }
  1129. /// Get arguments for atan2 function.
  1130. /// \param abs half-precision floating-point value
  1131. /// \return \a abs and sqrt(1 - \a abs^2) as Q0.30
  1132. inline std::pair<uint32,uint32> atan2_args(unsigned int abs) {
  1133. int exp = -15;
  1134. for(; abs<0x400; abs<<=1,--exp) ;
  1135. exp += abs >> 10;
  1136. uint32 my = ((abs&0x3FF)|0x400) << 5, r = my * my;
  1137. int rexp = 2 * exp;
  1138. r = 0x40000000 - ((rexp>-31) ? ((r>>-rexp)|((r&((static_cast<uint32>(1)<<-rexp)-1))!=0)) : 1);
  1139. for(rexp=0; r<0x40000000; r<<=1,--rexp) ;
  1140. uint32 mx = sqrt<30>(r, rexp);
  1141. int d = exp - rexp;
  1142. if(d < 0)
  1143. return std::make_pair((d<-14) ? ((my>>(-d-14))+((my>>(-d-15))&1)) : (my<<(14+d)), (mx<<14)+(r<<13)/mx);
  1144. if(d > 0)
  1145. return std::make_pair(my<<14, (d>14) ? ((mx>>(d-14))+((mx>>(d-15))&1)) : ((d==14) ? mx : ((mx<<(14-d))+(r<<(13-d))/mx)));
  1146. return std::make_pair(my<<13, (mx<<13)+(r<<12)/mx);
  1147. }
  1148. /// Get exponentials for hyperbolic computation
  1149. /// \param abs half-precision floating-point value
  1150. /// \param exp variable to take unbiased exponent of larger result
  1151. /// \param n number of BKM iterations (at most 32)
  1152. /// \return exp(abs) and exp(-\a abs) as Q1.31 with same exponent
  1153. inline std::pair<uint32,uint32> hyperbolic_args(unsigned int abs, int &exp, unsigned int n = 32) {
  1154. uint32 mx = detail::multiply64(static_cast<uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29), my;
  1155. int e = (abs>>10) + (abs<=0x3FF);
  1156. if(e < 14) {
  1157. exp = 0;
  1158. mx >>= 14 - e;
  1159. } else {
  1160. exp = mx >> (45-e);
  1161. mx = (mx<<(e-14)) & 0x7FFFFFFF;
  1162. }
  1163. mx = exp2(mx, n);
  1164. int d = exp << 1, s;
  1165. if(mx > 0x80000000) {
  1166. my = divide64(0x80000000, mx, s);
  1167. my |= s;
  1168. ++d;
  1169. } else
  1170. my = mx;
  1171. return std::make_pair(mx, (d<31) ? ((my>>d)|((my&((static_cast<uint32>(1)<<d)-1))!=0)) : 1);
  1172. }
  1173. /// Postprocessing for binary exponential.
  1174. /// \tparam R rounding mode to use
  1175. /// \tparam I `true` to always raise INEXACT exception, `false` to raise only for rounded results
  1176. /// \param m mantissa as Q1.31
  1177. /// \param exp absolute value of unbiased exponent
  1178. /// \param esign sign of actual exponent
  1179. /// \param sign sign bit of result
  1180. /// \return value converted to half-precision
  1181. /// \exception FE_OVERFLOW on overflows
  1182. /// \exception FE_UNDERFLOW on underflows
  1183. /// \exception FE_INEXACT if value had to be rounded or \a I is `true`
  1184. template<std::float_round_style R,bool I> unsigned int exp2_post(uint32 m, int exp, bool esign, unsigned int sign = 0) {
  1185. int s = 0;
  1186. if(esign) {
  1187. if(m > 0x80000000) {
  1188. m = divide64(0x80000000, m, s);
  1189. ++exp;
  1190. }
  1191. if(exp > 25)
  1192. return underflow<R>(sign);
  1193. else if(exp == 25)
  1194. return rounded<R,I>(sign, 1, (m&0x7FFFFFFF)!=0);
  1195. exp = -exp;
  1196. } else if(exp > 15)
  1197. return overflow<R>(sign);
  1198. return fixed2half<R,31,false,false,I>(m, exp+14, sign, s);
  1199. }
  1200. /// Postprocessing for binary logarithm.
  1201. /// \tparam R rounding mode to use
  1202. /// \tparam L logarithm for base transformation as Q1.31
  1203. /// \param m fractional part of logarithm as Q0.31
  1204. /// \param ilog signed integer part of logarithm
  1205. /// \param exp biased exponent of result
  1206. /// \param sign sign bit of result
  1207. /// \return value base-transformed and converted to half-precision
  1208. /// \exception FE_OVERFLOW on overflows
  1209. /// \exception FE_UNDERFLOW on underflows
  1210. /// \exception FE_INEXACT if no other exception occurred
  1211. template<std::float_round_style R,uint32 L> unsigned int log2_post(uint32 m, int ilog, int exp, unsigned int sign = 0) {
  1212. uint32 msign = sign_mask(ilog);
  1213. m = (((static_cast<uint32>(ilog)<<27)+(m>>4))^msign) - msign;
  1214. if(!m)
  1215. return 0;
  1216. for(; m<0x80000000; m<<=1,--exp) ;
  1217. int i = m >= L, s;
  1218. exp += i;
  1219. m >>= 1 + i;
  1220. sign ^= msign & 0x8000;
  1221. if(exp < -11)
  1222. return underflow<R>(sign);
  1223. m = divide64(m, L, s);
  1224. return fixed2half<R,30,false,false,true>(m, exp, sign, 1);
  1225. }
  1226. /// Hypotenuse square root and postprocessing.
  1227. /// \tparam R rounding mode to use
  1228. /// \param r mantissa as Q2.30
  1229. /// \param exp unbiased exponent
  1230. /// \return square root converted to half-precision
  1231. /// \exception FE_OVERFLOW on overflows
  1232. /// \exception FE_UNDERFLOW on underflows
  1233. /// \exception FE_INEXACT if value had to be rounded
  1234. template<std::float_round_style R> unsigned int hypot_post(uint32 r, int exp) {
  1235. int i = r >> 31;
  1236. if((exp+=i) > 46)
  1237. return overflow<R>();
  1238. if(exp < -34)
  1239. return underflow<R>();
  1240. r = (r>>i) | (r&i);
  1241. uint32 m = sqrt<30>(r, exp+=15);
  1242. return fixed2half<R,15,false,false,false>(m, exp-1, 0, r!=0);
  1243. }
  1244. /// Division and postprocessing for tangents.
  1245. /// \tparam R rounding mode to use
  1246. /// \param my dividend as Q1.31
  1247. /// \param mx divisor as Q1.31
  1248. /// \param exp biased exponent of result
  1249. /// \param sign sign bit of result
  1250. /// \return quotient converted to half-precision
  1251. /// \exception FE_OVERFLOW on overflows
  1252. /// \exception FE_UNDERFLOW on underflows
  1253. /// \exception FE_INEXACT if no other exception occurred
  1254. template<std::float_round_style R> unsigned int tangent_post(uint32 my, uint32 mx, int exp, unsigned int sign = 0) {
  1255. int i = my >= mx, s;
  1256. exp += i;
  1257. if(exp > 29)
  1258. return overflow<R>(sign);
  1259. if(exp < -11)
  1260. return underflow<R>(sign);
  1261. uint32 m = divide64(my>>(i+1), mx, s);
  1262. return fixed2half<R,30,false,false,true>(m, exp, sign, s);
  1263. }
  1264. /// Area function and postprocessing.
  1265. /// This computes the value directly in Q2.30 using the representation `asinh|acosh(x) = log(x+sqrt(x^2+|-1))`.
  1266. /// \tparam R rounding mode to use
  1267. /// \tparam S `true` for asinh, `false` for acosh
  1268. /// \param arg half-precision argument
  1269. /// \return asinh|acosh(\a arg) converted to half-precision
  1270. /// \exception FE_OVERFLOW on overflows
  1271. /// \exception FE_UNDERFLOW on underflows
  1272. /// \exception FE_INEXACT if no other exception occurred
  1273. template<std::float_round_style R,bool S> unsigned int area(unsigned int arg) {
  1274. int abs = arg & 0x7FFF, expx = (abs>>10) + (abs<=0x3FF) - 15, expy = -15, ilog, i;
  1275. uint32 mx = static_cast<uint32>((abs&0x3FF)|((abs>0x3FF)<<10)) << 20, my, r;
  1276. for(; abs<0x400; abs<<=1,--expy) ;
  1277. expy += abs >> 10;
  1278. r = ((abs&0x3FF)|0x400) << 5;
  1279. r *= r;
  1280. i = r >> 31;
  1281. expy = 2*expy + i;
  1282. r >>= i;
  1283. if(S) {
  1284. if(expy < 0) {
  1285. r = 0x40000000 + ((expy>-30) ? ((r>>-expy)|((r&((static_cast<uint32>(1)<<-expy)-1))!=0)) : 1);
  1286. expy = 0;
  1287. } else {
  1288. r += 0x40000000 >> expy;
  1289. i = r >> 31;
  1290. r = (r>>i) | (r&i);
  1291. expy += i;
  1292. }
  1293. } else {
  1294. r -= 0x40000000 >> expy;
  1295. for(; r<0x40000000; r<<=1,--expy) ;
  1296. }
  1297. my = sqrt<30>(r, expy);
  1298. my = (my<<15) + (r<<14)/my;
  1299. if(S) {
  1300. mx >>= expy - expx;
  1301. ilog = expy;
  1302. } else {
  1303. my >>= expx - expy;
  1304. ilog = expx;
  1305. }
  1306. my += mx;
  1307. i = my >> 31;
  1308. static const int G = S && (R==std::round_to_nearest);
  1309. return log2_post<R,0xB8AA3B2A>(log2(my>>i, 26+S+G)+(G<<3), ilog+i, 17, arg&(static_cast<unsigned>(S)<<15));
  1310. }
  1311. /// Class for 1.31 unsigned floating-point computation
  1312. struct f31 {
  1313. /// Constructor.
  1314. /// \param mant mantissa as 1.31
  1315. /// \param e exponent
  1316. constexpr f31(uint32 mant, int e) : m(mant), exp(e) {}
  1317. /// Constructor.
  1318. /// \param abs unsigned half-precision value
  1319. f31(unsigned int abs) : exp(-15) {
  1320. for(; abs<0x400; abs<<=1,--exp) ;
  1321. m = static_cast<uint32>((abs&0x3FF)|0x400) << 21;
  1322. exp += (abs>>10);
  1323. }
  1324. /// Addition operator.
  1325. /// \param a first operand
  1326. /// \param b second operand
  1327. /// \return \a a + \a b
  1328. friend f31 operator+(f31 a, f31 b) {
  1329. if(b.exp > a.exp)
  1330. std::swap(a, b);
  1331. int d = a.exp - b.exp;
  1332. uint32 m = a.m + ((d<32) ? (b.m>>d) : 0);
  1333. int i = (m&0xFFFFFFFF) < a.m;
  1334. return f31(((m+i)>>i)|0x80000000, a.exp+i);
  1335. }
  1336. /// Subtraction operator.
  1337. /// \param a first operand
  1338. /// \param b second operand
  1339. /// \return \a a - \a b
  1340. friend f31 operator-(f31 a, f31 b) {
  1341. int d = a.exp - b.exp, exp = a.exp;
  1342. uint32 m = a.m - ((d<32) ? (b.m>>d) : 0);
  1343. if(!m)
  1344. return f31(0, -32);
  1345. for(; m<0x80000000; m<<=1,--exp) ;
  1346. return f31(m, exp);
  1347. }
  1348. /// Multiplication operator.
  1349. /// \param a first operand
  1350. /// \param b second operand
  1351. /// \return \a a * \a b
  1352. friend f31 operator*(f31 a, f31 b) {
  1353. uint32 m = multiply64(a.m, b.m);
  1354. int i = m >> 31;
  1355. return f31(m<<(1-i), a.exp + b.exp + i);
  1356. }
  1357. /// Division operator.
  1358. /// \param a first operand
  1359. /// \param b second operand
  1360. /// \return \a a / \a b
  1361. friend f31 operator/(f31 a, f31 b) {
  1362. int i = a.m >= b.m, s;
  1363. uint32 m = divide64((a.m+i)>>i, b.m, s);
  1364. return f31(m, a.exp - b.exp + i - 1);
  1365. }
  1366. uint32 m; ///< mantissa as 1.31.
  1367. int exp; ///< exponent.
  1368. };
  1369. /// Error function and postprocessing.
  1370. /// This computes the value directly in Q1.31 using the approximations given
  1371. /// [here](https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions).
  1372. /// \tparam R rounding mode to use
  1373. /// \tparam C `true` for comlementary error function, `false` else
  1374. /// \param arg half-precision function argument
  1375. /// \return approximated value of error function in half-precision
  1376. /// \exception FE_OVERFLOW on overflows
  1377. /// \exception FE_UNDERFLOW on underflows
  1378. /// \exception FE_INEXACT if no other exception occurred
  1379. template<std::float_round_style R,bool C> unsigned int erf(unsigned int arg) {
  1380. unsigned int abs = arg & 0x7FFF, sign = arg & 0x8000;
  1381. f31 x(abs), x2 = x * x * f31(0xB8AA3B29, 0), t = f31(0x80000000, 0) / (f31(0x80000000, 0)+f31(0xA7BA054A, -2)*x), t2 = t * t;
  1382. f31 e = ((f31(0x87DC2213, 0)*t2+f31(0xB5F0E2AE, 0))*t2+f31(0x82790637, -2)-(f31(0xBA00E2B8, 0)*t2+f31(0x91A98E62, -2))*t) * t /
  1383. ((x2.exp<0) ? f31(exp2((x2.exp>-32) ? (x2.m>>-x2.exp) : 0, 30), 0) : f31(exp2((x2.m<<x2.exp)&0x7FFFFFFF, 22), x2.m>>(31-x2.exp)));
  1384. return (!C || sign) ? fixed2half<R,31,false,true,true>(0x80000000-(e.m>>(C-e.exp)), 14+C, sign&(C-1U)) :
  1385. (e.exp<-25) ? underflow<R>() : fixed2half<R,30,false,false,true>(e.m>>1, e.exp+14, 0, e.m&1);
  1386. }
  1387. /// Gamma function and postprocessing.
  1388. /// This approximates the value of either the gamma function or its logarithm directly in Q1.31.
  1389. /// \tparam R rounding mode to use
  1390. /// \tparam L `true` for lograithm of gamma function, `false` for gamma function
  1391. /// \param arg half-precision floating-point value
  1392. /// \return lgamma/tgamma(\a arg) in half-precision
  1393. /// \exception FE_OVERFLOW on overflows
  1394. /// \exception FE_UNDERFLOW on underflows
  1395. /// \exception FE_INEXACT if \a arg is not a positive integer
  1396. template<std::float_round_style R,bool L> unsigned int gamma(unsigned int arg) {
  1397. /* static const double p[] ={ 2.50662827563479526904, 225.525584619175212544, -268.295973841304927459, 80.9030806934622512966, -5.00757863970517583837, 0.0114684895434781459556 };
  1398. double t = arg + 4.65, s = p[0];
  1399. for(unsigned int i=0; i<5; ++i)
  1400. s += p[i+1] / (arg+i);
  1401. return std::log(s) + (arg-0.5)*std::log(t) - t;
  1402. */ static const f31 pi(0xC90FDAA2, 1), lbe(0xB8AA3B29, 0);
  1403. unsigned int abs = arg & 0x7FFF, sign = arg & 0x8000;
  1404. bool bsign = sign != 0;
  1405. f31 z(abs), x = sign ? (z+f31(0x80000000, 0)) : z, t = x + f31(0x94CCCCCD, 2), s =
  1406. f31(0xA06C9901, 1) + f31(0xBBE654E2, -7)/(x+f31(0x80000000, 2)) + f31(0xA1CE6098, 6)/(x+f31(0x80000000, 1))
  1407. + f31(0xE1868CB7, 7)/x - f31(0x8625E279, 8)/(x+f31(0x80000000, 0)) - f31(0xA03E158F, 2)/(x+f31(0xC0000000, 1));
  1408. int i = (s.exp>=2) + (s.exp>=4) + (s.exp>=8) + (s.exp>=16);
  1409. s = f31((static_cast<uint32>(s.exp)<<(31-i))+(log2(s.m>>1, 28)>>i), i) / lbe;
  1410. if(x.exp != -1 || x.m != 0x80000000) {
  1411. i = (t.exp>=2) + (t.exp>=4) + (t.exp>=8);
  1412. f31 l = f31((static_cast<uint32>(t.exp)<<(31-i))+(log2(t.m>>1, 30)>>i), i) / lbe;
  1413. s = (x.exp<-1) ? (s-(f31(0x80000000, -1)-x)*l) : (s+(x-f31(0x80000000, -1))*l);
  1414. }
  1415. s = x.exp ? (s-t) : (t-s);
  1416. if(bsign) {
  1417. if(z.exp >= 0) {
  1418. sign &= (L|((z.m>>(31-z.exp))&1)) - 1;
  1419. for(z=f31((z.m<<(1+z.exp))&0xFFFFFFFF, -1); z.m<0x80000000; z.m<<=1,--z.exp) ;
  1420. }
  1421. if(z.exp == -1)
  1422. z = f31(0x80000000, 0) - z;
  1423. if(z.exp < -1) {
  1424. z = z * pi;
  1425. z.m = sincos(z.m>>(1-z.exp), 30).first;
  1426. for(z.exp=1; z.m<0x80000000; z.m<<=1,--z.exp) ;
  1427. }
  1428. else
  1429. z = f31(0x80000000, 0);
  1430. } if(L) {
  1431. if(bsign) {
  1432. f31 l(0x92868247, 0);
  1433. if(z.exp < 0) {
  1434. uint32 m = log2((z.m+1)>>1, 27);
  1435. z = f31(-((static_cast<uint32>(z.exp)<<26)+(m>>5)), 5);
  1436. for(; z.m<0x80000000; z.m<<=1,--z.exp) ;
  1437. l = l + z / lbe;
  1438. }
  1439. sign = static_cast<unsigned>(x.exp&&(l.exp<s.exp||(l.exp==s.exp&&l.m<s.m))) << 15;
  1440. s = sign ? (s-l) : x.exp ? (l-s) : (l+s);
  1441. } else {
  1442. sign = static_cast<unsigned>(x.exp==0) << 15;
  1443. if(s.exp < -24)
  1444. return underflow<R>(sign);
  1445. if(s.exp > 15)
  1446. return overflow<R>(sign);
  1447. }
  1448. } else {
  1449. s = s * lbe;
  1450. uint32 m;
  1451. if(s.exp < 0) {
  1452. m = s.m >> -s.exp;
  1453. s.exp = 0;
  1454. } else {
  1455. m = (s.m<<s.exp) & 0x7FFFFFFF;
  1456. s.exp = (s.m>>(31-s.exp));
  1457. }
  1458. s.m = exp2(m, 27);
  1459. if(!x.exp)
  1460. s = f31(0x80000000, 0) / s;
  1461. if(bsign) {
  1462. if(z.exp < 0)
  1463. s = s * z;
  1464. s = pi / s;
  1465. if(s.exp < -24)
  1466. return underflow<R>(sign);
  1467. } else if(z.exp > 0 && !(z.m&((1<<(31-z.exp))-1)))
  1468. return ((s.exp+14)<<10) + (s.m>>21);
  1469. if(s.exp > 15)
  1470. return overflow<R>(sign);
  1471. }
  1472. return fixed2half<R,31,false,false,true>(s.m, s.exp+14, sign);
  1473. }
  1474. /// \}
  1475. template<class,class,std::float_round_style> struct half_caster;
  1476. }
  1477. /// Half-precision floating-point type.
  1478. /// This class implements an IEEE-conformant half-precision floating-point type with the usual arithmetic
  1479. /// operators and conversions. It is implicitly convertible to single-precision floating-point, which makes artihmetic
  1480. /// expressions and functions with mixed-type operands to be of the most precise operand type.
  1481. ///
  1482. /// According to the C++98/03 definition, the half type is not a POD type. But according to C++11's less strict and
  1483. /// extended definitions it is both a standard layout type and a trivially copyable type (even if not a POD type), which
  1484. /// means it can be standard-conformantly copied using raw binary copies. But in this context some more words about the
  1485. /// actual size of the type. Although the half is representing an IEEE 16-bit type, it does not neccessarily have to be of
  1486. /// exactly 16-bits size. But on any reasonable implementation the actual binary representation of this type will most
  1487. /// probably not ivolve any additional "magic" or padding beyond the simple binary representation of the underlying 16-bit
  1488. /// IEEE number, even if not strictly guaranteed by the standard. But even then it only has an actual size of 16 bits if
  1489. /// your C++ implementation supports an unsigned integer type of exactly 16 bits width. But this should be the case on
  1490. /// nearly any reasonable platform.
  1491. ///
  1492. /// So if your C++ implementation is not totally exotic or imposes special alignment requirements, it is a reasonable
  1493. /// assumption that the data of a half is just comprised of the 2 bytes of the underlying IEEE representation.
  1494. class half {
  1495. public:
  1496. /// \name Construction and assignment
  1497. /// \{
  1498. /// Default constructor.
  1499. /// This initializes the half to 0. Although this does not match the builtin types' default-initialization semantics
  1500. /// and may be less efficient than no initialization, it is needed to provide proper value-initialization semantics.
  1501. constexpr half() noexcept : data_() {}
  1502. /// Conversion constructor.
  1503. /// \param rhs float to convert
  1504. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1505. //explicit half(float rhs) : data_(static_cast<detail::uint16>(detail::float2half<round_style>(rhs))) {}
  1506. /// Conversion constructor.
  1507. /// \param rhs float to convert
  1508. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1509. template<class T>
  1510. half(T rhs) : data_(static_cast<detail::uint16>(detail::float2half<round_style>(static_cast<float>(rhs)))) {}
  1511. /// Conversion to single-precision.
  1512. /// \return single precision value representing expression value
  1513. operator float() const { return detail::half2float<float>(data_); }
  1514. /// Assignment operator.
  1515. /// \param rhs single-precision value to copy from
  1516. /// \return reference to this half
  1517. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1518. half& operator=(const float &rhs) { data_ = static_cast<detail::uint16>(detail::float2half<round_style>(rhs)); return *this; }
  1519. template<class T>
  1520. half& operator=(const T &rhs) { return *this = static_cast<float>(rhs); }
  1521. /// \}
  1522. /// \name Arithmetic updates
  1523. /// \{
  1524. /// Arithmetic assignment.
  1525. /// \tparam T type of concrete half expression
  1526. /// \param rhs half expression to add
  1527. /// \return reference to this half
  1528. /// \exception FE_... according to operator+(half,half)
  1529. half& operator+=(half rhs) { return *this = *this + rhs; }
  1530. /// Arithmetic assignment.
  1531. /// \tparam T type of concrete half expression
  1532. /// \param rhs half expression to subtract
  1533. /// \return reference to this half
  1534. /// \exception FE_... according to operator-(half,half)
  1535. half& operator-=(half rhs) { return *this = *this - rhs; }
  1536. /// Arithmetic assignment.
  1537. /// \tparam T type of concrete half expression
  1538. /// \param rhs half expression to multiply with
  1539. /// \return reference to this half
  1540. /// \exception FE_... according to operator*(half,half)
  1541. half& operator*=(half rhs) { return *this = *this * rhs; }
  1542. /// Arithmetic assignment.
  1543. /// \tparam T type of concrete half expression
  1544. /// \param rhs half expression to divide by
  1545. /// \return reference to this half
  1546. /// \exception FE_... according to operator/(half,half)
  1547. half& operator/=(half rhs) { return *this = *this / rhs; }
  1548. /*
  1549. /// Arithmetic assignment.
  1550. /// \param rhs single-precision value to add
  1551. /// \return reference to this half
  1552. /// \exception FE_... according to operator=()
  1553. half& operator+=(float rhs) { return *this = *this + rhs; }
  1554. /// Arithmetic assignment.
  1555. /// \param rhs single-precision value to subtract
  1556. /// \return reference to this half
  1557. /// \exception FE_... according to operator=()
  1558. half& operator-=(float rhs) { return *this = *this - rhs; }
  1559. /// Arithmetic assignment.
  1560. /// \param rhs single-precision value to multiply with
  1561. /// \return reference to this half
  1562. /// \exception FE_... according to operator=()
  1563. half& operator*=(float rhs) { return *this = *this * rhs; }
  1564. /// Arithmetic assignment.
  1565. /// \param rhs single-precision value to divide by
  1566. /// \return reference to this half
  1567. /// \exception FE_... according to operator=()
  1568. half& operator/=(float rhs) { return *this = *this / rhs; }
  1569. */
  1570. /// \}
  1571. /// \name Increment and decrement
  1572. /// \{
  1573. /// Prefix increment.
  1574. /// \return incremented half value
  1575. /// \exception FE_... according to operator+(half,half)
  1576. half& operator++() { return *this = *this + half(detail::binary, 0x3C00); }
  1577. /// Prefix decrement.
  1578. /// \return decremented half value
  1579. /// \exception FE_... according to operator-(half,half)
  1580. half& operator--() { return *this = *this + half(detail::binary, 0xBC00); }
  1581. /// Postfix increment.
  1582. /// \return non-incremented half value
  1583. /// \exception FE_... according to operator+(half,half)
  1584. half operator++(int) { half out(*this); ++*this; return out; }
  1585. /// Postfix decrement.
  1586. /// \return non-decremented half value
  1587. /// \exception FE_... according to operator-(half,half)
  1588. half operator--(int) { half out(*this); --*this; return out; }
  1589. /// \}
  1590. detail::uint16 get_data()const{ return data_; }
  1591. private:
  1592. /// Rounding mode to use
  1593. static const std::float_round_style round_style = (std::float_round_style)(HALF_ROUND_STYLE);
  1594. /// Constructor.
  1595. /// \param bits binary representation to set half to
  1596. constexpr half(detail::binary_t, unsigned int bits) noexcept : data_(static_cast<detail::uint16>(bits)) {}
  1597. /// Internal binary representation
  1598. detail::uint16 data_;
  1599. friend constexpr_NOERR bool operator==(half, half);
  1600. template<class T> friend constexpr_NOERR bool operator==(half, T);
  1601. template<class T> friend constexpr_NOERR bool operator==(T, half);
  1602. friend constexpr_NOERR bool operator!=(half, half);
  1603. template<class T> friend constexpr_NOERR bool operator!=(half, T);
  1604. template<class T> friend constexpr_NOERR bool operator!=(T, half);
  1605. friend constexpr_NOERR bool operator<(half, half);
  1606. template<class T> friend constexpr_NOERR bool operator<(half, T);
  1607. template<class T> friend constexpr_NOERR bool operator<(T, half);
  1608. friend constexpr_NOERR bool operator>(half, half);
  1609. template<class T> friend constexpr_NOERR bool operator>(half, T);
  1610. template<class T> friend constexpr_NOERR bool operator>(T, half);
  1611. friend constexpr_NOERR bool operator<=(half, half);
  1612. template<class T> friend constexpr_NOERR bool operator<=(half, T);
  1613. template<class T> friend constexpr_NOERR bool operator<=(T, half);
  1614. friend constexpr_NOERR bool operator>=(half, half);
  1615. template<class T> friend constexpr_NOERR bool operator>=(half, T);
  1616. template<class T> friend constexpr_NOERR bool operator>=(T, half);
  1617. friend constexpr half operator+(half);
  1618. friend constexpr half operator-(half);
  1619. friend half operator+(half, half);
  1620. template<class T> friend half operator+(half, T);
  1621. template<class T> friend half operator+(T, half);
  1622. friend half operator-(half, half);
  1623. template<class T> friend half operator-(half, T);
  1624. template<class T> friend half operator-(T, half);
  1625. friend half operator*(half, half);
  1626. template<class T> friend half operator*(half, T);
  1627. template<class T> friend half operator*(T, half);
  1628. friend half operator/(half, half);
  1629. template<class T> friend half operator/(half, T);
  1630. template<class T> friend half operator/(T, half);
  1631. template<class charT,class traits> friend std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits>&, half);
  1632. template<class charT,class traits> friend std::basic_istream<charT,traits>& operator>>(std::basic_istream<charT,traits>&, half&);
  1633. friend constexpr half fabs(half);
  1634. friend half fmod(half, half);
  1635. friend half remainder(half, half);
  1636. friend half remquo(half, half, int*);
  1637. friend half fma(half, half, half);
  1638. friend constexpr_NOERR half fmax(half, half);
  1639. friend constexpr_NOERR half fmin(half, half);
  1640. friend half fdim(half, half);
  1641. friend half nanh(const char*);
  1642. friend half exp(half);
  1643. friend half exp2(half);
  1644. friend half expm1(half);
  1645. friend half log(half);
  1646. friend half log10(half);
  1647. friend half log2(half);
  1648. friend half log1p(half);
  1649. friend half sqrt(half);
  1650. friend half cbrt(half);
  1651. friend half hypot(half, half);
  1652. friend half hypot(half, half, half);
  1653. friend half pow(half, half);
  1654. friend void sincos(half, half*, half*);
  1655. friend half sin(half);
  1656. friend half cos(half);
  1657. friend half tan(half);
  1658. friend half asin(half);
  1659. friend half acos(half);
  1660. friend half atan(half);
  1661. friend half atan2(half, half);
  1662. friend half sinh(half);
  1663. friend half cosh(half);
  1664. friend half tanh(half);
  1665. friend half asinh(half);
  1666. friend half acosh(half);
  1667. friend half atanh(half);
  1668. friend half erf(half);
  1669. friend half erfc(half);
  1670. friend half lgamma(half);
  1671. friend half tgamma(half);
  1672. friend half ceil(half);
  1673. friend half floor(half);
  1674. friend half trunc(half);
  1675. friend half round(half);
  1676. friend long lround(half);
  1677. friend half rint(half);
  1678. friend long lrint(half);
  1679. friend half nearbyint(half);
  1680. friend long long llround(half);
  1681. friend long long llrint(half);
  1682. friend half frexp(half, int*);
  1683. friend half scalbln(half, long);
  1684. friend half modf(half, half*);
  1685. friend int ilogb(half);
  1686. friend half logb(half);
  1687. friend half nextafter(half, half);
  1688. friend half nexttoward(half, long double);
  1689. friend constexpr half copysign(half, half);
  1690. friend constexpr int fpclassify(half);
  1691. friend constexpr bool isfinite(half);
  1692. friend constexpr bool isinf(half);
  1693. friend constexpr bool isnan(half);
  1694. friend constexpr bool isnormal(half);
  1695. friend constexpr bool signbit(half);
  1696. friend constexpr bool isgreater(half, half);
  1697. friend constexpr bool isgreaterequal(half, half);
  1698. friend constexpr bool isless(half, half);
  1699. friend constexpr bool islessequal(half, half);
  1700. friend constexpr bool islessgreater(half, half);
  1701. template<class,class,std::float_round_style> friend struct detail::half_caster;
  1702. friend class std::numeric_limits<half>;
  1703. friend struct std::hash<half>;
  1704. friend half literal::operator "" _h(long double);
  1705. };
  1706. namespace literal {
  1707. /// Half literal.
  1708. /// While this returns a properly rounded half-precision value, half literals can unfortunately not be constant
  1709. /// expressions due to rather involved conversions. So don't expect this to be a literal literal without involving
  1710. /// conversion operations at runtime. It is a convenience feature, not a performance optimization.
  1711. /// \param value literal value
  1712. /// \return half with of given value (possibly rounded)
  1713. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1714. inline half operator "" _h(long double value) { return half(detail::binary, detail::float2half<half::round_style>(value)); }
  1715. }
  1716. namespace detail {
  1717. /// Helper class for half casts.
  1718. /// This class template has to be specialized for all valid cast arguments to define an appropriate static
  1719. /// `cast` member function and a corresponding `type` member denoting its return type.
  1720. /// \tparam T destination type
  1721. /// \tparam U source type
  1722. /// \tparam R rounding mode to use
  1723. template<class T,class U,std::float_round_style R=(std::float_round_style)(HALF_ROUND_STYLE)> struct half_caster {};
  1724. template<class U,std::float_round_style R> struct half_caster<half,U,R> {
  1725. static_assert(std::is_arithmetic<U>::value, "half_cast from non-arithmetic type unsupported");
  1726. static half cast(U arg) { return cast_impl(arg, is_float<U>()); };
  1727. private:
  1728. static half cast_impl(U arg, true_type) { return half(binary, float2half<R>(arg)); }
  1729. static half cast_impl(U arg, false_type) { return half(binary, int2half<R>(arg)); }
  1730. };
  1731. template<class T,std::float_round_style R> struct half_caster<T,half,R> {
  1732. static_assert(std::is_arithmetic<T>::value, "half_cast to non-arithmetic type unsupported");
  1733. static T cast(half arg) { return cast_impl(arg, is_float<T>()); }
  1734. private:
  1735. static T cast_impl(half arg, true_type) { return half2float<T>(arg.data_); }
  1736. static T cast_impl(half arg, false_type) { return half2int<R,true,true,T>(arg.data_); }
  1737. };
  1738. template<std::float_round_style R> struct half_caster<half,half,R> {
  1739. static half cast(half arg) { return arg; }
  1740. };
  1741. }
  1742. }
  1743. /// Extensions to the C++ standard library.
  1744. namespace std {
  1745. /// Numeric limits for half-precision floats.
  1746. /// **See also:** Documentation for [std::numeric_limits](https://en.cppreference.com/w/cpp/types/numeric_limits)
  1747. template<> class numeric_limits<half_float::half> {
  1748. public:
  1749. /// Is template specialization.
  1750. static constexpr bool is_specialized = true;
  1751. /// Supports signed values.
  1752. static constexpr bool is_signed = true;
  1753. /// Is not an integer type.
  1754. static constexpr bool is_integer = false;
  1755. /// Is not exact.
  1756. static constexpr bool is_exact = false;
  1757. /// Doesn't provide modulo arithmetic.
  1758. static constexpr bool is_modulo = false;
  1759. /// Has a finite set of values.
  1760. static constexpr bool is_bounded = true;
  1761. /// IEEE conformant.
  1762. static constexpr bool is_iec559 = true;
  1763. /// Supports infinity.
  1764. static constexpr bool has_infinity = true;
  1765. /// Supports quiet NaNs.
  1766. static constexpr bool has_quiet_NaN = true;
  1767. /// Supports signaling NaNs.
  1768. static constexpr bool has_signaling_NaN = true;
  1769. /// Supports subnormal values.
  1770. static constexpr float_denorm_style has_denorm = denorm_present;
  1771. /// Supports no denormalization detection.
  1772. static constexpr bool has_denorm_loss = false;
  1773. #if HALF_ERRHANDLING_THROWS
  1774. static constexpr bool traps = true;
  1775. #else
  1776. /// Traps only if [HALF_ERRHANDLING_THROW_...](\ref HALF_ERRHANDLING_THROW_INVALID) is acitvated.
  1777. static constexpr bool traps = false;
  1778. #endif
  1779. /// Does not support no pre-rounding underflow detection.
  1780. static constexpr bool tinyness_before = false;
  1781. /// Rounding mode.
  1782. static constexpr float_round_style round_style = half_float::half::round_style;
  1783. /// Significant digits.
  1784. static constexpr int digits = 11;
  1785. /// Significant decimal digits.
  1786. static constexpr int digits10 = 3;
  1787. /// Required decimal digits to represent all possible values.
  1788. static constexpr int max_digits10 = 5;
  1789. /// Number base.
  1790. static constexpr int radix = 2;
  1791. /// One more than smallest exponent.
  1792. static constexpr int min_exponent = -13;
  1793. /// Smallest normalized representable power of 10.
  1794. static constexpr int min_exponent10 = -4;
  1795. /// One more than largest exponent
  1796. static constexpr int max_exponent = 16;
  1797. /// Largest finitely representable power of 10.
  1798. static constexpr int max_exponent10 = 4;
  1799. /// Smallest positive normal value.
  1800. static constexpr half_float::half min() noexcept { return half_float::half(half_float::detail::binary, 0x0400); }
  1801. /// Smallest finite value.
  1802. static constexpr half_float::half lowest() noexcept { return half_float::half(half_float::detail::binary, 0xFBFF); }
  1803. /// Largest finite value.
  1804. static constexpr half_float::half max() noexcept { return half_float::half(half_float::detail::binary, 0x7BFF); }
  1805. /// Difference between 1 and next representable value.
  1806. static constexpr half_float::half epsilon() noexcept { return half_float::half(half_float::detail::binary, 0x1400); }
  1807. /// Maximum rounding error in ULP (units in the last place).
  1808. static constexpr half_float::half round_error() noexcept
  1809. { return half_float::half(half_float::detail::binary, (round_style==std::round_to_nearest) ? 0x3800 : 0x3C00); }
  1810. /// Positive infinity.
  1811. static constexpr half_float::half infinity() noexcept { return half_float::half(half_float::detail::binary, 0x7C00); }
  1812. /// Quiet NaN.
  1813. static constexpr half_float::half quiet_NaN() noexcept { return half_float::half(half_float::detail::binary, 0x7FFF); }
  1814. /// Signaling NaN.
  1815. static constexpr half_float::half signaling_NaN() noexcept { return half_float::half(half_float::detail::binary, 0x7DFF); }
  1816. /// Smallest positive subnormal value.
  1817. static constexpr half_float::half denorm_min() noexcept { return half_float::half(half_float::detail::binary, 0x0001); }
  1818. };
  1819. /// Hash function for half-precision floats.
  1820. /// **See also:** Documentation for [std::hash](https://en.cppreference.com/w/cpp/utility/hash)
  1821. template<> struct hash<half_float::half> {
  1822. /// Type of function argument.
  1823. typedef half_float::half argument_type;
  1824. /// Function return type.
  1825. typedef size_t result_type;
  1826. /// Compute hash function.
  1827. /// \param arg half to hash
  1828. /// \return hash value
  1829. result_type operator()(argument_type arg) const { return hash<half_float::detail::uint16>()(arg.data_&-static_cast<unsigned>(arg.data_!=0x8000)); }
  1830. };
  1831. }
  1832. namespace half_float {
  1833. /// \anchor compop
  1834. /// \name Comparison operators
  1835. /// \{
  1836. /// Comparison for equality.
  1837. /// \param x first operand
  1838. /// \param y second operand
  1839. /// \retval true if operands equal
  1840. /// \retval false else
  1841. /// \exception FE_INVALID if \a x or \a y is NaN
  1842. inline constexpr_NOERR bool operator==(half x, half y) {
  1843. return !detail::compsignal(x.data_, y.data_) && (x.data_==y.data_ || !((x.data_|y.data_)&0x7FFF));
  1844. }
  1845. template<class T>
  1846. inline constexpr_NOERR bool operator==(half x, T y) { return x == static_cast<half>(y); }
  1847. template<class T>
  1848. inline constexpr_NOERR bool operator==(T x, half y) { return static_cast<half>(x) == y; }
  1849. /// Comparison for inequality.
  1850. /// \param x first operand
  1851. /// \param y second operand
  1852. /// \retval true if operands not equal
  1853. /// \retval false else
  1854. /// \exception FE_INVALID if \a x or \a y is NaN
  1855. inline constexpr_NOERR bool operator!=(half x, half y) {
  1856. return detail::compsignal(x.data_, y.data_) || (x.data_!=y.data_ && ((x.data_|y.data_)&0x7FFF));
  1857. }
  1858. template<class T>
  1859. inline constexpr_NOERR bool operator!=(half x, T y) { return x != static_cast<half>(y); }
  1860. template<class T>
  1861. inline constexpr_NOERR bool operator!=(T x, half y) { return static_cast<half>(x) != y; }
  1862. /// Comparison for less than.
  1863. /// \param x first operand
  1864. /// \param y second operand
  1865. /// \retval true if \a x less than \a y
  1866. /// \retval false else
  1867. /// \exception FE_INVALID if \a x or \a y is NaN
  1868. inline constexpr_NOERR bool operator<(half x, half y) {
  1869. return !detail::compsignal(x.data_, y.data_) &&
  1870. ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) < ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
  1871. }
  1872. template<class T>
  1873. inline constexpr_NOERR bool operator<(half x, T y) { return x < static_cast<half>(y); }
  1874. template<class T>
  1875. inline constexpr_NOERR bool operator<(T x, half y) { return static_cast<half>(x) < y; }
  1876. /// Comparison for greater than.
  1877. /// \param x first operand
  1878. /// \param y second operand
  1879. /// \retval true if \a x greater than \a y
  1880. /// \retval false else
  1881. /// \exception FE_INVALID if \a x or \a y is NaN
  1882. inline constexpr_NOERR bool operator>(half x, half y) {
  1883. return !detail::compsignal(x.data_, y.data_) &&
  1884. ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) > ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
  1885. }
  1886. template<class T>
  1887. inline constexpr_NOERR bool operator>(half x, T y) { return x > static_cast<half>(y); }
  1888. template<class T>
  1889. inline constexpr_NOERR bool operator>(T x, half y) { return static_cast<half>(x) > y; }
  1890. /// Comparison for less equal.
  1891. /// \param x first operand
  1892. /// \param y second operand
  1893. /// \retval true if \a x less equal \a y
  1894. /// \retval false else
  1895. /// \exception FE_INVALID if \a x or \a y is NaN
  1896. inline constexpr_NOERR bool operator<=(half x, half y) {
  1897. return !detail::compsignal(x.data_, y.data_) &&
  1898. ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) <= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
  1899. }
  1900. template<class T>
  1901. inline constexpr_NOERR bool operator<=(half x, T y) { return x <= static_cast<half>(y); }
  1902. template<class T>
  1903. inline constexpr_NOERR bool operator<=(T x, half y) { return static_cast<half>(x) <= y; }
  1904. /// Comparison for greater equal.
  1905. /// \param x first operand
  1906. /// \param y second operand
  1907. /// \retval true if \a x greater equal \a y
  1908. /// \retval false else
  1909. /// \exception FE_INVALID if \a x or \a y is NaN
  1910. inline constexpr_NOERR bool operator>=(half x, half y) {
  1911. return !detail::compsignal(x.data_, y.data_) &&
  1912. ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) >= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15));
  1913. }
  1914. template<class T>
  1915. inline constexpr_NOERR bool operator>=(half x, T y) { return x >= static_cast<half>(y); }
  1916. template<class T>
  1917. inline constexpr_NOERR bool operator>=(T x, half y) { return static_cast<half>(x) >= y; }
  1918. /// \}
  1919. /// \anchor arithmetics
  1920. /// \name Arithmetic operators
  1921. /// \{
  1922. /// Identity.
  1923. /// \param arg operand
  1924. /// \return unchanged operand
  1925. inline constexpr half operator+(half arg) { return arg; }
  1926. /// Negation.
  1927. /// \param arg operand
  1928. /// \return negated operand
  1929. inline constexpr half operator-(half arg) { return half(detail::binary, arg.data_^0x8000); }
  1930. /// Addition.
  1931. /// This operation is exact to rounding for all rounding modes.
  1932. /// \param x left operand
  1933. /// \param y right operand
  1934. /// \return sum of half expressions
  1935. /// \exception FE_INVALID if \a x and \a y are infinities with different signs or signaling NaNs
  1936. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1937. inline half operator+(half x, half y) {
  1938. #ifdef HALF_ARITHMETIC_TYPE
  1939. return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)+detail::half2float<detail::internal_t>(y.data_)));
  1940. #else
  1941. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF;
  1942. bool sub = ((x.data_^y.data_)&0x8000) != 0;
  1943. if(absx >= 0x7C00 || absy >= 0x7C00)
  1944. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) : (absy!=0x7C00) ? x.data_ :
  1945. (sub && absx==0x7C00) ? detail::invalid() : y.data_);
  1946. if(!absx)
  1947. return absy ? y : half(detail::binary, (half::round_style==std::round_toward_neg_infinity) ? (x.data_|y.data_) : (x.data_&y.data_));
  1948. if(!absy)
  1949. return x;
  1950. unsigned int sign = ((sub && absy>absx) ? y.data_ : x.data_) & 0x8000;
  1951. if(absy > absx)
  1952. std::swap(absx, absy);
  1953. int exp = (absx>>10) + (absx<=0x3FF), d = exp - (absy>>10) - (absy<=0x3FF), mx = ((absx&0x3FF)|((absx>0x3FF)<<10)) << 3, my;
  1954. if(d < 13) {
  1955. my = ((absy&0x3FF)|((absy>0x3FF)<<10)) << 3;
  1956. my = (my>>d) | ((my&((1<<d)-1))!=0);
  1957. } else
  1958. my = 1;
  1959. if(sub) {
  1960. if(!(mx-=my))
  1961. return half(detail::binary, static_cast<unsigned>(half::round_style==std::round_toward_neg_infinity)<<15);
  1962. for(; mx<0x2000 && exp>1; mx<<=1,--exp) ;
  1963. } else {
  1964. mx += my;
  1965. int i = mx >> 14;
  1966. if((exp+=i) > 30)
  1967. return half(detail::binary, detail::overflow<half::round_style>(sign));
  1968. mx = (mx>>i) | (mx&i);
  1969. }
  1970. return half(detail::binary, detail::rounded<half::round_style,false>(sign+((exp-1)<<10)+(mx>>3), (mx>>2)&1, (mx&0x3)!=0));
  1971. #endif
  1972. }
  1973. template<class T>
  1974. inline half operator+(half x, T y) { return x + static_cast<half>(y); }
  1975. template<class T>
  1976. inline half operator+(T x, half y) { return static_cast<half>(x) + y; }
  1977. /// Subtraction.
  1978. /// This operation is exact to rounding for all rounding modes.
  1979. /// \param x left operand
  1980. /// \param y right operand
  1981. /// \return difference of half expressions
  1982. /// \exception FE_INVALID if \a x and \a y are infinities with equal signs or signaling NaNs
  1983. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  1984. inline half operator-(half x, half y) {
  1985. #ifdef HALF_ARITHMETIC_TYPE
  1986. return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)-detail::half2float<detail::internal_t>(y.data_)));
  1987. #else
  1988. return x + (-y);
  1989. #endif
  1990. }
  1991. template<class T>
  1992. inline half operator-(half x, T y) { return x - static_cast<half>(y); }
  1993. template<class T>
  1994. inline half operator-(T x, half y) { return static_cast<half>(x) - y; }
  1995. /// Multiplication.
  1996. /// This operation is exact to rounding for all rounding modes.
  1997. /// \param x left operand
  1998. /// \param y right operand
  1999. /// \return product of half expressions
  2000. /// \exception FE_INVALID if multiplying 0 with infinity or if \a x or \a y is signaling NaN
  2001. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2002. inline half operator*(half x, half y) {
  2003. #ifdef HALF_ARITHMETIC_TYPE
  2004. return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)*detail::half2float<detail::internal_t>(y.data_)));
  2005. #else
  2006. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = -16;
  2007. unsigned int sign = (x.data_^y.data_) & 0x8000;
  2008. if(absx >= 0x7C00 || absy >= 0x7C00)
  2009. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2010. ((absx==0x7C00 && !absy)||(absy==0x7C00 && !absx)) ? detail::invalid() : (sign|0x7C00));
  2011. if(!absx || !absy)
  2012. return half(detail::binary, sign);
  2013. for(; absx<0x400; absx<<=1,--exp) ;
  2014. for(; absy<0x400; absy<<=1,--exp) ;
  2015. detail::uint32 m = static_cast<detail::uint32>((absx&0x3FF)|0x400) * static_cast<detail::uint32>((absy&0x3FF)|0x400);
  2016. int i = m >> 21, s = m & i;
  2017. exp += (absx>>10) + (absy>>10) + i;
  2018. if(exp > 29)
  2019. return half(detail::binary, detail::overflow<half::round_style>(sign));
  2020. else if(exp < -11)
  2021. return half(detail::binary, detail::underflow<half::round_style>(sign));
  2022. return half(detail::binary, detail::fixed2half<half::round_style,20,false,false,false>(m>>i, exp, sign, s));
  2023. #endif
  2024. }
  2025. template<class T>
  2026. inline half operator*(half x, T y) { return x * static_cast<half>(y); }
  2027. template<class T>
  2028. inline half operator*(T x, half y) { return static_cast<half>(x) * y; }
  2029. /// Division.
  2030. /// This operation is exact to rounding for all rounding modes.
  2031. /// \param x left operand
  2032. /// \param y right operand
  2033. /// \return quotient of half expressions
  2034. /// \exception FE_INVALID if dividing 0s or infinities with each other or if \a x or \a y is signaling NaN
  2035. /// \exception FE_DIVBYZERO if dividing finite value by 0
  2036. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2037. inline half operator/(half x, half y) {
  2038. #ifdef HALF_ARITHMETIC_TYPE
  2039. return half(detail::binary, detail::float2half<half::round_style>(detail::half2float<detail::internal_t>(x.data_)/detail::half2float<detail::internal_t>(y.data_)));
  2040. #else
  2041. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = 14;
  2042. unsigned int sign = (x.data_^y.data_) & 0x8000;
  2043. if(absx >= 0x7C00 || absy >= 0x7C00)
  2044. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2045. (absx==absy) ? detail::invalid() : (sign|((absx==0x7C00) ? 0x7C00 : 0)));
  2046. if(!absx)
  2047. return half(detail::binary, absy ? sign : detail::invalid());
  2048. if(!absy)
  2049. return half(detail::binary, detail::pole(sign));
  2050. for(; absx<0x400; absx<<=1,--exp) ;
  2051. for(; absy<0x400; absy<<=1,++exp) ;
  2052. detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
  2053. int i = mx < my;
  2054. exp += (absx>>10) - (absy>>10) - i;
  2055. if(exp > 29)
  2056. return half(detail::binary, detail::overflow<half::round_style>(sign));
  2057. else if(exp < -11)
  2058. return half(detail::binary, detail::underflow<half::round_style>(sign));
  2059. mx <<= 12 + i;
  2060. my <<= 1;
  2061. return half(detail::binary, detail::fixed2half<half::round_style,11,false,false,false>(mx/my, exp, sign, mx%my!=0));
  2062. #endif
  2063. }
  2064. template<class T>
  2065. inline half operator/(half x, T y) { return x / static_cast<half>(y); }
  2066. template<class T>
  2067. inline half operator/(T x, half y) { return static_cast<half>(x) / y; }
  2068. /// \}
  2069. /// \anchor streaming
  2070. /// \name Input and output
  2071. /// \{
  2072. /// Output operator.
  2073. /// This uses the built-in functionality for streaming out floating-point numbers.
  2074. /// \param out output stream to write into
  2075. /// \param arg half expression to write
  2076. /// \return reference to output stream
  2077. template<class charT,class traits> std::basic_ostream<charT,traits>& operator<<(std::basic_ostream<charT,traits> &out, half arg) {
  2078. #ifdef HALF_ARITHMETIC_TYPE
  2079. return out << detail::half2float<detail::internal_t>(arg.data_);
  2080. #else
  2081. return out << detail::half2float<float>(arg.data_);
  2082. #endif
  2083. }
  2084. /// Input operator.
  2085. /// This uses the built-in functionality for streaming in floating-point numbers, specifically double precision floating
  2086. /// point numbers (unless overridden with [HALF_ARITHMETIC_TYPE](\ref HALF_ARITHMETIC_TYPE)). So the input string is first
  2087. /// rounded to double precision using the underlying platform's current floating-point rounding mode before being rounded
  2088. /// to half-precision using the library's half-precision rounding mode.
  2089. /// \param in input stream to read from
  2090. /// \param arg half to read into
  2091. /// \return reference to input stream
  2092. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2093. template<class charT,class traits> std::basic_istream<charT,traits>& operator>>(std::basic_istream<charT,traits> &in, half &arg) {
  2094. #ifdef HALF_ARITHMETIC_TYPE
  2095. detail::internal_t f;
  2096. #else
  2097. double f;
  2098. #endif
  2099. if(in >> f)
  2100. arg.data_ = detail::float2half<half::round_style>(f);
  2101. return in;
  2102. }
  2103. /// \}
  2104. /// \anchor basic
  2105. /// \name Basic mathematical operations
  2106. /// \{
  2107. /// Absolute value.
  2108. /// **See also:** Documentation for [std::fabs](https://en.cppreference.com/w/cpp/numeric/math/fabs).
  2109. /// \param arg operand
  2110. /// \return absolute value of \a arg
  2111. inline constexpr half fabs(half arg) { return half(detail::binary, arg.data_&0x7FFF); }
  2112. /// Absolute value.
  2113. /// **See also:** Documentation for [std::abs](https://en.cppreference.com/w/cpp/numeric/math/fabs).
  2114. /// \param arg operand
  2115. /// \return absolute value of \a arg
  2116. inline constexpr half abs(half arg) { return fabs(arg); }
  2117. /// Remainder of division.
  2118. /// **See also:** Documentation for [std::fmod](https://en.cppreference.com/w/cpp/numeric/math/fmod).
  2119. /// \param x first operand
  2120. /// \param y second operand
  2121. /// \return remainder of floating-point division.
  2122. /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
  2123. inline half fmod(half x, half y) {
  2124. unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, sign = x.data_ & 0x8000;
  2125. if(absx >= 0x7C00 || absy >= 0x7C00)
  2126. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2127. (absx==0x7C00) ? detail::invalid() : x.data_);
  2128. if(!absy)
  2129. return half(detail::binary, detail::invalid());
  2130. if(!absx)
  2131. return x;
  2132. if(absx == absy)
  2133. return half(detail::binary, sign);
  2134. return half(detail::binary, sign|detail::mod<false,false>(absx, absy));
  2135. }
  2136. /// Remainder of division.
  2137. /// **See also:** Documentation for [std::remainder](https://en.cppreference.com/w/cpp/numeric/math/remainder).
  2138. /// \param x first operand
  2139. /// \param y second operand
  2140. /// \return remainder of floating-point division.
  2141. /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
  2142. inline half remainder(half x, half y) {
  2143. unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, sign = x.data_ & 0x8000;
  2144. if(absx >= 0x7C00 || absy >= 0x7C00)
  2145. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2146. (absx==0x7C00) ? detail::invalid() : x.data_);
  2147. if(!absy)
  2148. return half(detail::binary, detail::invalid());
  2149. if(absx == absy)
  2150. return half(detail::binary, sign);
  2151. return half(detail::binary, sign^detail::mod<false,true>(absx, absy));
  2152. }
  2153. /// Remainder of division.
  2154. /// **See also:** Documentation for [std::remquo](https://en.cppreference.com/w/cpp/numeric/math/remquo).
  2155. /// \param x first operand
  2156. /// \param y second operand
  2157. /// \param quo address to store some bits of quotient at
  2158. /// \return remainder of floating-point division.
  2159. /// \exception FE_INVALID if \a x is infinite or \a y is 0 or if \a x or \a y is signaling NaN
  2160. inline half remquo(half x, half y, int *quo) {
  2161. unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, value = x.data_ & 0x8000;
  2162. if(absx >= 0x7C00 || absy >= 0x7C00)
  2163. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2164. (absx==0x7C00) ? detail::invalid() : (*quo = 0, x.data_));
  2165. if(!absy)
  2166. return half(detail::binary, detail::invalid());
  2167. bool qsign = ((value^y.data_)&0x8000) != 0;
  2168. int q = 1;
  2169. if(absx != absy)
  2170. value ^= detail::mod<true, true>(absx, absy, &q);
  2171. return *quo = qsign ? -q : q, half(detail::binary, value);
  2172. }
  2173. /// Fused multiply add.
  2174. /// This function is exact to rounding for all rounding modes.
  2175. ///
  2176. /// **See also:** Documentation for [std::fma](https://en.cppreference.com/w/cpp/numeric/math/fma).
  2177. /// \param x first operand
  2178. /// \param y second operand
  2179. /// \param z third operand
  2180. /// \return ( \a x * \a y ) + \a z rounded as one operation.
  2181. /// \exception FE_INVALID according to operator*() and operator+() unless any argument is a quiet NaN and no argument is a signaling NaN
  2182. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding the final addition
  2183. inline half fma(half x, half y, half z) {
  2184. #ifdef HALF_ARITHMETIC_TYPE
  2185. detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_), fz = detail::half2float<detail::internal_t>(z.data_);
  2186. #if FP_FAST_FMA
  2187. return half(detail::binary, detail::float2half<half::round_style>(std::fma(fx, fy, fz)));
  2188. #else
  2189. return half(detail::binary, detail::float2half<half::round_style>(fx*fy+fz));
  2190. #endif
  2191. #else
  2192. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, absz = z.data_ & 0x7FFF, exp = -15;
  2193. unsigned int sign = (x.data_^y.data_) & 0x8000;
  2194. bool sub = ((sign^z.data_)&0x8000) != 0;
  2195. if(absx >= 0x7C00 || absy >= 0x7C00 || absz >= 0x7C00)
  2196. return (absx>0x7C00 || absy>0x7C00 || absz>0x7C00) ? half(detail::binary, detail::signal(x.data_, y.data_, z.data_)) :
  2197. (absx==0x7C00) ? half(detail::binary, (!absy || (sub && absz==0x7C00)) ? detail::invalid() : (sign|0x7C00)) :
  2198. (absy==0x7C00) ? half(detail::binary, (!absx || (sub && absz==0x7C00)) ? detail::invalid() : (sign|0x7C00)) : z;
  2199. if(!absx || !absy)
  2200. return absz ? z : half(detail::binary, (half::round_style==std::round_toward_neg_infinity) ? (z.data_|sign) : (z.data_&sign));
  2201. for(; absx<0x400; absx<<=1,--exp) ;
  2202. for(; absy<0x400; absy<<=1,--exp) ;
  2203. detail::uint32 m = static_cast<detail::uint32>((absx&0x3FF)|0x400) * static_cast<detail::uint32>((absy&0x3FF)|0x400);
  2204. int i = m >> 21;
  2205. exp += (absx>>10) + (absy>>10) + i;
  2206. m <<= 3 - i;
  2207. if(absz) {
  2208. int expz = 0;
  2209. for(; absz<0x400; absz<<=1,--expz) ;
  2210. expz += absz >> 10;
  2211. detail::uint32 mz = static_cast<detail::uint32>((absz&0x3FF)|0x400) << 13;
  2212. if(expz > exp || (expz == exp && mz > m)) {
  2213. std::swap(m, mz);
  2214. std::swap(exp, expz);
  2215. if(sub)
  2216. sign = z.data_ & 0x8000;
  2217. }
  2218. int d = exp - expz;
  2219. mz = (d<23) ? ((mz>>d)|((mz&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
  2220. if(sub) {
  2221. m = m - mz;
  2222. if(!m)
  2223. return half(detail::binary, static_cast<unsigned>(half::round_style==std::round_toward_neg_infinity)<<15);
  2224. for(; m<0x800000; m<<=1,--exp) ;
  2225. } else {
  2226. m += mz;
  2227. i = m >> 24;
  2228. m = (m>>i) | (m&i);
  2229. exp += i;
  2230. }
  2231. }
  2232. if(exp > 30)
  2233. return half(detail::binary, detail::overflow<half::round_style>(sign));
  2234. else if(exp < -10)
  2235. return half(detail::binary, detail::underflow<half::round_style>(sign));
  2236. return half(detail::binary, detail::fixed2half<half::round_style,23,false,false,false>(m, exp-1, sign));
  2237. #endif
  2238. }
  2239. /// Maximum of half expressions.
  2240. /// **See also:** Documentation for [std::fmax](https://en.cppreference.com/w/cpp/numeric/math/fmax).
  2241. /// \param x first operand
  2242. /// \param y second operand
  2243. /// \return maximum of operands, ignoring quiet NaNs
  2244. /// \exception FE_INVALID if \a x or \a y is signaling NaN
  2245. inline constexpr_NOERR half fmax(half x, half y) {
  2246. return half(detail::binary, (!isnan(y) && (isnan(x) || (x.data_^(0x8000|(0x8000-(x.data_>>15)))) <
  2247. (y.data_^(0x8000|(0x8000-(y.data_>>15)))))) ? detail::select(y.data_, x.data_) : detail::select(x.data_, y.data_));
  2248. }
  2249. /// Minimum of half expressions.
  2250. /// **See also:** Documentation for [std::fmin](https://en.cppreference.com/w/cpp/numeric/math/fmin).
  2251. /// \param x first operand
  2252. /// \param y second operand
  2253. /// \return minimum of operands, ignoring quiet NaNs
  2254. /// \exception FE_INVALID if \a x or \a y is signaling NaN
  2255. inline constexpr_NOERR half fmin(half x, half y) {
  2256. return half(detail::binary, (!isnan(y) && (isnan(x) || (x.data_^(0x8000|(0x8000-(x.data_>>15)))) >
  2257. (y.data_^(0x8000|(0x8000-(y.data_>>15)))))) ? detail::select(y.data_, x.data_) : detail::select(x.data_, y.data_));
  2258. }
  2259. /// Positive difference.
  2260. /// This function is exact to rounding for all rounding modes.
  2261. ///
  2262. /// **See also:** Documentation for [std::fdim](https://en.cppreference.com/w/cpp/numeric/math/fdim).
  2263. /// \param x first operand
  2264. /// \param y second operand
  2265. /// \return \a x - \a y or 0 if difference negative
  2266. /// \exception FE_... according to operator-(half,half)
  2267. inline half fdim(half x, half y) {
  2268. if(isnan(x) || isnan(y))
  2269. return half(detail::binary, detail::signal(x.data_, y.data_));
  2270. return (x.data_^(0x8000|(0x8000-(x.data_>>15)))) <= (y.data_^(0x8000|(0x8000-(y.data_>>15)))) ? half(detail::binary, 0) : (x-y);
  2271. }
  2272. /// Get NaN value.
  2273. /// **See also:** Documentation for [std::nan](https://en.cppreference.com/w/cpp/numeric/math/nan).
  2274. /// \param arg string code
  2275. /// \return quiet NaN
  2276. inline half nanh(const char *arg) {
  2277. unsigned int value = 0x7FFF;
  2278. while(*arg)
  2279. value ^= static_cast<unsigned>(*arg++) & 0xFF;
  2280. return half(detail::binary, value);
  2281. }
  2282. /// \}
  2283. /// \anchor exponential
  2284. /// \name Exponential functions
  2285. /// \{
  2286. /// Exponential function.
  2287. /// This function is exact to rounding for all rounding modes.
  2288. ///
  2289. /// **See also:** Documentation for [std::exp](https://en.cppreference.com/w/cpp/numeric/math/exp).
  2290. /// \param arg function argument
  2291. /// \return e raised to \a arg
  2292. /// \exception FE_INVALID for signaling NaN
  2293. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2294. inline half exp(half arg) {
  2295. #ifdef HALF_ARITHMETIC_TYPE
  2296. return half(detail::binary, detail::float2half<half::round_style>(std::exp(detail::half2float<detail::internal_t>(arg.data_))));
  2297. #else
  2298. int abs = arg.data_ & 0x7FFF;
  2299. if(!abs)
  2300. return half(detail::binary, 0x3C00);
  2301. if(abs >= 0x7C00)
  2302. return half(detail::binary, (abs==0x7C00) ? (0x7C00&((arg.data_>>15)-1U)) : detail::signal(arg.data_));
  2303. if(abs >= 0x4C80)
  2304. return half(detail::binary, (arg.data_&0x8000) ? detail::underflow<half::round_style>() : detail::overflow<half::round_style>());
  2305. detail::uint32 m = detail::multiply64(static_cast<detail::uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29);
  2306. int e = (abs>>10) + (abs<=0x3FF), exp;
  2307. if(e < 14) {
  2308. exp = 0;
  2309. m >>= 14 - e;
  2310. } else {
  2311. exp = m >> (45-e);
  2312. m = (m<<(e-14)) & 0x7FFFFFFF;
  2313. }
  2314. return half(detail::binary, detail::exp2_post<half::round_style,true>(detail::exp2(m, 26), exp, (arg.data_&0x8000)!=0));
  2315. #endif
  2316. }
  2317. /// Binary exponential.
  2318. /// This function is exact to rounding for all rounding modes.
  2319. ///
  2320. /// **See also:** Documentation for [std::exp2](https://en.cppreference.com/w/cpp/numeric/math/exp2).
  2321. /// \param arg function argument
  2322. /// \return 2 raised to \a arg
  2323. /// \exception FE_INVALID for signaling NaN
  2324. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2325. inline half exp2(half arg) {
  2326. #if defined(HALF_ARITHMETIC_TYPE)
  2327. return half(detail::binary, detail::float2half<half::round_style>(std::exp2(detail::half2float<detail::internal_t>(arg.data_))));
  2328. #else
  2329. int abs = arg.data_ & 0x7FFF;
  2330. if(!abs)
  2331. return half(detail::binary, 0x3C00);
  2332. if(abs >= 0x7C00)
  2333. return half(detail::binary, (abs==0x7C00) ? (0x7C00&((arg.data_>>15)-1U)) : detail::signal(arg.data_));
  2334. if(abs >= 0x4E40)
  2335. return half(detail::binary, (arg.data_&0x8000) ? detail::underflow<half::round_style>() : detail::overflow<half::round_style>());
  2336. int e = (abs>>10) + (abs<=0x3FF), exp = (abs&0x3FF) + ((abs>0x3FF)<<10);
  2337. detail::uint32 m = detail::exp2((static_cast<detail::uint32>(exp)<<(6+e))&0x7FFFFFFF, 28);
  2338. exp >>= 25 - e;
  2339. if(m == 0x80000000) {
  2340. if(arg.data_&0x8000)
  2341. exp = -exp;
  2342. else if(exp > 15)
  2343. return half(detail::binary, detail::overflow<half::round_style>());
  2344. return half(detail::binary, detail::fixed2half<half::round_style,31,false,false,false>(m, exp+14));
  2345. }
  2346. return half(detail::binary, detail::exp2_post<half::round_style,true>(m, exp, (arg.data_&0x8000)!=0));
  2347. #endif
  2348. }
  2349. /// Exponential minus one.
  2350. /// This function may be 1 ULP off the correctly rounded exact result in <0.05% of inputs for `std::round_to_nearest`
  2351. /// and in <1% of inputs for any other rounding mode.
  2352. ///
  2353. /// **See also:** Documentation for [std::expm1](https://en.cppreference.com/w/cpp/numeric/math/expm1).
  2354. /// \param arg function argument
  2355. /// \return e raised to \a arg and subtracted by 1
  2356. /// \exception FE_INVALID for signaling NaN
  2357. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2358. inline half expm1(half arg) {
  2359. #if defined(HALF_ARITHMETIC_TYPE)
  2360. return half(detail::binary, detail::float2half<half::round_style>(std::expm1(detail::half2float<detail::internal_t>(arg.data_))));
  2361. #else
  2362. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
  2363. if(!abs)
  2364. return arg;
  2365. if(abs >= 0x7C00)
  2366. return half(detail::binary, (abs==0x7C00) ? (0x7C00+(sign>>1)) : detail::signal(arg.data_));
  2367. if(abs >= 0x4A00)
  2368. return half(detail::binary, (arg.data_&0x8000) ? detail::rounded<half::round_style,true>(0xBBFF, 1, 1) : detail::overflow<half::round_style>());
  2369. detail::uint32 m = detail::multiply64(static_cast<detail::uint32>((abs&0x3FF)+((abs>0x3FF)<<10))<<21, 0xB8AA3B29);
  2370. int e = (abs>>10) + (abs<=0x3FF), exp;
  2371. if(e < 14) {
  2372. exp = 0;
  2373. m >>= 14 - e;
  2374. } else {
  2375. exp = m >> (45-e);
  2376. m = (m<<(e-14)) & 0x7FFFFFFF;
  2377. }
  2378. m = detail::exp2(m);
  2379. if(sign) {
  2380. int s = 0;
  2381. if(m > 0x80000000) {
  2382. ++exp;
  2383. m = detail::divide64(0x80000000, m, s);
  2384. }
  2385. m = 0x80000000 - ((m>>exp)|((m&((static_cast<detail::uint32>(1)<<exp)-1))!=0)|s);
  2386. exp = 0;
  2387. } else
  2388. m -= (exp<31) ? (0x80000000>>exp) : 1;
  2389. for(exp+=14; m<0x80000000 && exp; m<<=1,--exp) ;
  2390. if(exp > 29)
  2391. return half(detail::binary, detail::overflow<half::round_style>());
  2392. return half(detail::binary, detail::rounded<half::round_style,true>(sign+(exp<<10)+(m>>21), (m>>20)&1, (m&0xFFFFF)!=0));
  2393. #endif
  2394. }
  2395. /// Natural logarithm.
  2396. /// This function is exact to rounding for all rounding modes.
  2397. ///
  2398. /// **See also:** Documentation for [std::log](https://en.cppreference.com/w/cpp/numeric/math/log).
  2399. /// \param arg function argument
  2400. /// \return logarithm of \a arg to base e
  2401. /// \exception FE_INVALID for signaling NaN or negative argument
  2402. /// \exception FE_DIVBYZERO for 0
  2403. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2404. inline half log(half arg) {
  2405. #ifdef HALF_ARITHMETIC_TYPE
  2406. return half(detail::binary, detail::float2half<half::round_style>(std::log(detail::half2float<detail::internal_t>(arg.data_))));
  2407. #else
  2408. int abs = arg.data_ & 0x7FFF, exp = -15;
  2409. if(!abs)
  2410. return half(detail::binary, detail::pole(0x8000));
  2411. if(arg.data_ & 0x8000)
  2412. return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
  2413. if(abs >= 0x7C00)
  2414. return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
  2415. for(; abs<0x400; abs<<=1,--exp) ;
  2416. exp += abs >> 10;
  2417. return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(
  2418. detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 27)+8, exp, 17));
  2419. #endif
  2420. }
  2421. /// Common logarithm.
  2422. /// This function is exact to rounding for all rounding modes.
  2423. ///
  2424. /// **See also:** Documentation for [std::log10](https://en.cppreference.com/w/cpp/numeric/math/log10).
  2425. /// \param arg function argument
  2426. /// \return logarithm of \a arg to base 10
  2427. /// \exception FE_INVALID for signaling NaN or negative argument
  2428. /// \exception FE_DIVBYZERO for 0
  2429. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2430. inline half log10(half arg) {
  2431. #ifdef HALF_ARITHMETIC_TYPE
  2432. return half(detail::binary, detail::float2half<half::round_style>(std::log10(detail::half2float<detail::internal_t>(arg.data_))));
  2433. #else
  2434. int abs = arg.data_ & 0x7FFF, exp = -15;
  2435. if(!abs)
  2436. return half(detail::binary, detail::pole(0x8000));
  2437. if(arg.data_ & 0x8000)
  2438. return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
  2439. if(abs >= 0x7C00)
  2440. return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
  2441. switch(abs) {
  2442. case 0x4900: return half(detail::binary, 0x3C00);
  2443. case 0x5640: return half(detail::binary, 0x4000);
  2444. case 0x63D0: return half(detail::binary, 0x4200);
  2445. case 0x70E2: return half(detail::binary, 0x4400);
  2446. }
  2447. for(; abs<0x400; abs<<=1,--exp) ;
  2448. exp += abs >> 10;
  2449. return half(detail::binary, detail::log2_post<half::round_style,0xD49A784C>(
  2450. detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 27)+8, exp, 16));
  2451. #endif
  2452. }
  2453. /// Binary logarithm.
  2454. /// This function is exact to rounding for all rounding modes.
  2455. ///
  2456. /// **See also:** Documentation for [std::log2](https://en.cppreference.com/w/cpp/numeric/math/log2).
  2457. /// \param arg function argument
  2458. /// \return logarithm of \a arg to base 2
  2459. /// \exception FE_INVALID for signaling NaN or negative argument
  2460. /// \exception FE_DIVBYZERO for 0
  2461. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2462. inline half log2(half arg) {
  2463. #if defined(HALF_ARITHMETIC_TYPE)
  2464. return half(detail::binary, detail::float2half<half::round_style>(std::log2(detail::half2float<detail::internal_t>(arg.data_))));
  2465. #else
  2466. int abs = arg.data_ & 0x7FFF, exp = -15, s = 0;
  2467. if(!abs)
  2468. return half(detail::binary, detail::pole(0x8000));
  2469. if(arg.data_ & 0x8000)
  2470. return half(detail::binary, (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
  2471. if(abs >= 0x7C00)
  2472. return (abs==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
  2473. if(abs == 0x3C00)
  2474. return half(detail::binary, 0);
  2475. for(; abs<0x400; abs<<=1,--exp) ;
  2476. exp += (abs>>10);
  2477. if(!(abs&0x3FF)) {
  2478. unsigned int value = static_cast<unsigned>(exp<0) << 15, m = std::abs(exp) << 6;
  2479. for(exp=18; m<0x400; m<<=1,--exp) ;
  2480. return half(detail::binary, value+(exp<<10)+m);
  2481. }
  2482. detail::uint32 ilog = exp, sign = detail::sign_mask(ilog), m =
  2483. (((ilog<<27)+(detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 28)>>4))^sign) - sign;
  2484. if(!m)
  2485. return half(detail::binary, 0);
  2486. for(exp=14; m<0x8000000 && exp; m<<=1,--exp) ;
  2487. for(; m>0xFFFFFFF; m>>=1,++exp)
  2488. s |= m & 1;
  2489. return half(detail::binary, detail::fixed2half<half::round_style,27,false,false,true>(m, exp, sign&0x8000, s));
  2490. #endif
  2491. }
  2492. /// Natural logarithm plus one.
  2493. /// This function may be 1 ULP off the correctly rounded exact result in <0.05% of inputs for `std::round_to_nearest`
  2494. /// and in ~1% of inputs for any other rounding mode.
  2495. ///
  2496. /// **See also:** Documentation for [std::log1p](https://en.cppreference.com/w/cpp/numeric/math/log1p).
  2497. /// \param arg function argument
  2498. /// \return logarithm of \a arg plus 1 to base e
  2499. /// \exception FE_INVALID for signaling NaN or argument <-1
  2500. /// \exception FE_DIVBYZERO for -1
  2501. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2502. inline half log1p(half arg) {
  2503. #if defined(HALF_ARITHMETIC_TYPE)
  2504. return half(detail::binary, detail::float2half<half::round_style>(std::log1p(detail::half2float<detail::internal_t>(arg.data_))));
  2505. #else
  2506. if(arg.data_ >= 0xBC00)
  2507. return half(detail::binary, (arg.data_==0xBC00) ? detail::pole(0x8000) : (arg.data_<=0xFC00) ? detail::invalid() : detail::signal(arg.data_));
  2508. int abs = arg.data_ & 0x7FFF, exp = -15;
  2509. if(!abs || abs >= 0x7C00)
  2510. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  2511. for(; abs<0x400; abs<<=1,--exp) ;
  2512. exp += abs >> 10;
  2513. detail::uint32 m = static_cast<detail::uint32>((abs&0x3FF)|0x400) << 20;
  2514. if(arg.data_ & 0x8000) {
  2515. m = 0x40000000 - (m>>-exp);
  2516. for(exp=0; m<0x40000000; m<<=1,--exp) ;
  2517. } else {
  2518. if(exp < 0) {
  2519. m = 0x40000000 + (m>>-exp);
  2520. exp = 0;
  2521. } else {
  2522. m += 0x40000000 >> exp;
  2523. int i = m >> 31;
  2524. m >>= i;
  2525. exp += i;
  2526. }
  2527. }
  2528. return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(detail::log2(m), exp, 17));
  2529. #endif
  2530. }
  2531. /// \}
  2532. /// \anchor power
  2533. /// \name Power functions
  2534. /// \{
  2535. /// Square root.
  2536. /// This function is exact to rounding for all rounding modes.
  2537. ///
  2538. /// **See also:** Documentation for [std::sqrt](https://en.cppreference.com/w/cpp/numeric/math/sqrt).
  2539. /// \param arg function argument
  2540. /// \return square root of \a arg
  2541. /// \exception FE_INVALID for signaling NaN and negative arguments
  2542. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2543. inline half sqrt(half arg) {
  2544. #ifdef HALF_ARITHMETIC_TYPE
  2545. return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(detail::half2float<detail::internal_t>(arg.data_))));
  2546. #else
  2547. int abs = arg.data_ & 0x7FFF, exp = 15;
  2548. if(!abs || arg.data_ >= 0x7C00)
  2549. return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (arg.data_>0x8000) ? detail::invalid() : arg.data_);
  2550. for(; abs<0x400; abs<<=1,--exp) ;
  2551. detail::uint32 r = static_cast<detail::uint32>((abs&0x3FF)|0x400) << 10, m = detail::sqrt<20>(r, exp+=abs>>10);
  2552. return half(detail::binary, detail::rounded<half::round_style,false>((exp<<10)+(m&0x3FF), r>m, r!=0));
  2553. #endif
  2554. }
  2555. /// Cubic root.
  2556. /// This function is exact to rounding for all rounding modes.
  2557. ///
  2558. /// **See also:** Documentation for [std::cbrt](https://en.cppreference.com/w/cpp/numeric/math/cbrt).
  2559. /// \param arg function argument
  2560. /// \return cubic root of \a arg
  2561. /// \exception FE_INVALID for signaling NaN
  2562. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2563. inline half cbrt(half arg) {
  2564. #if defined(HALF_ARITHMETIC_TYPE)
  2565. return half(detail::binary, detail::float2half<half::round_style>(std::cbrt(detail::half2float<detail::internal_t>(arg.data_))));
  2566. #else
  2567. int abs = arg.data_ & 0x7FFF, exp = -15;
  2568. if(!abs || abs == 0x3C00 || abs >= 0x7C00)
  2569. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  2570. for(; abs<0x400; abs<<=1, --exp);
  2571. detail::uint32 ilog = exp + (abs>>10), sign = detail::sign_mask(ilog), f, m =
  2572. (((ilog<<27)+(detail::log2(static_cast<detail::uint32>((abs&0x3FF)|0x400)<<20, 24)>>4))^sign) - sign;
  2573. for(exp=2; m<0x80000000; m<<=1,--exp) ;
  2574. m = detail::multiply64(m, 0xAAAAAAAB);
  2575. int i = m >> 31, s;
  2576. exp += i;
  2577. m <<= 1 - i;
  2578. if(exp < 0) {
  2579. f = m >> -exp;
  2580. exp = 0;
  2581. } else {
  2582. f = (m<<exp) & 0x7FFFFFFF;
  2583. exp = m >> (31-exp);
  2584. }
  2585. m = detail::exp2(f, (half::round_style==std::round_to_nearest) ? 29 : 26);
  2586. if(sign) {
  2587. if(m > 0x80000000) {
  2588. m = detail::divide64(0x80000000, m, s);
  2589. ++exp;
  2590. }
  2591. exp = -exp;
  2592. }
  2593. return half(detail::binary, (half::round_style==std::round_to_nearest) ?
  2594. detail::fixed2half<half::round_style,31,false,false,false>(m, exp+14, arg.data_&0x8000) :
  2595. detail::fixed2half<half::round_style,23,false,false,false>((m+0x80)>>8, exp+14, arg.data_&0x8000));
  2596. #endif
  2597. }
  2598. /// Hypotenuse function.
  2599. /// This function is exact to rounding for all rounding modes.
  2600. ///
  2601. /// **See also:** Documentation for [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot).
  2602. /// \param x first argument
  2603. /// \param y second argument
  2604. /// \return square root of sum of squares without internal over- or underflows
  2605. /// \exception FE_INVALID if \a x or \a y is signaling NaN
  2606. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding of the final square root
  2607. inline half hypot(half x, half y) {
  2608. #ifdef HALF_ARITHMETIC_TYPE
  2609. detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_);
  2610. return half(detail::binary, detail::float2half<half::round_style>(std::hypot(fx, fy)));
  2611. #else
  2612. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, expx = 0, expy = 0;
  2613. if(absx >= 0x7C00 || absy >= 0x7C00)
  2614. return half(detail::binary, (absx==0x7C00) ? detail::select(0x7C00, y.data_) :
  2615. (absy==0x7C00) ? detail::select(0x7C00, x.data_) : detail::signal(x.data_, y.data_));
  2616. if(!absx)
  2617. return half(detail::binary, absy ? detail::check_underflow(absy) : 0);
  2618. if(!absy)
  2619. return half(detail::binary, detail::check_underflow(absx));
  2620. if(absy > absx)
  2621. std::swap(absx, absy);
  2622. for(; absx<0x400; absx<<=1,--expx) ;
  2623. for(; absy<0x400; absy<<=1,--expy) ;
  2624. detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
  2625. mx *= mx;
  2626. my *= my;
  2627. int ix = mx >> 21, iy = my >> 21;
  2628. expx = 2*(expx+(absx>>10)) - 15 + ix;
  2629. expy = 2*(expy+(absy>>10)) - 15 + iy;
  2630. mx <<= 10 - ix;
  2631. my <<= 10 - iy;
  2632. int d = expx - expy;
  2633. my = (d<30) ? ((my>>d)|((my&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
  2634. return half(detail::binary, detail::hypot_post<half::round_style>(mx+my, expx));
  2635. #endif
  2636. }
  2637. /// Hypotenuse function.
  2638. /// This function is exact to rounding for all rounding modes.
  2639. ///
  2640. /// **See also:** Documentation for [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot).
  2641. /// \param x first argument
  2642. /// \param y second argument
  2643. /// \param z third argument
  2644. /// \return square root of sum of squares without internal over- or underflows
  2645. /// \exception FE_INVALID if \a x, \a y or \a z is signaling NaN
  2646. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding of the final square root
  2647. inline half hypot(half x, half y, half z) {
  2648. #ifdef HALF_ARITHMETIC_TYPE
  2649. detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_), fz = detail::half2float<detail::internal_t>(z.data_);
  2650. return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(fx*fx+fy*fy+fz*fz)));
  2651. #else
  2652. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, absz = z.data_ & 0x7FFF, expx = 0, expy = 0, expz = 0;
  2653. if(!absx)
  2654. return hypot(y, z);
  2655. if(!absy)
  2656. return hypot(x, z);
  2657. if(!absz)
  2658. return hypot(x, y);
  2659. if(absx >= 0x7C00 || absy >= 0x7C00 || absz >= 0x7C00)
  2660. return half(detail::binary, (absx==0x7C00) ? detail::select(0x7C00, detail::select(y.data_, z.data_)) :
  2661. (absy==0x7C00) ? detail::select(0x7C00, detail::select(x.data_, z.data_)) :
  2662. (absz==0x7C00) ? detail::select(0x7C00, detail::select(x.data_, y.data_)) :
  2663. detail::signal(x.data_, y.data_, z.data_));
  2664. if(absz > absy)
  2665. std::swap(absy, absz);
  2666. if(absy > absx)
  2667. std::swap(absx, absy);
  2668. if(absz > absy)
  2669. std::swap(absy, absz);
  2670. for(; absx<0x400; absx<<=1,--expx) ;
  2671. for(; absy<0x400; absy<<=1,--expy) ;
  2672. for(; absz<0x400; absz<<=1,--expz) ;
  2673. detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400, mz = (absz&0x3FF) | 0x400;
  2674. mx *= mx;
  2675. my *= my;
  2676. mz *= mz;
  2677. int ix = mx >> 21, iy = my >> 21, iz = mz >> 21;
  2678. expx = 2*(expx+(absx>>10)) - 15 + ix;
  2679. expy = 2*(expy+(absy>>10)) - 15 + iy;
  2680. expz = 2*(expz+(absz>>10)) - 15 + iz;
  2681. mx <<= 10 - ix;
  2682. my <<= 10 - iy;
  2683. mz <<= 10 - iz;
  2684. int d = expy - expz;
  2685. mz = (d<30) ? ((mz>>d)|((mz&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
  2686. my += mz;
  2687. if(my & 0x80000000) {
  2688. my = (my>>1) | (my&1);
  2689. if(++expy > expx) {
  2690. std::swap(mx, my);
  2691. std::swap(expx, expy);
  2692. }
  2693. }
  2694. d = expx - expy;
  2695. my = (d<30) ? ((my>>d)|((my&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
  2696. return half(detail::binary, detail::hypot_post<half::round_style>(mx+my, expx));
  2697. #endif
  2698. }
  2699. /// Power function.
  2700. /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in ~0.00025% of inputs.
  2701. ///
  2702. /// **See also:** Documentation for [std::pow](https://en.cppreference.com/w/cpp/numeric/math/pow).
  2703. /// \param x base
  2704. /// \param y exponent
  2705. /// \return \a x raised to \a y
  2706. /// \exception FE_INVALID if \a x or \a y is signaling NaN or if \a x is finite an negative and \a y is finite and not integral
  2707. /// \exception FE_DIVBYZERO if \a x is 0 and \a y is negative
  2708. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2709. inline half pow(half x, half y) {
  2710. #ifdef HALF_ARITHMETIC_TYPE
  2711. return half(detail::binary, detail::float2half<half::round_style>(std::pow(detail::half2float<detail::internal_t>(x.data_), detail::half2float<detail::internal_t>(y.data_))));
  2712. #else
  2713. int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, exp = -15;
  2714. if(!absy || x.data_ == 0x3C00)
  2715. return half(detail::binary, detail::select(0x3C00, (x.data_==0x3C00) ? y.data_ : x.data_));
  2716. bool is_int = absy >= 0x6400 || (absy>=0x3C00 && !(absy&((1<<(25-(absy>>10)))-1)));
  2717. unsigned int sign = x.data_ & (static_cast<unsigned>((absy<0x6800)&&is_int&&((absy>>(25-(absy>>10)))&1))<<15);
  2718. if(absx >= 0x7C00 || absy >= 0x7C00)
  2719. return half(detail::binary, (absx>0x7C00 || absy>0x7C00) ? detail::signal(x.data_, y.data_) :
  2720. (absy==0x7C00) ? ((absx==0x3C00) ? 0x3C00 : (!absx && y.data_==0xFC00) ? detail::pole() :
  2721. (0x7C00&-((y.data_>>15)^(absx>0x3C00)))) : (sign|(0x7C00&((y.data_>>15)-1U))));
  2722. if(!absx)
  2723. return half(detail::binary, (y.data_&0x8000) ? detail::pole(sign) : sign);
  2724. if((x.data_&0x8000) && !is_int)
  2725. return half(detail::binary, detail::invalid());
  2726. if(x.data_ == 0xBC00)
  2727. return half(detail::binary, sign|0x3C00);
  2728. if(y.data_ == 0x3800)
  2729. return sqrt(x);
  2730. if(y.data_ == 0x3C00)
  2731. return half(detail::binary, detail::check_underflow(x.data_));
  2732. if(y.data_ == 0x4000)
  2733. return x * x;
  2734. for(; absx<0x400; absx<<=1,--exp) ;
  2735. detail::uint32 ilog = exp + (absx>>10), msign = detail::sign_mask(ilog), f, m =
  2736. (((ilog<<27)+((detail::log2(static_cast<detail::uint32>((absx&0x3FF)|0x400)<<20)+8)>>4))^msign) - msign;
  2737. for(exp=-11; m<0x80000000; m<<=1,--exp) ;
  2738. for(; absy<0x400; absy<<=1,--exp) ;
  2739. m = detail::multiply64(m, static_cast<detail::uint32>((absy&0x3FF)|0x400)<<21);
  2740. int i = m >> 31;
  2741. exp += (absy>>10) + i;
  2742. m <<= 1 - i;
  2743. if(exp < 0) {
  2744. f = m >> -exp;
  2745. exp = 0;
  2746. } else {
  2747. f = (m<<exp) & 0x7FFFFFFF;
  2748. exp = m >> (31-exp);
  2749. }
  2750. return half(detail::binary, detail::exp2_post<half::round_style,false>(detail::exp2(f), exp, ((msign&1)^(y.data_>>15))!=0, sign));
  2751. #endif
  2752. }
  2753. /// \}
  2754. /// \anchor trigonometric
  2755. /// \name Trigonometric functions
  2756. /// \{
  2757. /// Compute sine and cosine simultaneously.
  2758. /// This returns the same results as sin() and cos() but is faster than calling each function individually.
  2759. ///
  2760. /// This function is exact to rounding for all rounding modes.
  2761. /// \param arg function argument
  2762. /// \param sin variable to take sine of \a arg
  2763. /// \param cos variable to take cosine of \a arg
  2764. /// \exception FE_INVALID for signaling NaN or infinity
  2765. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2766. inline void sincos(half arg, half *sin, half *cos) {
  2767. #ifdef HALF_ARITHMETIC_TYPE
  2768. detail::internal_t f = detail::half2float<detail::internal_t>(arg.data_);
  2769. *sin = half(detail::binary, detail::float2half<half::round_style>(std::sin(f)));
  2770. *cos = half(detail::binary, detail::float2half<half::round_style>(std::cos(f)));
  2771. #else
  2772. int abs = arg.data_ & 0x7FFF, sign = arg.data_ >> 15, k;
  2773. if(abs >= 0x7C00)
  2774. *sin = *cos = half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  2775. else if(!abs) {
  2776. *sin = arg;
  2777. *cos = half(detail::binary, 0x3C00);
  2778. } else if(abs < 0x2500) {
  2779. *sin = half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
  2780. *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
  2781. } else {
  2782. if(half::round_style != std::round_to_nearest) {
  2783. switch(abs) {
  2784. case 0x48B7:
  2785. *sin = half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x1D07, 1, 1));
  2786. *cos = half(detail::binary, detail::rounded<half::round_style,true>(0xBBFF, 1, 1));
  2787. return;
  2788. case 0x598C:
  2789. *sin = half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
  2790. *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x80FC, 1, 1));
  2791. return;
  2792. case 0x6A64:
  2793. *sin = half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x3BFE, 1, 1));
  2794. *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x27FF, 1, 1));
  2795. return;
  2796. case 0x6D8C:
  2797. *sin = half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x0FE6, 1, 1));
  2798. *cos = half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
  2799. return;
  2800. }
  2801. }
  2802. std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
  2803. switch(k & 3) {
  2804. case 1: sc = std::make_pair(sc.second, -sc.first); break;
  2805. case 2: sc = std::make_pair(-sc.first, -sc.second); break;
  2806. case 3: sc = std::make_pair(-sc.second, sc.first); break;
  2807. }
  2808. *sin = half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((sc.first^-static_cast<detail::uint32>(sign))+sign));
  2809. *cos = half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>(sc.second));
  2810. }
  2811. #endif
  2812. }
  2813. /// Sine function.
  2814. /// This function is exact to rounding for all rounding modes.
  2815. ///
  2816. /// **See also:** Documentation for [std::sin](https://en.cppreference.com/w/cpp/numeric/math/sin).
  2817. /// \param arg function argument
  2818. /// \return sine value of \a arg
  2819. /// \exception FE_INVALID for signaling NaN or infinity
  2820. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2821. inline half sin(half arg) {
  2822. #ifdef HALF_ARITHMETIC_TYPE
  2823. return half(detail::binary, detail::float2half<half::round_style>(std::sin(detail::half2float<detail::internal_t>(arg.data_))));
  2824. #else
  2825. int abs = arg.data_ & 0x7FFF, k;
  2826. if(!abs)
  2827. return arg;
  2828. if(abs >= 0x7C00)
  2829. return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  2830. if(abs < 0x2900)
  2831. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
  2832. if(half::round_style != std::round_to_nearest)
  2833. switch(abs) {
  2834. case 0x48B7: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x1D07, 1, 1));
  2835. case 0x6A64: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x3BFE, 1, 1));
  2836. case 0x6D8C: return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x0FE6, 1, 1));
  2837. }
  2838. std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
  2839. detail::uint32 sign = -static_cast<detail::uint32>(((k>>1)&1)^(arg.data_>>15));
  2840. return half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((((k&1) ? sc.second : sc.first)^sign) - sign));
  2841. #endif
  2842. }
  2843. /// Cosine function.
  2844. /// This function is exact to rounding for all rounding modes.
  2845. ///
  2846. /// **See also:** Documentation for [std::cos](https://en.cppreference.com/w/cpp/numeric/math/cos).
  2847. /// \param arg function argument
  2848. /// \return cosine value of \a arg
  2849. /// \exception FE_INVALID for signaling NaN or infinity
  2850. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2851. inline half cos(half arg) {
  2852. #ifdef HALF_ARITHMETIC_TYPE
  2853. return half(detail::binary, detail::float2half<half::round_style>(std::cos(detail::half2float<detail::internal_t>(arg.data_))));
  2854. #else
  2855. int abs = arg.data_ & 0x7FFF, k;
  2856. if(!abs)
  2857. return half(detail::binary, 0x3C00);
  2858. if(abs >= 0x7C00)
  2859. return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  2860. if(abs < 0x2500)
  2861. return half(detail::binary, detail::rounded<half::round_style,true>(0x3BFF, 1, 1));
  2862. if(half::round_style != std::round_to_nearest && abs == 0x598C)
  2863. return half(detail::binary, detail::rounded<half::round_style,true>(0x80FC, 1, 1));
  2864. std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 28);
  2865. detail::uint32 sign = -static_cast<detail::uint32>(((k>>1)^k)&1);
  2866. return half(detail::binary, detail::fixed2half<half::round_style,30,true,true,true>((((k&1) ? sc.first : sc.second)^sign) - sign));
  2867. #endif
  2868. }
  2869. /// Tangent function.
  2870. /// This function is exact to rounding for all rounding modes.
  2871. ///
  2872. /// **See also:** Documentation for [std::tan](https://en.cppreference.com/w/cpp/numeric/math/tan).
  2873. /// \param arg function argument
  2874. /// \return tangent value of \a arg
  2875. /// \exception FE_INVALID for signaling NaN or infinity
  2876. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2877. inline half tan(half arg) {
  2878. #ifdef HALF_ARITHMETIC_TYPE
  2879. return half(detail::binary, detail::float2half<half::round_style>(std::tan(detail::half2float<detail::internal_t>(arg.data_))));
  2880. #else
  2881. int abs = arg.data_ & 0x7FFF, exp = 13, k;
  2882. if(!abs)
  2883. return arg;
  2884. if(abs >= 0x7C00)
  2885. return half(detail::binary, (abs==0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  2886. if(abs < 0x2700)
  2887. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
  2888. if(half::round_style != std::round_to_nearest)
  2889. switch(abs) {
  2890. case 0x658C: return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x07E6, 1, 1));
  2891. case 0x7330: return half(detail::binary, detail::rounded<half::round_style,true>((~arg.data_&0x8000)|0x4B62, 1, 1));
  2892. }
  2893. std::pair<detail::uint32,detail::uint32> sc = detail::sincos(detail::angle_arg(abs, k), 30);
  2894. if(k & 1)
  2895. sc = std::make_pair(-sc.second, sc.first);
  2896. detail::uint32 signy = detail::sign_mask(sc.first), signx = detail::sign_mask(sc.second);
  2897. detail::uint32 my = (sc.first^signy) - signy, mx = (sc.second^signx) - signx;
  2898. for(; my<0x80000000; my<<=1,--exp) ;
  2899. for(; mx<0x80000000; mx<<=1,++exp) ;
  2900. return half(detail::binary, detail::tangent_post<half::round_style>(my, mx, exp, (signy^signx^arg.data_)&0x8000));
  2901. #endif
  2902. }
  2903. /// Arc sine.
  2904. /// This function is exact to rounding for all rounding modes.
  2905. ///
  2906. /// **See also:** Documentation for [std::asin](https://en.cppreference.com/w/cpp/numeric/math/asin).
  2907. /// \param arg function argument
  2908. /// \return arc sine value of \a arg
  2909. /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
  2910. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2911. inline half asin(half arg) {
  2912. #ifdef HALF_ARITHMETIC_TYPE
  2913. return half(detail::binary, detail::float2half<half::round_style>(std::asin(detail::half2float<detail::internal_t>(arg.data_))));
  2914. #else
  2915. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
  2916. if(!abs)
  2917. return arg;
  2918. if(abs >= 0x3C00)
  2919. return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (abs>0x3C00) ? detail::invalid() :
  2920. detail::rounded<half::round_style,true>(sign|0x3E48, 0, 1));
  2921. if(abs < 0x2900)
  2922. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
  2923. if(half::round_style != std::round_to_nearest && (abs == 0x2B44 || abs == 0x2DC3))
  2924. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_+1, 1, 1));
  2925. std::pair<detail::uint32,detail::uint32> sc = detail::atan2_args(abs);
  2926. detail::uint32 m = detail::atan2(sc.first, sc.second, (half::round_style==std::round_to_nearest) ? 27 : 26);
  2927. return half(detail::binary, detail::fixed2half<half::round_style,30,false,true,true>(m, 14, sign));
  2928. #endif
  2929. }
  2930. /// Arc cosine function.
  2931. /// This function is exact to rounding for all rounding modes.
  2932. ///
  2933. /// **See also:** Documentation for [std::acos](https://en.cppreference.com/w/cpp/numeric/math/acos).
  2934. /// \param arg function argument
  2935. /// \return arc cosine value of \a arg
  2936. /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
  2937. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2938. inline half acos(half arg) {
  2939. #ifdef HALF_ARITHMETIC_TYPE
  2940. return half(detail::binary, detail::float2half<half::round_style>(std::acos(detail::half2float<detail::internal_t>(arg.data_))));
  2941. #else
  2942. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ >> 15;
  2943. if(!abs)
  2944. return half(detail::binary, detail::rounded<half::round_style,true>(0x3E48, 0, 1));
  2945. if(abs >= 0x3C00)
  2946. return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (abs>0x3C00) ? detail::invalid() :
  2947. sign ? detail::rounded<half::round_style,true>(0x4248, 0, 1) : 0);
  2948. std::pair<detail::uint32,detail::uint32> cs = detail::atan2_args(abs);
  2949. detail::uint32 m = detail::atan2(cs.second, cs.first, 28);
  2950. return half(detail::binary, detail::fixed2half<half::round_style,31,false,true,true>(sign ? (0xC90FDAA2-m) : m, 15, 0, sign));
  2951. #endif
  2952. }
  2953. /// Arc tangent function.
  2954. /// This function is exact to rounding for all rounding modes.
  2955. ///
  2956. /// **See also:** Documentation for [std::atan](https://en.cppreference.com/w/cpp/numeric/math/atan).
  2957. /// \param arg function argument
  2958. /// \return arc tangent value of \a arg
  2959. /// \exception FE_INVALID for signaling NaN
  2960. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2961. inline half atan(half arg) {
  2962. #ifdef HALF_ARITHMETIC_TYPE
  2963. return half(detail::binary, detail::float2half<half::round_style>(std::atan(detail::half2float<detail::internal_t>(arg.data_))));
  2964. #else
  2965. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
  2966. if(!abs)
  2967. return arg;
  2968. if(abs >= 0x7C00)
  2969. return half(detail::binary, (abs==0x7C00) ? detail::rounded<half::round_style,true>(sign|0x3E48, 0, 1) : detail::signal(arg.data_));
  2970. if(abs <= 0x2700)
  2971. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
  2972. int exp = (abs>>10) + (abs<=0x3FF);
  2973. detail::uint32 my = (abs&0x3FF) | ((abs>0x3FF)<<10);
  2974. detail::uint32 m = (exp>15) ? detail::atan2(my<<19, 0x20000000>>(exp-15), (half::round_style==std::round_to_nearest) ? 26 : 24) :
  2975. detail::atan2(my<<(exp+4), 0x20000000, (half::round_style==std::round_to_nearest) ? 30 : 28);
  2976. return half(detail::binary, detail::fixed2half<half::round_style,30,false,true,true>(m, 14, sign));
  2977. #endif
  2978. }
  2979. /// Arc tangent function.
  2980. /// This function may be 1 ULP off the correctly rounded exact result in ~0.005% of inputs for `std::round_to_nearest`,
  2981. /// in ~0.1% of inputs for `std::round_toward_zero` and in ~0.02% of inputs for any other rounding mode.
  2982. ///
  2983. /// **See also:** Documentation for [std::atan2](https://en.cppreference.com/w/cpp/numeric/math/atan2).
  2984. /// \param y numerator
  2985. /// \param x denominator
  2986. /// \return arc tangent value
  2987. /// \exception FE_INVALID if \a x or \a y is signaling NaN
  2988. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  2989. inline half atan2(half y, half x) {
  2990. #ifdef HALF_ARITHMETIC_TYPE
  2991. return half(detail::binary, detail::float2half<half::round_style>(std::atan2(detail::half2float<detail::internal_t>(y.data_), detail::half2float<detail::internal_t>(x.data_))));
  2992. #else
  2993. unsigned int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, signx = x.data_ >> 15, signy = y.data_ & 0x8000;
  2994. if(absx >= 0x7C00 || absy >= 0x7C00) {
  2995. if(absx > 0x7C00 || absy > 0x7C00)
  2996. return half(detail::binary, detail::signal(x.data_, y.data_));
  2997. if(absy == 0x7C00)
  2998. return half(detail::binary, (absx<0x7C00) ? detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1) :
  2999. signx ? detail::rounded<half::round_style,true>(signy|0x40B6, 0, 1) :
  3000. detail::rounded<half::round_style,true>(signy|0x3A48, 0, 1));
  3001. return (x.data_==0x7C00) ? half(detail::binary, signy) : half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1));
  3002. }
  3003. if(!absy)
  3004. return signx ? half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1)) : y;
  3005. if(!absx)
  3006. return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1));
  3007. int d = (absy>>10) + (absy<=0x3FF) - (absx>>10) - (absx<=0x3FF);
  3008. if(d > (signx ? 18 : 12))
  3009. return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x3E48, 0, 1));
  3010. if(signx && d < -11)
  3011. return half(detail::binary, detail::rounded<half::round_style,true>(signy|0x4248, 0, 1));
  3012. if(!signx && d < ((half::round_style==std::round_toward_zero) ? -15 : -9)) {
  3013. for(; absy<0x400; absy<<=1,--d) ;
  3014. detail::uint32 mx = ((absx<<1)&0x7FF) | 0x800, my = ((absy<<1)&0x7FF) | 0x800;
  3015. int i = my < mx;
  3016. d -= i;
  3017. if(d < -25)
  3018. return half(detail::binary, detail::underflow<half::round_style>(signy));
  3019. my <<= 11 + i;
  3020. return half(detail::binary, detail::fixed2half<half::round_style,11,false,false,true>(my/mx, d+14, signy, my%mx!=0));
  3021. }
  3022. detail::uint32 m = detail::atan2( ((absy&0x3FF)|((absy>0x3FF)<<10))<<(19+((d<0) ? d : (d>0) ? 0 : -1)),
  3023. ((absx&0x3FF)|((absx>0x3FF)<<10))<<(19-((d>0) ? d : (d<0) ? 0 : 1)));
  3024. return half(detail::binary, detail::fixed2half<half::round_style,31,false,true,true>(signx ? (0xC90FDAA2-m) : m, 15, signy, signx));
  3025. #endif
  3026. }
  3027. /// \}
  3028. /// \anchor hyperbolic
  3029. /// \name Hyperbolic functions
  3030. /// \{
  3031. /// Hyperbolic sine.
  3032. /// This function is exact to rounding for all rounding modes.
  3033. ///
  3034. /// **See also:** Documentation for [std::sinh](https://en.cppreference.com/w/cpp/numeric/math/sinh).
  3035. /// \param arg function argument
  3036. /// \return hyperbolic sine value of \a arg
  3037. /// \exception FE_INVALID for signaling NaN
  3038. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3039. inline half sinh(half arg) {
  3040. #ifdef HALF_ARITHMETIC_TYPE
  3041. return half(detail::binary, detail::float2half<half::round_style>(std::sinh(detail::half2float<detail::internal_t>(arg.data_))));
  3042. #else
  3043. int abs = arg.data_ & 0x7FFF, exp;
  3044. if(!abs || abs >= 0x7C00)
  3045. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  3046. if(abs <= 0x2900)
  3047. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
  3048. std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, (half::round_style==std::round_to_nearest) ? 29 : 27);
  3049. detail::uint32 m = mm.first - mm.second;
  3050. for(exp+=13; m<0x80000000 && exp; m<<=1,--exp) ;
  3051. unsigned int sign = arg.data_ & 0x8000;
  3052. if(exp > 29)
  3053. return half(detail::binary, detail::overflow<half::round_style>(sign));
  3054. return half(detail::binary, detail::fixed2half<half::round_style,31,false,false,true>(m, exp, sign));
  3055. #endif
  3056. }
  3057. /// Hyperbolic cosine.
  3058. /// This function is exact to rounding for all rounding modes.
  3059. ///
  3060. /// **See also:** Documentation for [std::cosh](https://en.cppreference.com/w/cpp/numeric/math/cosh).
  3061. /// \param arg function argument
  3062. /// \return hyperbolic cosine value of \a arg
  3063. /// \exception FE_INVALID for signaling NaN
  3064. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3065. inline half cosh(half arg) {
  3066. #ifdef HALF_ARITHMETIC_TYPE
  3067. return half(detail::binary, detail::float2half<half::round_style>(std::cosh(detail::half2float<detail::internal_t>(arg.data_))));
  3068. #else
  3069. int abs = arg.data_ & 0x7FFF, exp;
  3070. if(!abs)
  3071. return half(detail::binary, 0x3C00);
  3072. if(abs >= 0x7C00)
  3073. return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : 0x7C00);
  3074. std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, (half::round_style==std::round_to_nearest) ? 23 : 26);
  3075. detail::uint32 m = mm.first + mm.second, i = (~m&0xFFFFFFFF) >> 31;
  3076. m = (m>>i) | (m&i) | 0x80000000;
  3077. if((exp+=13+i) > 29)
  3078. return half(detail::binary, detail::overflow<half::round_style>());
  3079. return half(detail::binary, detail::fixed2half<half::round_style,31,false,false,true>(m, exp));
  3080. #endif
  3081. }
  3082. /// Hyperbolic tangent.
  3083. /// This function is exact to rounding for all rounding modes.
  3084. ///
  3085. /// **See also:** Documentation for [std::tanh](https://en.cppreference.com/w/cpp/numeric/math/tanh).
  3086. /// \param arg function argument
  3087. /// \return hyperbolic tangent value of \a arg
  3088. /// \exception FE_INVALID for signaling NaN
  3089. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3090. inline half tanh(half arg) {
  3091. #ifdef HALF_ARITHMETIC_TYPE
  3092. return half(detail::binary, detail::float2half<half::round_style>(std::tanh(detail::half2float<detail::internal_t>(arg.data_))));
  3093. #else
  3094. int abs = arg.data_ & 0x7FFF, exp;
  3095. if(!abs)
  3096. return arg;
  3097. if(abs >= 0x7C00)
  3098. return half(detail::binary, (abs>0x7C00) ? detail::signal(arg.data_) : (arg.data_-0x4000));
  3099. if(abs >= 0x4500)
  3100. return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
  3101. if(abs < 0x2700)
  3102. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
  3103. if(half::round_style != std::round_to_nearest && abs == 0x2D3F)
  3104. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-3, 0, 1));
  3105. std::pair<detail::uint32,detail::uint32> mm = detail::hyperbolic_args(abs, exp, 27);
  3106. detail::uint32 my = mm.first - mm.second - (half::round_style!=std::round_to_nearest), mx = mm.first + mm.second, i = (~mx&0xFFFFFFFF) >> 31;
  3107. for(exp=13; my<0x80000000; my<<=1,--exp) ;
  3108. mx = (mx>>i) | 0x80000000;
  3109. return half(detail::binary, detail::tangent_post<half::round_style>(my, mx, exp-i, arg.data_&0x8000));
  3110. #endif
  3111. }
  3112. /// Hyperbolic area sine.
  3113. /// This function is exact to rounding for all rounding modes.
  3114. ///
  3115. /// **See also:** Documentation for [std::asinh](https://en.cppreference.com/w/cpp/numeric/math/asinh).
  3116. /// \param arg function argument
  3117. /// \return area sine value of \a arg
  3118. /// \exception FE_INVALID for signaling NaN
  3119. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3120. inline half asinh(half arg) {
  3121. #if defined(HALF_ARITHMETIC_TYPE)
  3122. return half(detail::binary, detail::float2half<half::round_style>(std::asinh(detail::half2float<detail::internal_t>(arg.data_))));
  3123. #else
  3124. int abs = arg.data_ & 0x7FFF;
  3125. if(!abs || abs >= 0x7C00)
  3126. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  3127. if(abs <= 0x2900)
  3128. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-1, 1, 1));
  3129. if(half::round_style != std::round_to_nearest)
  3130. switch(abs)
  3131. {
  3132. case 0x32D4: return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-13, 1, 1));
  3133. case 0x3B5B: return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_-197, 1, 1));
  3134. }
  3135. return half(detail::binary, detail::area<half::round_style,true>(arg.data_));
  3136. #endif
  3137. }
  3138. /// Hyperbolic area cosine.
  3139. /// This function is exact to rounding for all rounding modes.
  3140. ///
  3141. /// **See also:** Documentation for [std::acosh](https://en.cppreference.com/w/cpp/numeric/math/acosh).
  3142. /// \param arg function argument
  3143. /// \return area cosine value of \a arg
  3144. /// \exception FE_INVALID for signaling NaN or arguments <1
  3145. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3146. inline half acosh(half arg) {
  3147. #if defined(HALF_ARITHMETIC_TYPE)
  3148. return half(detail::binary, detail::float2half<half::round_style>(std::acosh(detail::half2float<detail::internal_t>(arg.data_))));
  3149. #else
  3150. int abs = arg.data_ & 0x7FFF;
  3151. if((arg.data_&0x8000) || abs < 0x3C00)
  3152. return half(detail::binary, (abs<=0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  3153. if(abs == 0x3C00)
  3154. return half(detail::binary, 0);
  3155. if(arg.data_ >= 0x7C00)
  3156. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  3157. return half(detail::binary, detail::area<half::round_style,false>(arg.data_));
  3158. #endif
  3159. }
  3160. /// Hyperbolic area tangent.
  3161. /// This function is exact to rounding for all rounding modes.
  3162. ///
  3163. /// **See also:** Documentation for [std::atanh](https://en.cppreference.com/w/cpp/numeric/math/atanh).
  3164. /// \param arg function argument
  3165. /// \return area tangent value of \a arg
  3166. /// \exception FE_INVALID for signaling NaN or if abs(\a arg) > 1
  3167. /// \exception FE_DIVBYZERO for +/-1
  3168. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3169. inline half atanh(half arg) {
  3170. #if defined(HALF_ARITHMETIC_TYPE)
  3171. return half(detail::binary, detail::float2half<half::round_style>(std::atanh(detail::half2float<detail::internal_t>(arg.data_))));
  3172. #else
  3173. int abs = arg.data_ & 0x7FFF, exp = 0;
  3174. if(!abs)
  3175. return arg;
  3176. if(abs >= 0x3C00)
  3177. return half(detail::binary, (abs==0x3C00) ? detail::pole(arg.data_&0x8000) : (abs<=0x7C00) ? detail::invalid() : detail::signal(arg.data_));
  3178. if(abs < 0x2700)
  3179. return half(detail::binary, detail::rounded<half::round_style,true>(arg.data_, 0, 1));
  3180. detail::uint32 m = static_cast<detail::uint32>((abs&0x3FF)|((abs>0x3FF)<<10)) << ((abs>>10)+(abs<=0x3FF)+6), my = 0x80000000 + m, mx = 0x80000000 - m;
  3181. for(; mx<0x80000000; mx<<=1,++exp) ;
  3182. int i = my >= mx, s;
  3183. return half(detail::binary, detail::log2_post<half::round_style,0xB8AA3B2A>(detail::log2(
  3184. (detail::divide64(my>>i, mx, s)+1)>>1, 27)+0x10, exp+i-1, 16, arg.data_&0x8000));
  3185. #endif
  3186. }
  3187. /// \}
  3188. /// \anchor special
  3189. /// \name Error and gamma functions
  3190. /// \{
  3191. /// Error function.
  3192. /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.5% of inputs.
  3193. ///
  3194. /// **See also:** Documentation for [std::erf](https://en.cppreference.com/w/cpp/numeric/math/erf).
  3195. /// \param arg function argument
  3196. /// \return error function value of \a arg
  3197. /// \exception FE_INVALID for signaling NaN
  3198. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3199. inline half erf(half arg) {
  3200. #if defined(HALF_ARITHMETIC_TYPE)
  3201. return half(detail::binary, detail::float2half<half::round_style>(std::erf(detail::half2float<detail::internal_t>(arg.data_))));
  3202. #else
  3203. unsigned int abs = arg.data_ & 0x7FFF;
  3204. if(!abs || abs >= 0x7C00)
  3205. return (abs>=0x7C00) ? half(detail::binary, (abs==0x7C00) ? (arg.data_-0x4000) : detail::signal(arg.data_)) : arg;
  3206. if(abs >= 0x4200)
  3207. return half(detail::binary, detail::rounded<half::round_style,true>((arg.data_&0x8000)|0x3BFF, 1, 1));
  3208. return half(detail::binary, detail::erf<half::round_style,false>(arg.data_));
  3209. #endif
  3210. }
  3211. /// Complementary error function.
  3212. /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.5% of inputs.
  3213. ///
  3214. /// **See also:** Documentation for [std::erfc](https://en.cppreference.com/w/cpp/numeric/math/erfc).
  3215. /// \param arg function argument
  3216. /// \return 1 minus error function value of \a arg
  3217. /// \exception FE_INVALID for signaling NaN
  3218. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3219. inline half erfc(half arg) {
  3220. #if defined(HALF_ARITHMETIC_TYPE)
  3221. return half(detail::binary, detail::float2half<half::round_style>(std::erfc(detail::half2float<detail::internal_t>(arg.data_))));
  3222. #else
  3223. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
  3224. if(abs >= 0x7C00)
  3225. return (abs>=0x7C00) ? half(detail::binary, (abs==0x7C00) ? (sign>>1) : detail::signal(arg.data_)) : arg;
  3226. if(!abs)
  3227. return half(detail::binary, 0x3C00);
  3228. if(abs >= 0x4400)
  3229. return half(detail::binary, detail::rounded<half::round_style,true>((sign>>1)-(sign>>15), sign>>15, 1));
  3230. return half(detail::binary, detail::erf<half::round_style,true>(arg.data_));
  3231. #endif
  3232. }
  3233. /// Natural logarithm of gamma function.
  3234. /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in ~0.025% of inputs.
  3235. ///
  3236. /// **See also:** Documentation for [std::lgamma](https://en.cppreference.com/w/cpp/numeric/math/lgamma).
  3237. /// \param arg function argument
  3238. /// \return natural logarith of gamma function for \a arg
  3239. /// \exception FE_INVALID for signaling NaN
  3240. /// \exception FE_DIVBYZERO for 0 or negative integer arguments
  3241. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3242. inline half lgamma(half arg) {
  3243. #if defined(HALF_ARITHMETIC_TYPE)
  3244. return half(detail::binary, detail::float2half<half::round_style>(std::lgamma(detail::half2float<detail::internal_t>(arg.data_))));
  3245. #else
  3246. int abs = arg.data_ & 0x7FFF;
  3247. if(abs >= 0x7C00)
  3248. return half(detail::binary, (abs==0x7C00) ? 0x7C00 : detail::signal(arg.data_));
  3249. if(!abs || arg.data_ >= 0xE400 || (arg.data_ >= 0xBC00 && !(abs&((1<<(25-(abs>>10)))-1))))
  3250. return half(detail::binary, detail::pole());
  3251. if(arg.data_ == 0x3C00 || arg.data_ == 0x4000)
  3252. return half(detail::binary, 0);
  3253. return half(detail::binary, detail::gamma<half::round_style,true>(arg.data_));
  3254. #endif
  3255. }
  3256. /// Gamma function.
  3257. /// This function may be 1 ULP off the correctly rounded exact result for any rounding mode in <0.25% of inputs.
  3258. ///
  3259. /// **See also:** Documentation for [std::tgamma](https://en.cppreference.com/w/cpp/numeric/math/tgamma).
  3260. /// \param arg function argument
  3261. /// \return gamma function value of \a arg
  3262. /// \exception FE_INVALID for signaling NaN, negative infinity or negative integer arguments
  3263. /// \exception FE_DIVBYZERO for 0
  3264. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3265. inline half tgamma(half arg) {
  3266. #if defined(HALF_ARITHMETIC_TYPE)
  3267. return half(detail::binary, detail::float2half<half::round_style>(std::tgamma(detail::half2float<detail::internal_t>(arg.data_))));
  3268. #else
  3269. unsigned int abs = arg.data_ & 0x7FFF;
  3270. if(!abs)
  3271. return half(detail::binary, detail::pole(arg.data_));
  3272. if(abs >= 0x7C00)
  3273. return (arg.data_==0x7C00) ? arg : half(detail::binary, detail::signal(arg.data_));
  3274. if(arg.data_ >= 0xE400 || (arg.data_ >= 0xBC00 && !(abs&((1<<(25-(abs>>10)))-1))))
  3275. return half(detail::binary, detail::invalid());
  3276. if(arg.data_ >= 0xCA80)
  3277. return half(detail::binary, detail::underflow<half::round_style>((1-((abs>>(25-(abs>>10)))&1))<<15));
  3278. if(arg.data_ <= 0x100 || (arg.data_ >= 0x4900 && arg.data_ < 0x8000))
  3279. return half(detail::binary, detail::overflow<half::round_style>());
  3280. if(arg.data_ == 0x3C00)
  3281. return arg;
  3282. return half(detail::binary, detail::gamma<half::round_style,false>(arg.data_));
  3283. #endif
  3284. }
  3285. /// \}
  3286. /// \anchor rounding
  3287. /// \name Rounding
  3288. /// \{
  3289. /// Nearest integer not less than half value.
  3290. /// **See also:** Documentation for [std::ceil](https://en.cppreference.com/w/cpp/numeric/math/ceil).
  3291. /// \param arg half to round
  3292. /// \return nearest integer not less than \a arg
  3293. /// \exception FE_INVALID for signaling NaN
  3294. /// \exception FE_INEXACT if value had to be rounded
  3295. inline half ceil(half arg) { return half(detail::binary, detail::integral<std::round_toward_infinity,true,true>(arg.data_)); }
  3296. /// Nearest integer not greater than half value.
  3297. /// **See also:** Documentation for [std::floor](https://en.cppreference.com/w/cpp/numeric/math/floor).
  3298. /// \param arg half to round
  3299. /// \return nearest integer not greater than \a arg
  3300. /// \exception FE_INVALID for signaling NaN
  3301. /// \exception FE_INEXACT if value had to be rounded
  3302. inline half floor(half arg) { return half(detail::binary, detail::integral<std::round_toward_neg_infinity,true,true>(arg.data_)); }
  3303. /// Nearest integer not greater in magnitude than half value.
  3304. /// **See also:** Documentation for [std::trunc](https://en.cppreference.com/w/cpp/numeric/math/trunc).
  3305. /// \param arg half to round
  3306. /// \return nearest integer not greater in magnitude than \a arg
  3307. /// \exception FE_INVALID for signaling NaN
  3308. /// \exception FE_INEXACT if value had to be rounded
  3309. inline half trunc(half arg) { return half(detail::binary, detail::integral<std::round_toward_zero,true,true>(arg.data_)); }
  3310. /// Nearest integer.
  3311. /// **See also:** Documentation for [std::round](https://en.cppreference.com/w/cpp/numeric/math/round).
  3312. /// \param arg half to round
  3313. /// \return nearest integer, rounded away from zero in half-way cases
  3314. /// \exception FE_INVALID for signaling NaN
  3315. /// \exception FE_INEXACT if value had to be rounded
  3316. inline half round(half arg) { return half(detail::binary, detail::integral<std::round_to_nearest,false,true>(arg.data_)); }
  3317. /// Nearest integer.
  3318. /// **See also:** Documentation for [std::lround](https://en.cppreference.com/w/cpp/numeric/math/round).
  3319. /// \param arg half to round
  3320. /// \return nearest integer, rounded away from zero in half-way cases
  3321. /// \exception FE_INVALID if value is not representable as `long`
  3322. inline long lround(half arg) { return detail::half2int<std::round_to_nearest,false,false,long>(arg.data_); }
  3323. /// Nearest integer using half's internal rounding mode.
  3324. /// **See also:** Documentation for [std::rint](https://en.cppreference.com/w/cpp/numeric/math/rint).
  3325. /// \param arg half expression to round
  3326. /// \return nearest integer using default rounding mode
  3327. /// \exception FE_INVALID for signaling NaN
  3328. /// \exception FE_INEXACT if value had to be rounded
  3329. inline half rint(half arg) { return half(detail::binary, detail::integral<half::round_style,true,true>(arg.data_)); }
  3330. /// Nearest integer using half's internal rounding mode.
  3331. /// **See also:** Documentation for [std::lrint](https://en.cppreference.com/w/cpp/numeric/math/rint).
  3332. /// \param arg half expression to round
  3333. /// \return nearest integer using default rounding mode
  3334. /// \exception FE_INVALID if value is not representable as `long`
  3335. /// \exception FE_INEXACT if value had to be rounded
  3336. inline long lrint(half arg) { return detail::half2int<half::round_style,true,true,long>(arg.data_); }
  3337. /// Nearest integer using half's internal rounding mode.
  3338. /// **See also:** Documentation for [std::nearbyint](https://en.cppreference.com/w/cpp/numeric/math/nearbyint).
  3339. /// \param arg half expression to round
  3340. /// \return nearest integer using default rounding mode
  3341. /// \exception FE_INVALID for signaling NaN
  3342. inline half nearbyint(half arg) { return half(detail::binary, detail::integral<half::round_style,true,false>(arg.data_)); }
  3343. /// Nearest integer.
  3344. /// **See also:** Documentation for [std::llround](https://en.cppreference.com/w/cpp/numeric/math/round).
  3345. /// \param arg half to round
  3346. /// \return nearest integer, rounded away from zero in half-way cases
  3347. /// \exception FE_INVALID if value is not representable as `long long`
  3348. inline long long llround(half arg) { return detail::half2int<std::round_to_nearest,false,false,long long>(arg.data_); }
  3349. /// Nearest integer using half's internal rounding mode.
  3350. /// **See also:** Documentation for [std::llrint](https://en.cppreference.com/w/cpp/numeric/math/rint).
  3351. /// \param arg half expression to round
  3352. /// \return nearest integer using default rounding mode
  3353. /// \exception FE_INVALID if value is not representable as `long long`
  3354. /// \exception FE_INEXACT if value had to be rounded
  3355. inline long long llrint(half arg) { return detail::half2int<half::round_style,true,true,long long>(arg.data_); }
  3356. /// \}
  3357. /// \anchor float
  3358. /// \name Floating point manipulation
  3359. /// \{
  3360. /// Decompress floating-point number.
  3361. /// **See also:** Documentation for [std::frexp](https://en.cppreference.com/w/cpp/numeric/math/frexp).
  3362. /// \param arg number to decompress
  3363. /// \param exp address to store exponent at
  3364. /// \return significant in range [0.5, 1)
  3365. /// \exception FE_INVALID for signaling NaN
  3366. inline half frexp(half arg, int *exp) {
  3367. *exp = 0;
  3368. unsigned int abs = arg.data_ & 0x7FFF;
  3369. if(abs >= 0x7C00 || !abs)
  3370. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  3371. for(; abs<0x400; abs<<=1,--*exp) ;
  3372. *exp += (abs>>10) - 14;
  3373. return half(detail::binary, (arg.data_&0x8000)|0x3800|(abs&0x3FF));
  3374. }
  3375. /// Multiply by power of two.
  3376. /// This function is exact to rounding for all rounding modes.
  3377. ///
  3378. /// **See also:** Documentation for [std::scalbln](https://en.cppreference.com/w/cpp/numeric/math/scalbn).
  3379. /// \param arg number to modify
  3380. /// \param exp power of two to multiply with
  3381. /// \return \a arg multplied by 2 raised to \a exp
  3382. /// \exception FE_INVALID for signaling NaN
  3383. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3384. inline half scalbln(half arg, long exp) {
  3385. unsigned int abs = arg.data_ & 0x7FFF, sign = arg.data_ & 0x8000;
  3386. if(abs >= 0x7C00 || !abs)
  3387. return (abs>0x7C00) ? half(detail::binary, detail::signal(arg.data_)) : arg;
  3388. for(; abs<0x400; abs<<=1,--exp) ;
  3389. exp += abs >> 10;
  3390. if(exp > 30)
  3391. return half(detail::binary, detail::overflow<half::round_style>(sign));
  3392. else if(exp < -10)
  3393. return half(detail::binary, detail::underflow<half::round_style>(sign));
  3394. else if(exp > 0)
  3395. return half(detail::binary, sign|(exp<<10)|(abs&0x3FF));
  3396. unsigned int m = (abs&0x3FF) | 0x400;
  3397. return half(detail::binary, detail::rounded<half::round_style,false>(sign|(m>>(1-exp)), (m>>-exp)&1, (m&((1<<-exp)-1))!=0));
  3398. }
  3399. /// Multiply by power of two.
  3400. /// This function is exact to rounding for all rounding modes.
  3401. ///
  3402. /// **See also:** Documentation for [std::scalbn](https://en.cppreference.com/w/cpp/numeric/math/scalbn).
  3403. /// \param arg number to modify
  3404. /// \param exp power of two to multiply with
  3405. /// \return \a arg multplied by 2 raised to \a exp
  3406. /// \exception FE_INVALID for signaling NaN
  3407. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3408. inline half scalbn(half arg, int exp) { return scalbln(arg, exp); }
  3409. /// Multiply by power of two.
  3410. /// This function is exact to rounding for all rounding modes.
  3411. ///
  3412. /// **See also:** Documentation for [std::ldexp](https://en.cppreference.com/w/cpp/numeric/math/ldexp).
  3413. /// \param arg number to modify
  3414. /// \param exp power of two to multiply with
  3415. /// \return \a arg multplied by 2 raised to \a exp
  3416. /// \exception FE_INVALID for signaling NaN
  3417. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3418. inline half ldexp(half arg, int exp) { return scalbln(arg, exp); }
  3419. /// Extract integer and fractional parts.
  3420. /// **See also:** Documentation for [std::modf](https://en.cppreference.com/w/cpp/numeric/math/modf).
  3421. /// \param arg number to decompress
  3422. /// \param iptr address to store integer part at
  3423. /// \return fractional part
  3424. /// \exception FE_INVALID for signaling NaN
  3425. inline half modf(half arg, half *iptr) {
  3426. unsigned int abs = arg.data_ & 0x7FFF;
  3427. if(abs > 0x7C00) {
  3428. arg = half(detail::binary, detail::signal(arg.data_));
  3429. return *iptr = arg, arg;
  3430. }
  3431. if(abs >= 0x6400)
  3432. return *iptr = arg, half(detail::binary, arg.data_&0x8000);
  3433. if(abs < 0x3C00)
  3434. return iptr->data_ = arg.data_ & 0x8000, arg;
  3435. unsigned int exp = abs >> 10, mask = (1<<(25-exp)) - 1, m = arg.data_ & mask;
  3436. iptr->data_ = arg.data_ & ~mask;
  3437. if(!m)
  3438. return half(detail::binary, arg.data_&0x8000);
  3439. for(; m<0x400; m<<=1,--exp) ;
  3440. return half(detail::binary, (arg.data_&0x8000)|(exp<<10)|(m&0x3FF));
  3441. }
  3442. /// Extract exponent.
  3443. /// **See also:** Documentation for [std::ilogb](https://en.cppreference.com/w/cpp/numeric/math/ilogb).
  3444. /// \param arg number to query
  3445. /// \return floating-point exponent
  3446. /// \retval FP_ILOGB0 for zero
  3447. /// \retval FP_ILOGBNAN for NaN
  3448. /// \retval INT_MAX for infinity
  3449. /// \exception FE_INVALID for 0 or infinite values
  3450. inline int ilogb(half arg) {
  3451. int abs = arg.data_ & 0x7FFF, exp;
  3452. if(!abs || abs >= 0x7C00) {
  3453. detail::raise(FE_INVALID);
  3454. return !abs ? FP_ILOGB0 : (abs==0x7C00) ? INT_MAX : FP_ILOGBNAN;
  3455. }
  3456. for(exp=(abs>>10)-15; abs<0x200; abs<<=1,--exp) ;
  3457. return exp;
  3458. }
  3459. /// Extract exponent.
  3460. /// **See also:** Documentation for [std::logb](https://en.cppreference.com/w/cpp/numeric/math/logb).
  3461. /// \param arg number to query
  3462. /// \return floating-point exponent
  3463. /// \exception FE_INVALID for signaling NaN
  3464. /// \exception FE_DIVBYZERO for 0
  3465. inline half logb(half arg) {
  3466. int abs = arg.data_ & 0x7FFF, exp;
  3467. if(!abs)
  3468. return half(detail::binary, detail::pole(0x8000));
  3469. if(abs >= 0x7C00)
  3470. return half(detail::binary, (abs==0x7C00) ? 0x7C00 : detail::signal(arg.data_));
  3471. for(exp=(abs>>10)-15; abs<0x200; abs<<=1,--exp) ;
  3472. unsigned int value = static_cast<unsigned>(exp<0) << 15;
  3473. if(exp) {
  3474. unsigned int m = std::abs(exp) << 6;
  3475. for(exp=18; m<0x400; m<<=1,--exp) ;
  3476. value |= (exp<<10) + m;
  3477. }
  3478. return half(detail::binary, value);
  3479. }
  3480. /// Next representable value.
  3481. /// **See also:** Documentation for [std::nextafter](https://en.cppreference.com/w/cpp/numeric/math/nextafter).
  3482. /// \param from value to compute next representable value for
  3483. /// \param to direction towards which to compute next value
  3484. /// \return next representable value after \a from in direction towards \a to
  3485. /// \exception FE_INVALID for signaling NaN
  3486. /// \exception FE_OVERFLOW for infinite result from finite argument
  3487. /// \exception FE_UNDERFLOW for subnormal result
  3488. inline half nextafter(half from, half to) {
  3489. int fabs = from.data_ & 0x7FFF, tabs = to.data_ & 0x7FFF;
  3490. if(fabs > 0x7C00 || tabs > 0x7C00)
  3491. return half(detail::binary, detail::signal(from.data_, to.data_));
  3492. if(from.data_ == to.data_ || !(fabs|tabs))
  3493. return to;
  3494. if(!fabs) {
  3495. detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT);
  3496. return half(detail::binary, (to.data_&0x8000)+1);
  3497. }
  3498. unsigned int out = from.data_ + (((from.data_>>15)^static_cast<unsigned>(
  3499. (from.data_^(0x8000|(0x8000-(from.data_>>15))))<(to.data_^(0x8000|(0x8000-(to.data_>>15))))))<<1) - 1;
  3500. detail::raise(FE_OVERFLOW, fabs<0x7C00 && (out&0x7C00)==0x7C00);
  3501. detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT && (out&0x7C00)<0x400);
  3502. return half(detail::binary, out);
  3503. }
  3504. /// Next representable value.
  3505. /// **See also:** Documentation for [std::nexttoward](https://en.cppreference.com/w/cpp/numeric/math/nexttoward).
  3506. /// \param from value to compute next representable value for
  3507. /// \param to direction towards which to compute next value
  3508. /// \return next representable value after \a from in direction towards \a to
  3509. /// \exception FE_INVALID for signaling NaN
  3510. /// \exception FE_OVERFLOW for infinite result from finite argument
  3511. /// \exception FE_UNDERFLOW for subnormal result
  3512. inline half nexttoward(half from, long double to) {
  3513. int fabs = from.data_ & 0x7FFF;
  3514. if(fabs > 0x7C00)
  3515. return half(detail::binary, detail::signal(from.data_));
  3516. long double lfrom = static_cast<long double>(from);
  3517. if(detail::builtin_isnan(to) || lfrom == to)
  3518. return half(static_cast<float>(to));
  3519. if(!fabs) {
  3520. detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT);
  3521. return half(detail::binary, (static_cast<unsigned>(detail::builtin_signbit(to))<<15)+1);
  3522. }
  3523. unsigned int out = from.data_ + (((from.data_>>15)^static_cast<unsigned>(lfrom<to))<<1) - 1;
  3524. detail::raise(FE_OVERFLOW, (out&0x7FFF)==0x7C00);
  3525. detail::raise(FE_UNDERFLOW, !HALF_ERRHANDLING_UNDERFLOW_TO_INEXACT && (out&0x7FFF)<0x400);
  3526. return half(detail::binary, out);
  3527. }
  3528. /// Take sign.
  3529. /// **See also:** Documentation for [std::copysign](https://en.cppreference.com/w/cpp/numeric/math/copysign).
  3530. /// \param x value to change sign for
  3531. /// \param y value to take sign from
  3532. /// \return value equal to \a x in magnitude and to \a y in sign
  3533. inline constexpr half copysign(half x, half y) { return half(detail::binary, x.data_^((x.data_^y.data_)&0x8000)); }
  3534. /// \}
  3535. /// \anchor classification
  3536. /// \name Floating point classification
  3537. /// \{
  3538. /// Classify floating-point value.
  3539. /// **See also:** Documentation for [std::fpclassify](https://en.cppreference.com/w/cpp/numeric/math/fpclassify).
  3540. /// \param arg number to classify
  3541. /// \retval FP_ZERO for positive and negative zero
  3542. /// \retval FP_SUBNORMAL for subnormal numbers
  3543. /// \retval FP_INFINITY for positive and negative infinity
  3544. /// \retval FP_NAN for NaNs
  3545. /// \retval FP_NORMAL for all other (normal) values
  3546. inline constexpr int fpclassify(half arg) {
  3547. return !(arg.data_&0x7FFF) ? FP_ZERO :
  3548. ((arg.data_&0x7FFF)<0x400) ? FP_SUBNORMAL :
  3549. ((arg.data_&0x7FFF)<0x7C00) ? FP_NORMAL :
  3550. ((arg.data_&0x7FFF)==0x7C00) ? FP_INFINITE :
  3551. FP_NAN;
  3552. }
  3553. /// Check if finite number.
  3554. /// **See also:** Documentation for [std::isfinite](https://en.cppreference.com/w/cpp/numeric/math/isfinite).
  3555. /// \param arg number to check
  3556. /// \retval true if neither infinity nor NaN
  3557. /// \retval false else
  3558. inline constexpr bool isfinite(half arg) { return (arg.data_&0x7C00) != 0x7C00; }
  3559. /// Check for infinity.
  3560. /// **See also:** Documentation for [std::isinf](https://en.cppreference.com/w/cpp/numeric/math/isinf).
  3561. /// \param arg number to check
  3562. /// \retval true for positive or negative infinity
  3563. /// \retval false else
  3564. inline constexpr bool isinf(half arg) { return (arg.data_&0x7FFF) == 0x7C00; }
  3565. /// Check for NaN.
  3566. /// **See also:** Documentation for [std::isnan](https://en.cppreference.com/w/cpp/numeric/math/isnan).
  3567. /// \param arg number to check
  3568. /// \retval true for NaNs
  3569. /// \retval false else
  3570. inline constexpr bool isnan(half arg) { return (arg.data_&0x7FFF) > 0x7C00; }
  3571. /// Check if normal number.
  3572. /// **See also:** Documentation for [std::isnormal](https://en.cppreference.com/w/cpp/numeric/math/isnormal).
  3573. /// \param arg number to check
  3574. /// \retval true if normal number
  3575. /// \retval false if either subnormal, zero, infinity or NaN
  3576. inline constexpr bool isnormal(half arg) { return ((arg.data_&0x7C00)!=0) & ((arg.data_&0x7C00)!=0x7C00); }
  3577. /// Check sign.
  3578. /// **See also:** Documentation for [std::signbit](https://en.cppreference.com/w/cpp/numeric/math/signbit).
  3579. /// \param arg number to check
  3580. /// \retval true for negative number
  3581. /// \retval false for positive number
  3582. inline constexpr bool signbit(half arg) { return (arg.data_&0x8000) != 0; }
  3583. /// \}
  3584. /// \anchor compfunc
  3585. /// \name Comparison
  3586. /// \{
  3587. /// Quiet comparison for greater than.
  3588. /// **See also:** Documentation for [std::isgreater](https://en.cppreference.com/w/cpp/numeric/math/isgreater).
  3589. /// \param x first operand
  3590. /// \param y second operand
  3591. /// \retval true if \a x greater than \a y
  3592. /// \retval false else
  3593. inline constexpr bool isgreater(half x, half y) {
  3594. return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) > ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
  3595. }
  3596. /// Quiet comparison for greater equal.
  3597. /// **See also:** Documentation for [std::isgreaterequal](https://en.cppreference.com/w/cpp/numeric/math/isgreaterequal).
  3598. /// \param x first operand
  3599. /// \param y second operand
  3600. /// \retval true if \a x greater equal \a y
  3601. /// \retval false else
  3602. inline constexpr bool isgreaterequal(half x, half y) {
  3603. return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) >= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
  3604. }
  3605. /// Quiet comparison for less than.
  3606. /// **See also:** Documentation for [std::isless](https://en.cppreference.com/w/cpp/numeric/math/isless).
  3607. /// \param x first operand
  3608. /// \param y second operand
  3609. /// \retval true if \a x less than \a y
  3610. /// \retval false else
  3611. inline constexpr bool isless(half x, half y) {
  3612. return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) < ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
  3613. }
  3614. /// Quiet comparison for less equal.
  3615. /// **See also:** Documentation for [std::islessequal](https://en.cppreference.com/w/cpp/numeric/math/islessequal).
  3616. /// \param x first operand
  3617. /// \param y second operand
  3618. /// \retval true if \a x less equal \a y
  3619. /// \retval false else
  3620. inline constexpr bool islessequal(half x, half y) {
  3621. return ((x.data_^(0x8000|(0x8000-(x.data_>>15))))+(x.data_>>15)) <= ((y.data_^(0x8000|(0x8000-(y.data_>>15))))+(y.data_>>15)) && !isnan(x) && !isnan(y);
  3622. }
  3623. /// Quiet comarison for less or greater.
  3624. /// **See also:** Documentation for [std::islessgreater](https://en.cppreference.com/w/cpp/numeric/math/islessgreater).
  3625. /// \param x first operand
  3626. /// \param y second operand
  3627. /// \retval true if either less or greater
  3628. /// \retval false else
  3629. inline constexpr bool islessgreater(half x, half y) {
  3630. return x.data_!=y.data_ && ((x.data_|y.data_)&0x7FFF) && !isnan(x) && !isnan(y);
  3631. }
  3632. /// Quiet check if unordered.
  3633. /// **See also:** Documentation for [std::isunordered](https://en.cppreference.com/w/cpp/numeric/math/isunordered).
  3634. /// \param x first operand
  3635. /// \param y second operand
  3636. /// \retval true if unordered (one or two NaN operands)
  3637. /// \retval false else
  3638. inline constexpr bool isunordered(half x, half y) { return isnan(x) || isnan(y); }
  3639. /// \}
  3640. /// \anchor casting
  3641. /// \name Casting
  3642. /// \{
  3643. /// Cast to or from half-precision floating-point number.
  3644. /// This casts between [half](\ref half_float::half) and any built-in arithmetic type. The values are converted
  3645. /// directly using the default rounding mode, without any roundtrip over `float` that a `static_cast` would otherwise do.
  3646. ///
  3647. /// Using this cast with neither of the two types being a [half](\ref half_float::half) or with any of the two types
  3648. /// not being a built-in arithmetic type (apart from [half](\ref half_float::half), of course) results in a compiler
  3649. /// error and casting between [half](\ref half_float::half)s returns the argument unmodified.
  3650. /// \tparam T destination type (half or built-in arithmetic type)
  3651. /// \tparam U source type (half or built-in arithmetic type)
  3652. /// \param arg value to cast
  3653. /// \return \a arg converted to destination type
  3654. /// \exception FE_INVALID if \a T is integer type and result is not representable as \a T
  3655. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3656. template<class T,class U> T half_cast(U arg) { return detail::half_caster<T,U>::cast(arg); }
  3657. /// Cast to or from half-precision floating-point number.
  3658. /// This casts between [half](\ref half_float::half) and any built-in arithmetic type. The values are converted
  3659. /// directly using the specified rounding mode, without any roundtrip over `float` that a `static_cast` would otherwise do.
  3660. ///
  3661. /// Using this cast with neither of the two types being a [half](\ref half_float::half) or with any of the two types
  3662. /// not being a built-in arithmetic type (apart from [half](\ref half_float::half), of course) results in a compiler
  3663. /// error and casting between [half](\ref half_float::half)s returns the argument unmodified.
  3664. /// \tparam T destination type (half or built-in arithmetic type)
  3665. /// \tparam R rounding mode to use.
  3666. /// \tparam U source type (half or built-in arithmetic type)
  3667. /// \param arg value to cast
  3668. /// \return \a arg converted to destination type
  3669. /// \exception FE_INVALID if \a T is integer type and result is not representable as \a T
  3670. /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding
  3671. template<class T,std::float_round_style R,class U> T half_cast(U arg) { return detail::half_caster<T,U,R>::cast(arg); }
  3672. /// \}
  3673. /// \}
  3674. /// \anchor errors
  3675. /// \name Error handling
  3676. /// \{
  3677. /// Clear exception flags.
  3678. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3679. /// but in that case manual flag management is the only way to raise flags.
  3680. ///
  3681. /// **See also:** Documentation for [std::feclearexcept](https://en.cppreference.com/w/cpp/numeric/fenv/feclearexcept).
  3682. /// \param excepts OR of exceptions to clear
  3683. /// \retval 0 all selected flags cleared successfully
  3684. inline int feclearexcept(int excepts) { detail::errflags() &= ~excepts; return 0; }
  3685. /// Test exception flags.
  3686. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3687. /// but in that case manual flag management is the only way to raise flags.
  3688. ///
  3689. /// **See also:** Documentation for [std::fetestexcept](https://en.cppreference.com/w/cpp/numeric/fenv/fetestexcept).
  3690. /// \param excepts OR of exceptions to test
  3691. /// \return OR of selected exceptions if raised
  3692. inline int fetestexcept(int excepts) { return detail::errflags() & excepts; }
  3693. /// Raise exception flags.
  3694. /// This raises the specified floating point exceptions and also invokes any additional automatic exception handling as
  3695. /// configured with the [HALF_ERRHANDLIG_...](\ref HALF_ERRHANDLING_ERRNO) preprocessor symbols.
  3696. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3697. /// but in that case manual flag management is the only way to raise flags.
  3698. ///
  3699. /// **See also:** Documentation for [std::feraiseexcept](https://en.cppreference.com/w/cpp/numeric/fenv/feraiseexcept).
  3700. /// \param excepts OR of exceptions to raise
  3701. /// \retval 0 all selected exceptions raised successfully
  3702. inline int feraiseexcept(int excepts) { detail::errflags() |= excepts; detail::raise(excepts); return 0; }
  3703. /// Save exception flags.
  3704. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3705. /// but in that case manual flag management is the only way to raise flags.
  3706. ///
  3707. /// **See also:** Documentation for [std::fegetexceptflag](https://en.cppreference.com/w/cpp/numeric/fenv/feexceptflag).
  3708. /// \param flagp adress to store flag state at
  3709. /// \param excepts OR of flags to save
  3710. /// \retval 0 for success
  3711. inline int fegetexceptflag(int *flagp, int excepts) { *flagp = detail::errflags() & excepts; return 0; }
  3712. /// Restore exception flags.
  3713. /// This only copies the specified exception state (including unset flags) without incurring any additional exception handling.
  3714. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3715. /// but in that case manual flag management is the only way to raise flags.
  3716. ///
  3717. /// **See also:** Documentation for [std::fesetexceptflag](https://en.cppreference.com/w/cpp/numeric/fenv/feexceptflag).
  3718. /// \param flagp adress to take flag state from
  3719. /// \param excepts OR of flags to restore
  3720. /// \retval 0 for success
  3721. inline int fesetexceptflag(const int *flagp, int excepts) { detail::errflags() = (detail::errflags()|(*flagp&excepts)) & (*flagp|~excepts); return 0; }
  3722. /// Throw C++ exceptions based on set exception flags.
  3723. /// This function manually throws a corresponding C++ exception if one of the specified flags is set,
  3724. /// no matter if automatic throwing (via [HALF_ERRHANDLING_THROW_...](\ref HALF_ERRHANDLING_THROW_INVALID)) is enabled or not.
  3725. /// This function works even if [automatic exception flag handling](\ref HALF_ERRHANDLING_FLAGS) is disabled,
  3726. /// but in that case manual flag management is the only way to raise flags.
  3727. /// \param excepts OR of exceptions to test
  3728. /// \param msg error message to use for exception description
  3729. /// \throw std::domain_error if `FE_INVALID` or `FE_DIVBYZERO` is selected and set
  3730. /// \throw std::overflow_error if `FE_OVERFLOW` is selected and set
  3731. /// \throw std::underflow_error if `FE_UNDERFLOW` is selected and set
  3732. /// \throw std::range_error if `FE_INEXACT` is selected and set
  3733. inline void fethrowexcept(int excepts, const char *msg = "") {
  3734. excepts &= detail::errflags();
  3735. #if HALF_ERRHANDLING_THROWS
  3736. #ifdef HALF_ERRHANDLING_THROW_INVALID
  3737. if(excepts & FE_INVALID)
  3738. throw std::domain_error(msg);
  3739. #endif
  3740. #ifdef HALF_ERRHANDLING_THROW_DIVBYZERO
  3741. if(excepts & FE_DIVBYZERO)
  3742. throw std::domain_error(msg);
  3743. #endif
  3744. #ifdef HALF_ERRHANDLING_THROW_OVERFLOW
  3745. if(excepts & FE_OVERFLOW)
  3746. throw std::overflow_error(msg);
  3747. #endif
  3748. #ifdef HALF_ERRHANDLING_THROW_UNDERFLOW
  3749. if(excepts & FE_UNDERFLOW)
  3750. throw std::underflow_error(msg);
  3751. #endif
  3752. #ifdef HALF_ERRHANDLING_THROW_INEXACT
  3753. if(excepts & FE_INEXACT)
  3754. throw std::range_error(msg);
  3755. #endif
  3756. #else
  3757. std::fprintf(stderr, "%s\n", msg);
  3758. std::terminate();
  3759. #endif
  3760. }
  3761. /// \}
  3762. }
  3763. #undef HALF_UNUSED_NOERR
  3764. #undef constexpr_NOERR
  3765. #undef HALF_TWOS_COMPLEMENT_INT
  3766. #ifdef HALF_POP_WARNINGS
  3767. #pragma warning(pop)
  3768. #undef HALF_POP_WARNINGS
  3769. #endif