0011-riscv32-Add-fenv-and-math.patch 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. From 748bf9c7b74ee056d4d4346f49ea6f824d845f08 Mon Sep 17 00:00:00 2001
  2. From: Stefan O'Rear <sorear@fastmail.com>
  3. Date: Thu, 3 Sep 2020 05:45:44 -0400
  4. Subject: [PATCH 11/16] riscv32: Add fenv and math
  5. These are identical to riscv64.
  6. ---
  7. src/fenv/riscv32/fenv-sf.c | 3 ++
  8. src/fenv/riscv32/fenv.S | 56 ++++++++++++++++++++++++++++++++++++
  9. src/math/riscv32/copysign.c | 15 ++++++++++
  10. src/math/riscv32/copysignf.c | 15 ++++++++++
  11. src/math/riscv32/fabs.c | 15 ++++++++++
  12. src/math/riscv32/fabsf.c | 15 ++++++++++
  13. src/math/riscv32/fma.c | 15 ++++++++++
  14. src/math/riscv32/fmaf.c | 15 ++++++++++
  15. src/math/riscv32/fmax.c | 15 ++++++++++
  16. src/math/riscv32/fmaxf.c | 15 ++++++++++
  17. src/math/riscv32/fmin.c | 15 ++++++++++
  18. src/math/riscv32/fminf.c | 15 ++++++++++
  19. src/math/riscv32/sqrt.c | 15 ++++++++++
  20. src/math/riscv32/sqrtf.c | 15 ++++++++++
  21. 14 files changed, 239 insertions(+)
  22. create mode 100644 src/fenv/riscv32/fenv-sf.c
  23. create mode 100644 src/fenv/riscv32/fenv.S
  24. create mode 100644 src/math/riscv32/copysign.c
  25. create mode 100644 src/math/riscv32/copysignf.c
  26. create mode 100644 src/math/riscv32/fabs.c
  27. create mode 100644 src/math/riscv32/fabsf.c
  28. create mode 100644 src/math/riscv32/fma.c
  29. create mode 100644 src/math/riscv32/fmaf.c
  30. create mode 100644 src/math/riscv32/fmax.c
  31. create mode 100644 src/math/riscv32/fmaxf.c
  32. create mode 100644 src/math/riscv32/fmin.c
  33. create mode 100644 src/math/riscv32/fminf.c
  34. create mode 100644 src/math/riscv32/sqrt.c
  35. create mode 100644 src/math/riscv32/sqrtf.c
  36. diff --git a/src/fenv/riscv32/fenv-sf.c b/src/fenv/riscv32/fenv-sf.c
  37. new file mode 100644
  38. index 00000000..ecd3cb5c
  39. --- /dev/null
  40. +++ b/src/fenv/riscv32/fenv-sf.c
  41. @@ -0,0 +1,3 @@
  42. +#ifndef __riscv_flen
  43. +#include "../fenv.c"
  44. +#endif
  45. diff --git a/src/fenv/riscv32/fenv.S b/src/fenv/riscv32/fenv.S
  46. new file mode 100644
  47. index 00000000..0ea78bf9
  48. --- /dev/null
  49. +++ b/src/fenv/riscv32/fenv.S
  50. @@ -0,0 +1,56 @@
  51. +#ifdef __riscv_flen
  52. +
  53. +.global feclearexcept
  54. +.type feclearexcept, %function
  55. +feclearexcept:
  56. + csrc fflags, a0
  57. + li a0, 0
  58. + ret
  59. +
  60. +.global feraiseexcept
  61. +.type feraiseexcept, %function
  62. +feraiseexcept:
  63. + csrs fflags, a0
  64. + li a0, 0
  65. + ret
  66. +
  67. +.global fetestexcept
  68. +.type fetestexcept, %function
  69. +fetestexcept:
  70. + frflags t0
  71. + and a0, t0, a0
  72. + ret
  73. +
  74. +.global fegetround
  75. +.type fegetround, %function
  76. +fegetround:
  77. + frrm a0
  78. + ret
  79. +
  80. +.global __fesetround
  81. +.type __fesetround, %function
  82. +__fesetround:
  83. + fsrm t0, a0
  84. + li a0, 0
  85. + ret
  86. +
  87. +.global fegetenv
  88. +.type fegetenv, %function
  89. +fegetenv:
  90. + frcsr t0
  91. + sw t0, 0(a0)
  92. + li a0, 0
  93. + ret
  94. +
  95. +.global fesetenv
  96. +.type fesetenv, %function
  97. +fesetenv:
  98. + li t2, -1
  99. + li t1, 0
  100. + beq a0, t2, 1f
  101. + lw t1, 0(a0)
  102. +1: fscsr t1
  103. + li a0, 0
  104. + ret
  105. +
  106. +#endif
  107. diff --git a/src/math/riscv32/copysign.c b/src/math/riscv32/copysign.c
  108. new file mode 100644
  109. index 00000000..c7854178
  110. --- /dev/null
  111. +++ b/src/math/riscv32/copysign.c
  112. @@ -0,0 +1,15 @@
  113. +#include <math.h>
  114. +
  115. +#if __riscv_flen >= 64
  116. +
  117. +double copysign(double x, double y)
  118. +{
  119. + __asm__ ("fsgnj.d %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  120. + return x;
  121. +}
  122. +
  123. +#else
  124. +
  125. +#include "../copysign.c"
  126. +
  127. +#endif
  128. diff --git a/src/math/riscv32/copysignf.c b/src/math/riscv32/copysignf.c
  129. new file mode 100644
  130. index 00000000..a125611a
  131. --- /dev/null
  132. +++ b/src/math/riscv32/copysignf.c
  133. @@ -0,0 +1,15 @@
  134. +#include <math.h>
  135. +
  136. +#if __riscv_flen >= 32
  137. +
  138. +float copysignf(float x, float y)
  139. +{
  140. + __asm__ ("fsgnj.s %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  141. + return x;
  142. +}
  143. +
  144. +#else
  145. +
  146. +#include "../copysignf.c"
  147. +
  148. +#endif
  149. diff --git a/src/math/riscv32/fabs.c b/src/math/riscv32/fabs.c
  150. new file mode 100644
  151. index 00000000..5290b6f0
  152. --- /dev/null
  153. +++ b/src/math/riscv32/fabs.c
  154. @@ -0,0 +1,15 @@
  155. +#include <math.h>
  156. +
  157. +#if __riscv_flen >= 64
  158. +
  159. +double fabs(double x)
  160. +{
  161. + __asm__ ("fabs.d %0, %1" : "=f"(x) : "f"(x));
  162. + return x;
  163. +}
  164. +
  165. +#else
  166. +
  167. +#include "../fabs.c"
  168. +
  169. +#endif
  170. diff --git a/src/math/riscv32/fabsf.c b/src/math/riscv32/fabsf.c
  171. new file mode 100644
  172. index 00000000..f5032e35
  173. --- /dev/null
  174. +++ b/src/math/riscv32/fabsf.c
  175. @@ -0,0 +1,15 @@
  176. +#include <math.h>
  177. +
  178. +#if __riscv_flen >= 32
  179. +
  180. +float fabsf(float x)
  181. +{
  182. + __asm__ ("fabs.s %0, %1" : "=f"(x) : "f"(x));
  183. + return x;
  184. +}
  185. +
  186. +#else
  187. +
  188. +#include "../fabsf.c"
  189. +
  190. +#endif
  191. diff --git a/src/math/riscv32/fma.c b/src/math/riscv32/fma.c
  192. new file mode 100644
  193. index 00000000..99b05713
  194. --- /dev/null
  195. +++ b/src/math/riscv32/fma.c
  196. @@ -0,0 +1,15 @@
  197. +#include <math.h>
  198. +
  199. +#if __riscv_flen >= 64
  200. +
  201. +double fma(double x, double y, double z)
  202. +{
  203. + __asm__ ("fmadd.d %0, %1, %2, %3" : "=f"(x) : "f"(x), "f"(y), "f"(z));
  204. + return x;
  205. +}
  206. +
  207. +#else
  208. +
  209. +#include "../fma.c"
  210. +
  211. +#endif
  212. diff --git a/src/math/riscv32/fmaf.c b/src/math/riscv32/fmaf.c
  213. new file mode 100644
  214. index 00000000..f9dc47ed
  215. --- /dev/null
  216. +++ b/src/math/riscv32/fmaf.c
  217. @@ -0,0 +1,15 @@
  218. +#include <math.h>
  219. +
  220. +#if __riscv_flen >= 32
  221. +
  222. +float fmaf(float x, float y, float z)
  223. +{
  224. + __asm__ ("fmadd.s %0, %1, %2, %3" : "=f"(x) : "f"(x), "f"(y), "f"(z));
  225. + return x;
  226. +}
  227. +
  228. +#else
  229. +
  230. +#include "../fmaf.c"
  231. +
  232. +#endif
  233. diff --git a/src/math/riscv32/fmax.c b/src/math/riscv32/fmax.c
  234. new file mode 100644
  235. index 00000000..023709cd
  236. --- /dev/null
  237. +++ b/src/math/riscv32/fmax.c
  238. @@ -0,0 +1,15 @@
  239. +#include <math.h>
  240. +
  241. +#if __riscv_flen >= 64
  242. +
  243. +double fmax(double x, double y)
  244. +{
  245. + __asm__ ("fmax.d %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  246. + return x;
  247. +}
  248. +
  249. +#else
  250. +
  251. +#include "../fmax.c"
  252. +
  253. +#endif
  254. diff --git a/src/math/riscv32/fmaxf.c b/src/math/riscv32/fmaxf.c
  255. new file mode 100644
  256. index 00000000..863d2bd1
  257. --- /dev/null
  258. +++ b/src/math/riscv32/fmaxf.c
  259. @@ -0,0 +1,15 @@
  260. +#include <math.h>
  261. +
  262. +#if __riscv_flen >= 32
  263. +
  264. +float fmaxf(float x, float y)
  265. +{
  266. + __asm__ ("fmax.s %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  267. + return x;
  268. +}
  269. +
  270. +#else
  271. +
  272. +#include "../fmaxf.c"
  273. +
  274. +#endif
  275. diff --git a/src/math/riscv32/fmin.c b/src/math/riscv32/fmin.c
  276. new file mode 100644
  277. index 00000000..a4e3b067
  278. --- /dev/null
  279. +++ b/src/math/riscv32/fmin.c
  280. @@ -0,0 +1,15 @@
  281. +#include <math.h>
  282. +
  283. +#if __riscv_flen >= 64
  284. +
  285. +double fmin(double x, double y)
  286. +{
  287. + __asm__ ("fmin.d %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  288. + return x;
  289. +}
  290. +
  291. +#else
  292. +
  293. +#include "../fmin.c"
  294. +
  295. +#endif
  296. diff --git a/src/math/riscv32/fminf.c b/src/math/riscv32/fminf.c
  297. new file mode 100644
  298. index 00000000..32156e80
  299. --- /dev/null
  300. +++ b/src/math/riscv32/fminf.c
  301. @@ -0,0 +1,15 @@
  302. +#include <math.h>
  303. +
  304. +#if __riscv_flen >= 32
  305. +
  306. +float fminf(float x, float y)
  307. +{
  308. + __asm__ ("fmin.s %0, %1, %2" : "=f"(x) : "f"(x), "f"(y));
  309. + return x;
  310. +}
  311. +
  312. +#else
  313. +
  314. +#include "../fminf.c"
  315. +
  316. +#endif
  317. diff --git a/src/math/riscv32/sqrt.c b/src/math/riscv32/sqrt.c
  318. new file mode 100644
  319. index 00000000..867a504c
  320. --- /dev/null
  321. +++ b/src/math/riscv32/sqrt.c
  322. @@ -0,0 +1,15 @@
  323. +#include <math.h>
  324. +
  325. +#if __riscv_flen >= 64
  326. +
  327. +double sqrt(double x)
  328. +{
  329. + __asm__ ("fsqrt.d %0, %1" : "=f"(x) : "f"(x));
  330. + return x;
  331. +}
  332. +
  333. +#else
  334. +
  335. +#include "../sqrt.c"
  336. +
  337. +#endif
  338. diff --git a/src/math/riscv32/sqrtf.c b/src/math/riscv32/sqrtf.c
  339. new file mode 100644
  340. index 00000000..610c2cf8
  341. --- /dev/null
  342. +++ b/src/math/riscv32/sqrtf.c
  343. @@ -0,0 +1,15 @@
  344. +#include <math.h>
  345. +
  346. +#if __riscv_flen >= 32
  347. +
  348. +float sqrtf(float x)
  349. +{
  350. + __asm__ ("fsqrt.s %0, %1" : "=f"(x) : "f"(x));
  351. + return x;
  352. +}
  353. +
  354. +#else
  355. +
  356. +#include "../sqrtf.c"
  357. +
  358. +#endif
  359. --
  360. 2.29.2