MathFunctions.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_MATHFUNCTIONS_H
  11. #define EIGEN_MATHFUNCTIONS_H
  12. // TODO this should better be moved to NumTraits
  13. // Source: WolframAlpha
  14. #define EIGEN_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406L
  15. #define EIGEN_LOG2E 1.442695040888963407359924681001892137426645954152985934135449406931109219L
  16. #define EIGEN_LN2 0.693147180559945309417232121458176568075500134360255254120680009493393621L
  17. namespace Eigen {
  18. // On WINCE, std::abs is defined for int only, so let's defined our own overloads:
  19. // This issue has been confirmed with MSVC 2008 only, but the issue might exist for more recent versions too.
  20. #if EIGEN_OS_WINCE && EIGEN_COMP_MSVC && EIGEN_COMP_MSVC<=1500
  21. long abs(long x) { return (labs(x)); }
  22. double abs(double x) { return (fabs(x)); }
  23. float abs(float x) { return (fabsf(x)); }
  24. long double abs(long double x) { return (fabsl(x)); }
  25. #endif
  26. namespace internal {
  27. /** \internal \class global_math_functions_filtering_base
  28. *
  29. * What it does:
  30. * Defines a typedef 'type' as follows:
  31. * - if type T has a member typedef Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl, then
  32. * global_math_functions_filtering_base<T>::type is a typedef for it.
  33. * - otherwise, global_math_functions_filtering_base<T>::type is a typedef for T.
  34. *
  35. * How it's used:
  36. * To allow to defined the global math functions (like sin...) in certain cases, like the Array expressions.
  37. * When you do sin(array1+array2), the object array1+array2 has a complicated expression type, all what you want to know
  38. * is that it inherits ArrayBase. So we implement a partial specialization of sin_impl for ArrayBase<Derived>.
  39. * So we must make sure to use sin_impl<ArrayBase<Derived> > and not sin_impl<Derived>, otherwise our partial specialization
  40. * won't be used. How does sin know that? That's exactly what global_math_functions_filtering_base tells it.
  41. *
  42. * How it's implemented:
  43. * SFINAE in the style of enable_if. Highly susceptible of breaking compilers. With GCC, it sure does work, but if you replace
  44. * the typename dummy by an integer template parameter, it doesn't work anymore!
  45. */
  46. template<typename T, typename dummy = void>
  47. struct global_math_functions_filtering_base
  48. {
  49. typedef T type;
  50. };
  51. template<typename T> struct always_void { typedef void type; };
  52. template<typename T>
  53. struct global_math_functions_filtering_base
  54. <T,
  55. typename always_void<typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl>::type
  56. >
  57. {
  58. typedef typename T::Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl type;
  59. };
  60. #define EIGEN_MATHFUNC_IMPL(func, scalar) Eigen::internal::func##_impl<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>
  61. #define EIGEN_MATHFUNC_RETVAL(func, scalar) typename Eigen::internal::func##_retval<typename Eigen::internal::global_math_functions_filtering_base<scalar>::type>::type
  62. /****************************************************************************
  63. * Implementation of real *
  64. ****************************************************************************/
  65. template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
  66. struct real_default_impl
  67. {
  68. typedef typename NumTraits<Scalar>::Real RealScalar;
  69. EIGEN_DEVICE_FUNC
  70. static inline RealScalar run(const Scalar& x)
  71. {
  72. return x;
  73. }
  74. };
  75. template<typename Scalar>
  76. struct real_default_impl<Scalar,true>
  77. {
  78. typedef typename NumTraits<Scalar>::Real RealScalar;
  79. EIGEN_DEVICE_FUNC
  80. static inline RealScalar run(const Scalar& x)
  81. {
  82. using std::real;
  83. return real(x);
  84. }
  85. };
  86. template<typename Scalar> struct real_impl : real_default_impl<Scalar> {};
  87. #if defined(EIGEN_GPU_COMPILE_PHASE)
  88. template<typename T>
  89. struct real_impl<std::complex<T> >
  90. {
  91. typedef T RealScalar;
  92. EIGEN_DEVICE_FUNC
  93. static inline T run(const std::complex<T>& x)
  94. {
  95. return x.real();
  96. }
  97. };
  98. #endif
  99. template<typename Scalar>
  100. struct real_retval
  101. {
  102. typedef typename NumTraits<Scalar>::Real type;
  103. };
  104. /****************************************************************************
  105. * Implementation of imag *
  106. ****************************************************************************/
  107. template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
  108. struct imag_default_impl
  109. {
  110. typedef typename NumTraits<Scalar>::Real RealScalar;
  111. EIGEN_DEVICE_FUNC
  112. static inline RealScalar run(const Scalar&)
  113. {
  114. return RealScalar(0);
  115. }
  116. };
  117. template<typename Scalar>
  118. struct imag_default_impl<Scalar,true>
  119. {
  120. typedef typename NumTraits<Scalar>::Real RealScalar;
  121. EIGEN_DEVICE_FUNC
  122. static inline RealScalar run(const Scalar& x)
  123. {
  124. using std::imag;
  125. return imag(x);
  126. }
  127. };
  128. template<typename Scalar> struct imag_impl : imag_default_impl<Scalar> {};
  129. #if defined(EIGEN_GPU_COMPILE_PHASE)
  130. template<typename T>
  131. struct imag_impl<std::complex<T> >
  132. {
  133. typedef T RealScalar;
  134. EIGEN_DEVICE_FUNC
  135. static inline T run(const std::complex<T>& x)
  136. {
  137. return x.imag();
  138. }
  139. };
  140. #endif
  141. template<typename Scalar>
  142. struct imag_retval
  143. {
  144. typedef typename NumTraits<Scalar>::Real type;
  145. };
  146. /****************************************************************************
  147. * Implementation of real_ref *
  148. ****************************************************************************/
  149. template<typename Scalar>
  150. struct real_ref_impl
  151. {
  152. typedef typename NumTraits<Scalar>::Real RealScalar;
  153. EIGEN_DEVICE_FUNC
  154. static inline RealScalar& run(Scalar& x)
  155. {
  156. return reinterpret_cast<RealScalar*>(&x)[0];
  157. }
  158. EIGEN_DEVICE_FUNC
  159. static inline const RealScalar& run(const Scalar& x)
  160. {
  161. return reinterpret_cast<const RealScalar*>(&x)[0];
  162. }
  163. };
  164. template<typename Scalar>
  165. struct real_ref_retval
  166. {
  167. typedef typename NumTraits<Scalar>::Real & type;
  168. };
  169. /****************************************************************************
  170. * Implementation of imag_ref *
  171. ****************************************************************************/
  172. template<typename Scalar, bool IsComplex>
  173. struct imag_ref_default_impl
  174. {
  175. typedef typename NumTraits<Scalar>::Real RealScalar;
  176. EIGEN_DEVICE_FUNC
  177. static inline RealScalar& run(Scalar& x)
  178. {
  179. return reinterpret_cast<RealScalar*>(&x)[1];
  180. }
  181. EIGEN_DEVICE_FUNC
  182. static inline const RealScalar& run(const Scalar& x)
  183. {
  184. return reinterpret_cast<RealScalar*>(&x)[1];
  185. }
  186. };
  187. template<typename Scalar>
  188. struct imag_ref_default_impl<Scalar, false>
  189. {
  190. EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
  191. static inline Scalar run(Scalar&)
  192. {
  193. return Scalar(0);
  194. }
  195. EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
  196. static inline const Scalar run(const Scalar&)
  197. {
  198. return Scalar(0);
  199. }
  200. };
  201. template<typename Scalar>
  202. struct imag_ref_impl : imag_ref_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
  203. template<typename Scalar>
  204. struct imag_ref_retval
  205. {
  206. typedef typename NumTraits<Scalar>::Real & type;
  207. };
  208. /****************************************************************************
  209. * Implementation of conj *
  210. ****************************************************************************/
  211. template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
  212. struct conj_default_impl
  213. {
  214. EIGEN_DEVICE_FUNC
  215. static inline Scalar run(const Scalar& x)
  216. {
  217. return x;
  218. }
  219. };
  220. template<typename Scalar>
  221. struct conj_default_impl<Scalar,true>
  222. {
  223. EIGEN_DEVICE_FUNC
  224. static inline Scalar run(const Scalar& x)
  225. {
  226. using std::conj;
  227. return conj(x);
  228. }
  229. };
  230. template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
  231. struct conj_impl : conj_default_impl<Scalar, IsComplex> {};
  232. template<typename Scalar>
  233. struct conj_retval
  234. {
  235. typedef Scalar type;
  236. };
  237. /****************************************************************************
  238. * Implementation of abs2 *
  239. ****************************************************************************/
  240. template<typename Scalar,bool IsComplex>
  241. struct abs2_impl_default
  242. {
  243. typedef typename NumTraits<Scalar>::Real RealScalar;
  244. EIGEN_DEVICE_FUNC
  245. static inline RealScalar run(const Scalar& x)
  246. {
  247. return x*x;
  248. }
  249. };
  250. template<typename Scalar>
  251. struct abs2_impl_default<Scalar, true> // IsComplex
  252. {
  253. typedef typename NumTraits<Scalar>::Real RealScalar;
  254. EIGEN_DEVICE_FUNC
  255. static inline RealScalar run(const Scalar& x)
  256. {
  257. return x.real()*x.real() + x.imag()*x.imag();
  258. }
  259. };
  260. template<typename Scalar>
  261. struct abs2_impl
  262. {
  263. typedef typename NumTraits<Scalar>::Real RealScalar;
  264. EIGEN_DEVICE_FUNC
  265. static inline RealScalar run(const Scalar& x)
  266. {
  267. return abs2_impl_default<Scalar,NumTraits<Scalar>::IsComplex>::run(x);
  268. }
  269. };
  270. template<typename Scalar>
  271. struct abs2_retval
  272. {
  273. typedef typename NumTraits<Scalar>::Real type;
  274. };
  275. /****************************************************************************
  276. * Implementation of sqrt/rsqrt *
  277. ****************************************************************************/
  278. template<typename Scalar>
  279. struct sqrt_impl
  280. {
  281. EIGEN_DEVICE_FUNC
  282. static EIGEN_ALWAYS_INLINE Scalar run(const Scalar& x)
  283. {
  284. EIGEN_USING_STD(sqrt);
  285. return sqrt(x);
  286. }
  287. };
  288. // Complex sqrt defined in MathFunctionsImpl.h.
  289. template<typename T> EIGEN_DEVICE_FUNC std::complex<T> complex_sqrt(const std::complex<T>& a_x);
  290. // Custom implementation is faster than `std::sqrt`, works on
  291. // GPU, and correctly handles special cases (unlike MSVC).
  292. template<typename T>
  293. struct sqrt_impl<std::complex<T> >
  294. {
  295. EIGEN_DEVICE_FUNC
  296. static EIGEN_ALWAYS_INLINE std::complex<T> run(const std::complex<T>& x)
  297. {
  298. return complex_sqrt<T>(x);
  299. }
  300. };
  301. template<typename Scalar>
  302. struct sqrt_retval
  303. {
  304. typedef Scalar type;
  305. };
  306. // Default implementation relies on numext::sqrt, at bottom of file.
  307. template<typename T>
  308. struct rsqrt_impl;
  309. // Complex rsqrt defined in MathFunctionsImpl.h.
  310. template<typename T> EIGEN_DEVICE_FUNC std::complex<T> complex_rsqrt(const std::complex<T>& a_x);
  311. template<typename T>
  312. struct rsqrt_impl<std::complex<T> >
  313. {
  314. EIGEN_DEVICE_FUNC
  315. static EIGEN_ALWAYS_INLINE std::complex<T> run(const std::complex<T>& x)
  316. {
  317. return complex_rsqrt<T>(x);
  318. }
  319. };
  320. template<typename Scalar>
  321. struct rsqrt_retval
  322. {
  323. typedef Scalar type;
  324. };
  325. /****************************************************************************
  326. * Implementation of norm1 *
  327. ****************************************************************************/
  328. template<typename Scalar, bool IsComplex>
  329. struct norm1_default_impl;
  330. template<typename Scalar>
  331. struct norm1_default_impl<Scalar,true>
  332. {
  333. typedef typename NumTraits<Scalar>::Real RealScalar;
  334. EIGEN_DEVICE_FUNC
  335. static inline RealScalar run(const Scalar& x)
  336. {
  337. EIGEN_USING_STD(abs);
  338. return abs(x.real()) + abs(x.imag());
  339. }
  340. };
  341. template<typename Scalar>
  342. struct norm1_default_impl<Scalar, false>
  343. {
  344. EIGEN_DEVICE_FUNC
  345. static inline Scalar run(const Scalar& x)
  346. {
  347. EIGEN_USING_STD(abs);
  348. return abs(x);
  349. }
  350. };
  351. template<typename Scalar>
  352. struct norm1_impl : norm1_default_impl<Scalar, NumTraits<Scalar>::IsComplex> {};
  353. template<typename Scalar>
  354. struct norm1_retval
  355. {
  356. typedef typename NumTraits<Scalar>::Real type;
  357. };
  358. /****************************************************************************
  359. * Implementation of hypot *
  360. ****************************************************************************/
  361. template<typename Scalar> struct hypot_impl;
  362. template<typename Scalar>
  363. struct hypot_retval
  364. {
  365. typedef typename NumTraits<Scalar>::Real type;
  366. };
  367. /****************************************************************************
  368. * Implementation of cast *
  369. ****************************************************************************/
  370. template<typename OldType, typename NewType, typename EnableIf = void>
  371. struct cast_impl
  372. {
  373. EIGEN_DEVICE_FUNC
  374. static inline NewType run(const OldType& x)
  375. {
  376. return static_cast<NewType>(x);
  377. }
  378. };
  379. // Casting from S -> Complex<T> leads to an implicit conversion from S to T,
  380. // generating warnings on clang. Here we explicitly cast the real component.
  381. template<typename OldType, typename NewType>
  382. struct cast_impl<OldType, NewType,
  383. typename internal::enable_if<
  384. !NumTraits<OldType>::IsComplex && NumTraits<NewType>::IsComplex
  385. >::type>
  386. {
  387. EIGEN_DEVICE_FUNC
  388. static inline NewType run(const OldType& x)
  389. {
  390. typedef typename NumTraits<NewType>::Real NewReal;
  391. return static_cast<NewType>(static_cast<NewReal>(x));
  392. }
  393. };
  394. // here, for once, we're plainly returning NewType: we don't want cast to do weird things.
  395. template<typename OldType, typename NewType>
  396. EIGEN_DEVICE_FUNC
  397. inline NewType cast(const OldType& x)
  398. {
  399. return cast_impl<OldType, NewType>::run(x);
  400. }
  401. /****************************************************************************
  402. * Implementation of round *
  403. ****************************************************************************/
  404. template<typename Scalar>
  405. struct round_impl
  406. {
  407. EIGEN_DEVICE_FUNC
  408. static inline Scalar run(const Scalar& x)
  409. {
  410. EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
  411. #if EIGEN_HAS_CXX11_MATH
  412. EIGEN_USING_STD(round);
  413. #endif
  414. return Scalar(round(x));
  415. }
  416. };
  417. #if !EIGEN_HAS_CXX11_MATH
  418. #if EIGEN_HAS_C99_MATH
  419. // Use ::roundf for float.
  420. template<>
  421. struct round_impl<float> {
  422. EIGEN_DEVICE_FUNC
  423. static inline float run(const float& x)
  424. {
  425. return ::roundf(x);
  426. }
  427. };
  428. #else
  429. template<typename Scalar>
  430. struct round_using_floor_ceil_impl
  431. {
  432. EIGEN_DEVICE_FUNC
  433. static inline Scalar run(const Scalar& x)
  434. {
  435. EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
  436. // Without C99 round/roundf, resort to floor/ceil.
  437. EIGEN_USING_STD(floor);
  438. EIGEN_USING_STD(ceil);
  439. // If not enough precision to resolve a decimal at all, return the input.
  440. // Otherwise, adding 0.5 can trigger an increment by 1.
  441. const Scalar limit = Scalar(1ull << (NumTraits<Scalar>::digits() - 1));
  442. if (x >= limit || x <= -limit) {
  443. return x;
  444. }
  445. return (x > Scalar(0)) ? Scalar(floor(x + Scalar(0.5))) : Scalar(ceil(x - Scalar(0.5)));
  446. }
  447. };
  448. template<>
  449. struct round_impl<float> : round_using_floor_ceil_impl<float> {};
  450. template<>
  451. struct round_impl<double> : round_using_floor_ceil_impl<double> {};
  452. #endif // EIGEN_HAS_C99_MATH
  453. #endif // !EIGEN_HAS_CXX11_MATH
  454. template<typename Scalar>
  455. struct round_retval
  456. {
  457. typedef Scalar type;
  458. };
  459. /****************************************************************************
  460. * Implementation of rint *
  461. ****************************************************************************/
  462. template<typename Scalar>
  463. struct rint_impl {
  464. EIGEN_DEVICE_FUNC
  465. static inline Scalar run(const Scalar& x)
  466. {
  467. EIGEN_STATIC_ASSERT((!NumTraits<Scalar>::IsComplex), NUMERIC_TYPE_MUST_BE_REAL)
  468. #if EIGEN_HAS_CXX11_MATH
  469. EIGEN_USING_STD(rint);
  470. #endif
  471. return rint(x);
  472. }
  473. };
  474. #if !EIGEN_HAS_CXX11_MATH
  475. template<>
  476. struct rint_impl<double> {
  477. EIGEN_DEVICE_FUNC
  478. static inline double run(const double& x)
  479. {
  480. return ::rint(x);
  481. }
  482. };
  483. template<>
  484. struct rint_impl<float> {
  485. EIGEN_DEVICE_FUNC
  486. static inline float run(const float& x)
  487. {
  488. return ::rintf(x);
  489. }
  490. };
  491. #endif
  492. template<typename Scalar>
  493. struct rint_retval
  494. {
  495. typedef Scalar type;
  496. };
  497. /****************************************************************************
  498. * Implementation of arg *
  499. ****************************************************************************/
  500. // Visual Studio 2017 has a bug where arg(float) returns 0 for negative inputs.
  501. // This seems to be fixed in VS 2019.
  502. #if EIGEN_HAS_CXX11_MATH && (!EIGEN_COMP_MSVC || EIGEN_COMP_MSVC >= 1920)
  503. // std::arg is only defined for types of std::complex, or integer types or float/double/long double
  504. template<typename Scalar,
  505. bool HasStdImpl = NumTraits<Scalar>::IsComplex || is_integral<Scalar>::value
  506. || is_same<Scalar, float>::value || is_same<Scalar, double>::value
  507. || is_same<Scalar, long double>::value >
  508. struct arg_default_impl;
  509. template<typename Scalar>
  510. struct arg_default_impl<Scalar, true> {
  511. typedef typename NumTraits<Scalar>::Real RealScalar;
  512. EIGEN_DEVICE_FUNC
  513. static inline RealScalar run(const Scalar& x)
  514. {
  515. #if defined(EIGEN_HIP_DEVICE_COMPILE)
  516. // HIP does not seem to have a native device side implementation for the math routine "arg"
  517. using std::arg;
  518. #else
  519. EIGEN_USING_STD(arg);
  520. #endif
  521. return static_cast<RealScalar>(arg(x));
  522. }
  523. };
  524. // Must be non-complex floating-point type (e.g. half/bfloat16).
  525. template<typename Scalar>
  526. struct arg_default_impl<Scalar, false> {
  527. typedef typename NumTraits<Scalar>::Real RealScalar;
  528. EIGEN_DEVICE_FUNC
  529. static inline RealScalar run(const Scalar& x)
  530. {
  531. return (x < Scalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
  532. }
  533. };
  534. #else
  535. template<typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
  536. struct arg_default_impl
  537. {
  538. typedef typename NumTraits<Scalar>::Real RealScalar;
  539. EIGEN_DEVICE_FUNC
  540. static inline RealScalar run(const Scalar& x)
  541. {
  542. return (x < RealScalar(0)) ? RealScalar(EIGEN_PI) : RealScalar(0);
  543. }
  544. };
  545. template<typename Scalar>
  546. struct arg_default_impl<Scalar,true>
  547. {
  548. typedef typename NumTraits<Scalar>::Real RealScalar;
  549. EIGEN_DEVICE_FUNC
  550. static inline RealScalar run(const Scalar& x)
  551. {
  552. EIGEN_USING_STD(arg);
  553. return arg(x);
  554. }
  555. };
  556. #endif
  557. template<typename Scalar> struct arg_impl : arg_default_impl<Scalar> {};
  558. template<typename Scalar>
  559. struct arg_retval
  560. {
  561. typedef typename NumTraits<Scalar>::Real type;
  562. };
  563. /****************************************************************************
  564. * Implementation of expm1 *
  565. ****************************************************************************/
  566. // This implementation is based on GSL Math's expm1.
  567. namespace std_fallback {
  568. // fallback expm1 implementation in case there is no expm1(Scalar) function in namespace of Scalar,
  569. // or that there is no suitable std::expm1 function available. Implementation
  570. // attributed to Kahan. See: http://www.plunk.org/~hatch/rightway.php.
  571. template<typename Scalar>
  572. EIGEN_DEVICE_FUNC inline Scalar expm1(const Scalar& x) {
  573. EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
  574. typedef typename NumTraits<Scalar>::Real RealScalar;
  575. EIGEN_USING_STD(exp);
  576. Scalar u = exp(x);
  577. if (numext::equal_strict(u, Scalar(1))) {
  578. return x;
  579. }
  580. Scalar um1 = u - RealScalar(1);
  581. if (numext::equal_strict(um1, Scalar(-1))) {
  582. return RealScalar(-1);
  583. }
  584. EIGEN_USING_STD(log);
  585. Scalar logu = log(u);
  586. return numext::equal_strict(u, logu) ? u : (u - RealScalar(1)) * x / logu;
  587. }
  588. }
  589. template<typename Scalar>
  590. struct expm1_impl {
  591. EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x)
  592. {
  593. EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
  594. #if EIGEN_HAS_CXX11_MATH
  595. using std::expm1;
  596. #else
  597. using std_fallback::expm1;
  598. #endif
  599. return expm1(x);
  600. }
  601. };
  602. template<typename Scalar>
  603. struct expm1_retval
  604. {
  605. typedef Scalar type;
  606. };
  607. /****************************************************************************
  608. * Implementation of log *
  609. ****************************************************************************/
  610. // Complex log defined in MathFunctionsImpl.h.
  611. template<typename T> EIGEN_DEVICE_FUNC std::complex<T> complex_log(const std::complex<T>& z);
  612. template<typename Scalar>
  613. struct log_impl {
  614. EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x)
  615. {
  616. EIGEN_USING_STD(log);
  617. return static_cast<Scalar>(log(x));
  618. }
  619. };
  620. template<typename Scalar>
  621. struct log_impl<std::complex<Scalar> > {
  622. EIGEN_DEVICE_FUNC static inline std::complex<Scalar> run(const std::complex<Scalar>& z)
  623. {
  624. return complex_log(z);
  625. }
  626. };
  627. /****************************************************************************
  628. * Implementation of log1p *
  629. ****************************************************************************/
  630. namespace std_fallback {
  631. // fallback log1p implementation in case there is no log1p(Scalar) function in namespace of Scalar,
  632. // or that there is no suitable std::log1p function available
  633. template<typename Scalar>
  634. EIGEN_DEVICE_FUNC inline Scalar log1p(const Scalar& x) {
  635. EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
  636. typedef typename NumTraits<Scalar>::Real RealScalar;
  637. EIGEN_USING_STD(log);
  638. Scalar x1p = RealScalar(1) + x;
  639. Scalar log_1p = log_impl<Scalar>::run(x1p);
  640. const bool is_small = numext::equal_strict(x1p, Scalar(1));
  641. const bool is_inf = numext::equal_strict(x1p, log_1p);
  642. return (is_small || is_inf) ? x : x * (log_1p / (x1p - RealScalar(1)));
  643. }
  644. }
  645. template<typename Scalar>
  646. struct log1p_impl {
  647. EIGEN_DEVICE_FUNC static inline Scalar run(const Scalar& x)
  648. {
  649. EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
  650. #if EIGEN_HAS_CXX11_MATH
  651. using std::log1p;
  652. #else
  653. using std_fallback::log1p;
  654. #endif
  655. return log1p(x);
  656. }
  657. };
  658. // Specialization for complex types that are not supported by std::log1p.
  659. template <typename RealScalar>
  660. struct log1p_impl<std::complex<RealScalar> > {
  661. EIGEN_DEVICE_FUNC static inline std::complex<RealScalar> run(
  662. const std::complex<RealScalar>& x) {
  663. EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
  664. return std_fallback::log1p(x);
  665. }
  666. };
  667. template<typename Scalar>
  668. struct log1p_retval
  669. {
  670. typedef Scalar type;
  671. };
  672. /****************************************************************************
  673. * Implementation of pow *
  674. ****************************************************************************/
  675. template<typename ScalarX,typename ScalarY, bool IsInteger = NumTraits<ScalarX>::IsInteger&&NumTraits<ScalarY>::IsInteger>
  676. struct pow_impl
  677. {
  678. //typedef Scalar retval;
  679. typedef typename ScalarBinaryOpTraits<ScalarX,ScalarY,internal::scalar_pow_op<ScalarX,ScalarY> >::ReturnType result_type;
  680. static EIGEN_DEVICE_FUNC inline result_type run(const ScalarX& x, const ScalarY& y)
  681. {
  682. EIGEN_USING_STD(pow);
  683. return pow(x, y);
  684. }
  685. };
  686. template<typename ScalarX,typename ScalarY>
  687. struct pow_impl<ScalarX,ScalarY, true>
  688. {
  689. typedef ScalarX result_type;
  690. static EIGEN_DEVICE_FUNC inline ScalarX run(ScalarX x, ScalarY y)
  691. {
  692. ScalarX res(1);
  693. eigen_assert(!NumTraits<ScalarY>::IsSigned || y >= 0);
  694. if(y & 1) res *= x;
  695. y >>= 1;
  696. while(y)
  697. {
  698. x *= x;
  699. if(y&1) res *= x;
  700. y >>= 1;
  701. }
  702. return res;
  703. }
  704. };
  705. /****************************************************************************
  706. * Implementation of random *
  707. ****************************************************************************/
  708. template<typename Scalar,
  709. bool IsComplex,
  710. bool IsInteger>
  711. struct random_default_impl {};
  712. template<typename Scalar>
  713. struct random_impl : random_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
  714. template<typename Scalar>
  715. struct random_retval
  716. {
  717. typedef Scalar type;
  718. };
  719. template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y);
  720. template<typename Scalar> inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random();
  721. template<typename Scalar>
  722. struct random_default_impl<Scalar, false, false>
  723. {
  724. static inline Scalar run(const Scalar& x, const Scalar& y)
  725. {
  726. return x + (y-x) * Scalar(std::rand()) / Scalar(RAND_MAX);
  727. }
  728. static inline Scalar run()
  729. {
  730. return run(Scalar(NumTraits<Scalar>::IsSigned ? -1 : 0), Scalar(1));
  731. }
  732. };
  733. enum {
  734. meta_floor_log2_terminate,
  735. meta_floor_log2_move_up,
  736. meta_floor_log2_move_down,
  737. meta_floor_log2_bogus
  738. };
  739. template<unsigned int n, int lower, int upper> struct meta_floor_log2_selector
  740. {
  741. enum { middle = (lower + upper) / 2,
  742. value = (upper <= lower + 1) ? int(meta_floor_log2_terminate)
  743. : (n < (1 << middle)) ? int(meta_floor_log2_move_down)
  744. : (n==0) ? int(meta_floor_log2_bogus)
  745. : int(meta_floor_log2_move_up)
  746. };
  747. };
  748. template<unsigned int n,
  749. int lower = 0,
  750. int upper = sizeof(unsigned int) * CHAR_BIT - 1,
  751. int selector = meta_floor_log2_selector<n, lower, upper>::value>
  752. struct meta_floor_log2 {};
  753. template<unsigned int n, int lower, int upper>
  754. struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_down>
  755. {
  756. enum { value = meta_floor_log2<n, lower, meta_floor_log2_selector<n, lower, upper>::middle>::value };
  757. };
  758. template<unsigned int n, int lower, int upper>
  759. struct meta_floor_log2<n, lower, upper, meta_floor_log2_move_up>
  760. {
  761. enum { value = meta_floor_log2<n, meta_floor_log2_selector<n, lower, upper>::middle, upper>::value };
  762. };
  763. template<unsigned int n, int lower, int upper>
  764. struct meta_floor_log2<n, lower, upper, meta_floor_log2_terminate>
  765. {
  766. enum { value = (n >= ((unsigned int)(1) << (lower+1))) ? lower+1 : lower };
  767. };
  768. template<unsigned int n, int lower, int upper>
  769. struct meta_floor_log2<n, lower, upper, meta_floor_log2_bogus>
  770. {
  771. // no value, error at compile time
  772. };
  773. template<typename Scalar>
  774. struct random_default_impl<Scalar, false, true>
  775. {
  776. static inline Scalar run(const Scalar& x, const Scalar& y)
  777. {
  778. if (y <= x)
  779. return x;
  780. // ScalarU is the unsigned counterpart of Scalar, possibly Scalar itself.
  781. typedef typename make_unsigned<Scalar>::type ScalarU;
  782. // ScalarX is the widest of ScalarU and unsigned int.
  783. // We'll deal only with ScalarX and unsigned int below thus avoiding signed
  784. // types and arithmetic and signed overflows (which are undefined behavior).
  785. typedef typename conditional<(ScalarU(-1) > unsigned(-1)), ScalarU, unsigned>::type ScalarX;
  786. // The following difference doesn't overflow, provided our integer types are two's
  787. // complement and have the same number of padding bits in signed and unsigned variants.
  788. // This is the case in most modern implementations of C++.
  789. ScalarX range = ScalarX(y) - ScalarX(x);
  790. ScalarX offset = 0;
  791. ScalarX divisor = 1;
  792. ScalarX multiplier = 1;
  793. const unsigned rand_max = RAND_MAX;
  794. if (range <= rand_max) divisor = (rand_max + 1) / (range + 1);
  795. else multiplier = 1 + range / (rand_max + 1);
  796. // Rejection sampling.
  797. do {
  798. offset = (unsigned(std::rand()) * multiplier) / divisor;
  799. } while (offset > range);
  800. return Scalar(ScalarX(x) + offset);
  801. }
  802. static inline Scalar run()
  803. {
  804. #ifdef EIGEN_MAKING_DOCS
  805. return run(Scalar(NumTraits<Scalar>::IsSigned ? -10 : 0), Scalar(10));
  806. #else
  807. enum { rand_bits = meta_floor_log2<(unsigned int)(RAND_MAX)+1>::value,
  808. scalar_bits = sizeof(Scalar) * CHAR_BIT,
  809. shift = EIGEN_PLAIN_ENUM_MAX(0, int(rand_bits) - int(scalar_bits)),
  810. offset = NumTraits<Scalar>::IsSigned ? (1 << (EIGEN_PLAIN_ENUM_MIN(rand_bits,scalar_bits)-1)) : 0
  811. };
  812. return Scalar((std::rand() >> shift) - offset);
  813. #endif
  814. }
  815. };
  816. template<typename Scalar>
  817. struct random_default_impl<Scalar, true, false>
  818. {
  819. static inline Scalar run(const Scalar& x, const Scalar& y)
  820. {
  821. return Scalar(random(x.real(), y.real()),
  822. random(x.imag(), y.imag()));
  823. }
  824. static inline Scalar run()
  825. {
  826. typedef typename NumTraits<Scalar>::Real RealScalar;
  827. return Scalar(random<RealScalar>(), random<RealScalar>());
  828. }
  829. };
  830. template<typename Scalar>
  831. inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random(const Scalar& x, const Scalar& y)
  832. {
  833. return EIGEN_MATHFUNC_IMPL(random, Scalar)::run(x, y);
  834. }
  835. template<typename Scalar>
  836. inline EIGEN_MATHFUNC_RETVAL(random, Scalar) random()
  837. {
  838. return EIGEN_MATHFUNC_IMPL(random, Scalar)::run();
  839. }
  840. // Implementation of is* functions
  841. // std::is* do not work with fast-math and gcc, std::is* are available on MSVC 2013 and newer, as well as in clang.
  842. #if (EIGEN_HAS_CXX11_MATH && !(EIGEN_COMP_GNUC_STRICT && __FINITE_MATH_ONLY__)) || (EIGEN_COMP_MSVC>=1800) || (EIGEN_COMP_CLANG)
  843. #define EIGEN_USE_STD_FPCLASSIFY 1
  844. #else
  845. #define EIGEN_USE_STD_FPCLASSIFY 0
  846. #endif
  847. template<typename T>
  848. EIGEN_DEVICE_FUNC
  849. typename internal::enable_if<internal::is_integral<T>::value,bool>::type
  850. isnan_impl(const T&) { return false; }
  851. template<typename T>
  852. EIGEN_DEVICE_FUNC
  853. typename internal::enable_if<internal::is_integral<T>::value,bool>::type
  854. isinf_impl(const T&) { return false; }
  855. template<typename T>
  856. EIGEN_DEVICE_FUNC
  857. typename internal::enable_if<internal::is_integral<T>::value,bool>::type
  858. isfinite_impl(const T&) { return true; }
  859. template<typename T>
  860. EIGEN_DEVICE_FUNC
  861. typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
  862. isfinite_impl(const T& x)
  863. {
  864. #if defined(EIGEN_GPU_COMPILE_PHASE)
  865. return (::isfinite)(x);
  866. #elif EIGEN_USE_STD_FPCLASSIFY
  867. using std::isfinite;
  868. return isfinite EIGEN_NOT_A_MACRO (x);
  869. #else
  870. return x<=NumTraits<T>::highest() && x>=NumTraits<T>::lowest();
  871. #endif
  872. }
  873. template<typename T>
  874. EIGEN_DEVICE_FUNC
  875. typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
  876. isinf_impl(const T& x)
  877. {
  878. #if defined(EIGEN_GPU_COMPILE_PHASE)
  879. return (::isinf)(x);
  880. #elif EIGEN_USE_STD_FPCLASSIFY
  881. using std::isinf;
  882. return isinf EIGEN_NOT_A_MACRO (x);
  883. #else
  884. return x>NumTraits<T>::highest() || x<NumTraits<T>::lowest();
  885. #endif
  886. }
  887. template<typename T>
  888. EIGEN_DEVICE_FUNC
  889. typename internal::enable_if<(!internal::is_integral<T>::value)&&(!NumTraits<T>::IsComplex),bool>::type
  890. isnan_impl(const T& x)
  891. {
  892. #if defined(EIGEN_GPU_COMPILE_PHASE)
  893. return (::isnan)(x);
  894. #elif EIGEN_USE_STD_FPCLASSIFY
  895. using std::isnan;
  896. return isnan EIGEN_NOT_A_MACRO (x);
  897. #else
  898. return x != x;
  899. #endif
  900. }
  901. #if (!EIGEN_USE_STD_FPCLASSIFY)
  902. #if EIGEN_COMP_MSVC
  903. template<typename T> EIGEN_DEVICE_FUNC bool isinf_msvc_helper(T x)
  904. {
  905. return _fpclass(x)==_FPCLASS_NINF || _fpclass(x)==_FPCLASS_PINF;
  906. }
  907. //MSVC defines a _isnan builtin function, but for double only
  908. EIGEN_DEVICE_FUNC inline bool isnan_impl(const long double& x) { return _isnan(x)!=0; }
  909. EIGEN_DEVICE_FUNC inline bool isnan_impl(const double& x) { return _isnan(x)!=0; }
  910. EIGEN_DEVICE_FUNC inline bool isnan_impl(const float& x) { return _isnan(x)!=0; }
  911. EIGEN_DEVICE_FUNC inline bool isinf_impl(const long double& x) { return isinf_msvc_helper(x); }
  912. EIGEN_DEVICE_FUNC inline bool isinf_impl(const double& x) { return isinf_msvc_helper(x); }
  913. EIGEN_DEVICE_FUNC inline bool isinf_impl(const float& x) { return isinf_msvc_helper(x); }
  914. #elif (defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ && EIGEN_COMP_GNUC)
  915. #if EIGEN_GNUC_AT_LEAST(5,0)
  916. #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((optimize("no-finite-math-only")))
  917. #else
  918. // NOTE the inline qualifier and noinline attribute are both needed: the former is to avoid linking issue (duplicate symbol),
  919. // while the second prevent too aggressive optimizations in fast-math mode:
  920. #define EIGEN_TMP_NOOPT_ATTRIB EIGEN_DEVICE_FUNC inline __attribute__((noinline,optimize("no-finite-math-only")))
  921. #endif
  922. template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const long double& x) { return __builtin_isnan(x); }
  923. template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const double& x) { return __builtin_isnan(x); }
  924. template<> EIGEN_TMP_NOOPT_ATTRIB bool isnan_impl(const float& x) { return __builtin_isnan(x); }
  925. template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const double& x) { return __builtin_isinf(x); }
  926. template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const float& x) { return __builtin_isinf(x); }
  927. template<> EIGEN_TMP_NOOPT_ATTRIB bool isinf_impl(const long double& x) { return __builtin_isinf(x); }
  928. #undef EIGEN_TMP_NOOPT_ATTRIB
  929. #endif
  930. #endif
  931. // The following overload are defined at the end of this file
  932. template<typename T> EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x);
  933. template<typename T> EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x);
  934. template<typename T> EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x);
  935. template<typename T> T generic_fast_tanh_float(const T& a_x);
  936. } // end namespace internal
  937. /****************************************************************************
  938. * Generic math functions *
  939. ****************************************************************************/
  940. namespace numext {
  941. #if (!defined(EIGEN_GPUCC) || defined(EIGEN_CONSTEXPR_ARE_DEVICE_FUNC))
  942. template<typename T>
  943. EIGEN_DEVICE_FUNC
  944. EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
  945. {
  946. EIGEN_USING_STD(min)
  947. return min EIGEN_NOT_A_MACRO (x,y);
  948. }
  949. template<typename T>
  950. EIGEN_DEVICE_FUNC
  951. EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
  952. {
  953. EIGEN_USING_STD(max)
  954. return max EIGEN_NOT_A_MACRO (x,y);
  955. }
  956. #else
  957. template<typename T>
  958. EIGEN_DEVICE_FUNC
  959. EIGEN_ALWAYS_INLINE T mini(const T& x, const T& y)
  960. {
  961. return y < x ? y : x;
  962. }
  963. template<>
  964. EIGEN_DEVICE_FUNC
  965. EIGEN_ALWAYS_INLINE float mini(const float& x, const float& y)
  966. {
  967. return fminf(x, y);
  968. }
  969. template<>
  970. EIGEN_DEVICE_FUNC
  971. EIGEN_ALWAYS_INLINE double mini(const double& x, const double& y)
  972. {
  973. return fmin(x, y);
  974. }
  975. template<>
  976. EIGEN_DEVICE_FUNC
  977. EIGEN_ALWAYS_INLINE long double mini(const long double& x, const long double& y)
  978. {
  979. #if defined(EIGEN_HIPCC)
  980. // no "fminl" on HIP yet
  981. return (x < y) ? x : y;
  982. #else
  983. return fminl(x, y);
  984. #endif
  985. }
  986. template<typename T>
  987. EIGEN_DEVICE_FUNC
  988. EIGEN_ALWAYS_INLINE T maxi(const T& x, const T& y)
  989. {
  990. return x < y ? y : x;
  991. }
  992. template<>
  993. EIGEN_DEVICE_FUNC
  994. EIGEN_ALWAYS_INLINE float maxi(const float& x, const float& y)
  995. {
  996. return fmaxf(x, y);
  997. }
  998. template<>
  999. EIGEN_DEVICE_FUNC
  1000. EIGEN_ALWAYS_INLINE double maxi(const double& x, const double& y)
  1001. {
  1002. return fmax(x, y);
  1003. }
  1004. template<>
  1005. EIGEN_DEVICE_FUNC
  1006. EIGEN_ALWAYS_INLINE long double maxi(const long double& x, const long double& y)
  1007. {
  1008. #if defined(EIGEN_HIPCC)
  1009. // no "fmaxl" on HIP yet
  1010. return (x > y) ? x : y;
  1011. #else
  1012. return fmaxl(x, y);
  1013. #endif
  1014. }
  1015. #endif
  1016. #if defined(SYCL_DEVICE_ONLY)
  1017. #define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
  1018. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
  1019. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
  1020. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
  1021. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
  1022. #define SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
  1023. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_char) \
  1024. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_short) \
  1025. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_int) \
  1026. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_long)
  1027. #define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
  1028. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
  1029. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
  1030. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
  1031. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
  1032. #define SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
  1033. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uchar) \
  1034. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ushort) \
  1035. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_uint) \
  1036. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_ulong)
  1037. #define SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(NAME, FUNC) \
  1038. SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY(NAME, FUNC) \
  1039. SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY(NAME, FUNC)
  1040. #define SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(NAME, FUNC) \
  1041. SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY(NAME, FUNC) \
  1042. SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY(NAME, FUNC)
  1043. #define SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(NAME, FUNC) \
  1044. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
  1045. SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC,cl::sycl::cl_double)
  1046. #define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(NAME, FUNC) \
  1047. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, cl::sycl::cl_float) \
  1048. SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC,cl::sycl::cl_double)
  1049. #define SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(NAME, FUNC, RET_TYPE) \
  1050. SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_float) \
  1051. SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, cl::sycl::cl_double)
  1052. #define SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
  1053. template<> \
  1054. EIGEN_DEVICE_FUNC \
  1055. EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE& x) { \
  1056. return cl::sycl::FUNC(x); \
  1057. }
  1058. #define SYCL_SPECIALIZE_UNARY_FUNC(NAME, FUNC, TYPE) \
  1059. SYCL_SPECIALIZE_GEN_UNARY_FUNC(NAME, FUNC, TYPE, TYPE)
  1060. #define SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE1, ARG_TYPE2) \
  1061. template<> \
  1062. EIGEN_DEVICE_FUNC \
  1063. EIGEN_ALWAYS_INLINE RET_TYPE NAME(const ARG_TYPE1& x, const ARG_TYPE2& y) { \
  1064. return cl::sycl::FUNC(x, y); \
  1065. }
  1066. #define SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE) \
  1067. SYCL_SPECIALIZE_GEN1_BINARY_FUNC(NAME, FUNC, RET_TYPE, ARG_TYPE, ARG_TYPE)
  1068. #define SYCL_SPECIALIZE_BINARY_FUNC(NAME, FUNC, TYPE) \
  1069. SYCL_SPECIALIZE_GEN2_BINARY_FUNC(NAME, FUNC, TYPE, TYPE)
  1070. SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(mini, min)
  1071. SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(mini, fmin)
  1072. SYCL_SPECIALIZE_INTEGER_TYPES_BINARY(maxi, max)
  1073. SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(maxi, fmax)
  1074. #endif
  1075. template<typename Scalar>
  1076. EIGEN_DEVICE_FUNC
  1077. inline EIGEN_MATHFUNC_RETVAL(real, Scalar) real(const Scalar& x)
  1078. {
  1079. return EIGEN_MATHFUNC_IMPL(real, Scalar)::run(x);
  1080. }
  1081. template<typename Scalar>
  1082. EIGEN_DEVICE_FUNC
  1083. inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(const Scalar& x)
  1084. {
  1085. return internal::real_ref_impl<Scalar>::run(x);
  1086. }
  1087. template<typename Scalar>
  1088. EIGEN_DEVICE_FUNC
  1089. inline EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) real_ref(Scalar& x)
  1090. {
  1091. return EIGEN_MATHFUNC_IMPL(real_ref, Scalar)::run(x);
  1092. }
  1093. template<typename Scalar>
  1094. EIGEN_DEVICE_FUNC
  1095. inline EIGEN_MATHFUNC_RETVAL(imag, Scalar) imag(const Scalar& x)
  1096. {
  1097. return EIGEN_MATHFUNC_IMPL(imag, Scalar)::run(x);
  1098. }
  1099. template<typename Scalar>
  1100. EIGEN_DEVICE_FUNC
  1101. inline EIGEN_MATHFUNC_RETVAL(arg, Scalar) arg(const Scalar& x)
  1102. {
  1103. return EIGEN_MATHFUNC_IMPL(arg, Scalar)::run(x);
  1104. }
  1105. template<typename Scalar>
  1106. EIGEN_DEVICE_FUNC
  1107. inline typename internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(const Scalar& x)
  1108. {
  1109. return internal::imag_ref_impl<Scalar>::run(x);
  1110. }
  1111. template<typename Scalar>
  1112. EIGEN_DEVICE_FUNC
  1113. inline EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) imag_ref(Scalar& x)
  1114. {
  1115. return EIGEN_MATHFUNC_IMPL(imag_ref, Scalar)::run(x);
  1116. }
  1117. template<typename Scalar>
  1118. EIGEN_DEVICE_FUNC
  1119. inline EIGEN_MATHFUNC_RETVAL(conj, Scalar) conj(const Scalar& x)
  1120. {
  1121. return EIGEN_MATHFUNC_IMPL(conj, Scalar)::run(x);
  1122. }
  1123. template<typename Scalar>
  1124. EIGEN_DEVICE_FUNC
  1125. inline EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(const Scalar& x)
  1126. {
  1127. return EIGEN_MATHFUNC_IMPL(abs2, Scalar)::run(x);
  1128. }
  1129. EIGEN_DEVICE_FUNC
  1130. inline bool abs2(bool x) { return x; }
  1131. template<typename T>
  1132. EIGEN_DEVICE_FUNC
  1133. EIGEN_ALWAYS_INLINE T absdiff(const T& x, const T& y)
  1134. {
  1135. return x > y ? x - y : y - x;
  1136. }
  1137. template<>
  1138. EIGEN_DEVICE_FUNC
  1139. EIGEN_ALWAYS_INLINE float absdiff(const float& x, const float& y)
  1140. {
  1141. return fabsf(x - y);
  1142. }
  1143. template<>
  1144. EIGEN_DEVICE_FUNC
  1145. EIGEN_ALWAYS_INLINE double absdiff(const double& x, const double& y)
  1146. {
  1147. return fabs(x - y);
  1148. }
  1149. #if !defined(EIGEN_GPUCC)
  1150. // HIP and CUDA do not support long double.
  1151. template<>
  1152. EIGEN_DEVICE_FUNC
  1153. EIGEN_ALWAYS_INLINE long double absdiff(const long double& x, const long double& y) {
  1154. return fabsl(x - y);
  1155. }
  1156. #endif
  1157. template<typename Scalar>
  1158. EIGEN_DEVICE_FUNC
  1159. inline EIGEN_MATHFUNC_RETVAL(norm1, Scalar) norm1(const Scalar& x)
  1160. {
  1161. return EIGEN_MATHFUNC_IMPL(norm1, Scalar)::run(x);
  1162. }
  1163. template<typename Scalar>
  1164. EIGEN_DEVICE_FUNC
  1165. inline EIGEN_MATHFUNC_RETVAL(hypot, Scalar) hypot(const Scalar& x, const Scalar& y)
  1166. {
  1167. return EIGEN_MATHFUNC_IMPL(hypot, Scalar)::run(x, y);
  1168. }
  1169. #if defined(SYCL_DEVICE_ONLY)
  1170. SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(hypot, hypot)
  1171. #endif
  1172. template<typename Scalar>
  1173. EIGEN_DEVICE_FUNC
  1174. inline EIGEN_MATHFUNC_RETVAL(log1p, Scalar) log1p(const Scalar& x)
  1175. {
  1176. return EIGEN_MATHFUNC_IMPL(log1p, Scalar)::run(x);
  1177. }
  1178. #if defined(SYCL_DEVICE_ONLY)
  1179. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log1p, log1p)
  1180. #endif
  1181. #if defined(EIGEN_GPUCC)
  1182. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1183. float log1p(const float &x) { return ::log1pf(x); }
  1184. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1185. double log1p(const double &x) { return ::log1p(x); }
  1186. #endif
  1187. template<typename ScalarX,typename ScalarY>
  1188. EIGEN_DEVICE_FUNC
  1189. inline typename internal::pow_impl<ScalarX,ScalarY>::result_type pow(const ScalarX& x, const ScalarY& y)
  1190. {
  1191. return internal::pow_impl<ScalarX,ScalarY>::run(x, y);
  1192. }
  1193. #if defined(SYCL_DEVICE_ONLY)
  1194. SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(pow, pow)
  1195. #endif
  1196. template<typename T> EIGEN_DEVICE_FUNC bool (isnan) (const T &x) { return internal::isnan_impl(x); }
  1197. template<typename T> EIGEN_DEVICE_FUNC bool (isinf) (const T &x) { return internal::isinf_impl(x); }
  1198. template<typename T> EIGEN_DEVICE_FUNC bool (isfinite)(const T &x) { return internal::isfinite_impl(x); }
  1199. #if defined(SYCL_DEVICE_ONLY)
  1200. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isnan, isnan, bool)
  1201. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isinf, isinf, bool)
  1202. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE(isfinite, isfinite, bool)
  1203. #endif
  1204. template<typename Scalar>
  1205. EIGEN_DEVICE_FUNC
  1206. inline EIGEN_MATHFUNC_RETVAL(rint, Scalar) rint(const Scalar& x)
  1207. {
  1208. return EIGEN_MATHFUNC_IMPL(rint, Scalar)::run(x);
  1209. }
  1210. template<typename Scalar>
  1211. EIGEN_DEVICE_FUNC
  1212. inline EIGEN_MATHFUNC_RETVAL(round, Scalar) round(const Scalar& x)
  1213. {
  1214. return EIGEN_MATHFUNC_IMPL(round, Scalar)::run(x);
  1215. }
  1216. #if defined(SYCL_DEVICE_ONLY)
  1217. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(round, round)
  1218. #endif
  1219. template<typename T>
  1220. EIGEN_DEVICE_FUNC
  1221. T (floor)(const T& x)
  1222. {
  1223. EIGEN_USING_STD(floor)
  1224. return floor(x);
  1225. }
  1226. #if defined(SYCL_DEVICE_ONLY)
  1227. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(floor, floor)
  1228. #endif
  1229. #if defined(EIGEN_GPUCC)
  1230. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1231. float floor(const float &x) { return ::floorf(x); }
  1232. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1233. double floor(const double &x) { return ::floor(x); }
  1234. #endif
  1235. template<typename T>
  1236. EIGEN_DEVICE_FUNC
  1237. T (ceil)(const T& x)
  1238. {
  1239. EIGEN_USING_STD(ceil);
  1240. return ceil(x);
  1241. }
  1242. #if defined(SYCL_DEVICE_ONLY)
  1243. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(ceil, ceil)
  1244. #endif
  1245. #if defined(EIGEN_GPUCC)
  1246. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1247. float ceil(const float &x) { return ::ceilf(x); }
  1248. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1249. double ceil(const double &x) { return ::ceil(x); }
  1250. #endif
  1251. /** Log base 2 for 32 bits positive integers.
  1252. * Conveniently returns 0 for x==0. */
  1253. inline int log2(int x)
  1254. {
  1255. eigen_assert(x>=0);
  1256. unsigned int v(x);
  1257. static const int table[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 };
  1258. v |= v >> 1;
  1259. v |= v >> 2;
  1260. v |= v >> 4;
  1261. v |= v >> 8;
  1262. v |= v >> 16;
  1263. return table[(v * 0x07C4ACDDU) >> 27];
  1264. }
  1265. /** \returns the square root of \a x.
  1266. *
  1267. * It is essentially equivalent to
  1268. * \code using std::sqrt; return sqrt(x); \endcode
  1269. * but slightly faster for float/double and some compilers (e.g., gcc), thanks to
  1270. * specializations when SSE is enabled.
  1271. *
  1272. * It's usage is justified in performance critical functions, like norm/normalize.
  1273. */
  1274. template<typename Scalar>
  1275. EIGEN_DEVICE_FUNC
  1276. EIGEN_ALWAYS_INLINE EIGEN_MATHFUNC_RETVAL(sqrt, Scalar) sqrt(const Scalar& x)
  1277. {
  1278. return EIGEN_MATHFUNC_IMPL(sqrt, Scalar)::run(x);
  1279. }
  1280. // Boolean specialization, avoids implicit float to bool conversion (-Wimplicit-conversion-floating-point-to-bool).
  1281. template<>
  1282. EIGEN_DEFINE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS EIGEN_DEVICE_FUNC
  1283. bool sqrt<bool>(const bool &x) { return x; }
  1284. #if defined(SYCL_DEVICE_ONLY)
  1285. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sqrt, sqrt)
  1286. #endif
  1287. /** \returns the reciprocal square root of \a x. **/
  1288. template<typename T>
  1289. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1290. T rsqrt(const T& x)
  1291. {
  1292. return internal::rsqrt_impl<T>::run(x);
  1293. }
  1294. template<typename T>
  1295. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1296. T log(const T &x) {
  1297. return internal::log_impl<T>::run(x);
  1298. }
  1299. #if defined(SYCL_DEVICE_ONLY)
  1300. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(log, log)
  1301. #endif
  1302. #if defined(EIGEN_GPUCC)
  1303. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1304. float log(const float &x) { return ::logf(x); }
  1305. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1306. double log(const double &x) { return ::log(x); }
  1307. #endif
  1308. template<typename T>
  1309. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1310. typename internal::enable_if<NumTraits<T>::IsSigned || NumTraits<T>::IsComplex,typename NumTraits<T>::Real>::type
  1311. abs(const T &x) {
  1312. EIGEN_USING_STD(abs);
  1313. return abs(x);
  1314. }
  1315. template<typename T>
  1316. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1317. typename internal::enable_if<!(NumTraits<T>::IsSigned || NumTraits<T>::IsComplex),typename NumTraits<T>::Real>::type
  1318. abs(const T &x) {
  1319. return x;
  1320. }
  1321. #if defined(SYCL_DEVICE_ONLY)
  1322. SYCL_SPECIALIZE_INTEGER_TYPES_UNARY(abs, abs)
  1323. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(abs, fabs)
  1324. #endif
  1325. #if defined(EIGEN_GPUCC)
  1326. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1327. float abs(const float &x) { return ::fabsf(x); }
  1328. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1329. double abs(const double &x) { return ::fabs(x); }
  1330. template <> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1331. float abs(const std::complex<float>& x) {
  1332. return ::hypotf(x.real(), x.imag());
  1333. }
  1334. template <> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1335. double abs(const std::complex<double>& x) {
  1336. return ::hypot(x.real(), x.imag());
  1337. }
  1338. #endif
  1339. template<typename T>
  1340. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1341. T exp(const T &x) {
  1342. EIGEN_USING_STD(exp);
  1343. return exp(x);
  1344. }
  1345. #if defined(SYCL_DEVICE_ONLY)
  1346. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(exp, exp)
  1347. #endif
  1348. #if defined(EIGEN_GPUCC)
  1349. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1350. float exp(const float &x) { return ::expf(x); }
  1351. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1352. double exp(const double &x) { return ::exp(x); }
  1353. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1354. std::complex<float> exp(const std::complex<float>& x) {
  1355. float com = ::expf(x.real());
  1356. float res_real = com * ::cosf(x.imag());
  1357. float res_imag = com * ::sinf(x.imag());
  1358. return std::complex<float>(res_real, res_imag);
  1359. }
  1360. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1361. std::complex<double> exp(const std::complex<double>& x) {
  1362. double com = ::exp(x.real());
  1363. double res_real = com * ::cos(x.imag());
  1364. double res_imag = com * ::sin(x.imag());
  1365. return std::complex<double>(res_real, res_imag);
  1366. }
  1367. #endif
  1368. template<typename Scalar>
  1369. EIGEN_DEVICE_FUNC
  1370. inline EIGEN_MATHFUNC_RETVAL(expm1, Scalar) expm1(const Scalar& x)
  1371. {
  1372. return EIGEN_MATHFUNC_IMPL(expm1, Scalar)::run(x);
  1373. }
  1374. #if defined(SYCL_DEVICE_ONLY)
  1375. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(expm1, expm1)
  1376. #endif
  1377. #if defined(EIGEN_GPUCC)
  1378. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1379. float expm1(const float &x) { return ::expm1f(x); }
  1380. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1381. double expm1(const double &x) { return ::expm1(x); }
  1382. #endif
  1383. template<typename T>
  1384. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1385. T cos(const T &x) {
  1386. EIGEN_USING_STD(cos);
  1387. return cos(x);
  1388. }
  1389. #if defined(SYCL_DEVICE_ONLY)
  1390. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cos,cos)
  1391. #endif
  1392. #if defined(EIGEN_GPUCC)
  1393. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1394. float cos(const float &x) { return ::cosf(x); }
  1395. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1396. double cos(const double &x) { return ::cos(x); }
  1397. #endif
  1398. template<typename T>
  1399. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1400. T sin(const T &x) {
  1401. EIGEN_USING_STD(sin);
  1402. return sin(x);
  1403. }
  1404. #if defined(SYCL_DEVICE_ONLY)
  1405. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sin, sin)
  1406. #endif
  1407. #if defined(EIGEN_GPUCC)
  1408. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1409. float sin(const float &x) { return ::sinf(x); }
  1410. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1411. double sin(const double &x) { return ::sin(x); }
  1412. #endif
  1413. template<typename T>
  1414. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1415. T tan(const T &x) {
  1416. EIGEN_USING_STD(tan);
  1417. return tan(x);
  1418. }
  1419. #if defined(SYCL_DEVICE_ONLY)
  1420. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tan, tan)
  1421. #endif
  1422. #if defined(EIGEN_GPUCC)
  1423. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1424. float tan(const float &x) { return ::tanf(x); }
  1425. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1426. double tan(const double &x) { return ::tan(x); }
  1427. #endif
  1428. template<typename T>
  1429. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1430. T acos(const T &x) {
  1431. EIGEN_USING_STD(acos);
  1432. return acos(x);
  1433. }
  1434. #if EIGEN_HAS_CXX11_MATH
  1435. template<typename T>
  1436. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1437. T acosh(const T &x) {
  1438. EIGEN_USING_STD(acosh);
  1439. return static_cast<T>(acosh(x));
  1440. }
  1441. #endif
  1442. #if defined(SYCL_DEVICE_ONLY)
  1443. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acos, acos)
  1444. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(acosh, acosh)
  1445. #endif
  1446. #if defined(EIGEN_GPUCC)
  1447. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1448. float acos(const float &x) { return ::acosf(x); }
  1449. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1450. double acos(const double &x) { return ::acos(x); }
  1451. #endif
  1452. template<typename T>
  1453. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1454. T asin(const T &x) {
  1455. EIGEN_USING_STD(asin);
  1456. return asin(x);
  1457. }
  1458. #if EIGEN_HAS_CXX11_MATH
  1459. template<typename T>
  1460. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1461. T asinh(const T &x) {
  1462. EIGEN_USING_STD(asinh);
  1463. return static_cast<T>(asinh(x));
  1464. }
  1465. #endif
  1466. #if defined(SYCL_DEVICE_ONLY)
  1467. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asin, asin)
  1468. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(asinh, asinh)
  1469. #endif
  1470. #if defined(EIGEN_GPUCC)
  1471. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1472. float asin(const float &x) { return ::asinf(x); }
  1473. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1474. double asin(const double &x) { return ::asin(x); }
  1475. #endif
  1476. template<typename T>
  1477. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1478. T atan(const T &x) {
  1479. EIGEN_USING_STD(atan);
  1480. return static_cast<T>(atan(x));
  1481. }
  1482. #if EIGEN_HAS_CXX11_MATH
  1483. template<typename T>
  1484. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1485. T atanh(const T &x) {
  1486. EIGEN_USING_STD(atanh);
  1487. return static_cast<T>(atanh(x));
  1488. }
  1489. #endif
  1490. #if defined(SYCL_DEVICE_ONLY)
  1491. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atan, atan)
  1492. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(atanh, atanh)
  1493. #endif
  1494. #if defined(EIGEN_GPUCC)
  1495. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1496. float atan(const float &x) { return ::atanf(x); }
  1497. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1498. double atan(const double &x) { return ::atan(x); }
  1499. #endif
  1500. template<typename T>
  1501. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1502. T cosh(const T &x) {
  1503. EIGEN_USING_STD(cosh);
  1504. return static_cast<T>(cosh(x));
  1505. }
  1506. #if defined(SYCL_DEVICE_ONLY)
  1507. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(cosh, cosh)
  1508. #endif
  1509. #if defined(EIGEN_GPUCC)
  1510. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1511. float cosh(const float &x) { return ::coshf(x); }
  1512. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1513. double cosh(const double &x) { return ::cosh(x); }
  1514. #endif
  1515. template<typename T>
  1516. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1517. T sinh(const T &x) {
  1518. EIGEN_USING_STD(sinh);
  1519. return static_cast<T>(sinh(x));
  1520. }
  1521. #if defined(SYCL_DEVICE_ONLY)
  1522. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(sinh, sinh)
  1523. #endif
  1524. #if defined(EIGEN_GPUCC)
  1525. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1526. float sinh(const float &x) { return ::sinhf(x); }
  1527. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1528. double sinh(const double &x) { return ::sinh(x); }
  1529. #endif
  1530. template<typename T>
  1531. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1532. T tanh(const T &x) {
  1533. EIGEN_USING_STD(tanh);
  1534. return tanh(x);
  1535. }
  1536. #if (!defined(EIGEN_GPUCC)) && EIGEN_FAST_MATH && !defined(SYCL_DEVICE_ONLY)
  1537. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1538. float tanh(float x) { return internal::generic_fast_tanh_float(x); }
  1539. #endif
  1540. #if defined(SYCL_DEVICE_ONLY)
  1541. SYCL_SPECIALIZE_FLOATING_TYPES_UNARY(tanh, tanh)
  1542. #endif
  1543. #if defined(EIGEN_GPUCC)
  1544. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1545. float tanh(const float &x) { return ::tanhf(x); }
  1546. template<> EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1547. double tanh(const double &x) { return ::tanh(x); }
  1548. #endif
  1549. template <typename T>
  1550. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1551. T fmod(const T& a, const T& b) {
  1552. EIGEN_USING_STD(fmod);
  1553. return fmod(a, b);
  1554. }
  1555. #if defined(SYCL_DEVICE_ONLY)
  1556. SYCL_SPECIALIZE_FLOATING_TYPES_BINARY(fmod, fmod)
  1557. #endif
  1558. #if defined(EIGEN_GPUCC)
  1559. template <>
  1560. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1561. float fmod(const float& a, const float& b) {
  1562. return ::fmodf(a, b);
  1563. }
  1564. template <>
  1565. EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
  1566. double fmod(const double& a, const double& b) {
  1567. return ::fmod(a, b);
  1568. }
  1569. #endif
  1570. #if defined(SYCL_DEVICE_ONLY)
  1571. #undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_BINARY
  1572. #undef SYCL_SPECIALIZE_SIGNED_INTEGER_TYPES_UNARY
  1573. #undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_BINARY
  1574. #undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY
  1575. #undef SYCL_SPECIALIZE_INTEGER_TYPES_BINARY
  1576. #undef SYCL_SPECIALIZE_UNSIGNED_INTEGER_TYPES_UNARY
  1577. #undef SYCL_SPECIALIZE_FLOATING_TYPES_BINARY
  1578. #undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY
  1579. #undef SYCL_SPECIALIZE_FLOATING_TYPES_UNARY_FUNC_RET_TYPE
  1580. #undef SYCL_SPECIALIZE_GEN_UNARY_FUNC
  1581. #undef SYCL_SPECIALIZE_UNARY_FUNC
  1582. #undef SYCL_SPECIALIZE_GEN1_BINARY_FUNC
  1583. #undef SYCL_SPECIALIZE_GEN2_BINARY_FUNC
  1584. #undef SYCL_SPECIALIZE_BINARY_FUNC
  1585. #endif
  1586. } // end namespace numext
  1587. namespace internal {
  1588. template<typename T>
  1589. EIGEN_DEVICE_FUNC bool isfinite_impl(const std::complex<T>& x)
  1590. {
  1591. return (numext::isfinite)(numext::real(x)) && (numext::isfinite)(numext::imag(x));
  1592. }
  1593. template<typename T>
  1594. EIGEN_DEVICE_FUNC bool isnan_impl(const std::complex<T>& x)
  1595. {
  1596. return (numext::isnan)(numext::real(x)) || (numext::isnan)(numext::imag(x));
  1597. }
  1598. template<typename T>
  1599. EIGEN_DEVICE_FUNC bool isinf_impl(const std::complex<T>& x)
  1600. {
  1601. return ((numext::isinf)(numext::real(x)) || (numext::isinf)(numext::imag(x))) && (!(numext::isnan)(x));
  1602. }
  1603. /****************************************************************************
  1604. * Implementation of fuzzy comparisons *
  1605. ****************************************************************************/
  1606. template<typename Scalar,
  1607. bool IsComplex,
  1608. bool IsInteger>
  1609. struct scalar_fuzzy_default_impl {};
  1610. template<typename Scalar>
  1611. struct scalar_fuzzy_default_impl<Scalar, false, false>
  1612. {
  1613. typedef typename NumTraits<Scalar>::Real RealScalar;
  1614. template<typename OtherScalar> EIGEN_DEVICE_FUNC
  1615. static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
  1616. {
  1617. return numext::abs(x) <= numext::abs(y) * prec;
  1618. }
  1619. EIGEN_DEVICE_FUNC
  1620. static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
  1621. {
  1622. return numext::abs(x - y) <= numext::mini(numext::abs(x), numext::abs(y)) * prec;
  1623. }
  1624. EIGEN_DEVICE_FUNC
  1625. static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar& prec)
  1626. {
  1627. return x <= y || isApprox(x, y, prec);
  1628. }
  1629. };
  1630. template<typename Scalar>
  1631. struct scalar_fuzzy_default_impl<Scalar, false, true>
  1632. {
  1633. typedef typename NumTraits<Scalar>::Real RealScalar;
  1634. template<typename OtherScalar> EIGEN_DEVICE_FUNC
  1635. static inline bool isMuchSmallerThan(const Scalar& x, const Scalar&, const RealScalar&)
  1636. {
  1637. return x == Scalar(0);
  1638. }
  1639. EIGEN_DEVICE_FUNC
  1640. static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar&)
  1641. {
  1642. return x == y;
  1643. }
  1644. EIGEN_DEVICE_FUNC
  1645. static inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y, const RealScalar&)
  1646. {
  1647. return x <= y;
  1648. }
  1649. };
  1650. template<typename Scalar>
  1651. struct scalar_fuzzy_default_impl<Scalar, true, false>
  1652. {
  1653. typedef typename NumTraits<Scalar>::Real RealScalar;
  1654. template<typename OtherScalar> EIGEN_DEVICE_FUNC
  1655. static inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y, const RealScalar& prec)
  1656. {
  1657. return numext::abs2(x) <= numext::abs2(y) * prec * prec;
  1658. }
  1659. EIGEN_DEVICE_FUNC
  1660. static inline bool isApprox(const Scalar& x, const Scalar& y, const RealScalar& prec)
  1661. {
  1662. return numext::abs2(x - y) <= numext::mini(numext::abs2(x), numext::abs2(y)) * prec * prec;
  1663. }
  1664. };
  1665. template<typename Scalar>
  1666. struct scalar_fuzzy_impl : scalar_fuzzy_default_impl<Scalar, NumTraits<Scalar>::IsComplex, NumTraits<Scalar>::IsInteger> {};
  1667. template<typename Scalar, typename OtherScalar> EIGEN_DEVICE_FUNC
  1668. inline bool isMuchSmallerThan(const Scalar& x, const OtherScalar& y,
  1669. const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
  1670. {
  1671. return scalar_fuzzy_impl<Scalar>::template isMuchSmallerThan<OtherScalar>(x, y, precision);
  1672. }
  1673. template<typename Scalar> EIGEN_DEVICE_FUNC
  1674. inline bool isApprox(const Scalar& x, const Scalar& y,
  1675. const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
  1676. {
  1677. return scalar_fuzzy_impl<Scalar>::isApprox(x, y, precision);
  1678. }
  1679. template<typename Scalar> EIGEN_DEVICE_FUNC
  1680. inline bool isApproxOrLessThan(const Scalar& x, const Scalar& y,
  1681. const typename NumTraits<Scalar>::Real &precision = NumTraits<Scalar>::dummy_precision())
  1682. {
  1683. return scalar_fuzzy_impl<Scalar>::isApproxOrLessThan(x, y, precision);
  1684. }
  1685. /******************************************
  1686. *** The special case of the bool type ***
  1687. ******************************************/
  1688. template<> struct random_impl<bool>
  1689. {
  1690. static inline bool run()
  1691. {
  1692. return random<int>(0,1)==0 ? false : true;
  1693. }
  1694. static inline bool run(const bool& a, const bool& b)
  1695. {
  1696. return random<int>(a, b)==0 ? false : true;
  1697. }
  1698. };
  1699. template<> struct scalar_fuzzy_impl<bool>
  1700. {
  1701. typedef bool RealScalar;
  1702. template<typename OtherScalar> EIGEN_DEVICE_FUNC
  1703. static inline bool isMuchSmallerThan(const bool& x, const bool&, const bool&)
  1704. {
  1705. return !x;
  1706. }
  1707. EIGEN_DEVICE_FUNC
  1708. static inline bool isApprox(bool x, bool y, bool)
  1709. {
  1710. return x == y;
  1711. }
  1712. EIGEN_DEVICE_FUNC
  1713. static inline bool isApproxOrLessThan(const bool& x, const bool& y, const bool&)
  1714. {
  1715. return (!x) || y;
  1716. }
  1717. };
  1718. } // end namespace internal
  1719. // Default implementations that rely on other numext implementations
  1720. namespace internal {
  1721. // Specialization for complex types that are not supported by std::expm1.
  1722. template <typename RealScalar>
  1723. struct expm1_impl<std::complex<RealScalar> > {
  1724. EIGEN_DEVICE_FUNC static inline std::complex<RealScalar> run(
  1725. const std::complex<RealScalar>& x) {
  1726. EIGEN_STATIC_ASSERT_NON_INTEGER(RealScalar)
  1727. RealScalar xr = x.real();
  1728. RealScalar xi = x.imag();
  1729. // expm1(z) = exp(z) - 1
  1730. // = exp(x + i * y) - 1
  1731. // = exp(x) * (cos(y) + i * sin(y)) - 1
  1732. // = exp(x) * cos(y) - 1 + i * exp(x) * sin(y)
  1733. // Imag(expm1(z)) = exp(x) * sin(y)
  1734. // Real(expm1(z)) = exp(x) * cos(y) - 1
  1735. // = exp(x) * cos(y) - 1.
  1736. // = expm1(x) + exp(x) * (cos(y) - 1)
  1737. // = expm1(x) + exp(x) * (2 * sin(y / 2) ** 2)
  1738. RealScalar erm1 = numext::expm1<RealScalar>(xr);
  1739. RealScalar er = erm1 + RealScalar(1.);
  1740. RealScalar sin2 = numext::sin(xi / RealScalar(2.));
  1741. sin2 = sin2 * sin2;
  1742. RealScalar s = numext::sin(xi);
  1743. RealScalar real_part = erm1 - RealScalar(2.) * er * sin2;
  1744. return std::complex<RealScalar>(real_part, er * s);
  1745. }
  1746. };
  1747. template<typename T>
  1748. struct rsqrt_impl {
  1749. EIGEN_DEVICE_FUNC
  1750. static EIGEN_ALWAYS_INLINE T run(const T& x) {
  1751. return T(1)/numext::sqrt(x);
  1752. }
  1753. };
  1754. #if defined(EIGEN_GPU_COMPILE_PHASE)
  1755. template<typename T>
  1756. struct conj_impl<std::complex<T>, true>
  1757. {
  1758. EIGEN_DEVICE_FUNC
  1759. static inline std::complex<T> run(const std::complex<T>& x)
  1760. {
  1761. return std::complex<T>(numext::real(x), -numext::imag(x));
  1762. }
  1763. };
  1764. #endif
  1765. } // end namespace internal
  1766. } // end namespace Eigen
  1767. #endif // EIGEN_MATHFUNCTIONS_H