2013-07-17 05:29:34 -04:00
|
|
|
/*
|
2015-10-04 14:51:05 -04:00
|
|
|
Copyright (C) 2013 Tim Mayberry
|
2013-07-17 05:29:34 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pbd/ffs.h"
|
|
|
|
|
2013-07-18 07:08:34 -04:00
|
|
|
#ifndef COMPILER_MSVC
|
2013-07-17 05:29:34 -04:00
|
|
|
#include <strings.h>
|
2013-07-18 07:08:34 -04:00
|
|
|
#endif
|
2013-07-17 05:29:34 -04:00
|
|
|
|
|
|
|
namespace PBD {
|
|
|
|
int
|
|
|
|
ffs (int x)
|
|
|
|
{
|
2013-07-18 07:08:34 -04:00
|
|
|
#if defined (COMPILER_MINGW)
|
2013-07-17 05:29:34 -04:00
|
|
|
return __builtin_ffs(x);
|
2013-07-18 07:08:34 -04:00
|
|
|
#elif defined (COMPILER_MSVC)
|
|
|
|
unsigned long index;
|
2015-10-05 10:17:49 -04:00
|
|
|
#ifdef WIN64
|
2013-07-18 07:08:34 -04:00
|
|
|
if (0 != _BitScanForward64(&index, (__int64)x))
|
|
|
|
#else
|
|
|
|
if (0 != _BitScanForward(&index, (unsigned long)x))
|
|
|
|
#endif
|
|
|
|
index++; // Make the result 1-based
|
|
|
|
else
|
|
|
|
index = 0; // All bits were zero
|
|
|
|
|
|
|
|
return (int)index;
|
2013-07-17 05:29:34 -04:00
|
|
|
#else
|
2013-07-17 13:49:12 -04:00
|
|
|
return ::ffs(x);
|
2013-07-17 05:29:34 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|