net_incr32.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /****************************************************************************
  2. * net/utils/net_incr32.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/net/netdev.h>
  25. #ifndef CONFIG_NET_ARCH_INCR32
  26. /****************************************************************************
  27. * Private Functions
  28. ****************************************************************************/
  29. /****************************************************************************
  30. * Name: net_carry32
  31. *
  32. * Description:
  33. * Calculate the Internet checksum over a buffer.
  34. *
  35. ****************************************************************************/
  36. static inline void net_carry32(FAR uint8_t *sum, uint16_t op16)
  37. {
  38. if (sum[2] < (op16 >> 8))
  39. {
  40. ++sum[1];
  41. if (sum[1] == 0)
  42. {
  43. ++sum[0];
  44. }
  45. }
  46. if (sum[3] < (op16 & 0xff))
  47. {
  48. ++sum[2];
  49. if (sum[2] == 0)
  50. {
  51. ++sum[1];
  52. if (sum[1] == 0)
  53. {
  54. ++sum[0];
  55. }
  56. }
  57. }
  58. }
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: net_incr32
  64. *
  65. * Description:
  66. *
  67. * Carry out a 32-bit addition.
  68. *
  69. * By defining CONFIG_NET_ARCH_INCR32, the architecture can replace
  70. * net_incr32 with hardware assisted solutions.
  71. *
  72. * Input Parameters:
  73. * op32 - A pointer to a 4-byte array representing a 32-bit integer in
  74. * network byte order (big endian). This value may not be word
  75. * aligned. The value pointed to by op32 is modified in place
  76. *
  77. * op16 - A 16-bit integer in host byte order.
  78. *
  79. ****************************************************************************/
  80. void net_incr32(FAR uint8_t *op32, uint16_t op16)
  81. {
  82. op32[3] += (op16 & 0xff);
  83. op32[2] += (op16 >> 8);
  84. net_carry32(op32, op16);
  85. }
  86. #endif /* CONFIG_NET_ARCH_INCR32 */