xref: /isa-l/mem/mem_zero_detect_base.c (revision aaa78d6a7c0a3e3a48e4997bd5b8f78e71070835)
16e201339SJohn Kariuki /**********************************************************************
26e201339SJohn Kariuki   Copyright(c) 2011-2018 Intel Corporation All rights reserved.
36e201339SJohn Kariuki 
46e201339SJohn Kariuki   Redistribution and use in source and binary forms, with or without
56e201339SJohn Kariuki   modification, are permitted provided that the following conditions
66e201339SJohn Kariuki   are met:
76e201339SJohn Kariuki     * Redistributions of source code must retain the above copyright
86e201339SJohn Kariuki       notice, this list of conditions and the following disclaimer.
96e201339SJohn Kariuki     * Redistributions in binary form must reproduce the above copyright
106e201339SJohn Kariuki       notice, this list of conditions and the following disclaimer in
116e201339SJohn Kariuki       the documentation and/or other materials provided with the
126e201339SJohn Kariuki       distribution.
136e201339SJohn Kariuki     * Neither the name of Intel Corporation nor the names of its
146e201339SJohn Kariuki       contributors may be used to endorse or promote products derived
156e201339SJohn Kariuki       from this software without specific prior written permission.
166e201339SJohn Kariuki 
176e201339SJohn Kariuki   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186e201339SJohn Kariuki   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196e201339SJohn Kariuki   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206e201339SJohn Kariuki   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216e201339SJohn Kariuki   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226e201339SJohn Kariuki   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236e201339SJohn Kariuki   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246e201339SJohn Kariuki   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256e201339SJohn Kariuki   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266e201339SJohn Kariuki   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276e201339SJohn Kariuki   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286e201339SJohn Kariuki **********************************************************************/
296e201339SJohn Kariuki 
306e201339SJohn Kariuki #include <stdint.h>
316e201339SJohn Kariuki #include <stddef.h>
32a3169750SRoy Oursler #include "unaligned.h"
336e201339SJohn Kariuki 
34*aaa78d6aSMarcel Cornu int
mem_zero_detect_base(void * buf,size_t n)35*aaa78d6aSMarcel Cornu mem_zero_detect_base(void *buf, size_t n)
366e201339SJohn Kariuki {
37a3169750SRoy Oursler         uint8_t *c = buf;
38a3169750SRoy Oursler         uintmax_t a = 0;
396e201339SJohn Kariuki 
406e201339SJohn Kariuki         // Check buffer in native machine width comparisons
41a3169750SRoy Oursler         while (n >= sizeof(uintmax_t)) {
42a3169750SRoy Oursler                 n -= sizeof(uintmax_t);
43d3cfb2fbSIlya Leoshkevich                 if (load_le_umax(c) != 0)
446e201339SJohn Kariuki                         return -1;
45a3169750SRoy Oursler                 c += sizeof(uintmax_t);
466e201339SJohn Kariuki         }
476e201339SJohn Kariuki 
486e201339SJohn Kariuki         // Check remaining bytes
496e201339SJohn Kariuki         switch (n) {
506e201339SJohn Kariuki         case 7:
516e201339SJohn Kariuki                 a |= *c++; // fall through to case 6,5,4
526e201339SJohn Kariuki         case 6:
536e201339SJohn Kariuki                 a |= *c++; // fall through to case 5,4
546e201339SJohn Kariuki         case 5:
556e201339SJohn Kariuki                 a |= *c++; // fall through to case 4
566e201339SJohn Kariuki         case 4:
57d3cfb2fbSIlya Leoshkevich                 a |= load_le_u32(c);
586e201339SJohn Kariuki                 break;
596e201339SJohn Kariuki         case 3:
606e201339SJohn Kariuki                 a |= *c++; // fall through to case 2
616e201339SJohn Kariuki         case 2:
62d3cfb2fbSIlya Leoshkevich                 a |= load_le_u16(c);
636e201339SJohn Kariuki                 break;
646e201339SJohn Kariuki         case 1:
656e201339SJohn Kariuki                 a |= *c;
666e201339SJohn Kariuki                 break;
676e201339SJohn Kariuki         }
686e201339SJohn Kariuki 
696e201339SJohn Kariuki         return (a == 0) ? 0 : -1;
706e201339SJohn Kariuki }
71