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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #define INLINE inline
  71. #ifndef ASSEMBLER
  72. static inline int blas_quickdivide(blasint x, blasint y){
  73. return x / y;
  74. }
  75. #ifndef NO_AFFINITY
  76. static inline int WhereAmI(void){
  77. int ret = 0, counter = 0;
  78. __asm__ volatile (
  79. "rdtimel.w %[counter], %[id]"
  80. : [id]"=r"(ret), [counter]"=r"(counter)
  81. :
  82. : "memory"
  83. );
  84. return ret;
  85. }
  86. #endif
  87. static inline int get_cpu_model(char *model_name) {
  88. FILE *cpuinfo_file = fopen("/proc/cpuinfo", "r");
  89. if (!cpuinfo_file) {
  90. return 0;
  91. }
  92. char line[1024];
  93. while (fgets(line, sizeof(line), cpuinfo_file)) {
  94. if (strstr(line, "model name")) {
  95. char *token = strtok(line, ":");
  96. token = strtok(NULL, ":");
  97. while (*token == ' ')
  98. token++;
  99. char *end = token + strlen(token) - 1;
  100. while (end > token && (*end == '\n' || *end == '\r')) {
  101. *end = '\0';
  102. end--;
  103. }
  104. strcpy(model_name, token);
  105. fclose(cpuinfo_file);
  106. return 1;
  107. }
  108. }
  109. fclose(cpuinfo_file);
  110. return 0;
  111. }
  112. #ifdef DOUBLE
  113. #define GET_IMAGE(res) __asm__ __volatile__("fmov.d %0, $f2" : "=f"(res) : : "memory")
  114. #else
  115. #define GET_IMAGE(res) __asm__ __volatile__("fmov.s %0, $f2" : "=f"(res) : : "memory")
  116. #endif
  117. #define GET_IMAGE_CANCEL
  118. #else
  119. #ifdef DOUBLE
  120. #define LD fld.d
  121. #define ST fst.d
  122. #define MADD fmadd.d
  123. #define NMADD fnmadd.d
  124. #define MSUB fmsub.d
  125. #define NMSUB fnmsub.d
  126. #define ADD fadd.d
  127. #define SUB fsub.d
  128. #define MUL fmul.d
  129. #define MOV fmov.d
  130. #define CMOVT fsel
  131. #define MTC movgr2fr.d
  132. #define MTG movfr2gr.d
  133. #define FABS fabs.d
  134. #define FMIN fmin.d
  135. #define FMINA fmina.d
  136. #define FMAX fmax.d
  137. #define FMAXA fmaxa.d
  138. #define CMPEQ fcmp.ceq.d
  139. #define CMPLE fcmp.cle.d
  140. #define CMPLT fcmp.clt.d
  141. #define NEG fneg.d
  142. #define FFINT ffint.d.l
  143. #define XVFSUB xvfsub.d
  144. #define XVFADD xvfadd.d
  145. #define XVFMUL xvfmul.d
  146. #define XVFMADD xvfmadd.d
  147. #define XVFMIN xvfmin.d
  148. #define XVFMINA xvfmina.d
  149. #define XVFMAX xvfmax.d
  150. #define XVFMAXA xvfmaxa.d
  151. #define XVCMPEQ xvfcmp.ceq.d
  152. #define XVCMPLE xvfcmp.cle.d
  153. #define XVCMPLT xvfcmp.clt.d
  154. #define XVMUL xvfmul.d
  155. #define XVMSUB xvfmsub.d
  156. #define XVNMSUB xvfnmsub.d
  157. #define VFSUB vfsub.d
  158. #define VFADD vfadd.d
  159. #define VFMUL vfmul.d
  160. #define VFMADD vfmadd.d
  161. #define VFMIN vfmin.d
  162. #define VFMINA vfmina.d
  163. #define VFMAX vfmax.d
  164. #define VFMAXA vfmaxa.d
  165. #define VCMPEQ vfcmp.ceq.d
  166. #define VCMPLE vfcmp.cle.d
  167. #define VCMPLT vfcmp.clt.d
  168. #define VMUL vfmul.d
  169. #define VMSUB vfmsub.d
  170. #define VNMSUB vfnmsub.d
  171. #else
  172. #define LD fld.s
  173. #define ST fst.s
  174. #define MADD fmadd.s
  175. #define NMADD fnmadd.s
  176. #define MSUB fmsub.s
  177. #define NMSUB fnmsub.s
  178. #define ADD fadd.s
  179. #define SUB fsub.s
  180. #define MUL fmul.s
  181. #define MOV fmov.s
  182. #define CMOVT fsel
  183. #define MTC movgr2fr.w
  184. #define MTG movfr2gr.s
  185. #define FABS fabs.s
  186. #define FMIN fmin.s
  187. #define FMINA fmina.s
  188. #define FMAX fmax.s
  189. #define FMAXA fmaxa.s
  190. #define CMPEQ fcmp.ceq.s
  191. #define CMPLE fcmp.cle.s
  192. #define CMPLT fcmp.clt.s
  193. #define NEG fneg.s
  194. #define FFINT ffint.s.l
  195. #define XVFSUB xvfsub.s
  196. #define XVFADD xvfadd.s
  197. #define XVFMUL xvfmul.s
  198. #define XVFMADD xvfmadd.s
  199. #define XVFMIN xvfmin.s
  200. #define XVFMINA xvfmina.s
  201. #define XVFMAX xvfmax.s
  202. #define XVFMAXA xvfmaxa.s
  203. #define XVCMPEQ xvfcmp.ceq.s
  204. #define XVCMPLE xvfcmp.cle.s
  205. #define XVCMPLT xvfcmp.clt.s
  206. #define XVMUL xvfmul.s
  207. #define XVMSUB xvfmsub.s
  208. #define XVNMSUB xvfnmsub.s
  209. #define VFSUB vfsub.s
  210. #define VFADD vfadd.s
  211. #define VFMUL vfmul.s
  212. #define VFMADD vfmadd.s
  213. #define VFMIN vfmin.s
  214. #define VFMINA vfmina.s
  215. #define VFMAX vfmax.s
  216. #define VFMAXA vfmaxa.s
  217. #define VCMPEQ vfcmp.ceq.s
  218. #define VCMPLE vfcmp.cle.s
  219. #define VCMPLT vfcmp.clt.s
  220. #define VMUL vfmul.s
  221. #define VMSUB vfmsub.s
  222. #define VNMSUB vfnmsub.s
  223. #endif /* defined(DOUBLE) */
  224. #if defined(__64BIT__) && defined(USE64BITINT)
  225. #define LDINT ld.d
  226. #define LDARG ld.d
  227. #define SDARG st.d
  228. #elif defined(__64BIT__) && !defined(USE64BITINT)
  229. #define LDINT ld.w
  230. #define LDARG ld.d
  231. #define SDARG st.d
  232. #else
  233. #define LDINT ld.w
  234. #define LDARG ld.w
  235. #define SDARG st.w
  236. #endif
  237. #ifndef F_INTERFACE
  238. #define REALNAME ASMNAME
  239. #else
  240. #define REALNAME ASMFNAME
  241. #endif /* defined(F_INTERFACE) */
  242. #if defined(ASSEMBLER) && !defined(NEEDPARAM)
  243. #define PROLOGUE \
  244. .text ;\
  245. .align 5 ;\
  246. .globl REALNAME ;\
  247. .type REALNAME, @function ;\
  248. REALNAME: ;\
  249. #if defined(__linux__) && defined(__ELF__)
  250. #define GNUSTACK .section .note.GNU-stack,"",@progbits
  251. #else
  252. #define GNUSTACK
  253. #endif /* defined(__linux__) && defined(__ELF__) */
  254. #define EPILOGUE \
  255. .end REALNAME ;\
  256. GNUSTACK
  257. #define PROFCODE
  258. #define MOVT(dst, src, cc) \
  259. bceqz cc, 1f; \
  260. add.d dst, src, $r0; \
  261. 1:
  262. #endif /* defined(ASSEMBLER) && !defined(NEEDPARAM) */
  263. #endif /* defined(ASSEMBLER) */
  264. #define SEEK_ADDRESS
  265. #define BUFFER_SIZE ( 32 << 20)
  266. #define PAGESIZE (16UL << 10)
  267. #define FIXED_PAGESIZE (16UL << 10)
  268. #define HUGE_PAGESIZE ( 2 << 20)
  269. #define BASE_ADDRESS (START_ADDRESS - BUFFER_SIZE * MAX_CPU_NUMBER)
  270. #ifndef MAP_ANONYMOUS
  271. #define MAP_ANONYMOUS MAP_ANON
  272. #endif
  273. #endif