rs_cl.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. #include "rs_core.rsh"
  2. #include "rs_f16_util.h"
  3. extern float2 __attribute__((overloadable)) convert_float2(int2 c);
  4. extern float3 __attribute__((overloadable)) convert_float3(int3 c);
  5. extern float4 __attribute__((overloadable)) convert_float4(int4 c);
  6. extern int2 __attribute__((overloadable)) convert_int2(float2 c);
  7. extern int3 __attribute__((overloadable)) convert_int3(float3 c);
  8. extern int4 __attribute__((overloadable)) convert_int4(float4 c);
  9. extern float __attribute__((overloadable)) fmin(float v, float v2);
  10. extern float2 __attribute__((overloadable)) fmin(float2 v, float v2);
  11. extern float3 __attribute__((overloadable)) fmin(float3 v, float v2);
  12. extern float4 __attribute__((overloadable)) fmin(float4 v, float v2);
  13. extern float __attribute__((overloadable)) fmax(float v, float v2);
  14. extern float2 __attribute__((overloadable)) fmax(float2 v, float v2);
  15. extern float3 __attribute__((overloadable)) fmax(float3 v, float v2);
  16. extern float4 __attribute__((overloadable)) fmax(float4 v, float v2);
  17. // Float ops, 6.11.2
  18. #define FN_FUNC_FN(fnc) \
  19. extern float2 __attribute__((overloadable)) fnc(float2 v) { \
  20. float2 r; \
  21. r.x = fnc(v.x); \
  22. r.y = fnc(v.y); \
  23. return r; \
  24. } \
  25. extern float3 __attribute__((overloadable)) fnc(float3 v) { \
  26. float3 r; \
  27. r.x = fnc(v.x); \
  28. r.y = fnc(v.y); \
  29. r.z = fnc(v.z); \
  30. return r; \
  31. } \
  32. extern float4 __attribute__((overloadable)) fnc(float4 v) { \
  33. float4 r; \
  34. r.x = fnc(v.x); \
  35. r.y = fnc(v.y); \
  36. r.z = fnc(v.z); \
  37. r.w = fnc(v.w); \
  38. return r; \
  39. }
  40. #define IN_FUNC_FN(fnc) \
  41. extern int2 __attribute__((overloadable)) fnc(float2 v) { \
  42. int2 r; \
  43. r.x = fnc(v.x); \
  44. r.y = fnc(v.y); \
  45. return r; \
  46. } \
  47. extern int3 __attribute__((overloadable)) fnc(float3 v) { \
  48. int3 r; \
  49. r.x = fnc(v.x); \
  50. r.y = fnc(v.y); \
  51. r.z = fnc(v.z); \
  52. return r; \
  53. } \
  54. extern int4 __attribute__((overloadable)) fnc(float4 v) { \
  55. int4 r; \
  56. r.x = fnc(v.x); \
  57. r.y = fnc(v.y); \
  58. r.z = fnc(v.z); \
  59. r.w = fnc(v.w); \
  60. return r; \
  61. }
  62. #define FN_FUNC_FN_FN(fnc) \
  63. extern float2 __attribute__((overloadable)) fnc(float2 v1, float2 v2) { \
  64. float2 r; \
  65. r.x = fnc(v1.x, v2.x); \
  66. r.y = fnc(v1.y, v2.y); \
  67. return r; \
  68. } \
  69. extern float3 __attribute__((overloadable)) fnc(float3 v1, float3 v2) { \
  70. float3 r; \
  71. r.x = fnc(v1.x, v2.x); \
  72. r.y = fnc(v1.y, v2.y); \
  73. r.z = fnc(v1.z, v2.z); \
  74. return r; \
  75. } \
  76. extern float4 __attribute__((overloadable)) fnc(float4 v1, float4 v2) { \
  77. float4 r; \
  78. r.x = fnc(v1.x, v2.x); \
  79. r.y = fnc(v1.y, v2.y); \
  80. r.z = fnc(v1.z, v2.z); \
  81. r.w = fnc(v1.w, v2.w); \
  82. return r; \
  83. }
  84. #define FN_FUNC_FN_F(fnc) \
  85. extern float2 __attribute__((overloadable)) fnc(float2 v1, float v2) { \
  86. float2 r; \
  87. r.x = fnc(v1.x, v2); \
  88. r.y = fnc(v1.y, v2); \
  89. return r; \
  90. } \
  91. extern float3 __attribute__((overloadable)) fnc(float3 v1, float v2) { \
  92. float3 r; \
  93. r.x = fnc(v1.x, v2); \
  94. r.y = fnc(v1.y, v2); \
  95. r.z = fnc(v1.z, v2); \
  96. return r; \
  97. } \
  98. extern float4 __attribute__((overloadable)) fnc(float4 v1, float v2) { \
  99. float4 r; \
  100. r.x = fnc(v1.x, v2); \
  101. r.y = fnc(v1.y, v2); \
  102. r.z = fnc(v1.z, v2); \
  103. r.w = fnc(v1.w, v2); \
  104. return r; \
  105. }
  106. #define FN_FUNC_FN_IN(fnc) \
  107. extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 v2) { \
  108. float2 r; \
  109. r.x = fnc(v1.x, v2.x); \
  110. r.y = fnc(v1.y, v2.y); \
  111. return r; \
  112. } \
  113. extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 v2) { \
  114. float3 r; \
  115. r.x = fnc(v1.x, v2.x); \
  116. r.y = fnc(v1.y, v2.y); \
  117. r.z = fnc(v1.z, v2.z); \
  118. return r; \
  119. } \
  120. extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 v2) { \
  121. float4 r; \
  122. r.x = fnc(v1.x, v2.x); \
  123. r.y = fnc(v1.y, v2.y); \
  124. r.z = fnc(v1.z, v2.z); \
  125. r.w = fnc(v1.w, v2.w); \
  126. return r; \
  127. }
  128. #define FN_FUNC_FN_I(fnc) \
  129. extern float2 __attribute__((overloadable)) fnc(float2 v1, int v2) { \
  130. float2 r; \
  131. r.x = fnc(v1.x, v2); \
  132. r.y = fnc(v1.y, v2); \
  133. return r; \
  134. } \
  135. extern float3 __attribute__((overloadable)) fnc(float3 v1, int v2) { \
  136. float3 r; \
  137. r.x = fnc(v1.x, v2); \
  138. r.y = fnc(v1.y, v2); \
  139. r.z = fnc(v1.z, v2); \
  140. return r; \
  141. } \
  142. extern float4 __attribute__((overloadable)) fnc(float4 v1, int v2) { \
  143. float4 r; \
  144. r.x = fnc(v1.x, v2); \
  145. r.y = fnc(v1.y, v2); \
  146. r.z = fnc(v1.z, v2); \
  147. r.w = fnc(v1.w, v2); \
  148. return r; \
  149. }
  150. #define FN_FUNC_FN_PFN(fnc) \
  151. extern float2 __attribute__((overloadable)) \
  152. fnc(float2 v1, float2 *v2) { \
  153. float2 r; \
  154. float t[2]; \
  155. r.x = fnc(v1.x, &t[0]); \
  156. r.y = fnc(v1.y, &t[1]); \
  157. v2->x = t[0]; \
  158. v2->y = t[1]; \
  159. return r; \
  160. } \
  161. extern float3 __attribute__((overloadable)) \
  162. fnc(float3 v1, float3 *v2) { \
  163. float3 r; \
  164. float t[3]; \
  165. r.x = fnc(v1.x, &t[0]); \
  166. r.y = fnc(v1.y, &t[1]); \
  167. r.z = fnc(v1.z, &t[2]); \
  168. v2->x = t[0]; \
  169. v2->y = t[1]; \
  170. v2->z = t[2]; \
  171. return r; \
  172. } \
  173. extern float4 __attribute__((overloadable)) \
  174. fnc(float4 v1, float4 *v2) { \
  175. float4 r; \
  176. float t[4]; \
  177. r.x = fnc(v1.x, &t[0]); \
  178. r.y = fnc(v1.y, &t[1]); \
  179. r.z = fnc(v1.z, &t[2]); \
  180. r.w = fnc(v1.w, &t[3]); \
  181. v2->x = t[0]; \
  182. v2->y = t[1]; \
  183. v2->z = t[2]; \
  184. v2->w = t[3]; \
  185. return r; \
  186. }
  187. #define FN_FUNC_FN_PIN(fnc) \
  188. extern float2 __attribute__((overloadable)) fnc(float2 v1, int2 *v2) { \
  189. float2 r; \
  190. int t[2]; \
  191. r.x = fnc(v1.x, &t[0]); \
  192. r.y = fnc(v1.y, &t[1]); \
  193. v2->x = t[0]; \
  194. v2->y = t[1]; \
  195. return r; \
  196. } \
  197. extern float3 __attribute__((overloadable)) fnc(float3 v1, int3 *v2) { \
  198. float3 r; \
  199. int t[3]; \
  200. r.x = fnc(v1.x, &t[0]); \
  201. r.y = fnc(v1.y, &t[1]); \
  202. r.z = fnc(v1.z, &t[2]); \
  203. v2->x = t[0]; \
  204. v2->y = t[1]; \
  205. v2->z = t[2]; \
  206. return r; \
  207. } \
  208. extern float4 __attribute__((overloadable)) fnc(float4 v1, int4 *v2) { \
  209. float4 r; \
  210. int t[4]; \
  211. r.x = fnc(v1.x, &t[0]); \
  212. r.y = fnc(v1.y, &t[1]); \
  213. r.z = fnc(v1.z, &t[2]); \
  214. r.w = fnc(v1.w, &t[3]); \
  215. v2->x = t[0]; \
  216. v2->y = t[1]; \
  217. v2->z = t[2]; \
  218. v2->w = t[3]; \
  219. return r; \
  220. }
  221. #define FN_FUNC_FN_FN_FN(fnc) \
  222. extern float2 __attribute__((overloadable)) \
  223. fnc(float2 v1, float2 v2, float2 v3) { \
  224. float2 r; \
  225. r.x = fnc(v1.x, v2.x, v3.x); \
  226. r.y = fnc(v1.y, v2.y, v3.y); \
  227. return r; \
  228. } \
  229. extern float3 __attribute__((overloadable)) \
  230. fnc(float3 v1, float3 v2, float3 v3) { \
  231. float3 r; \
  232. r.x = fnc(v1.x, v2.x, v3.x); \
  233. r.y = fnc(v1.y, v2.y, v3.y); \
  234. r.z = fnc(v1.z, v2.z, v3.z); \
  235. return r; \
  236. } \
  237. extern float4 __attribute__((overloadable)) \
  238. fnc(float4 v1, float4 v2, float4 v3) { \
  239. float4 r; \
  240. r.x = fnc(v1.x, v2.x, v3.x); \
  241. r.y = fnc(v1.y, v2.y, v3.y); \
  242. r.z = fnc(v1.z, v2.z, v3.z); \
  243. r.w = fnc(v1.w, v2.w, v3.w); \
  244. return r; \
  245. }
  246. #define FN_FUNC_FN_FN_PIN(fnc) \
  247. extern float2 __attribute__((overloadable)) \
  248. fnc(float2 v1, float2 v2, int2 *v3) { \
  249. float2 r; \
  250. int t[2]; \
  251. r.x = fnc(v1.x, v2.x, &t[0]); \
  252. r.y = fnc(v1.y, v2.y, &t[1]); \
  253. v3->x = t[0]; \
  254. v3->y = t[1]; \
  255. return r; \
  256. } \
  257. extern float3 __attribute__((overloadable)) \
  258. fnc(float3 v1, float3 v2, int3 *v3) { \
  259. float3 r; \
  260. int t[3]; \
  261. r.x = fnc(v1.x, v2.x, &t[0]); \
  262. r.y = fnc(v1.y, v2.y, &t[1]); \
  263. r.z = fnc(v1.z, v2.z, &t[2]); \
  264. v3->x = t[0]; \
  265. v3->y = t[1]; \
  266. v3->z = t[2]; \
  267. return r; \
  268. } \
  269. extern float4 __attribute__((overloadable)) \
  270. fnc(float4 v1, float4 v2, int4 *v3) { \
  271. float4 r; \
  272. int t[4]; \
  273. r.x = fnc(v1.x, v2.x, &t[0]); \
  274. r.y = fnc(v1.y, v2.y, &t[1]); \
  275. r.z = fnc(v1.z, v2.z, &t[2]); \
  276. r.w = fnc(v1.w, v2.w, &t[3]); \
  277. v3->x = t[0]; \
  278. v3->y = t[1]; \
  279. v3->z = t[2]; \
  280. v3->w = t[3]; \
  281. return r; \
  282. }
  283. static const unsigned int iposinf = 0x7f800000;
  284. static const unsigned int ineginf = 0xff800000;
  285. static float posinf() {
  286. float f = *((float*)&iposinf);
  287. return f;
  288. }
  289. static unsigned int float_bits(float f) {
  290. /* TODO(jeanluc) Use this better approach once the Mac(SDK) build issues are fixed.
  291. // Get the bits while following the strict aliasing rules.
  292. unsigned int result;
  293. memcpy(&result, &f, sizeof(f));
  294. return result;
  295. */
  296. return *(unsigned int*)(char*)(&f);
  297. }
  298. static bool isinf(float f) {
  299. unsigned int i = float_bits(f);
  300. return (i == iposinf) || (i == ineginf);
  301. }
  302. static bool isnan(float f) {
  303. unsigned int i = float_bits(f);
  304. return (((i & 0x7f800000) == 0x7f800000) && (i & 0x007fffff));
  305. }
  306. static bool isposzero(float f) {
  307. return (float_bits(f) == 0x00000000);
  308. }
  309. static bool isnegzero(float f) {
  310. return (float_bits(f) == 0x80000000);
  311. }
  312. static bool iszero(float f) {
  313. return isposzero(f) || isnegzero(f);
  314. }
  315. extern float __attribute__((overloadable)) SC_acosf(float);
  316. float __attribute__((overloadable)) acos(float v) {
  317. return SC_acosf(v);
  318. }
  319. FN_FUNC_FN(acos)
  320. extern float __attribute__((overloadable)) SC_acoshf(float);
  321. float __attribute__((overloadable)) acosh(float v) {
  322. return SC_acoshf(v);
  323. }
  324. FN_FUNC_FN(acosh)
  325. extern float __attribute__((overloadable)) acospi(float v) {
  326. return acos(v) / M_PI;
  327. }
  328. FN_FUNC_FN(acospi)
  329. extern float __attribute__((overloadable)) SC_asinf(float);
  330. float __attribute__((overloadable)) asin(float v) {
  331. return SC_asinf(v);
  332. }
  333. FN_FUNC_FN(asin)
  334. extern float __attribute__((overloadable)) SC_asinhf(float);
  335. float __attribute__((overloadable)) asinh(float v) {
  336. return SC_asinhf(v);
  337. }
  338. FN_FUNC_FN(asinh)
  339. extern float __attribute__((overloadable)) asinpi(float v) {
  340. return asin(v) / M_PI;
  341. }
  342. FN_FUNC_FN(asinpi)
  343. extern float __attribute__((overloadable)) SC_atanf(float);
  344. float __attribute__((overloadable)) atan(float v) {
  345. return SC_atanf(v);
  346. }
  347. FN_FUNC_FN(atan)
  348. extern float __attribute__((overloadable)) SC_atan2f(float, float);
  349. float __attribute__((overloadable)) atan2(float v1, float v2) {
  350. return SC_atan2f(v1, v2);
  351. }
  352. FN_FUNC_FN_FN(atan2)
  353. extern float __attribute__((overloadable)) SC_atanhf(float);
  354. float __attribute__((overloadable)) atanh(float v) {
  355. return SC_atanhf(v);
  356. }
  357. FN_FUNC_FN(atanh)
  358. extern float __attribute__((overloadable)) atanpi(float v) {
  359. return atan(v) / M_PI;
  360. }
  361. FN_FUNC_FN(atanpi)
  362. extern float __attribute__((overloadable)) atan2pi(float y, float x) {
  363. return atan2(y, x) / M_PI;
  364. }
  365. FN_FUNC_FN_FN(atan2pi)
  366. extern float __attribute__((overloadable)) SC_cbrtf(float);
  367. float __attribute__((overloadable)) cbrt(float v) {
  368. return SC_cbrtf(v);
  369. }
  370. FN_FUNC_FN(cbrt)
  371. extern float __attribute__((overloadable)) SC_ceilf(float);
  372. float __attribute__((overloadable)) ceil(float v) {
  373. return SC_ceilf(v);
  374. }
  375. FN_FUNC_FN(ceil)
  376. extern float __attribute__((overloadable)) SC_copysignf(float, float);
  377. float __attribute__((overloadable)) copysign(float v1, float v2) {
  378. return SC_copysignf(v1, v2);
  379. }
  380. FN_FUNC_FN_FN(copysign)
  381. extern float __attribute__((overloadable)) SC_cosf(float);
  382. float __attribute__((overloadable)) cos(float v) {
  383. return SC_cosf(v);
  384. }
  385. FN_FUNC_FN(cos)
  386. extern float __attribute__((overloadable)) SC_coshf(float);
  387. float __attribute__((overloadable)) cosh(float v) {
  388. return SC_coshf(v);
  389. }
  390. FN_FUNC_FN(cosh)
  391. extern float __attribute__((overloadable)) cospi(float v) {
  392. return cos(v * M_PI);
  393. }
  394. FN_FUNC_FN(cospi)
  395. extern float __attribute__((overloadable)) SC_erfcf(float);
  396. float __attribute__((overloadable)) erfc(float v) {
  397. return SC_erfcf(v);
  398. }
  399. FN_FUNC_FN(erfc)
  400. extern float __attribute__((overloadable)) SC_erff(float);
  401. float __attribute__((overloadable)) erf(float v) {
  402. return SC_erff(v);
  403. }
  404. FN_FUNC_FN(erf)
  405. extern float __attribute__((overloadable)) SC_expf(float);
  406. float __attribute__((overloadable)) exp(float v) {
  407. return SC_expf(v);
  408. }
  409. FN_FUNC_FN(exp)
  410. extern float __attribute__((overloadable)) SC_exp2f(float);
  411. float __attribute__((overloadable)) exp2(float v) {
  412. return SC_exp2f(v);
  413. }
  414. FN_FUNC_FN(exp2)
  415. extern float __attribute__((overloadable)) pow(float, float);
  416. extern float __attribute__((overloadable)) exp10(float v) {
  417. return exp2(v * 3.321928095f);
  418. }
  419. FN_FUNC_FN(exp10)
  420. extern float __attribute__((overloadable)) SC_expm1f(float);
  421. float __attribute__((overloadable)) expm1(float v) {
  422. return SC_expm1f(v);
  423. }
  424. FN_FUNC_FN(expm1)
  425. extern float __attribute__((overloadable)) fabs(float v) {
  426. int i = *((int*)(void*)&v) & 0x7fffffff;
  427. return *((float*)(void*)&i);
  428. }
  429. FN_FUNC_FN(fabs)
  430. extern float __attribute__((overloadable)) SC_fdimf(float, float);
  431. float __attribute__((overloadable)) fdim(float v1, float v2) {
  432. return SC_fdimf(v1, v2);
  433. }
  434. FN_FUNC_FN_FN(fdim)
  435. extern float __attribute__((overloadable)) SC_floorf(float);
  436. float __attribute__((overloadable)) floor(float v) {
  437. return SC_floorf(v);
  438. }
  439. FN_FUNC_FN(floor)
  440. extern float __attribute__((overloadable)) SC_fmaf(float, float, float);
  441. float __attribute__((overloadable)) fma(float v1, float v2, float v3) {
  442. return SC_fmaf(v1, v2, v3);
  443. }
  444. FN_FUNC_FN_FN_FN(fma)
  445. extern float __attribute__((overloadable)) SC_fminf(float, float);
  446. extern float __attribute__((overloadable)) SC_fmodf(float, float);
  447. float __attribute__((overloadable)) fmod(float v1, float v2) {
  448. return SC_fmodf(v1, v2);
  449. }
  450. FN_FUNC_FN_FN(fmod)
  451. extern float __attribute__((overloadable)) fract(float v, float *iptr) {
  452. int i = (int)floor(v);
  453. if (iptr) {
  454. iptr[0] = i;
  455. }
  456. return fmin(v - i, 0x1.fffffep-1f);
  457. }
  458. FN_FUNC_FN_PFN(fract)
  459. extern float __attribute__((const, overloadable)) fract(float v) {
  460. float unused;
  461. return fract(v, &unused);
  462. }
  463. FN_FUNC_FN(fract)
  464. extern float __attribute__((overloadable)) SC_frexpf(float, int *);
  465. float __attribute__((overloadable)) frexp(float v1, int* v2) {
  466. return SC_frexpf(v1, v2);
  467. }
  468. FN_FUNC_FN_PIN(frexp)
  469. extern float __attribute__((overloadable)) SC_hypotf(float, float);
  470. float __attribute__((overloadable)) hypot(float v1, float v2) {
  471. return SC_hypotf(v1, v2);
  472. }
  473. FN_FUNC_FN_FN(hypot)
  474. extern int __attribute__((overloadable)) SC_ilogbf(float);
  475. int __attribute__((overloadable)) ilogb(float v) {
  476. return SC_ilogbf(v);
  477. }
  478. IN_FUNC_FN(ilogb)
  479. extern float __attribute__((overloadable)) SC_ldexpf(float, int);
  480. float __attribute__((overloadable)) ldexp(float v1, int v2) {
  481. return SC_ldexpf(v1, v2);
  482. }
  483. FN_FUNC_FN_IN(ldexp)
  484. FN_FUNC_FN_I(ldexp)
  485. extern float __attribute__((overloadable)) SC_lgammaf(float);
  486. float __attribute__((overloadable)) lgamma(float v) {
  487. return SC_lgammaf(v);
  488. }
  489. FN_FUNC_FN(lgamma)
  490. extern float __attribute__((overloadable)) SC_lgammaf_r(float, int*);
  491. float __attribute__((overloadable)) lgamma(float v, int* ptr) {
  492. return SC_lgammaf_r(v, ptr);
  493. }
  494. FN_FUNC_FN_PIN(lgamma)
  495. extern float __attribute__((overloadable)) SC_logf(float);
  496. float __attribute__((overloadable)) log(float v) {
  497. return SC_logf(v);
  498. }
  499. FN_FUNC_FN(log)
  500. extern float __attribute__((overloadable)) SC_log10f(float);
  501. float __attribute__((overloadable)) log10(float v) {
  502. return SC_log10f(v);
  503. }
  504. FN_FUNC_FN(log10)
  505. extern float __attribute__((overloadable)) log2(float v) {
  506. return log10(v) * 3.321928095f;
  507. }
  508. FN_FUNC_FN(log2)
  509. extern float __attribute__((overloadable)) SC_log1pf(float);
  510. float __attribute__((overloadable)) log1p(float v) {
  511. return SC_log1pf(v);
  512. }
  513. FN_FUNC_FN(log1p)
  514. extern float __attribute__((overloadable)) SC_logbf(float);
  515. float __attribute__((overloadable)) logb(float v) {
  516. return SC_logbf(v);
  517. }
  518. FN_FUNC_FN(logb)
  519. extern float __attribute__((overloadable)) mad(float a, float b, float c) {
  520. return a * b + c;
  521. }
  522. extern float2 __attribute__((overloadable)) mad(float2 a, float2 b, float2 c) {
  523. return a * b + c;
  524. }
  525. extern float3 __attribute__((overloadable)) mad(float3 a, float3 b, float3 c) {
  526. return a * b + c;
  527. }
  528. extern float4 __attribute__((overloadable)) mad(float4 a, float4 b, float4 c) {
  529. return a * b + c;
  530. }
  531. extern float __attribute__((overloadable)) SC_modff(float, float *);
  532. float __attribute__((overloadable)) modf(float v1, float *v2) {
  533. return SC_modff(v1, v2);
  534. }
  535. FN_FUNC_FN_PFN(modf);
  536. extern float __attribute__((overloadable)) nan(uint v) {
  537. float f[1];
  538. uint32_t *ip = (uint32_t *)f;
  539. *ip = v | 0x7fc00000;
  540. return f[0];
  541. }
  542. extern float __attribute__((overloadable)) SC_nextafterf(float, float);
  543. float __attribute__((overloadable)) nextafter(float v1, float v2) {
  544. return SC_nextafterf(v1, v2);
  545. }
  546. FN_FUNC_FN_FN(nextafter)
  547. // This function must be defined here if we're compiling with debug info
  548. // (libclcore_g.bc), because we need a C source to get debug information.
  549. // Otherwise the implementation can be found in IR.
  550. #if defined(RS_G_RUNTIME)
  551. extern float __attribute__((overloadable)) SC_powf(float, float);
  552. float __attribute__((overloadable)) pow(float v1, float v2) {
  553. return SC_powf(v1, v2);
  554. }
  555. #endif // defined(RS_G_RUNTIME)
  556. FN_FUNC_FN_FN(pow)
  557. extern float __attribute__((overloadable)) pown(float v, int p) {
  558. /* The mantissa of a float has fewer bits than an int (24 effective vs. 31).
  559. * For very large ints, we'll lose whether the exponent is even or odd, making
  560. * the selection of a correct sign incorrect. We correct this. Use copysign
  561. * to handle the negative zero case.
  562. */
  563. float sign = (p & 0x1) ? copysign(1.f, v) : 1.f;
  564. float f = pow(v, (float)p);
  565. return copysign(f, sign);
  566. }
  567. FN_FUNC_FN_IN(pown)
  568. extern float __attribute__((overloadable)) powr(float v, float p) {
  569. return pow(v, p);
  570. }
  571. extern float2 __attribute__((overloadable)) powr(float2 v, float2 p) {
  572. return pow(v, p);
  573. }
  574. extern float3 __attribute__((overloadable)) powr(float3 v, float3 p) {
  575. return pow(v, p);
  576. }
  577. extern float4 __attribute__((overloadable)) powr(float4 v, float4 p) {
  578. return pow(v, p);
  579. }
  580. extern float __attribute__((overloadable)) SC_remainderf(float, float);
  581. float __attribute__((overloadable)) remainder(float v1, float v2) {
  582. return SC_remainderf(v1, v2);
  583. }
  584. FN_FUNC_FN_FN(remainder)
  585. extern float __attribute__((overloadable)) SC_remquof(float, float, int *);
  586. float __attribute__((overloadable)) remquo(float v1, float v2, int *v3) {
  587. return SC_remquof(v1, v2, v3);
  588. }
  589. FN_FUNC_FN_FN_PIN(remquo)
  590. extern float __attribute__((overloadable)) SC_rintf(float);
  591. float __attribute__((overloadable)) rint(float v) {
  592. return SC_rintf(v);
  593. }
  594. FN_FUNC_FN(rint)
  595. extern float __attribute__((overloadable)) rootn(float v, int r) {
  596. if (r == 0) {
  597. return posinf();
  598. }
  599. if (iszero(v)) {
  600. if (r < 0) {
  601. if (r & 1) {
  602. return copysign(posinf(), v);
  603. } else {
  604. return posinf();
  605. }
  606. } else {
  607. if (r & 1) {
  608. return copysign(0.f, v);
  609. } else {
  610. return 0.f;
  611. }
  612. }
  613. }
  614. if (!isinf(v) && !isnan(v) && (v < 0.f)) {
  615. if (r & 1) {
  616. return (-1.f * pow(-1.f * v, 1.f / r));
  617. } else {
  618. return nan(0);
  619. }
  620. }
  621. return pow(v, 1.f / r);
  622. }
  623. FN_FUNC_FN_IN(rootn);
  624. extern float __attribute__((overloadable)) SC_roundf(float);
  625. float __attribute__((overloadable)) round(float v) {
  626. return SC_roundf(v);
  627. }
  628. FN_FUNC_FN(round)
  629. extern float __attribute__((overloadable)) SC_randf2(float, float);
  630. float __attribute__((overloadable)) rsRand(float min, float max) {
  631. return SC_randf2(min, max);
  632. }
  633. extern float __attribute__((overloadable)) rsqrt(float v) {
  634. return 1.f / sqrt(v);
  635. }
  636. #if !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME) || defined(RS_G_RUNTIME)
  637. // These functions must be defined here if we are not using the SSE
  638. // implementation, which includes when we are built as part of the
  639. // debug runtime (libclcore_debug.bc) or compiling with debug info.
  640. #if defined(RS_G_RUNTIME)
  641. extern float __attribute__((overloadable)) SC_sqrtf(float);
  642. float __attribute__((overloadable)) sqrt(float v) {
  643. return SC_sqrtf(v);
  644. }
  645. #endif // defined(RS_G_RUNTIME)
  646. FN_FUNC_FN(sqrt)
  647. #else
  648. extern float2 __attribute__((overloadable)) sqrt(float2);
  649. extern float3 __attribute__((overloadable)) sqrt(float3);
  650. extern float4 __attribute__((overloadable)) sqrt(float4);
  651. #endif // !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME) || defined(RS_G_RUNTIME)
  652. FN_FUNC_FN(rsqrt)
  653. extern float __attribute__((overloadable)) SC_sinf(float);
  654. float __attribute__((overloadable)) sin(float v) {
  655. return SC_sinf(v);
  656. }
  657. FN_FUNC_FN(sin)
  658. extern float __attribute__((overloadable)) sincos(float v, float *cosptr) {
  659. *cosptr = cos(v);
  660. return sin(v);
  661. }
  662. extern float2 __attribute__((overloadable)) sincos(float2 v, float2 *cosptr) {
  663. *cosptr = cos(v);
  664. return sin(v);
  665. }
  666. extern float3 __attribute__((overloadable)) sincos(float3 v, float3 *cosptr) {
  667. *cosptr = cos(v);
  668. return sin(v);
  669. }
  670. extern float4 __attribute__((overloadable)) sincos(float4 v, float4 *cosptr) {
  671. *cosptr = cos(v);
  672. return sin(v);
  673. }
  674. extern float __attribute__((overloadable)) SC_sinhf(float);
  675. float __attribute__((overloadable)) sinh(float v) {
  676. return SC_sinhf(v);
  677. }
  678. FN_FUNC_FN(sinh)
  679. extern float __attribute__((overloadable)) sinpi(float v) {
  680. return sin(v * M_PI);
  681. }
  682. FN_FUNC_FN(sinpi)
  683. extern float __attribute__((overloadable)) SC_tanf(float);
  684. float __attribute__((overloadable)) tan(float v) {
  685. return SC_tanf(v);
  686. }
  687. FN_FUNC_FN(tan)
  688. extern float __attribute__((overloadable)) SC_tanhf(float);
  689. float __attribute__((overloadable)) tanh(float v) {
  690. return SC_tanhf(v);
  691. }
  692. FN_FUNC_FN(tanh)
  693. extern float __attribute__((overloadable)) tanpi(float v) {
  694. return tan(v * M_PI);
  695. }
  696. FN_FUNC_FN(tanpi)
  697. extern float __attribute__((overloadable)) SC_tgammaf(float);
  698. float __attribute__((overloadable)) tgamma(float v) {
  699. return SC_tgammaf(v);
  700. }
  701. FN_FUNC_FN(tgamma)
  702. extern float __attribute__((overloadable)) SC_truncf(float);
  703. float __attribute__((overloadable)) trunc(float v) {
  704. return SC_truncf(v);
  705. }
  706. FN_FUNC_FN(trunc)
  707. // Int ops (partial), 6.11.3
  708. #define XN_FUNC_YN(typeout, fnc, typein) \
  709. extern typeout __attribute__((overloadable)) fnc(typein); \
  710. extern typeout##2 __attribute__((overloadable)) fnc(typein##2 v) { \
  711. typeout##2 r; \
  712. r.x = fnc(v.x); \
  713. r.y = fnc(v.y); \
  714. return r; \
  715. } \
  716. extern typeout##3 __attribute__((overloadable)) fnc(typein##3 v) { \
  717. typeout##3 r; \
  718. r.x = fnc(v.x); \
  719. r.y = fnc(v.y); \
  720. r.z = fnc(v.z); \
  721. return r; \
  722. } \
  723. extern typeout##4 __attribute__((overloadable)) fnc(typein##4 v) { \
  724. typeout##4 r; \
  725. r.x = fnc(v.x); \
  726. r.y = fnc(v.y); \
  727. r.z = fnc(v.z); \
  728. r.w = fnc(v.w); \
  729. return r; \
  730. }
  731. #define UIN_FUNC_IN(fnc) \
  732. XN_FUNC_YN(uchar, fnc, char) \
  733. XN_FUNC_YN(ushort, fnc, short) \
  734. XN_FUNC_YN(uint, fnc, int)
  735. #define IN_FUNC_IN(fnc) \
  736. XN_FUNC_YN(uchar, fnc, uchar) \
  737. XN_FUNC_YN(char, fnc, char) \
  738. XN_FUNC_YN(ushort, fnc, ushort) \
  739. XN_FUNC_YN(short, fnc, short) \
  740. XN_FUNC_YN(uint, fnc, uint) \
  741. XN_FUNC_YN(int, fnc, int)
  742. #define XN_FUNC_XN_XN_BODY(type, fnc, body) \
  743. extern type __attribute__((overloadable)) \
  744. fnc(type v1, type v2) { \
  745. return body; \
  746. } \
  747. extern type##2 __attribute__((overloadable)) \
  748. fnc(type##2 v1, type##2 v2) { \
  749. type##2 r; \
  750. r.x = fnc(v1.x, v2.x); \
  751. r.y = fnc(v1.y, v2.y); \
  752. return r; \
  753. } \
  754. extern type##3 __attribute__((overloadable)) \
  755. fnc(type##3 v1, type##3 v2) { \
  756. type##3 r; \
  757. r.x = fnc(v1.x, v2.x); \
  758. r.y = fnc(v1.y, v2.y); \
  759. r.z = fnc(v1.z, v2.z); \
  760. return r; \
  761. } \
  762. extern type##4 __attribute__((overloadable)) \
  763. fnc(type##4 v1, type##4 v2) { \
  764. type##4 r; \
  765. r.x = fnc(v1.x, v2.x); \
  766. r.y = fnc(v1.y, v2.y); \
  767. r.z = fnc(v1.z, v2.z); \
  768. r.w = fnc(v1.w, v2.w); \
  769. return r; \
  770. }
  771. #define IN_FUNC_IN_IN_BODY(fnc, body) \
  772. XN_FUNC_XN_XN_BODY(uchar, fnc, body) \
  773. XN_FUNC_XN_XN_BODY(char, fnc, body) \
  774. XN_FUNC_XN_XN_BODY(ushort, fnc, body) \
  775. XN_FUNC_XN_XN_BODY(short, fnc, body) \
  776. XN_FUNC_XN_XN_BODY(uint, fnc, body) \
  777. XN_FUNC_XN_XN_BODY(int, fnc, body) \
  778. XN_FUNC_XN_XN_BODY(float, fnc, body)
  779. /**
  780. * abs
  781. */
  782. extern uint32_t __attribute__((overloadable)) abs(int32_t v) {
  783. if (v < 0)
  784. return -v;
  785. return v;
  786. }
  787. extern uint16_t __attribute__((overloadable)) abs(int16_t v) {
  788. if (v < 0)
  789. return -v;
  790. return v;
  791. }
  792. extern uint8_t __attribute__((overloadable)) abs(int8_t v) {
  793. if (v < 0)
  794. return -v;
  795. return v;
  796. }
  797. /**
  798. * clz
  799. * __builtin_clz only accepts a 32-bit unsigned int, so every input will be
  800. * expanded to 32 bits. For our smaller data types, we need to subtract off
  801. * these unused top bits (that will be always be composed of zeros).
  802. */
  803. extern uint32_t __attribute__((overloadable)) clz(uint32_t v) {
  804. return __builtin_clz(v);
  805. }
  806. extern uint16_t __attribute__((overloadable)) clz(uint16_t v) {
  807. return __builtin_clz(v) - 16;
  808. }
  809. extern uint8_t __attribute__((overloadable)) clz(uint8_t v) {
  810. return __builtin_clz(v) - 24;
  811. }
  812. extern int32_t __attribute__((overloadable)) clz(int32_t v) {
  813. return __builtin_clz(v);
  814. }
  815. extern int16_t __attribute__((overloadable)) clz(int16_t v) {
  816. return __builtin_clz(((uint32_t)v) & 0x0000ffff) - 16;
  817. }
  818. extern int8_t __attribute__((overloadable)) clz(int8_t v) {
  819. return __builtin_clz(((uint32_t)v) & 0x000000ff) - 24;
  820. }
  821. UIN_FUNC_IN(abs)
  822. IN_FUNC_IN(clz)
  823. // 6.11.4
  824. extern float __attribute__((overloadable)) degrees(float radians) {
  825. return radians * (180.f / M_PI);
  826. }
  827. extern float2 __attribute__((overloadable)) degrees(float2 radians) {
  828. return radians * (180.f / M_PI);
  829. }
  830. extern float3 __attribute__((overloadable)) degrees(float3 radians) {
  831. return radians * (180.f / M_PI);
  832. }
  833. extern float4 __attribute__((overloadable)) degrees(float4 radians) {
  834. return radians * (180.f / M_PI);
  835. }
  836. extern float __attribute__((overloadable)) mix(float start, float stop, float amount) {
  837. return start + (stop - start) * amount;
  838. }
  839. extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float2 amount) {
  840. return start + (stop - start) * amount;
  841. }
  842. extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float3 amount) {
  843. return start + (stop - start) * amount;
  844. }
  845. extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float4 amount) {
  846. return start + (stop - start) * amount;
  847. }
  848. extern float2 __attribute__((overloadable)) mix(float2 start, float2 stop, float amount) {
  849. return start + (stop - start) * amount;
  850. }
  851. extern float3 __attribute__((overloadable)) mix(float3 start, float3 stop, float amount) {
  852. return start + (stop - start) * amount;
  853. }
  854. extern float4 __attribute__((overloadable)) mix(float4 start, float4 stop, float amount) {
  855. return start + (stop - start) * amount;
  856. }
  857. extern float __attribute__((overloadable)) radians(float degrees) {
  858. return degrees * (M_PI / 180.f);
  859. }
  860. extern float2 __attribute__((overloadable)) radians(float2 degrees) {
  861. return degrees * (M_PI / 180.f);
  862. }
  863. extern float3 __attribute__((overloadable)) radians(float3 degrees) {
  864. return degrees * (M_PI / 180.f);
  865. }
  866. extern float4 __attribute__((overloadable)) radians(float4 degrees) {
  867. return degrees * (M_PI / 180.f);
  868. }
  869. extern float __attribute__((overloadable)) step(float edge, float v) {
  870. return (v < edge) ? 0.f : 1.f;
  871. }
  872. extern float2 __attribute__((overloadable)) step(float2 edge, float2 v) {
  873. float2 r;
  874. r.x = (v.x < edge.x) ? 0.f : 1.f;
  875. r.y = (v.y < edge.y) ? 0.f : 1.f;
  876. return r;
  877. }
  878. extern float3 __attribute__((overloadable)) step(float3 edge, float3 v) {
  879. float3 r;
  880. r.x = (v.x < edge.x) ? 0.f : 1.f;
  881. r.y = (v.y < edge.y) ? 0.f : 1.f;
  882. r.z = (v.z < edge.z) ? 0.f : 1.f;
  883. return r;
  884. }
  885. extern float4 __attribute__((overloadable)) step(float4 edge, float4 v) {
  886. float4 r;
  887. r.x = (v.x < edge.x) ? 0.f : 1.f;
  888. r.y = (v.y < edge.y) ? 0.f : 1.f;
  889. r.z = (v.z < edge.z) ? 0.f : 1.f;
  890. r.w = (v.w < edge.w) ? 0.f : 1.f;
  891. return r;
  892. }
  893. extern float2 __attribute__((overloadable)) step(float2 edge, float v) {
  894. float2 r;
  895. r.x = (v < edge.x) ? 0.f : 1.f;
  896. r.y = (v < edge.y) ? 0.f : 1.f;
  897. return r;
  898. }
  899. extern float3 __attribute__((overloadable)) step(float3 edge, float v) {
  900. float3 r;
  901. r.x = (v < edge.x) ? 0.f : 1.f;
  902. r.y = (v < edge.y) ? 0.f : 1.f;
  903. r.z = (v < edge.z) ? 0.f : 1.f;
  904. return r;
  905. }
  906. extern float4 __attribute__((overloadable)) step(float4 edge, float v) {
  907. float4 r;
  908. r.x = (v < edge.x) ? 0.f : 1.f;
  909. r.y = (v < edge.y) ? 0.f : 1.f;
  910. r.z = (v < edge.z) ? 0.f : 1.f;
  911. r.w = (v < edge.w) ? 0.f : 1.f;
  912. return r;
  913. }
  914. extern float2 __attribute__((overloadable)) step(float edge, float2 v) {
  915. float2 r;
  916. r.x = (v.x < edge) ? 0.f : 1.f;
  917. r.y = (v.y < edge) ? 0.f : 1.f;
  918. return r;
  919. }
  920. extern float3 __attribute__((overloadable)) step(float edge, float3 v) {
  921. float3 r;
  922. r.x = (v.x < edge) ? 0.f : 1.f;
  923. r.y = (v.y < edge) ? 0.f : 1.f;
  924. r.z = (v.z < edge) ? 0.f : 1.f;
  925. return r;
  926. }
  927. extern float4 __attribute__((overloadable)) step(float edge, float4 v) {
  928. float4 r;
  929. r.x = (v.x < edge) ? 0.f : 1.f;
  930. r.y = (v.y < edge) ? 0.f : 1.f;
  931. r.z = (v.z < edge) ? 0.f : 1.f;
  932. r.w = (v.w < edge) ? 0.f : 1.f;
  933. return r;
  934. }
  935. extern float __attribute__((overloadable)) sign(float v) {
  936. if (v > 0) return 1.f;
  937. if (v < 0) return -1.f;
  938. return v;
  939. }
  940. FN_FUNC_FN(sign)
  941. // 6.11.5
  942. extern float3 __attribute__((overloadable)) cross(float3 lhs, float3 rhs) {
  943. float3 r;
  944. r.x = lhs.y * rhs.z - lhs.z * rhs.y;
  945. r.y = lhs.z * rhs.x - lhs.x * rhs.z;
  946. r.z = lhs.x * rhs.y - lhs.y * rhs.x;
  947. return r;
  948. }
  949. extern float4 __attribute__((overloadable)) cross(float4 lhs, float4 rhs) {
  950. float4 r;
  951. r.x = lhs.y * rhs.z - lhs.z * rhs.y;
  952. r.y = lhs.z * rhs.x - lhs.x * rhs.z;
  953. r.z = lhs.x * rhs.y - lhs.y * rhs.x;
  954. r.w = 0.f;
  955. return r;
  956. }
  957. #if !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME) || defined(RS_G_RUNTIME)
  958. // These functions must be defined here if we are not using the SSE
  959. // implementation, which includes when we are built as part of the
  960. // debug runtime (libclcore_debug.bc) or compiling with debug info.
  961. extern float __attribute__((overloadable)) dot(float lhs, float rhs) {
  962. return lhs * rhs;
  963. }
  964. extern float __attribute__((overloadable)) dot(float2 lhs, float2 rhs) {
  965. return lhs.x*rhs.x + lhs.y*rhs.y;
  966. }
  967. extern float __attribute__((overloadable)) dot(float3 lhs, float3 rhs) {
  968. return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
  969. }
  970. extern float __attribute__((overloadable)) dot(float4 lhs, float4 rhs) {
  971. return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z + lhs.w*rhs.w;
  972. }
  973. extern float __attribute__((overloadable)) length(float v) {
  974. return fabs(v);
  975. }
  976. extern float __attribute__((overloadable)) length(float2 v) {
  977. return sqrt(v.x*v.x + v.y*v.y);
  978. }
  979. extern float __attribute__((overloadable)) length(float3 v) {
  980. return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
  981. }
  982. extern float __attribute__((overloadable)) length(float4 v) {
  983. return sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
  984. }
  985. #else
  986. extern float __attribute__((overloadable)) length(float v);
  987. extern float __attribute__((overloadable)) length(float2 v);
  988. extern float __attribute__((overloadable)) length(float3 v);
  989. extern float __attribute__((overloadable)) length(float4 v);
  990. #endif // !defined(ARCH_X86_HAVE_SSSE3) || defined(RS_DEBUG_RUNTIME) || defined(RS_G_RUNTIME)
  991. extern float __attribute__((overloadable)) distance(float lhs, float rhs) {
  992. return length(lhs - rhs);
  993. }
  994. extern float __attribute__((overloadable)) distance(float2 lhs, float2 rhs) {
  995. return length(lhs - rhs);
  996. }
  997. extern float __attribute__((overloadable)) distance(float3 lhs, float3 rhs) {
  998. return length(lhs - rhs);
  999. }
  1000. extern float __attribute__((overloadable)) distance(float4 lhs, float4 rhs) {
  1001. return length(lhs - rhs);
  1002. }
  1003. /* For the normalization functions, vectors of length 0 should simply be
  1004. * returned (i.e. all the components of that vector are 0).
  1005. */
  1006. extern float __attribute__((overloadable)) normalize(float v) {
  1007. if (v == 0.0f) {
  1008. return 0.0f;
  1009. } else if (v < 0.0f) {
  1010. return -1.0f;
  1011. } else {
  1012. return 1.0f;
  1013. }
  1014. }
  1015. extern float2 __attribute__((overloadable)) normalize(float2 v) {
  1016. float l = length(v);
  1017. return l == 0.0f ? v : v / l;
  1018. }
  1019. extern float3 __attribute__((overloadable)) normalize(float3 v) {
  1020. float l = length(v);
  1021. return l == 0.0f ? v : v / l;
  1022. }
  1023. extern float4 __attribute__((overloadable)) normalize(float4 v) {
  1024. float l = length(v);
  1025. return l == 0.0f ? v : v / l;
  1026. }
  1027. extern float __attribute__((overloadable)) half_sqrt(float v) {
  1028. return sqrt(v);
  1029. }
  1030. FN_FUNC_FN(half_sqrt)
  1031. extern float __attribute__((overloadable)) fast_length(float v) {
  1032. return fabs(v);
  1033. }
  1034. extern float __attribute__((overloadable)) fast_length(float2 v) {
  1035. return half_sqrt(v.x*v.x + v.y*v.y);
  1036. }
  1037. extern float __attribute__((overloadable)) fast_length(float3 v) {
  1038. return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
  1039. }
  1040. extern float __attribute__((overloadable)) fast_length(float4 v) {
  1041. return half_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
  1042. }
  1043. extern float __attribute__((overloadable)) fast_distance(float lhs, float rhs) {
  1044. return fast_length(lhs - rhs);
  1045. }
  1046. extern float __attribute__((overloadable)) fast_distance(float2 lhs, float2 rhs) {
  1047. return fast_length(lhs - rhs);
  1048. }
  1049. extern float __attribute__((overloadable)) fast_distance(float3 lhs, float3 rhs) {
  1050. return fast_length(lhs - rhs);
  1051. }
  1052. extern float __attribute__((overloadable)) fast_distance(float4 lhs, float4 rhs) {
  1053. return fast_length(lhs - rhs);
  1054. }
  1055. extern float __attribute__((overloadable)) half_rsqrt(float);
  1056. /* For the normalization functions, vectors of length 0 should simply be
  1057. * returned (i.e. all the components of that vector are 0).
  1058. */
  1059. extern float __attribute__((overloadable)) fast_normalize(float v) {
  1060. if (v == 0.0f) {
  1061. return 0.0f;
  1062. } else if (v < 0.0f) {
  1063. return -1.0f;
  1064. } else {
  1065. return 1.0f;
  1066. }
  1067. }
  1068. // If the length is 0, then rlength should be NaN.
  1069. extern float2 __attribute__((overloadable)) fast_normalize(float2 v) {
  1070. float rlength = half_rsqrt(v.x*v.x + v.y*v.y);
  1071. return (rlength == rlength) ? v * rlength : v;
  1072. }
  1073. extern float3 __attribute__((overloadable)) fast_normalize(float3 v) {
  1074. float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z);
  1075. return (rlength == rlength) ? v * rlength : v;
  1076. }
  1077. extern float4 __attribute__((overloadable)) fast_normalize(float4 v) {
  1078. float rlength = half_rsqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
  1079. return (rlength == rlength) ? v * rlength : v;
  1080. }
  1081. extern float __attribute__((overloadable)) half_recip(float v) {
  1082. return 1.f / v;
  1083. }
  1084. /*
  1085. extern float __attribute__((overloadable)) approx_atan(float x) {
  1086. if (x == 0.f)
  1087. return 0.f;
  1088. if (x < 0.f)
  1089. return -1.f * approx_atan(-1.f * x);
  1090. if (x > 1.f)
  1091. return M_PI_2 - approx_atan(approx_recip(x));
  1092. return x * approx_recip(1.f + 0.28f * x*x);
  1093. }
  1094. FN_FUNC_FN(approx_atan)
  1095. */
  1096. typedef union
  1097. {
  1098. float fv;
  1099. int32_t iv;
  1100. } ieee_float_shape_type;
  1101. /* Get a 32 bit int from a float. */
  1102. #define GET_FLOAT_WORD(i,d) \
  1103. do { \
  1104. ieee_float_shape_type gf_u; \
  1105. gf_u.fv = (d); \
  1106. (i) = gf_u.iv; \
  1107. } while (0)
  1108. /* Set a float from a 32 bit int. */
  1109. #define SET_FLOAT_WORD(d,i) \
  1110. do { \
  1111. ieee_float_shape_type sf_u; \
  1112. sf_u.iv = (i); \
  1113. (d) = sf_u.fv; \
  1114. } while (0)
  1115. // Valid -125 to 125
  1116. extern float __attribute__((overloadable)) native_exp2(float v) {
  1117. int32_t iv = (int)v;
  1118. int32_t x = iv + (iv >> 31); // ~floor(v)
  1119. float r = (v - x);
  1120. float fo;
  1121. SET_FLOAT_WORD(fo, (x + 127) << 23);
  1122. r *= 0.694f; // ~ log(e) / log(2)
  1123. float r2 = r*r;
  1124. float adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
  1125. return fo * adj;
  1126. }
  1127. extern float2 __attribute__((overloadable)) native_exp2(float2 v) {
  1128. int2 iv = convert_int2(v);
  1129. int2 x = iv + (iv >> (int2)31);//floor(v);
  1130. float2 r = (v - convert_float2(x));
  1131. x += 127;
  1132. float2 fo = (float2)(x << (int2)23);
  1133. r *= 0.694f; // ~ log(e) / log(2)
  1134. float2 r2 = r*r;
  1135. float2 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
  1136. return fo * adj;
  1137. }
  1138. extern float4 __attribute__((overloadable)) native_exp2(float4 v) {
  1139. int4 iv = convert_int4(v);
  1140. int4 x = iv + (iv >> (int4)31);//floor(v);
  1141. float4 r = (v - convert_float4(x));
  1142. x += 127;
  1143. float4 fo = (float4)(x << (int4)23);
  1144. r *= 0.694f; // ~ log(e) / log(2)
  1145. float4 r2 = r*r;
  1146. float4 adj = 1.f + r + (r2 * 0.5f) + (r2*r * 0.166666f) + (r2*r2 * 0.0416666f);
  1147. return fo * adj;
  1148. }
  1149. extern float3 __attribute__((overloadable)) native_exp2(float3 v) {
  1150. float4 t = 1.f;
  1151. t.xyz = v;
  1152. return native_exp2(t).xyz;
  1153. }
  1154. extern float __attribute__((overloadable)) native_exp(float v) {
  1155. return native_exp2(v * 1.442695041f);
  1156. }
  1157. extern float2 __attribute__((overloadable)) native_exp(float2 v) {
  1158. return native_exp2(v * 1.442695041f);
  1159. }
  1160. extern float3 __attribute__((overloadable)) native_exp(float3 v) {
  1161. return native_exp2(v * 1.442695041f);
  1162. }
  1163. extern float4 __attribute__((overloadable)) native_exp(float4 v) {
  1164. return native_exp2(v * 1.442695041f);
  1165. }
  1166. extern float __attribute__((overloadable)) native_exp10(float v) {
  1167. return native_exp2(v * 3.321928095f);
  1168. }
  1169. extern float2 __attribute__((overloadable)) native_exp10(float2 v) {
  1170. return native_exp2(v * 3.321928095f);
  1171. }
  1172. extern float3 __attribute__((overloadable)) native_exp10(float3 v) {
  1173. return native_exp2(v * 3.321928095f);
  1174. }
  1175. extern float4 __attribute__((overloadable)) native_exp10(float4 v) {
  1176. return native_exp2(v * 3.321928095f);
  1177. }
  1178. extern float __attribute__((overloadable)) native_log2(float v) {
  1179. int32_t ibits;
  1180. GET_FLOAT_WORD(ibits, v);
  1181. int32_t e = (ibits >> 23) & 0xff;
  1182. ibits &= 0x7fffff;
  1183. ibits |= 127 << 23;
  1184. float ir;
  1185. SET_FLOAT_WORD(ir, ibits);
  1186. ir -= 1.5f;
  1187. float ir2 = ir*ir;
  1188. float adj2 = (0.405465108f / 0.693147181f) +
  1189. ((0.666666667f / 0.693147181f) * ir) -
  1190. ((0.222222222f / 0.693147181f) * ir2) +
  1191. ((0.098765432f / 0.693147181f) * ir*ir2) -
  1192. ((0.049382716f / 0.693147181f) * ir2*ir2) +
  1193. ((0.026337449f / 0.693147181f) * ir*ir2*ir2) -
  1194. ((0.014631916f / 0.693147181f) * ir2*ir2*ir2);
  1195. return (float)(e - 127) + adj2;
  1196. }
  1197. extern float2 __attribute__((overloadable)) native_log2(float2 v) {
  1198. float2 v2 = {native_log2(v.x), native_log2(v.y)};
  1199. return v2;
  1200. }
  1201. extern float3 __attribute__((overloadable)) native_log2(float3 v) {
  1202. float3 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z)};
  1203. return v2;
  1204. }
  1205. extern float4 __attribute__((overloadable)) native_log2(float4 v) {
  1206. float4 v2 = {native_log2(v.x), native_log2(v.y), native_log2(v.z), native_log2(v.w)};
  1207. return v2;
  1208. }
  1209. extern float __attribute__((overloadable)) native_log(float v) {
  1210. return native_log2(v) * (1.f / 1.442695041f);
  1211. }
  1212. extern float2 __attribute__((overloadable)) native_log(float2 v) {
  1213. return native_log2(v) * (1.f / 1.442695041f);
  1214. }
  1215. extern float3 __attribute__((overloadable)) native_log(float3 v) {
  1216. return native_log2(v) * (1.f / 1.442695041f);
  1217. }
  1218. extern float4 __attribute__((overloadable)) native_log(float4 v) {
  1219. return native_log2(v) * (1.f / 1.442695041f);
  1220. }
  1221. extern float __attribute__((overloadable)) native_log10(float v) {
  1222. return native_log2(v) * (1.f / 3.321928095f);
  1223. }
  1224. extern float2 __attribute__((overloadable)) native_log10(float2 v) {
  1225. return native_log2(v) * (1.f / 3.321928095f);
  1226. }
  1227. extern float3 __attribute__((overloadable)) native_log10(float3 v) {
  1228. return native_log2(v) * (1.f / 3.321928095f);
  1229. }
  1230. extern float4 __attribute__((overloadable)) native_log10(float4 v) {
  1231. return native_log2(v) * (1.f / 3.321928095f);
  1232. }
  1233. extern float __attribute__((overloadable)) native_powr(float v, float y) {
  1234. float v2 = native_log2(v);
  1235. v2 = fmax(v2 * y, -125.f);
  1236. return native_exp2(v2);
  1237. }
  1238. extern float2 __attribute__((overloadable)) native_powr(float2 v, float2 y) {
  1239. float2 v2 = native_log2(v);
  1240. v2 = fmax(v2 * y, -125.f);
  1241. return native_exp2(v2);
  1242. }
  1243. extern float3 __attribute__((overloadable)) native_powr(float3 v, float3 y) {
  1244. float3 v2 = native_log2(v);
  1245. v2 = fmax(v2 * y, -125.f);
  1246. return native_exp2(v2);
  1247. }
  1248. extern float4 __attribute__((overloadable)) native_powr(float4 v, float4 y) {
  1249. float4 v2 = native_log2(v);
  1250. v2 = fmax(v2 * y, -125.f);
  1251. return native_exp2(v2);
  1252. }
  1253. extern double __attribute__((overloadable)) min(double v1, double v2) {
  1254. return v1 < v2 ? v1 : v2;
  1255. }
  1256. extern double2 __attribute__((overloadable)) min(double2 v1, double2 v2) {
  1257. double2 r;
  1258. r.x = v1.x < v2.x ? v1.x : v2.x;
  1259. r.y = v1.y < v2.y ? v1.y : v2.y;
  1260. return r;
  1261. }
  1262. extern double3 __attribute__((overloadable)) min(double3 v1, double3 v2) {
  1263. double3 r;
  1264. r.x = v1.x < v2.x ? v1.x : v2.x;
  1265. r.y = v1.y < v2.y ? v1.y : v2.y;
  1266. r.z = v1.z < v2.z ? v1.z : v2.z;
  1267. return r;
  1268. }
  1269. extern double4 __attribute__((overloadable)) min(double4 v1, double4 v2) {
  1270. double4 r;
  1271. r.x = v1.x < v2.x ? v1.x : v2.x;
  1272. r.y = v1.y < v2.y ? v1.y : v2.y;
  1273. r.z = v1.z < v2.z ? v1.z : v2.z;
  1274. r.w = v1.w < v2.w ? v1.w : v2.w;
  1275. return r;
  1276. }
  1277. extern long __attribute__((overloadable)) min(long v1, long v2) {
  1278. return v1 < v2 ? v1 : v2;
  1279. }
  1280. extern long2 __attribute__((overloadable)) min(long2 v1, long2 v2) {
  1281. long2 r;
  1282. r.x = v1.x < v2.x ? v1.x : v2.x;
  1283. r.y = v1.y < v2.y ? v1.y : v2.y;
  1284. return r;
  1285. }
  1286. extern long3 __attribute__((overloadable)) min(long3 v1, long3 v2) {
  1287. long3 r;
  1288. r.x = v1.x < v2.x ? v1.x : v2.x;
  1289. r.y = v1.y < v2.y ? v1.y : v2.y;
  1290. r.z = v1.z < v2.z ? v1.z : v2.z;
  1291. return r;
  1292. }
  1293. extern long4 __attribute__((overloadable)) min(long4 v1, long4 v2) {
  1294. long4 r;
  1295. r.x = v1.x < v2.x ? v1.x : v2.x;
  1296. r.y = v1.y < v2.y ? v1.y : v2.y;
  1297. r.z = v1.z < v2.z ? v1.z : v2.z;
  1298. r.w = v1.w < v2.w ? v1.w : v2.w;
  1299. return r;
  1300. }
  1301. extern ulong __attribute__((overloadable)) min(ulong v1, ulong v2) {
  1302. return v1 < v2 ? v1 : v2;
  1303. }
  1304. extern ulong2 __attribute__((overloadable)) min(ulong2 v1, ulong2 v2) {
  1305. ulong2 r;
  1306. r.x = v1.x < v2.x ? v1.x : v2.x;
  1307. r.y = v1.y < v2.y ? v1.y : v2.y;
  1308. return r;
  1309. }
  1310. extern ulong3 __attribute__((overloadable)) min(ulong3 v1, ulong3 v2) {
  1311. ulong3 r;
  1312. r.x = v1.x < v2.x ? v1.x : v2.x;
  1313. r.y = v1.y < v2.y ? v1.y : v2.y;
  1314. r.z = v1.z < v2.z ? v1.z : v2.z;
  1315. return r;
  1316. }
  1317. extern ulong4 __attribute__((overloadable)) min(ulong4 v1, ulong4 v2) {
  1318. ulong4 r;
  1319. r.x = v1.x < v2.x ? v1.x : v2.x;
  1320. r.y = v1.y < v2.y ? v1.y : v2.y;
  1321. r.z = v1.z < v2.z ? v1.z : v2.z;
  1322. r.w = v1.w < v2.w ? v1.w : v2.w;
  1323. return r;
  1324. }
  1325. extern double __attribute__((overloadable)) max(double v1, double v2) {
  1326. return v1 > v2 ? v1 : v2;
  1327. }
  1328. extern double2 __attribute__((overloadable)) max(double2 v1, double2 v2) {
  1329. double2 r;
  1330. r.x = v1.x > v2.x ? v1.x : v2.x;
  1331. r.y = v1.y > v2.y ? v1.y : v2.y;
  1332. return r;
  1333. }
  1334. extern double3 __attribute__((overloadable)) max(double3 v1, double3 v2) {
  1335. double3 r;
  1336. r.x = v1.x > v2.x ? v1.x : v2.x;
  1337. r.y = v1.y > v2.y ? v1.y : v2.y;
  1338. r.z = v1.z > v2.z ? v1.z : v2.z;
  1339. return r;
  1340. }
  1341. extern double4 __attribute__((overloadable)) max(double4 v1, double4 v2) {
  1342. double4 r;
  1343. r.x = v1.x > v2.x ? v1.x : v2.x;
  1344. r.y = v1.y > v2.y ? v1.y : v2.y;
  1345. r.z = v1.z > v2.z ? v1.z : v2.z;
  1346. r.w = v1.w > v2.w ? v1.w : v2.w;
  1347. return r;
  1348. }
  1349. extern long __attribute__((overloadable)) max(long v1, long v2) {
  1350. return v1 > v2 ? v1 : v2;
  1351. }
  1352. extern long2 __attribute__((overloadable)) max(long2 v1, long2 v2) {
  1353. long2 r;
  1354. r.x = v1.x > v2.x ? v1.x : v2.x;
  1355. r.y = v1.y > v2.y ? v1.y : v2.y;
  1356. return r;
  1357. }
  1358. extern long3 __attribute__((overloadable)) max(long3 v1, long3 v2) {
  1359. long3 r;
  1360. r.x = v1.x > v2.x ? v1.x : v2.x;
  1361. r.y = v1.y > v2.y ? v1.y : v2.y;
  1362. r.z = v1.z > v2.z ? v1.z : v2.z;
  1363. return r;
  1364. }
  1365. extern long4 __attribute__((overloadable)) max(long4 v1, long4 v2) {
  1366. long4 r;
  1367. r.x = v1.x > v2.x ? v1.x : v2.x;
  1368. r.y = v1.y > v2.y ? v1.y : v2.y;
  1369. r.z = v1.z > v2.z ? v1.z : v2.z;
  1370. r.w = v1.w > v2.w ? v1.w : v2.w;
  1371. return r;
  1372. }
  1373. extern ulong __attribute__((overloadable)) max(ulong v1, ulong v2) {
  1374. return v1 > v2 ? v1 : v2;
  1375. }
  1376. extern ulong2 __attribute__((overloadable)) max(ulong2 v1, ulong2 v2) {
  1377. ulong2 r;
  1378. r.x = v1.x > v2.x ? v1.x : v2.x;
  1379. r.y = v1.y > v2.y ? v1.y : v2.y;
  1380. return r;
  1381. }
  1382. extern ulong3 __attribute__((overloadable)) max(ulong3 v1, ulong3 v2) {
  1383. ulong3 r;
  1384. r.x = v1.x > v2.x ? v1.x : v2.x;
  1385. r.y = v1.y > v2.y ? v1.y : v2.y;
  1386. r.z = v1.z > v2.z ? v1.z : v2.z;
  1387. return r;
  1388. }
  1389. extern ulong4 __attribute__((overloadable)) max(ulong4 v1, ulong4 v2) {
  1390. ulong4 r;
  1391. r.x = v1.x > v2.x ? v1.x : v2.x;
  1392. r.y = v1.y > v2.y ? v1.y : v2.y;
  1393. r.z = v1.z > v2.z ? v1.z : v2.z;
  1394. r.w = v1.w > v2.w ? v1.w : v2.w;
  1395. return r;
  1396. }
  1397. #define THUNK_NATIVE_F(fn) \
  1398. float __attribute__((overloadable)) native_##fn(float v) { return fn(v);} \
  1399. float2 __attribute__((overloadable)) native_##fn(float2 v) { return fn(v);} \
  1400. float3 __attribute__((overloadable)) native_##fn(float3 v) { return fn(v);} \
  1401. float4 __attribute__((overloadable)) native_##fn(float4 v) { return fn(v);}
  1402. #define THUNK_NATIVE_F_F(fn) \
  1403. float __attribute__((overloadable)) native_##fn(float v1, float v2) { return fn(v1, v2);} \
  1404. float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 v2) { return fn(v1, v2);} \
  1405. float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 v2) { return fn(v1, v2);} \
  1406. float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 v2) { return fn(v1, v2);}
  1407. #define THUNK_NATIVE_F_FP(fn) \
  1408. float __attribute__((overloadable)) native_##fn(float v1, float *v2) { return fn(v1, v2);} \
  1409. float2 __attribute__((overloadable)) native_##fn(float2 v1, float2 *v2) { return fn(v1, v2);} \
  1410. float3 __attribute__((overloadable)) native_##fn(float3 v1, float3 *v2) { return fn(v1, v2);} \
  1411. float4 __attribute__((overloadable)) native_##fn(float4 v1, float4 *v2) { return fn(v1, v2);}
  1412. #define THUNK_NATIVE_F_I(fn) \
  1413. float __attribute__((overloadable)) native_##fn(float v1, int v2) { return fn(v1, v2);} \
  1414. float2 __attribute__((overloadable)) native_##fn(float2 v1, int2 v2) { return fn(v1, v2);} \
  1415. float3 __attribute__((overloadable)) native_##fn(float3 v1, int3 v2) { return fn(v1, v2);} \
  1416. float4 __attribute__((overloadable)) native_##fn(float4 v1, int4 v2) { return fn(v1, v2);}
  1417. THUNK_NATIVE_F(acos)
  1418. THUNK_NATIVE_F(acosh)
  1419. THUNK_NATIVE_F(acospi)
  1420. THUNK_NATIVE_F(asin)
  1421. THUNK_NATIVE_F(asinh)
  1422. THUNK_NATIVE_F(asinpi)
  1423. THUNK_NATIVE_F(atan)
  1424. THUNK_NATIVE_F_F(atan2)
  1425. THUNK_NATIVE_F(atanh)
  1426. THUNK_NATIVE_F(atanpi)
  1427. THUNK_NATIVE_F_F(atan2pi)
  1428. THUNK_NATIVE_F(cbrt)
  1429. THUNK_NATIVE_F(cos)
  1430. THUNK_NATIVE_F(cosh)
  1431. THUNK_NATIVE_F(cospi)
  1432. THUNK_NATIVE_F(expm1)
  1433. THUNK_NATIVE_F_F(hypot)
  1434. THUNK_NATIVE_F(log1p)
  1435. THUNK_NATIVE_F_I(rootn)
  1436. THUNK_NATIVE_F(rsqrt)
  1437. THUNK_NATIVE_F(sqrt)
  1438. THUNK_NATIVE_F(sin)
  1439. THUNK_NATIVE_F_FP(sincos)
  1440. THUNK_NATIVE_F(sinh)
  1441. THUNK_NATIVE_F(sinpi)
  1442. THUNK_NATIVE_F(tan)
  1443. THUNK_NATIVE_F(tanh)
  1444. THUNK_NATIVE_F(tanpi)
  1445. #undef THUNK_NATIVE_F
  1446. #undef THUNK_NATIVE_F_F
  1447. #undef THUNK_NATIVE_F_I
  1448. #undef THUNK_NATIVE_F_FP
  1449. float __attribute__((overloadable)) native_normalize(float v) { return fast_normalize(v);}
  1450. float2 __attribute__((overloadable)) native_normalize(float2 v) { return fast_normalize(v);}
  1451. float3 __attribute__((overloadable)) native_normalize(float3 v) { return fast_normalize(v);}
  1452. float4 __attribute__((overloadable)) native_normalize(float4 v) { return fast_normalize(v);}
  1453. float __attribute__((overloadable)) native_distance(float v1, float v2) { return fast_distance(v1, v2);}
  1454. float __attribute__((overloadable)) native_distance(float2 v1, float2 v2) { return fast_distance(v1, v2);}
  1455. float __attribute__((overloadable)) native_distance(float3 v1, float3 v2) { return fast_distance(v1, v2);}
  1456. float __attribute__((overloadable)) native_distance(float4 v1, float4 v2) { return fast_distance(v1, v2);}
  1457. float __attribute__((overloadable)) native_length(float v) { return fast_length(v);}
  1458. float __attribute__((overloadable)) native_length(float2 v) { return fast_length(v);}
  1459. float __attribute__((overloadable)) native_length(float3 v) { return fast_length(v);}
  1460. float __attribute__((overloadable)) native_length(float4 v) { return fast_length(v);}
  1461. float __attribute__((overloadable)) native_divide(float v1, float v2) { return v1 / v2;}
  1462. float2 __attribute__((overloadable)) native_divide(float2 v1, float2 v2) { return v1 / v2;}
  1463. float3 __attribute__((overloadable)) native_divide(float3 v1, float3 v2) { return v1 / v2;}
  1464. float4 __attribute__((overloadable)) native_divide(float4 v1, float4 v2) { return v1 / v2;}
  1465. float __attribute__((overloadable)) native_recip(float v) { return 1.f / v;}
  1466. float2 __attribute__((overloadable)) native_recip(float2 v) { return ((float2)1.f) / v;}
  1467. float3 __attribute__((overloadable)) native_recip(float3 v) { return ((float3)1.f) / v;}
  1468. float4 __attribute__((overloadable)) native_recip(float4 v) { return ((float4)1.f) / v;}
  1469. #undef FN_FUNC_FN
  1470. #undef IN_FUNC_FN
  1471. #undef FN_FUNC_FN_FN
  1472. #undef FN_FUNC_FN_F
  1473. #undef FN_FUNC_FN_IN
  1474. #undef FN_FUNC_FN_I
  1475. #undef FN_FUNC_FN_PFN
  1476. #undef FN_FUNC_FN_PIN
  1477. #undef FN_FUNC_FN_FN_FN
  1478. #undef FN_FUNC_FN_FN_PIN
  1479. #undef XN_FUNC_YN
  1480. #undef UIN_FUNC_IN
  1481. #undef IN_FUNC_IN
  1482. #undef XN_FUNC_XN_XN_BODY
  1483. #undef IN_FUNC_IN_IN_BODY
  1484. static const unsigned short kHalfPositiveInfinity = 0x7c00;
  1485. /* Define f16 functions of the form
  1486. * HN output = fn(HN input)
  1487. * where HN is scalar or vector half type
  1488. */
  1489. #define HN_FUNC_HN(fn) \
  1490. extern half __attribute__((overloadable)) fn(half h) { \
  1491. return (half) fn((float) h); \
  1492. } \
  1493. extern half2 __attribute__((overloadable)) fn(half2 v) { \
  1494. return convert_half2(fn(convert_float2(v))); \
  1495. } \
  1496. extern half3 __attribute__((overloadable)) fn(half3 v) { \
  1497. return convert_half3(fn(convert_float3(v))); \
  1498. } \
  1499. extern half4 __attribute__((overloadable)) fn(half4 v) { \
  1500. return convert_half4(fn(convert_float4(v))); \
  1501. }
  1502. /* Define f16 functions of the form
  1503. * HN output = fn(HN input1, HN input2)
  1504. * where HN is scalar or vector half type
  1505. */
  1506. #define HN_FUNC_HN_HN(fn) \
  1507. extern half __attribute__((overloadable)) fn(half h1, half h2) { \
  1508. return (half) fn((float) h1, (float) h2); \
  1509. } \
  1510. extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2) { \
  1511. return convert_half2(fn(convert_float2(v1), \
  1512. convert_float2(v2))); \
  1513. } \
  1514. extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2) { \
  1515. return convert_half3(fn(convert_float3(v1), \
  1516. convert_float3(v2))); \
  1517. } \
  1518. extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2) { \
  1519. return convert_half4(fn(convert_float4(v1), \
  1520. convert_float4(v2))); \
  1521. }
  1522. /* Define f16 functions of the form
  1523. * HN output = fn(HN input1, half input2)
  1524. * where HN is scalar or vector half type
  1525. */
  1526. #define HN_FUNC_HN_H(fn) \
  1527. extern half2 __attribute__((overloadable)) fn(half2 v1, half v2) { \
  1528. return convert_half2(fn(convert_float2(v1), (float) v2)); \
  1529. } \
  1530. extern half3 __attribute__((overloadable)) fn(half3 v1, half v2) { \
  1531. return convert_half3(fn(convert_float3(v1), (float) v2)); \
  1532. } \
  1533. extern half4 __attribute__((overloadable)) fn(half4 v1, half v2) { \
  1534. return convert_half4(fn(convert_float4(v1), (float) v2)); \
  1535. }
  1536. /* Define f16 functions of the form
  1537. * HN output = fn(HN input1, HN input2, HN input3)
  1538. * where HN is scalar or vector half type
  1539. */
  1540. #define HN_FUNC_HN_HN_HN(fn) \
  1541. extern half __attribute__((overloadable)) fn(half h1, half h2, half h3) { \
  1542. return (half) fn((float) h1, (float) h2, (float) h3); \
  1543. } \
  1544. extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2, half2 v3) { \
  1545. return convert_half2(fn(convert_float2(v1), \
  1546. convert_float2(v2), \
  1547. convert_float2(v3))); \
  1548. } \
  1549. extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2, half3 v3) { \
  1550. return convert_half3(fn(convert_float3(v1), \
  1551. convert_float3(v2), \
  1552. convert_float3(v3))); \
  1553. } \
  1554. extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2, half4 v3) { \
  1555. return convert_half4(fn(convert_float4(v1), \
  1556. convert_float4(v2), \
  1557. convert_float4(v3))); \
  1558. }
  1559. /* Define f16 functions of the form
  1560. * HN output = fn(HN input1, IN input2)
  1561. * where HN is scalar or vector half type and IN the equivalent integer type
  1562. * of same vector length.
  1563. */
  1564. #define HN_FUNC_HN_IN(fn) \
  1565. extern half __attribute__((overloadable)) fn(half h1, int v) { \
  1566. return (half) fn((float) h1, v); \
  1567. } \
  1568. extern half2 __attribute__((overloadable)) fn(half2 v1, int2 v2) { \
  1569. return convert_half2(fn(convert_float2(v1), v2)); \
  1570. } \
  1571. extern half3 __attribute__((overloadable)) fn(half3 v1, int3 v2) { \
  1572. return convert_half3(fn(convert_float3(v1), v2)); \
  1573. } \
  1574. extern half4 __attribute__((overloadable)) fn(half4 v1, int4 v2) { \
  1575. return convert_half4(fn(convert_float4(v1), v2)); \
  1576. }
  1577. /* Define f16 functions of the form
  1578. * half output = fn(HN input1)
  1579. * where HN is a scalar or vector half type.
  1580. */
  1581. #define H_FUNC_HN(fn) \
  1582. extern half __attribute__((overloadable)) fn(half h) { \
  1583. return (half) fn((float) h); \
  1584. } \
  1585. extern half __attribute__((overloadable)) fn(half2 v) { \
  1586. return fn(convert_float2(v)); \
  1587. } \
  1588. extern half __attribute__((overloadable)) fn(half3 v) { \
  1589. return fn(convert_float3(v)); \
  1590. } \
  1591. extern half __attribute__((overloadable)) fn(half4 v) { \
  1592. return fn(convert_float4(v)); \
  1593. }
  1594. /* Define f16 functions of the form
  1595. * half output = fn(HN input1, HN input2)
  1596. * where HN is a scalar or vector half type.
  1597. */
  1598. #define H_FUNC_HN_HN(fn) \
  1599. extern half __attribute__((overloadable)) fn(half h1, half h2) { \
  1600. return (half) fn((float) h1, (float) h2); \
  1601. } \
  1602. extern half __attribute__((overloadable)) fn(half2 v1, half2 v2) { \
  1603. return fn(convert_float2(v1), convert_float2(v2)); \
  1604. } \
  1605. extern half __attribute__((overloadable)) fn(half3 v1, half3 v2) { \
  1606. return fn(convert_float3(v1), convert_float3(v2)); \
  1607. } \
  1608. extern half __attribute__((overloadable)) fn(half4 v1, half4 v2) { \
  1609. return fn(convert_float4(v1), convert_float4(v2)); \
  1610. }
  1611. #define SCALARIZE_HN_FUNC_HN_PHN(fnc) \
  1612. extern half2 __attribute__((overloadable)) fnc(half2 v1, half2 *v2) { \
  1613. half2 ret; \
  1614. half t[2]; \
  1615. ret.x = fnc(v1.x, &t[0]); \
  1616. ret.y = fnc(v1.y, &t[1]); \
  1617. v2->x = t[0]; \
  1618. v2->y = t[1]; \
  1619. return ret; \
  1620. } \
  1621. extern half3 __attribute__((overloadable)) fnc(half3 v1, half3 *v2) { \
  1622. half3 ret; \
  1623. half t[3]; \
  1624. ret.x = fnc(v1.x, &t[0]); \
  1625. ret.y = fnc(v1.y, &t[1]); \
  1626. ret.z = fnc(v1.z, &t[2]); \
  1627. v2->x = t[0]; \
  1628. v2->y = t[1]; \
  1629. v2->z = t[2]; \
  1630. return ret; \
  1631. } \
  1632. extern half4 __attribute__((overloadable)) fnc(half4 v1, half4 *v2) { \
  1633. half4 ret; \
  1634. half t[4]; \
  1635. ret.x = fnc(v1.x, &t[0]); \
  1636. ret.y = fnc(v1.y, &t[1]); \
  1637. ret.z = fnc(v1.z, &t[2]); \
  1638. ret.w = fnc(v1.w, &t[3]); \
  1639. v2->x = t[0]; \
  1640. v2->y = t[1]; \
  1641. v2->z = t[2]; \
  1642. v2->w = t[3]; \
  1643. return ret; \
  1644. }
  1645. /* Define f16 functions of the form
  1646. * HN output = fn(HN input1, HN input2)
  1647. * where HN is a vector half type. The functions are defined to call the
  1648. * scalar function of the same name.
  1649. */
  1650. #define SCALARIZE_HN_FUNC_HN_HN(fn) \
  1651. extern half2 __attribute__((overloadable)) fn(half2 v1, half2 v2) { \
  1652. half2 ret; \
  1653. ret.x = fn(v1.x, v2.x); \
  1654. ret.y = fn(v1.y, v2.y); \
  1655. return ret; \
  1656. } \
  1657. extern half3 __attribute__((overloadable)) fn(half3 v1, half3 v2) { \
  1658. half3 ret; \
  1659. ret.x = fn(v1.x, v2.x); \
  1660. ret.y = fn(v1.y, v2.y); \
  1661. ret.z = fn(v1.z, v2.z); \
  1662. return ret; \
  1663. } \
  1664. extern half4 __attribute__((overloadable)) fn(half4 v1, half4 v2) { \
  1665. half4 ret; \
  1666. ret.x = fn(v1.x, v2.x); \
  1667. ret.y = fn(v1.y, v2.y); \
  1668. ret.z = fn(v1.z, v2.z); \
  1669. ret.w = fn(v1.w, v2.w); \
  1670. return ret; \
  1671. } \
  1672. HN_FUNC_HN(acos);
  1673. HN_FUNC_HN(acosh);
  1674. HN_FUNC_HN(acospi);
  1675. HN_FUNC_HN(asin);
  1676. HN_FUNC_HN(asinh);
  1677. HN_FUNC_HN(asinpi);
  1678. HN_FUNC_HN(atan);
  1679. HN_FUNC_HN(atanh);
  1680. HN_FUNC_HN(atanpi);
  1681. HN_FUNC_HN_HN(atan2);
  1682. HN_FUNC_HN_HN(atan2pi);
  1683. HN_FUNC_HN(cbrt);
  1684. HN_FUNC_HN(ceil);
  1685. extern half __attribute__((overloadable)) copysign(half x, half y);
  1686. SCALARIZE_HN_FUNC_HN_HN(copysign);
  1687. HN_FUNC_HN(cos);
  1688. HN_FUNC_HN(cosh);
  1689. HN_FUNC_HN(cospi);
  1690. extern half3 __attribute__((overloadable)) cross(half3 lhs, half3 rhs) {
  1691. half3 r;
  1692. r.x = lhs.y * rhs.z - lhs.z * rhs.y;
  1693. r.y = lhs.z * rhs.x - lhs.x * rhs.z;
  1694. r.z = lhs.x * rhs.y - lhs.y * rhs.x;
  1695. return r;
  1696. }
  1697. extern half4 __attribute__((overloadable)) cross(half4 lhs, half4 rhs) {
  1698. half4 r;
  1699. r.x = lhs.y * rhs.z - lhs.z * rhs.y;
  1700. r.y = lhs.z * rhs.x - lhs.x * rhs.z;
  1701. r.z = lhs.x * rhs.y - lhs.y * rhs.x;
  1702. r.w = 0.f;
  1703. return r;
  1704. }
  1705. HN_FUNC_HN(degrees);
  1706. H_FUNC_HN_HN(distance);
  1707. H_FUNC_HN_HN(dot);
  1708. HN_FUNC_HN(erf);
  1709. HN_FUNC_HN(erfc);
  1710. HN_FUNC_HN(exp);
  1711. HN_FUNC_HN(exp10);
  1712. HN_FUNC_HN(exp2);
  1713. HN_FUNC_HN(expm1);
  1714. HN_FUNC_HN(fabs);
  1715. HN_FUNC_HN_HN(fdim);
  1716. HN_FUNC_HN(floor);
  1717. HN_FUNC_HN_HN_HN(fma);
  1718. HN_FUNC_HN_HN(fmax);
  1719. HN_FUNC_HN_H(fmax);
  1720. HN_FUNC_HN_HN(fmin);
  1721. HN_FUNC_HN_H(fmin);
  1722. HN_FUNC_HN_HN(fmod);
  1723. extern half __attribute__((overloadable)) fract(half v, half *iptr) {
  1724. // maxLessThanOne = 0.99951171875, the largest value < 1.0
  1725. half maxLessThanOne;
  1726. SET_HALF_WORD(maxLessThanOne, 0x3bff);
  1727. int i = (int) floor(v);
  1728. if (iptr) {
  1729. *iptr = i;
  1730. }
  1731. // return v - floor(v), if strictly less than one
  1732. return fmin(v - i, maxLessThanOne);
  1733. }
  1734. SCALARIZE_HN_FUNC_HN_PHN(fract);
  1735. extern half __attribute__((const, overloadable)) fract(half v) {
  1736. half unused;
  1737. return fract(v, &unused);
  1738. }
  1739. extern half2 __attribute__((const, overloadable)) fract(half2 v) {
  1740. half2 unused;
  1741. return fract(v, &unused);
  1742. }
  1743. extern half3 __attribute__((const, overloadable)) fract(half3 v) {
  1744. half3 unused;
  1745. return fract(v, &unused);
  1746. }
  1747. extern half4 __attribute__((const, overloadable)) fract(half4 v) {
  1748. half4 unused;
  1749. return fract(v, &unused);
  1750. }
  1751. extern half __attribute__((overloadable)) frexp(half x, int *eptr);
  1752. extern half2 __attribute__((overloadable)) frexp(half2 v1, int2 *eptr) {
  1753. half2 ret;
  1754. int e[2];
  1755. ret.x = frexp(v1.x, &e[0]);
  1756. ret.y = frexp(v1.y, &e[1]);
  1757. eptr->x = e[0];
  1758. eptr->y = e[1];
  1759. return ret;
  1760. }
  1761. extern half3 __attribute__((overloadable)) frexp(half3 v1, int3 *eptr) {
  1762. half3 ret;
  1763. int e[3];
  1764. ret.x = frexp(v1.x, &e[0]);
  1765. ret.y = frexp(v1.y, &e[1]);
  1766. ret.z = frexp(v1.z, &e[2]);
  1767. eptr->x = e[0];
  1768. eptr->y = e[1];
  1769. eptr->z = e[2];
  1770. return ret;
  1771. }
  1772. extern half4 __attribute__((overloadable)) frexp(half4 v1, int4 *eptr) {
  1773. half4 ret;
  1774. int e[4];
  1775. ret.x = frexp(v1.x, &e[0]);
  1776. ret.y = frexp(v1.y, &e[1]);
  1777. ret.z = frexp(v1.z, &e[2]);
  1778. ret.w = frexp(v1.w, &e[3]);
  1779. eptr->x = e[0];
  1780. eptr->y = e[1];
  1781. eptr->z = e[2];
  1782. eptr->w = e[3];
  1783. return ret;
  1784. }
  1785. HN_FUNC_HN_HN(hypot);
  1786. extern int __attribute__((overloadable)) ilogb(half x);
  1787. extern int2 __attribute__((overloadable)) ilogb(half2 v) {
  1788. int2 ret;
  1789. ret.x = ilogb(v.x);
  1790. ret.y = ilogb(v.y);
  1791. return ret;
  1792. }
  1793. extern int3 __attribute__((overloadable)) ilogb(half3 v) {
  1794. int3 ret;
  1795. ret.x = ilogb(v.x);
  1796. ret.y = ilogb(v.y);
  1797. ret.z = ilogb(v.z);
  1798. return ret;
  1799. }
  1800. extern int4 __attribute__((overloadable)) ilogb(half4 v) {
  1801. int4 ret;
  1802. ret.x = ilogb(v.x);
  1803. ret.y = ilogb(v.y);
  1804. ret.z = ilogb(v.z);
  1805. ret.w = ilogb(v.w);
  1806. return ret;
  1807. }
  1808. HN_FUNC_HN_IN(ldexp);
  1809. extern half2 __attribute__((overloadable)) ldexp(half2 v, int exponent) {
  1810. return convert_half2(ldexp(convert_float2(v), exponent));
  1811. }
  1812. extern half3 __attribute__((overloadable)) ldexp(half3 v, int exponent) {
  1813. return convert_half3(ldexp(convert_float3(v), exponent));
  1814. }
  1815. extern half4 __attribute__((overloadable)) ldexp(half4 v, int exponent) {
  1816. return convert_half4(ldexp(convert_float4(v), exponent));
  1817. }
  1818. H_FUNC_HN(length);
  1819. HN_FUNC_HN(lgamma);
  1820. extern half __attribute__((overloadable)) lgamma(half h, int *signp) {
  1821. return (half) lgamma((float) h, signp);
  1822. }
  1823. extern half2 __attribute__((overloadable)) lgamma(half2 v, int2 *signp) {
  1824. return convert_half2(lgamma(convert_float2(v), signp));
  1825. }
  1826. extern half3 __attribute__((overloadable)) lgamma(half3 v, int3 *signp) {
  1827. return convert_half3(lgamma(convert_float3(v), signp));
  1828. }
  1829. extern half4 __attribute__((overloadable)) lgamma(half4 v, int4 *signp) {
  1830. return convert_half4(lgamma(convert_float4(v), signp));
  1831. }
  1832. HN_FUNC_HN(log);
  1833. HN_FUNC_HN(log10);
  1834. HN_FUNC_HN(log1p);
  1835. HN_FUNC_HN(log2);
  1836. HN_FUNC_HN(logb);
  1837. HN_FUNC_HN_HN_HN(mad);
  1838. HN_FUNC_HN_HN(max);
  1839. HN_FUNC_HN_H(max); // TODO can this be arch-specific similar to _Z3maxDv2_ff?
  1840. HN_FUNC_HN_HN(min);
  1841. HN_FUNC_HN_H(min); // TODO can this be arch-specific similar to _Z3minDv2_ff?
  1842. extern half __attribute__((overloadable)) mix(half start, half stop, half amount) {
  1843. return start + (stop - start) * amount;
  1844. }
  1845. extern half2 __attribute__((overloadable)) mix(half2 start, half2 stop, half2 amount) {
  1846. return start + (stop - start) * amount;
  1847. }
  1848. extern half3 __attribute__((overloadable)) mix(half3 start, half3 stop, half3 amount) {
  1849. return start + (stop - start) * amount;
  1850. }
  1851. extern half4 __attribute__((overloadable)) mix(half4 start, half4 stop, half4 amount) {
  1852. return start + (stop - start) * amount;
  1853. }
  1854. extern half2 __attribute__((overloadable)) mix(half2 start, half2 stop, half amount) {
  1855. return start + (stop - start) * amount;
  1856. }
  1857. extern half3 __attribute__((overloadable)) mix(half3 start, half3 stop, half amount) {
  1858. return start + (stop - start) * amount;
  1859. }
  1860. extern half4 __attribute__((overloadable)) mix(half4 start, half4 stop, half amount) {
  1861. return start + (stop - start) * amount;
  1862. }
  1863. extern half __attribute__((overloadable)) modf(half x, half *iptr);
  1864. SCALARIZE_HN_FUNC_HN_PHN(modf);
  1865. half __attribute__((overloadable)) nan_half() {
  1866. unsigned short nan_short = kHalfPositiveInfinity | 0x0200;
  1867. half nan;
  1868. SET_HALF_WORD(nan, nan_short);
  1869. return nan;
  1870. }
  1871. HN_FUNC_HN(normalize);
  1872. extern half __attribute__((overloadable)) nextafter(half x, half y);
  1873. SCALARIZE_HN_FUNC_HN_HN(nextafter);
  1874. HN_FUNC_HN_HN(pow);
  1875. HN_FUNC_HN_IN(pown);
  1876. HN_FUNC_HN_HN(powr);
  1877. HN_FUNC_HN(radians);
  1878. HN_FUNC_HN_HN(remainder);
  1879. extern half __attribute__((overloadable)) remquo(half n, half d, int *quo) {
  1880. return (float) remquo((float) n, (float) d, quo);
  1881. }
  1882. extern half2 __attribute__((overloadable)) remquo(half2 n, half2 d, int2 *quo) {
  1883. return convert_half2(remquo(convert_float2(d), convert_float2(n), quo));
  1884. }
  1885. extern half3 __attribute__((overloadable)) remquo(half3 n, half3 d, int3 *quo) {
  1886. return convert_half3(remquo(convert_float3(d), convert_float3(n), quo));
  1887. }
  1888. extern half4 __attribute__((overloadable)) remquo(half4 n, half4 d, int4 *quo) {
  1889. return convert_half4(remquo(convert_float4(d), convert_float4(n), quo));
  1890. }
  1891. HN_FUNC_HN(rint);
  1892. HN_FUNC_HN_IN(rootn);
  1893. HN_FUNC_HN(round);
  1894. HN_FUNC_HN(rsqrt);
  1895. extern half __attribute__((overloadable)) sign(half h) {
  1896. if (h > 0) return (half) 1.f;
  1897. if (h < 0) return (half) -1.f;
  1898. return h;
  1899. }
  1900. extern half2 __attribute__((overloadable)) sign(half2 v) {
  1901. half2 ret;
  1902. ret.x = sign(v.x);
  1903. ret.y = sign(v.y);
  1904. return ret;
  1905. }
  1906. extern half3 __attribute__((overloadable)) sign(half3 v) {
  1907. half3 ret;
  1908. ret.x = sign(v.x);
  1909. ret.y = sign(v.y);
  1910. ret.z = sign(v.z);
  1911. return ret;
  1912. }
  1913. extern half4 __attribute__((overloadable)) sign(half4 v) {
  1914. half4 ret;
  1915. ret.x = sign(v.x);
  1916. ret.y = sign(v.y);
  1917. ret.z = sign(v.z);
  1918. ret.w = sign(v.w);
  1919. return ret;
  1920. }
  1921. HN_FUNC_HN(sin);
  1922. extern half __attribute__((overloadable)) sincos(half v, half *cosptr) {
  1923. *cosptr = cos(v);
  1924. return sin(v);
  1925. }
  1926. // TODO verify if LLVM eliminates the duplicate convert_float2
  1927. extern half2 __attribute__((overloadable)) sincos(half2 v, half2 *cosptr) {
  1928. *cosptr = cos(v);
  1929. return sin(v);
  1930. }
  1931. extern half3 __attribute__((overloadable)) sincos(half3 v, half3 *cosptr) {
  1932. *cosptr = cos(v);
  1933. return sin(v);
  1934. }
  1935. extern half4 __attribute__((overloadable)) sincos(half4 v, half4 *cosptr) {
  1936. *cosptr = cos(v);
  1937. return sin(v);
  1938. }
  1939. HN_FUNC_HN(sinh);
  1940. HN_FUNC_HN(sinpi);
  1941. HN_FUNC_HN(sqrt);
  1942. extern half __attribute__((overloadable)) step(half edge, half v) {
  1943. return (v < edge) ? 0.f : 1.f;
  1944. }
  1945. extern half2 __attribute__((overloadable)) step(half2 edge, half2 v) {
  1946. half2 r;
  1947. r.x = (v.x < edge.x) ? 0.f : 1.f;
  1948. r.y = (v.y < edge.y) ? 0.f : 1.f;
  1949. return r;
  1950. }
  1951. extern half3 __attribute__((overloadable)) step(half3 edge, half3 v) {
  1952. half3 r;
  1953. r.x = (v.x < edge.x) ? 0.f : 1.f;
  1954. r.y = (v.y < edge.y) ? 0.f : 1.f;
  1955. r.z = (v.z < edge.z) ? 0.f : 1.f;
  1956. return r;
  1957. }
  1958. extern half4 __attribute__((overloadable)) step(half4 edge, half4 v) {
  1959. half4 r;
  1960. r.x = (v.x < edge.x) ? 0.f : 1.f;
  1961. r.y = (v.y < edge.y) ? 0.f : 1.f;
  1962. r.z = (v.z < edge.z) ? 0.f : 1.f;
  1963. r.w = (v.w < edge.w) ? 0.f : 1.f;
  1964. return r;
  1965. }
  1966. extern half2 __attribute__((overloadable)) step(half2 edge, half v) {
  1967. half2 r;
  1968. r.x = (v < edge.x) ? 0.f : 1.f;
  1969. r.y = (v < edge.y) ? 0.f : 1.f;
  1970. return r;
  1971. }
  1972. extern half3 __attribute__((overloadable)) step(half3 edge, half v) {
  1973. half3 r;
  1974. r.x = (v < edge.x) ? 0.f : 1.f;
  1975. r.y = (v < edge.y) ? 0.f : 1.f;
  1976. r.z = (v < edge.z) ? 0.f : 1.f;
  1977. return r;
  1978. }
  1979. extern half4 __attribute__((overloadable)) step(half4 edge, half v) {
  1980. half4 r;
  1981. r.x = (v < edge.x) ? 0.f : 1.f;
  1982. r.y = (v < edge.y) ? 0.f : 1.f;
  1983. r.z = (v < edge.z) ? 0.f : 1.f;
  1984. r.w = (v < edge.w) ? 0.f : 1.f;
  1985. return r;
  1986. }
  1987. extern half2 __attribute__((overloadable)) step(half edge, half2 v) {
  1988. half2 r;
  1989. r.x = (v.x < edge) ? 0.f : 1.f;
  1990. r.y = (v.y < edge) ? 0.f : 1.f;
  1991. return r;
  1992. }
  1993. extern half3 __attribute__((overloadable)) step(half edge, half3 v) {
  1994. half3 r;
  1995. r.x = (v.x < edge) ? 0.f : 1.f;
  1996. r.y = (v.y < edge) ? 0.f : 1.f;
  1997. r.z = (v.z < edge) ? 0.f : 1.f;
  1998. return r;
  1999. }
  2000. extern half4 __attribute__((overloadable)) step(half edge, half4 v) {
  2001. half4 r;
  2002. r.x = (v.x < edge) ? 0.f : 1.f;
  2003. r.y = (v.y < edge) ? 0.f : 1.f;
  2004. r.z = (v.z < edge) ? 0.f : 1.f;
  2005. r.w = (v.w < edge) ? 0.f : 1.f;
  2006. return r;
  2007. }
  2008. HN_FUNC_HN(tan);
  2009. HN_FUNC_HN(tanh);
  2010. HN_FUNC_HN(tanpi);
  2011. HN_FUNC_HN(tgamma);
  2012. HN_FUNC_HN(trunc); // TODO: rethink: needs half-specific implementation?
  2013. HN_FUNC_HN(native_acos);
  2014. HN_FUNC_HN(native_acosh);
  2015. HN_FUNC_HN(native_acospi);
  2016. HN_FUNC_HN(native_asin);
  2017. HN_FUNC_HN(native_asinh);
  2018. HN_FUNC_HN(native_asinpi);
  2019. HN_FUNC_HN(native_atan);
  2020. HN_FUNC_HN(native_atanh);
  2021. HN_FUNC_HN(native_atanpi);
  2022. HN_FUNC_HN_HN(native_atan2);
  2023. HN_FUNC_HN_HN(native_atan2pi);
  2024. HN_FUNC_HN(native_cbrt);
  2025. HN_FUNC_HN(native_cos);
  2026. HN_FUNC_HN(native_cosh);
  2027. HN_FUNC_HN(native_cospi);
  2028. H_FUNC_HN_HN(native_distance);
  2029. HN_FUNC_HN_HN(native_divide);
  2030. HN_FUNC_HN(native_exp);
  2031. HN_FUNC_HN(native_exp10);
  2032. HN_FUNC_HN(native_exp2);
  2033. HN_FUNC_HN(native_expm1);
  2034. HN_FUNC_HN_HN(native_hypot);
  2035. H_FUNC_HN(native_length);
  2036. HN_FUNC_HN(native_log);
  2037. HN_FUNC_HN(native_log10);
  2038. HN_FUNC_HN(native_log1p);
  2039. HN_FUNC_HN(native_log2);
  2040. HN_FUNC_HN(native_normalize);
  2041. HN_FUNC_HN_HN(native_powr); // TODO are parameter limits different for half?
  2042. HN_FUNC_HN(native_recip);
  2043. HN_FUNC_HN_IN(native_rootn);
  2044. HN_FUNC_HN(native_rsqrt);
  2045. HN_FUNC_HN(native_sin);
  2046. extern half __attribute__((overloadable)) native_sincos(half v, half *cosptr) {
  2047. return sincos(v, cosptr);
  2048. }
  2049. extern half2 __attribute__((overloadable)) native_sincos(half2 v, half2 *cosptr) {
  2050. return sincos(v, cosptr);
  2051. }
  2052. extern half3 __attribute__((overloadable)) native_sincos(half3 v, half3 *cosptr) {
  2053. return sincos(v, cosptr);
  2054. }
  2055. extern half4 __attribute__((overloadable)) native_sincos(half4 v, half4 *cosptr) {
  2056. return sincos(v, cosptr);
  2057. }
  2058. HN_FUNC_HN(native_sinh);
  2059. HN_FUNC_HN(native_sinpi);
  2060. HN_FUNC_HN(native_sqrt);
  2061. HN_FUNC_HN(native_tan);
  2062. HN_FUNC_HN(native_tanh);
  2063. HN_FUNC_HN(native_tanpi);
  2064. #undef HN_FUNC_HN
  2065. #undef HN_FUNC_HN_HN
  2066. #undef HN_FUNC_HN_H
  2067. #undef HN_FUNC_HN_HN_HN
  2068. #undef HN_FUNC_HN_IN
  2069. #undef H_FUNC_HN
  2070. #undef H_FUNC_HN_HN
  2071. #undef SCALARIZE_HN_FUNC_HN_HN
  2072. // exports unavailable mathlib functions to compat lib
  2073. #ifdef RS_COMPATIBILITY_LIB
  2074. // !!! DANGER !!!
  2075. // These functions are potentially missing on older Android versions.
  2076. // Work around the issue by supplying our own variants.
  2077. // !!! DANGER !!!
  2078. // The logbl() implementation is taken from the latest bionic/, since
  2079. // double == long double on Android.
  2080. extern "C" long double logbl(long double x) { return logb(x); }
  2081. // __aeabi_idiv0 is a missing function in libcompiler_rt.so, so we just
  2082. // pick the simplest implementation based on the ARM EABI doc.
  2083. extern "C" int __aeabi_idiv0(int v) { return v; }
  2084. #endif // compatibility lib