You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_loongarch64.h 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*****************************************************************************
  2. Copyright (c) 2011-2020, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. /*********************************************************************/
  29. /* Copyright 2009, 2010 The University of Texas at Austin. */
  30. /* All rights reserved. */
  31. /* */
  32. /* Redistribution and use in source and binary forms, with or */
  33. /* without modification, are permitted provided that the following */
  34. /* conditions are met: */
  35. /* */
  36. /* 1. Redistributions of source code must retain the above */
  37. /* copyright notice, this list of conditions and the following */
  38. /* disclaimer. */
  39. /* */
  40. /* 2. Redistributions in binary form must reproduce the above */
  41. /* copyright notice, this list of conditions and the following */
  42. /* disclaimer in the documentation and/or other materials */
  43. /* provided with the distribution. */
  44. /* */
  45. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  46. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  47. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  48. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  49. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  50. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  51. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  52. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  53. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  54. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  55. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  56. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  57. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  58. /* POSSIBILITY OF SUCH DAMAGE. */
  59. /* */
  60. /* The views and conclusions contained in the software and */
  61. /* documentation are those of the authors and should not be */
  62. /* interpreted as representing official policies, either expressed */
  63. /* or implied, of The University of Texas at Austin. */
  64. /*********************************************************************/
  65. #ifndef COMMON_LOONGARCH64
  66. #define COMMON_LOONGARCH64
  67. #define MB __sync_synchronize()
  68. #define WMB __sync_synchronize()
  69. #define RMB __sync_synchronize()
  70. #ifndef ASSEMBLER
  71. static inline int blas_quickdivide(blasint x, blasint y){
  72. return x / y;
  73. }
  74. #ifndef NO_AFFINITY
  75. static inline int WhereAmI(void){
  76. int ret = 0, counter = 0;
  77. __asm__ volatile (
  78. "rdtimel.w %[counter], %[id]"
  79. : [id]"=r"(ret), [counter]"=r"(counter)
  80. :
  81. : "memory"
  82. );
  83. return ret;
  84. }
  85. #endif
  86. static inline int get_cpu_model(char *model_name) {
  87. FILE *cpuinfo_file = fopen("/proc/cpuinfo", "r");
  88. if (!cpuinfo_file) {
  89. return 0;
  90. }
  91. char line[1024];
  92. while (fgets(line, sizeof(line), cpuinfo_file)) {
  93. if (strstr(line, "model name")) {
  94. char *token = strtok(line, ":");
  95. token = strtok(NULL, ":");
  96. while (*token == ' ')
  97. token++;
  98. char *end = token + strlen(token) - 1;
  99. while (end > token && (*end == '\n' || *end == '\r')) {
  100. *end = '\0';
  101. end--;
  102. }
  103. strcpy(model_name, token);
  104. fclose(cpuinfo_file);
  105. return 1;
  106. }
  107. }
  108. fclose(cpuinfo_file);
  109. return 0;
  110. }
  111. #ifdef DOUBLE
  112. #define GET_IMAGE(res) __asm__ __volatile__("fmov.d %0, $f2" : "=f"(res) : : "memory")
  113. #else
  114. #define GET_IMAGE(res) __asm__ __volatile__("fmov.s %0, $f2" : "=f"(res) : : "memory")
  115. #endif
  116. #define GET_IMAGE_CANCEL
  117. #else
  118. #ifdef DOUBLE
  119. #define LD fld.d
  120. #define ST fst.d
  121. #define MADD fmadd.d
  122. #define NMADD fnmadd.d
  123. #define MSUB fmsub.d
  124. #define NMSUB fnmsub.d
  125. #define ADD fadd.d
  126. #define SUB fsub.d
  127. #define MUL fmul.d
  128. #define MOV fmov.d
  129. #define CMOVT fsel
  130. #define MTC movgr2fr.d
  131. #define MTG movfr2gr.d
  132. #define FABS fabs.d
  133. #define FMIN fmin.d
  134. #define FMINA fmina.d
  135. #define FMAX fmax.d
  136. #define FMAXA fmaxa.d
  137. #define CMPEQ fcmp.ceq.d
  138. #define CMPLE fcmp.cle.d
  139. #define CMPLT fcmp.clt.d
  140. #define NEG fneg.d
  141. #define FFINT ffint.d.l
  142. #define XVFSUB xvfsub.d
  143. #define XVFADD xvfadd.d
  144. #define XVFMUL xvfmul.d
  145. #define XVFMADD xvfmadd.d
  146. #define XVFMIN xvfmin.d
  147. #define XVFMINA xvfmina.d
  148. #define XVFMAX xvfmax.d
  149. #define XVFMAXA xvfmaxa.d
  150. #define XVCMPEQ xvfcmp.ceq.d
  151. #define XVCMPLE xvfcmp.cle.d
  152. #define XVCMPLT xvfcmp.clt.d
  153. #define XVMUL xvfmul.d
  154. #define XVMSUB xvfmsub.d
  155. #define XVNMSUB xvfnmsub.d
  156. #define VFSUB vfsub.d
  157. #define VFADD vfadd.d
  158. #define VFMUL vfmul.d
  159. #define VFMADD vfmadd.d
  160. #define VFMIN vfmin.d
  161. #define VFMINA vfmina.d
  162. #define VFMAX vfmax.d
  163. #define VFMAXA vfmaxa.d
  164. #define VCMPEQ vfcmp.ceq.d
  165. #define VCMPLE vfcmp.cle.d
  166. #define VCMPLT vfcmp.clt.d
  167. #define VMUL vfmul.d
  168. #define VMSUB vfmsub.d
  169. #define VNMSUB vfnmsub.d
  170. #else
  171. #define LD fld.s
  172. #define ST fst.s
  173. #define MADD fmadd.s
  174. #define NMADD fnmadd.s
  175. #define MSUB fmsub.s
  176. #define NMSUB fnmsub.s
  177. #define ADD fadd.s
  178. #define SUB fsub.s
  179. #define MUL fmul.s
  180. #define MOV fmov.s
  181. #define CMOVT fsel
  182. #define MTC movgr2fr.w
  183. #define MTG movfr2gr.s
  184. #define FABS fabs.s
  185. #define FMIN fmin.s
  186. #define FMINA fmina.s
  187. #define FMAX fmax.s
  188. #define FMAXA fmaxa.s
  189. #define CMPEQ fcmp.ceq.s
  190. #define CMPLE fcmp.cle.s
  191. #define CMPLT fcmp.clt.s
  192. #define NEG fneg.s
  193. #define FFINT ffint.s.l
  194. #define XVFSUB xvfsub.s
  195. #define XVFADD xvfadd.s
  196. #define XVFMUL xvfmul.s
  197. #define XVFMADD xvfmadd.s
  198. #define XVFMIN xvfmin.s
  199. #define XVFMINA xvfmina.s
  200. #define XVFMAX xvfmax.s
  201. #define XVFMAXA xvfmaxa.s
  202. #define XVCMPEQ xvfcmp.ceq.s
  203. #define XVCMPLE xvfcmp.cle.s
  204. #define XVCMPLT xvfcmp.clt.s
  205. #define XVMUL xvfmul.s
  206. #define XVMSUB xvfmsub.s
  207. #define XVNMSUB xvfnmsub.s
  208. #define VFSUB vfsub.s
  209. #define VFADD vfadd.s
  210. #define VFMUL vfmul.s
  211. #define VFMADD vfmadd.s
  212. #define VFMIN vfmin.s
  213. #define VFMINA vfmina.s
  214. #define VFMAX vfmax.s
  215. #define VFMAXA vfmaxa.s
  216. #define VCMPEQ vfcmp.ceq.s
  217. #define VCMPLE vfcmp.cle.s
  218. #define VCMPLT vfcmp.clt.s
  219. #define VMUL vfmul.s
  220. #define VMSUB vfmsub.s
  221. #define VNMSUB vfnmsub.s
  222. #endif /* defined(DOUBLE) */
  223. #if defined(__64BIT__) && defined(USE64BITINT)
  224. #define LDINT ld.d
  225. #define LDARG ld.d
  226. #define SDARG st.d
  227. #elif defined(__64BIT__) && !defined(USE64BITINT)
  228. #define LDINT ld.w
  229. #define LDARG ld.d
  230. #define SDARG st.d
  231. #else
  232. #define LDINT ld.w
  233. #define LDARG ld.w
  234. #define SDARG st.w
  235. #endif
  236. #ifndef F_INTERFACE
  237. #define REALNAME ASMNAME
  238. #else
  239. #define REALNAME ASMFNAME
  240. #endif /* defined(F_INTERFACE) */
  241. #if defined(ASSEMBLER) && !defined(NEEDPARAM)
  242. #define PROLOGUE \
  243. .text ;\
  244. .align 5 ;\
  245. .globl REALNAME ;\
  246. .type REALNAME, @function ;\
  247. REALNAME: ;\
  248. #if defined(__linux__) && defined(__ELF__)
  249. #define GNUSTACK .section .note.GNU-stack,"",@progbits
  250. #else
  251. #define GNUSTACK
  252. #endif /* defined(__linux__) && defined(__ELF__) */
  253. #ifdef __clang__
  254. #define EPILOGUE .end
  255. #else
  256. #define EPILOGUE \
  257. .end REALNAME ;\
  258. GNUSTACK
  259. #endif
  260. #define PROFCODE
  261. #define MOVT(dst, src, cc) \
  262. bceqz cc, 1f; \
  263. add.d dst, src, $r0; \
  264. 1:
  265. #endif /* defined(ASSEMBLER) && !defined(NEEDPARAM) */
  266. #endif /* defined(ASSEMBLER) */
  267. #define SEEK_ADDRESS
  268. #define BUFFER_SIZE ( 32 << 20)
  269. #define PAGESIZE (16UL << 10)
  270. #define FIXED_PAGESIZE (16UL << 10)
  271. #define HUGE_PAGESIZE ( 2 << 20)
  272. #define BASE_ADDRESS (START_ADDRESS - BUFFER_SIZE * MAX_CPU_NUMBER)
  273. #ifndef MAP_ANONYMOUS
  274. #define MAP_ANONYMOUS MAP_ANON
  275. #endif
  276. #endif