ffs.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:50 christos Exp $ */
  2. /*-
  3. * Copyright (c) 2002 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by ITOH Yasufumi.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  22. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <machine/asm.h>
  31. /*
  32. * ffs - find first bit set
  33. *
  34. * This code makes use of ``test 8bit'' and ``shift 8bit'' instructions.
  35. * The remaining 8bit is tested in every 2bit.
  36. */
  37. ENTRY(ffs)
  38. mov r4,r0 ! using r0 specific instructions
  39. tst #0xff,r0
  40. bf/s L8bit
  41. mov #0+1,r1 ! ret = 1..8
  42. tst r0,r0 ! ffs(0) is 0
  43. bt Lzero ! testing here to accelerate ret=1..8 cases
  44. shlr8 r0
  45. tst #0xff,r0
  46. bf/s L8bit
  47. mov #8+1,r1 ! ret = 9..16
  48. shlr8 r0
  49. tst #0xff,r0
  50. bf/s L8bit
  51. mov #16+1,r1 ! ret = 17..24
  52. shlr8 r0
  53. mov #24+1,r1 ! ret = 25..32
  54. L8bit:
  55. tst #0x0f,r0
  56. bt 4f
  57. tst #0x03,r0
  58. bt 2f
  59. tst #0x01,r0 ! not bit 0 -> T
  60. mov #0,r0
  61. rts
  62. addc r1,r0 ! 0 + r1 + T -> r0
  63. 2: tst #0x04,r0
  64. mov #2,r0
  65. rts
  66. addc r1,r0
  67. 4: tst #0x30,r0
  68. bt 6f
  69. tst #0x10,r0
  70. mov #4,r0
  71. rts
  72. addc r1,r0
  73. 6: tst #0x40,r0
  74. mov #6,r0
  75. rts
  76. addc r1,r0
  77. Lzero: rts
  78. nop