div_libc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /* Common bits for implementing software divide. */
  15. #include <sysdep.h>
  16. #ifdef __linux__
  17. # include <asm/gentrap.h>
  18. # include <asm/pal.h>
  19. #else
  20. # include <machine/pal.h>
  21. #endif
  22. /* These are not normal C functions. Argument registers are t10 and t11;
  23. the result goes in t12; the return address is in t9. Only t12 and AT
  24. may be clobbered. */
  25. #define X t10
  26. #define Y t11
  27. #define RV t12
  28. #define RA t9
  29. /* The secureplt format does not allow the division routines to be called
  30. via plt; there aren't enough registers free to be clobbered. Avoid
  31. setting the symbol type to STT_FUNC, so that the linker won't be tempted
  32. to create a plt entry. */
  33. #define funcnoplt notype
  34. /* None of these functions should use implicit anything. */
  35. .set nomacro
  36. .set noat
  37. /* Code fragment to invoke _mcount for profiling. This should be invoked
  38. directly after allocation of the stack frame. */
  39. .macro CALL_MCOUNT
  40. #ifdef PROF
  41. stq ra, 0(sp)
  42. stq pv, 8(sp)
  43. stq gp, 16(sp)
  44. cfi_rel_offset (ra, 0)
  45. cfi_rel_offset (pv, 8)
  46. cfi_rel_offset (gp, 16)
  47. br AT, 1f
  48. .set macro
  49. 1: ldgp gp, 0(AT)
  50. mov RA, ra
  51. lda AT, _mcount
  52. jsr AT, (AT), _mcount
  53. .set nomacro
  54. ldq ra, 0(sp)
  55. ldq pv, 8(sp)
  56. ldq gp, 16(sp)
  57. cfi_restore (ra)
  58. cfi_restore (pv)
  59. cfi_restore (gp)
  60. /* Realign subsequent code with what we'd have without this
  61. macro at all. This means aligned with one arithmetic insn
  62. used within the bundle. */
  63. .align 4
  64. nop
  65. #endif
  66. .endm
  67. /* In order to make the below work, all top-level divide routines must
  68. use the same frame size. */
  69. #define FRAME 64
  70. /* Code fragment to generate an integer divide-by-zero fault. When
  71. building libc.so, we arrange for there to be one copy of this code
  72. placed late in the dso, such that all branches are forward. When
  73. building libc.a, we use multiple copies to avoid having an out of
  74. range branch. Users should jump to DIVBYZERO. */
  75. .macro DO_DIVBYZERO
  76. #ifdef PIC
  77. #define DIVBYZERO __divbyzero
  78. .section .gnu.linkonce.t.divbyzero, "ax", @progbits
  79. .globl __divbyzero
  80. .type __divbyzero, @function
  81. .usepv __divbyzero, no
  82. .hidden __divbyzero
  83. #else
  84. #define DIVBYZERO $divbyzero
  85. #endif
  86. .align 4
  87. DIVBYZERO:
  88. cfi_startproc
  89. cfi_return_column (RA)
  90. cfi_def_cfa_offset (FRAME)
  91. mov a0, RV
  92. unop
  93. lda a0, GEN_INTDIV
  94. call_pal PAL_gentrap
  95. mov RV, a0
  96. clr RV
  97. lda sp, FRAME(sp)
  98. cfi_def_cfa_offset (0)
  99. ret $31, (RA), 1
  100. cfi_endproc
  101. .size DIVBYZERO, .-DIVBYZERO
  102. .endm
  103. /* Like the ev6 instructions, but fall back to stack use on prior machines. */
  104. .arch ev6
  105. .macro _ITOFS gr, fr, slot
  106. #ifdef __alpha_fix__
  107. itofs \gr, \fr
  108. #else
  109. stl \gr, \slot(sp)
  110. lds \fr, \slot(sp)
  111. #endif
  112. .endm
  113. .macro _ITOFT gr, fr, slot
  114. #ifdef __alpha_fix__
  115. itoft \gr, \fr
  116. #else
  117. stq \gr, \slot(sp)
  118. ldt \fr, \slot(sp)
  119. #endif
  120. .endm
  121. .macro _FTOIT fr, gr, slot
  122. #ifdef __alpha_fix__
  123. ftoit \fr, \gr
  124. #else
  125. stt \fr, \slot(sp)
  126. ldq \gr, \slot(sp)
  127. #endif
  128. .endm
  129. /* Similarly, but move two registers. Schedules better for pre-ev6. */
  130. .macro _ITOFT2 gr1, fr1, slot1, gr2, fr2, slot2
  131. #ifdef __alpha_fix__
  132. itoft \gr1, \fr1
  133. itoft \gr2, \fr2
  134. #else
  135. stq \gr1, \slot1(sp)
  136. stq \gr2, \slot2(sp)
  137. ldt \fr1, \slot1(sp)
  138. ldt \fr2, \slot2(sp)
  139. #endif
  140. .endm