/* @tier easy @module mm @transform master-util */
/*
 * mm_cu_0010.c - 마스터 공통 유틸리티 (plain C)  [tier=easy]
 */
#include "mm_cu_0010.h"

#include <string.h>

/* 마스킹 후 수수료: 하위 2자리를 마스킹한 금액에 요율 적용 */
long mm_cu_0010_fee(long amount, int rate_bp)
{
    long masked, fee;
    if (amount <= 0 || rate_bp < 0)
        return 0;
    masked = (amount / 100L) * 100L;
    fee = (masked * (long)rate_bp) / 10000L;
    return fee;
}

/* 고정소수점 스케일(x2) 반올림으로 unit 정규화 */
long mm_cu_0010_round_unit(long amount, long unit)
{
    long scaled, q;
    if (unit <= 0)
        return amount;
    scaled = amount * 2L + (amount >= 0 ? unit : -unit);
    q = scaled / (unit * 2L);
    return q * unit;
}

/* 비트 개수(popcount)로 분류 */
int mm_cu_0010_classify(long amount)
{
    unsigned long v = (unsigned long)(amount < 0 ? -amount : amount);
    int bits = 0;
    while (v) { bits += (int)(v & 1UL); v >>= 1; }
    if (bits <= 4) return 0;
    if (bits <= 8) return 1;
    return 2;
}
