bcmp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1987, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. #include <sys/libkern.h>
  34. #include <machine/endian.h>
  35. typedef const void *cvp;
  36. typedef const unsigned char *ustring;
  37. typedef unsigned long ul;
  38. typedef const unsigned long *culp;
  39. /*
  40. * bcmp -- vax cmpc3 instruction
  41. */
  42. int
  43. (bcmp)(const void *b1, const void *b2, size_t length)
  44. {
  45. #if BYTE_ORDER == LITTLE_ENDIAN
  46. /*
  47. * The following code is endian specific. Changing it from
  48. * little-endian to big-endian is fairly trivial, but making
  49. * it do both is more difficult.
  50. *
  51. * Note that this code will reference the entire longword which
  52. * includes the final byte to compare. I don't believe this is
  53. * a problem since AFAIK, objects are not protected at smaller
  54. * than longword boundaries.
  55. */
  56. int shl, shr, len = length;
  57. ustring p1 = b1, p2 = b2;
  58. ul va, vb;
  59. if (len == 0)
  60. return (0);
  61. /*
  62. * align p1 to a longword boundary
  63. */
  64. while ((long)p1 & (sizeof(long) - 1)) {
  65. if (*p1++ != *p2++)
  66. return (1);
  67. if (--len <= 0)
  68. return (0);
  69. }
  70. /*
  71. * align p2 to longword boundary and calculate the shift required to
  72. * align p1 and p2
  73. */
  74. shr = (long)p2 & (sizeof(long) - 1);
  75. if (shr != 0) {
  76. p2 -= shr; /* p2 now longword aligned */
  77. shr <<= 3; /* offset in bits */
  78. shl = (sizeof(long) << 3) - shr;
  79. va = *(culp)p2;
  80. p2 += sizeof(long);
  81. while ((len -= sizeof(long)) >= 0) {
  82. vb = *(culp)p2;
  83. p2 += sizeof(long);
  84. if (*(culp)p1 != (va >> shr | vb << shl))
  85. return (1);
  86. p1 += sizeof(long);
  87. va = vb;
  88. }
  89. /*
  90. * At this point, len is between -sizeof(long) and -1,
  91. * representing 0 .. sizeof(long)-1 bytes remaining.
  92. */
  93. if (!(len += sizeof(long)))
  94. return (0);
  95. len <<= 3; /* remaining length in bits */
  96. /*
  97. * The following is similar to the `if' condition
  98. * inside the above while loop. The ?: is necessary
  99. * to avoid accessing the longword after the longword
  100. * containing the last byte to be compared.
  101. */
  102. return ((((va >> shr | ((shl < len) ? *(culp)p2 << shl : 0)) ^
  103. *(culp)p1) & ((1L << len) - 1)) != 0);
  104. } else {
  105. /* p1 and p2 have common alignment so no shifting needed */
  106. while ((len -= sizeof(long)) >= 0) {
  107. if (*(culp)p1 != *(culp)p2)
  108. return (1);
  109. p1 += sizeof(long);
  110. p2 += sizeof(long);
  111. }
  112. /*
  113. * At this point, len is between -sizeof(long) and -1,
  114. * representing 0 .. sizeof(long)-1 bytes remaining.
  115. */
  116. if (!(len += sizeof(long)))
  117. return (0);
  118. return (((*(culp)p1 ^ *(culp)p2)
  119. & ((1L << (len << 3)) - 1)) != 0);
  120. }
  121. #else
  122. const char *p1, *p2;
  123. if (length == 0)
  124. return(0);
  125. p1 = b1;
  126. p2 = b2;
  127. do
  128. if (*p1++ != *p2++)
  129. break;
  130. while (--length);
  131. return(length);
  132. #endif
  133. }